Question

I'm currently tweaking some internals of ImageSnap in an attempted ad-hoc modification to take snapshots of a live camera image on the fly. I noticed that images got resized, without regard to aspect ratio, and traced the problem to the following line:

CVImageBufferRef frame;
// obtain frame
verbose("frame is %d x %d px\n", (int)CVPixelBufferGetWidth(frame),
                                 (int)CVPixelBufferGetHeight(frame));
CIImage *ciimg = [CIImage imageWithCVImageBuffer:frame];
verbose("CIImage has %d x %d px\n", (int)[ciimg extent].size.width,
                                    (int)[ciimg extent].size.height);

At least for one given device I'm using, the output of these lines is

frame is 1920 x 1080 px
CIImage has 1888 x 1062 px

So apparently the image was resized, although I don't see any part of the code requesting such a resize. Where did that come from, and even more importantly, how can I prevent it?

I'm using the following documentation, but can't find a hint in there:


Edit: As far as I can tell, there is no scaling but instead cropping going on, and this is due to the clean aperture setting mentioned in the Core Video Programming Guide. So the question is now how to either set this clean aperture to the full frame size, or how to avoid the conversion from taking that into account.

Was it helpful?

Solution

You can strip out the clean aperture data with

CVBufferRemoveAttachment(frame, kCVImageBufferCleanApertureKey);

before calling imageWithCVImageBuffer

OTHER TIPS

From the RosyWriter app from Apple:

Use extendedWidth instead of width to account for possible row extensions (sometimes used for memory alignment):

unsigned char *base = (unsigned char *)CVPixelBufferGetBaseAddress( pixelBuffer );
size_t width = CVPixelBufferGetWidth( pixelBuffer );
size_t height = CVPixelBufferGetHeight( pixelBuffer );
size_t stride = CVPixelBufferGetBytesPerRow( pixelBuffer );
size_t extendedWidth = stride / sizeof( uint32_t ); // each pixel is 4 bytes/32 bits

In case the first suggestion is unfruitful.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top