IPhone YUV channel orientation

我怕爱的太早我们不能终老 提交于 2019-12-08 09:50:59

问题


I am grabbing the YUV channel from the IPhone in the kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange format (YUV, bi-planar).

I intend to process the y-channel, so I grab it using

CVImageBufferRef pixelBuffer = CMSampleBufferGetImageBuffer( sampleBuffer );

CVPixelBufferLockBaseAddress( pixelBuffer, 0 );

int bufferHeight = CVPixelBufferGetHeight(pixelBuffer);
int bufferWidth = CVPixelBufferGetWidth(pixelBuffer);

uint8_t *y_channel = CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 0);

The problem is that the y_channel pixels appears rotated and mirrored (I draw them on an overlay layer to see what the look like:

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)context
{
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
    CGContextRef bitmapContext = CGBitmapContextCreate(rotated,
                                                       imageSize->x,
                                                       imageSize->y,
                                                       8, // bitsPerComponent
                                                       1*imageSize->x, // bytesPerRow
                                                       colorSpace,
                                                       kCGImageAlphaNone);

    CFRelease(colorSpace);

    CGImageRef cgImage = CGBitmapContextCreateImage(bitmapContext);

    CGContextDrawImage(context,  CGRectMake(0, 0, imageSize->x/2, imageSize->y/2), cgImage);

    CFRelease(cgImage);
    CFRelease(bitmapContext);
}

I have considered looping through the pixels and created a fixed version of the image, but I am wondering if there is a method to get the y_channel in the correct orientation (IE: not rotated in 90 degrees) straight from the camera.


回答1:


I don't believe there is a way to alter the orientation of the Y plane coming from the camera, but that shouldn't matter that much in your processing, because you should be able to work with it just fine in its native orientation. If you know that it's rotated 90 degrees, simply tweak your processing to work with it at that rotation.

Also, I believe the mirroring you see is due to your drawing into the Core Graphics coordinate space, where the origin is in the lower left. On the iPhone, CALayers that back UIViews get flipped so that the origin is in the upper left, which can cause images drawn using Quartz in these layers to be inverted.

For display, I would recommend not doing Quartz drawing like you show here, but instead use the Y channel image as an OpenGL ES texture. This will be far more performant. Also, you can simply specify the correct texture coordinates to automatically deal with any image rotation you want in a hardware-accelerated manner.

I describe how to do hardware-accelerated image processing on iOS devices using OpenGL ES 2.0 here, and I provide a sample application that does this processing using shaders and draws the result to the screen in a texture. I'm working with the BGRA colorspace in that example, but you should be able to pull out the Y channel and use it as a luminance texture in the same way.



来源:https://stackoverflow.com/questions/4359727/iphone-yuv-channel-orientation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!