iOS6:如何使用YUV到RGB的转换功能从cvPixelBufferref到CIImage

Rug*_*ger 8 objective-c core-image ios ios6

从iOS6开始,Apple通过此调用提供了使用原生YUV到CIImage的规定

initWithCVPixelBuffer:选项:

在核心图像编程指南中,他们提到了这个功能

利用iOS 6.0及更高版本中对YUV图像的支持.相机像素缓冲器本身是YUV,但大多数图像处理算法都需要RBGA数据.在两者之间进行转换需要付出代价.Core Image支持从CVPixelBuffer对象读取YUB并应用适当的颜色转换.

options = @ {(id)kCVPixelBufferPixelFormatTypeKey:@(kCVPixelFormatType_420YpCvCr88iPlanarFullRange)};

但是,我无法正常使用它.我有一个原始的YUV数据.所以,这就是我所做的

                void *YUV[3] = {data[0], data[1], data[2]};
                size_t planeWidth[3] = {width, width/2, width/2};
                size_t planeHeight[3] = {height, height/2, height/2};
                size_t planeBytesPerRow[3] = {stride, stride/2, stride/2};
                CVPixelBufferRef pixelBuffer = NULL;
                CVReturn ret = CVPixelBufferCreateWithPlanarBytes(kCFAllocatorDefault,
                               width, 
                               height,
                               kCVPixelFormatType_420YpCbCr8PlanarFullRange, 
                               nil,
                               width*height*1.5,
                               3, 
                               YUV,
                               planeWidth,
                               planeHeight, 
                               planeBytesPerRow, 
                               nil,
                               nil, nil, &pixelBuffer); 

    NSDict *opt =  @{ (id)kCVPixelBufferPixelFormatTypeKey :
                        @(kCVPixelFormatType_420YpCbCr8PlanarFullRange) };

CIImage *image = [[CIImage alloc]   initWithCVPixelBuffer:pixelBuffer options:opt];
Run Code Online (Sandbox Code Playgroud)

我的形象是零.我不知道我错过了什么.

编辑: 我在通话前添加了锁定和解锁基地址.另外,我转储了pixelbuffer的数据,以确保pixellbuffer能够正确保存数据.它看起来只是init调用的错误.仍然CIImage对象返回nil.

 CVPixelBufferLockBaseAddress(pixelBuffer, 0);
CIImage *image = [[CIImage alloc]   initWithCVPixelBuffer:pixelBuffer options:opt];
 CVPixelBufferUnlockBaseAddress(pixelBuffer,0);
Run Code Online (Sandbox Code Playgroud)

Quo*_*ion 1

控制台中应该有错误消息:initWithCVPixelBuffer failed because the CVPixelBufferRef is not IOSurface backed。请参阅 Apple 的技术问答 QA1781,了解如何创建 IOSurface 支持的CVPixelBuffer.

调用CVPixelBufferCreateWithBytes()CVPixelBufferCreateWithPlanarBytes()将导致CVPixelBuffers不受 IOSurface 支持...

...为此,您必须在使用创建像素缓冲区时kCVPixelBufferIOSurfacePropertiesKey在字典中指定。pixelBufferAttributesCVPixelBufferCreate()

NSDictionary *pixelBufferAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
    [NSDictionary dictionary], (id)kCVPixelBufferIOSurfacePropertiesKey,
    nil];
// you may add other keys as appropriate, e.g. kCVPixelBufferPixelFormatTypeKey,     kCVPixelBufferWidthKey, kCVPixelBufferHeightKey, etc.
 
CVPixelBufferRef pixelBuffer;
CVPixelBufferCreate(... (CFDictionaryRef)pixelBufferAttributes,  &pixelBuffer);
Run Code Online (Sandbox Code Playgroud)

或者,如果提供的字典CVPixelBuffers包含.CVPixelBufferPoolCreatePixelBuffer()pixelBufferAttributesCVPixelBufferPoolCreate()kCVPixelBufferIOSurfacePropertiesKey