微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

initWithCVPixelBuffer:options:

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

Take advantage of the support for YUV image in iOS 6.0 and later.
Camera pixel buffers are natively YUV but most image processing
algorithms expect RBGA data. There is a cost to converting between the
two. Core Image supports reading YUB from CVPixelBuffer objects and
applying the appropriate color transform.

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,&pixelBuffer); 

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

CIImage *image = [[CIImage alloc]   initWithCVPixelBuffer:pixelBuffer options:opt];

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

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

CVPixelBufferLockBaseAddress(pixelBuffer,0);
CIImage *image = [[CIImage alloc]   initWithCVPixelBuffer:pixelBuffer options:opt];
 CVPixelBufferUnlockBaseAddress(pixelBuffer,0);

解决方法

控制台中应该有错误消息:initWithCVPixelBuffer失败,因为CVPixelBufferRef不是IOSurface支持的.有关如何创建由IOSurface支持的CVPixelBuffer的信息,请参阅Apple的 Technical Q&A QA1781.

Calling CVPixelBufferCreateWithBytes() or CVPixelBufferCreateWithPlanarBytes() will result in CVPixelBuffers that are not IOSurface-backed…

…To do that,you must specify kCVPixelBufferIOSurfacePropertiesKey in the pixelBufferAttributes dictionary when creating the pixel buffer using CVPixelBufferCreate().

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

Alternatively,you can make IOSurface-backed CVPixelBuffers using CVPixelBufferPoolCreatePixelBuffer() from an existing pixel buffer pool,if the pixelBufferAttributes dictionary provided to CVPixelBufferPoolCreate() includes kCVPixelBufferIOSurfacePropertiesKey.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐