Doub
Posts: 24
Joined: Fri Apr 07, 2017 9:48 pm

MMAL cropping

Sun Apr 14, 2019 4:27 am

In my application I need only a square portion of the camera image, and 60 fps. So I'm running a camera v2 in mode 6, which gives me 1280x720 pixels. I'd like to only get 720x720 pixels, but choose which ones (aligned left, middle, right or anywhere in between).

I've been playing with the camera component port format (format.es.video.width/height, format.es.video.crop.x/y/width/height), and with PARAMETER_INPUT_CROP. But I can never get a square image with a proper aspect ratio and control over the left/right cropping position.

Can someone tell me how to get cropping to work properly? If you'd rather give me raspivid command line option it's fine too, I can translate them to MMAL API calls myself.

User avatar
HermannSW
Posts: 1605
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany
Contact: Website Twitter YouTube

Re: MMAL cropping

Sun Apr 14, 2019 3:05 pm

Doub wrote:
Sun Apr 14, 2019 4:27 am
If you'd rather give me raspivid command line option it's fine too, I can translate them to MMAL API calls myself.
I don't know MMAL, but you say it can be translated between MMAL and raspivid.

It seems there is a bug or restriction in raspivid. This command shows 1280x720 view:

Code: Select all

raspivid -md 6 -roi 0,0,1.0,1.0 -p 10,10,1280,720 -t 0
Now what I would expect is that this comand shows your square view (720/1280=0.5625):

Code: Select all

raspivid -md 6 -roi 0,0,0.5625,1.0 -p 10,10,720,720 -t 0
The -roi works fine: top, bottom and left keep the same, right is near the middle of full view.
But the window is scaled to half the rectangular size, not quadratic.

If I remember correctly there was a posting by 6by9 that you cannot change the preview aspect ratio to quadratic, but I cannot find that posting.
⇨https://stamm-wilbrandt.de/en/Raspberry_camera.html

https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://stamm-wilbrandt.de/github_repo_i420toh264
https://github.com/Hermann-SW/fork-raspiraw
https://twitter.com/HermannSW

User avatar
HermannSW
Posts: 1605
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany
Contact: Website Twitter YouTube

Re: MMAL cropping

Sun Apr 14, 2019 7:29 pm

I was wrong, only preview window has problems. This command does what you want, generate 720x720 video. And with first roi parameter you can control left/middle/right by 0.0/0.21875/0.4375:

Code: Select all

raspivid -md 6 -roi 0,0,0.5625,1.0 -w 720 -h 720 -n -o tst.h264
⇨https://stamm-wilbrandt.de/en/Raspberry_camera.html

https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://stamm-wilbrandt.de/github_repo_i420toh264
https://github.com/Hermann-SW/fork-raspiraw
https://twitter.com/HermannSW

Doub
Posts: 24
Joined: Fri Apr 07, 2017 9:48 pm

Re: MMAL cropping

Mon Apr 15, 2019 1:27 am

Well, I don't need to record video (yet), but I need the preview to work.

I managed to get something to work by changing the pixel aspect ratio (format.es.video.num/den) of the camera preview port. I feel like there's way too many scaling steps in this pipeline when all I want is stream pixels 1 for 1 from the camera to the screen.

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 7453
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: MMAL cropping

Mon Apr 15, 2019 6:56 am

The calculations are all buried in the firmware.

It'll first take the sensor mode and centre crop it to match your output aspect ratio. There is no way to access any of the pixels cropped in this step.

It then takes the proportions defined by PARAMETER_INPUT_CROP to crop any part out of that image, and it will resize it up to the output resolution (which may distort the image if w and h are different).
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

Doub
Posts: 24
Joined: Fri Apr 07, 2017 9:48 pm

Re: MMAL cropping

Tue Apr 16, 2019 7:01 am

6by9 wrote:
Mon Apr 15, 2019 6:56 am
The calculations are all buried in the firmware.
Is "firmware" stuff provided by Broadcom or by the Raspberry Pi Foundation? Is it likely to evolve and improve if I suggest non-breaking changes?
It'll first take the sensor mode and centre crop it
I don't want the centre, but instead select a square window that I can slide one way or the other, so I definitely need to make this a no-op.
to match your output aspect ratio.
What is the output here? Is it the video port, the preview port, some combination of the two or something else maybe?
It then takes the proportions defined by PARAMETER_INPUT_CROP to crop any part out of that image, and it will resize it up to the output resolution (which may distort the image if w and h are different).
So I have to set the output resolution to 16x9 aspect ratio to match the mode 6 and disable the first centre crop, but then I crop a 1x1 (square) portion of that, so IIUC there will be unavoidable distortion. Is playing with the pixel aspect ratio the best way to "negate" that distortion, or is there some better way?

Return to “Camera board”