首页 > 解决方案 > cvPixelBuffer 到 CGImage 的转换只给出黑白图像

问题描述

我正在尝试将原始相机传感器数据转换为彩色图像。数据首先在 [UInt16] 数组中提供,随后转换为 cvPixelBuffer。

以下 Swift 5 代码“仅”创建一个黑白图像并忽略 RGGB 像素数据的滤色器数组。

我也试过 VTCreateCGImageFromCVPixelBuffer 无济于事。它返回零。

   // imgRawData is a [UInt16] array

   var pixelBuffer: CVPixelBuffer?
   let attrs: [ CFString : Any ]  = [ kCVPixelBufferPixelFormatTypeKey : "rgg4" ]

   CVPixelBufferCreateWithBytes(kCFAllocatorDefault, width, height, kCVPixelFormatType_14Bayer_RGGB, &imgRawData, 2*width, nil, nil, attrs as CFDictionary, &pixelBuffer)
   
   // This creates a black-and-white image, not color

   let ciimg = CIImage(cvPixelBuffer: pixelBuffer! )

   let context: CIContext = CIContext.init(options: [ CIContextOption(rawValue: "workingFormat") : CIFormat.RGBA16 ] )
   guard let cgi = context.createCGImage(ciimg, from: ciimg.extent, format: CIFormat.RGBA16, colorSpace: CGColorSpace(name: CGColorSpace.sRGB), deferred: false)
    else { return dummyImg! }


   // This function returns nil
   var cgI: CGImage?
   VTCreateCGImageFromCVPixelBuffer(pixelBuffer!, options: nil, imageOut: &cgI)

任何提示都非常感谢。

至于去马赛克,我希望 CoreImage 或 CoreGraphics 负责 RGGB 像素颜色插值。

标签: swiftcore-graphicscore-imagecvpixelbufferdemosaicing

解决方案


推荐阅读