【问题标题】:iOS6 : How to use the conversion feature of YUV to RGB from cvPixelBufferref to CIImageiOS6:如何使用从 cvPixelBufferref 到 CIImage 的 YUV 到 RGB 的转换功能
【发布时间】:2013-11-20 00:27:37
【问题描述】:

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

initWithCVPixelBuffer:选项:

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

利用 iOS 6.0 及更高版本对 YUV 图像的支持。 相机像素缓冲区本机是 YUV,但大多数图像处理 算法需要 RBGA 数据。两者之间的转换是有成本的 二。 Core Image 支持从 CVPixelBuffer 对象中读取 YUB 和 应用适当的颜色变换。

选项 = @{ (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];

我的图像为零。知道我错过了什么。

编辑: 我在调用之前添加了锁定和解锁基地址。另外,我转储了像素缓冲区的数据以确保像素缓冲区正确保存数据。看起来只有 init 调用有问题。仍然 CIImage 对象返回 nil。

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

【问题讨论】:

  • 你能解决它吗? .如果是,请发布您的解决方案
  • 您好 Rugger,如果您能解决,请提供解决方案。谢谢

标签: ios objective-c ios6 core-image


【解决方案1】:

我正在解决一个类似的问题,并不断从 Apple 那里找到相同的报价,但没有任何关于如何在 YUV 颜色空间中工作的进一步信息。我遇到了以下问题:

默认情况下,Core Image 假定处理节点是每像素 128 位、线性光、使用 GenericRGB 颜色空间的预乘 RGBA 浮点值。您可以通过提供 Quartz 2D CGColorSpace 对象来指定不同的工作色彩空间。请注意,工作色彩空间必须基于 RGB。如果您有 YUV 数据作为输入(或其他不基于 RGB 的数据),您可以使用 ColorSync 函数转换为工作色彩空间。 (有关创建和使用 CGColorspace 对象的信息,请参阅 Quartz 2D 编程指南。) 使用 8 位 YUV 4:2:2 源,Core Image 每 GB 可以处理 240 个高清层。八位 YUV 是视频源的原生颜色格式,例如 DV、MPEG、未压缩的 D1 和 JPEG。您需要将 YUV 颜色空间转换为 Core Image 的 RGB 颜色空间。

我注意到没有 YUV 颜色空间,只有 Gray 和 RGB;和他们经过校准的表亲。我还不确定如何转换色彩空间,但如果我发现了一定会在这里报告。

【讨论】:

    【解决方案2】:

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

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

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

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

    或者,如果提供给CVPixelBufferPoolCreate()pixelBufferAttributes 字典包含kCVPixelBufferIOSurfacePropertiesKey,您可以使用现有像素缓冲池中的CVPixelBufferPoolCreatePixelBuffer() 来制作支持IOSurface 的CVPixelBuffers

    【讨论】:

      猜你喜欢
      • 2021-05-28
      • 1970-01-01
      • 2014-01-25
      • 2012-11-25
      • 1970-01-01
      • 2017-04-21
      • 2013-07-27
      • 1970-01-01
      • 2018-03-13
      相关资源
      最近更新 更多