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

前端之家收集整理的这篇文章主要介绍了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.

原文链接:https://www.f2er.com/iOS/333376.html

猜你在找的iOS相关文章