New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The crop attribute should be renamed zoom #146

Closed
kanawat opened this Issue Aug 11, 2014 · 5 comments

Comments

Projects
None yet
2 participants
@kanawat

kanawat commented Aug 11, 2014

Hello,

I guess this may just be a quick question rather than an issue report. I have been playing around with picamera for a week now. One thing I notice is that when I crop an image and try to preview it, either by fullscreen or using preview_window, the display image always shown with wrong aspect ratio. For example: if I set resolution to 1972x976 and set crop=(0.0, 0.0, 0.1, 0.5), I can't find a way to make preview to properly display it at 197x488 pixel, instead, the preview images seems to always resize/scale the cropped image into 4:3 aspect ratio.

Do you know how to fix this? Or am I doing anything wrong?

Thanks for the great work!

Kanawat.

@waveform80

This comment has been minimized.

Owner

waveform80 commented Aug 11, 2014

I'm probably not understanding exactly what you're trying to achieve here, but if you want the camera to have an output resolution of 197x488 you can simply set the resolution to that value:

import picamera
import time

with picamera.PiCamera():
    camera.resolution = (197, 488)
    camera.start_preview()
    time.sleep(10)

What the camera will do in this case is set its mode to 1296x972@30fps (because that's the closest mode it has given the requested resolution and framerate), and crop the result to 197x488.

What's slightly tricky to understand about the crop attribute is that it represents the portion of the sensor that will be used by the camera for input, and that it's already scaled (by the GPU) so a width and height of 100%, given by a crop value of (0.0, 0.0, 1.0, 1.0) represents a normal undistorted image. So, if you wanted to zoom in on the central portion of your 197x488 display you still need to specify a "square" aspect ratio for the width and height, e.g.:

import picamera

with picamera.PiCamera():
    camera.resolution = (197, 488)
    camera.start_preview()
    camera.crop = (0.25, 0.25, 0.5, 0.5)
    time.sleep(10)

Unfortunately the crop attribute is rather poorly named; I really should have called it zoom or roi (region of interest, which is what raspistill/raspivid call it).

@kanawat

This comment has been minimized.

kanawat commented Aug 11, 2014

Thank your for your answer. I think I understand it better now. Basically I tried to select a small region out of FOV but I confused "resolution" with "camera mode". When I set resolution = (1296,972), I was hoping that I forced the camera mode to 1296x972, but that was not the case.

Am I correct to say that "camera mode" is chosen not only from frame rate and resolution, but also crop width & height?

Setting frame rate aside, considering 2 cases below, in case1, camera mode should be 640x480 while in case 2, camera mode should be 1276x972. Am I right?

case1:

camera.resolution = (640,480)
camera.crop = (0,0,1.0,1.0)
camera.start_preview()

case2:

camera.resolution = (640,480)
camera.crop = (0.25,0.25,0.5,0.5)
camera.start_preview()
@waveform80

This comment has been minimized.

Owner

waveform80 commented Aug 11, 2014

Unfortunately no, there's a few misconceptions there:

  1. Firstly, the crop (region of interest/zoom) has nothing to do with the camera mode selection - it's purely down to resolution and framerate (controlled by the like-named properties in PiCamera).
  2. In the first case you give, assuming framerate is 30 (the default), then the camera mode won't be 640x480. Notice that in the table in camera modes the two 640x480 modes have a framerate range of 42.1-60.0fps and 60.1-90fps. However, the camera's framerate is 30 which is outside those ranges, so instead the camera will select the 1296x972 mode and downscale the output (it'll select this mode instead of 1296x730 because the aspect ratio is closer).
  3. In your second case the same will happen (because crop doesn't affect the mode and the resolution and presumably framerate are the same).
@kanawat

This comment has been minimized.

kanawat commented Aug 13, 2014

Hi Dave, Thanks for help clearing up things for me. I guess one more thing that add to my confusion is how crop works more like zoom when it still keep the resolution of the cropped image the same as original image, not smaller. Here's my final code that's try to capture and crop a small portion in the top left (or anywhere other than the central area) of the FOV and obtain a smaller resolution image using resize parameter during capturing. Hope I'm doing it right this time :-)

camera.resolution = (1296,972)
camera.framerate = 42
# force camera mode to 1296 x 972, capturing full FOV
camera.crop = (0,0,0.3,0.2)
# Need only 389 x 194 pixel (30% x 20% of FOV) on the top left of the FOV
camera.capture('test.jpg',use_video_port=True,resize=(389,194))

By doing so, I was able to achieve my desired goal. In this case, preview of cropped image still be in 4:3 ratio, which is in a wrong portion. But the test.jpg come out correctly.

@waveform80

This comment has been minimized.

Owner

waveform80 commented Aug 13, 2014

Yup ... as mentioned, crop is dreadfully badly named (which is my fault). I think for 1.8 I really should rename it to zoom as there seems to be a consensus (amongst the various people that have mentioned this) that that's a sensible alternative. I'll keep crop there as a deprecated alias until I get around to working on 2.0. So, for now I'll relabel this as a rename task for 1.8.

@waveform80 waveform80 changed the title from preview does not keep ratio of cropped image to The crop attribute should be renamed zoom Aug 13, 2014

@waveform80 waveform80 added the task label Aug 13, 2014

@waveform80 waveform80 added this to the 1.8 milestone Aug 13, 2014

@waveform80 waveform80 self-assigned this Aug 13, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment