本文介绍了iPhone YUV频道方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从iPhone上以kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange格式(YUV,双平面)获取YUV频道.

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

我打算处理y通道,所以我使用

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);

问题是y_channel像素显示为旋转并镜像的(我将它们绘制在覆盖层上以查看其外观:

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);
}

我已经考虑过遍历像素并创建图像的固定版本,但是我想知道是否有一种方法可以使y_channel直接从相机以正确的方向(即:未旋转90度)获得.

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.

推荐答案

我不相信有一种方法可以改变来自相机的Y平面的方向,但是在您的处理中没什么关系,因为您应该能够以其本机方向很好地使用它.如果您知道它旋转了90度,则只需调整您的处理过程即可在该旋转时使用它.

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.

此外,我相信您看到的镜像是由于您在Core Graphics坐标空间中进行的绘制而产生的,其原点位于左下角.在iPhone上,向后翻转UIViews的CALayers会发生翻转,因此原点位于左上角,这会导致在这些层中使用Quartz绘制的图像发生反转.

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.

对于显示,我建议不要像此处所示那样进行Quartz绘制,而应将Y通道图像用作OpenGL ES纹理.这将表现得更好.另外,您只需指定正确的纹理坐标即可以硬件加速的方式自动处理您想要的任何图像旋转.

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.

我描述了如何使用OpenGL ES 2.0在iOS设备上进行硬件加速的图像处理此处,我提供了示例应用程序,该应用程序使用着色器进行此处理,并将结果以纹理的形式绘制到屏幕上.在该示例中,我正在使用BGRA色彩空间,但是您应该能够拉出Y通道,并以相同的方式将其用作亮度纹理.

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.

这篇关于iPhone YUV频道方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 05:18