简体   繁体   English

Swift中如何将数值数据数组转换为RAW图像数据?

[英]How to convert a numerical data array into RAW image data in Swift?

I have a data array of Int16 or Int32 numerical values that are the raw image data from a 11MP camera chip with an RGGB pixel layout (CFA).我有一个 Int16 或 Int32 数值的数据数组,它们是来自具有 RGGB 像素布局 (CFA) 的 11MP 相机芯片的原始图像数据。 The data are exported by the camera driver as FITS data, which is basically a vector or long string of bytes or 16bit/pixel data in my case.数据由相机驱动程序导出为 FITS 数据,在我的例子中,它基本上是一个向量或长字节串或 16 位/像素数据。

I like to convert these data into a raw image format in Swift in order to use the powerful debayering and demosaicing features and algorithms in iOS/Swift.我喜欢将这些数据转换为 Swift 中的原始图像格式,以便在 iOS/Swift 中使用强大的去拜耳和去马赛克功能和算法。 I do not intend to demosaic myself, since iOS has a great library for this already (see WWDC2016 keynote on Raw Processing with Core Image ).我不打算自己去马赛克,因为 iOS 已经为此提供了一个很棒的库(请参阅 WWDC2016 关于使用核心图像进行原始处理的主题演讲)。

I need to make iOS “believe” my data are actual raw image data.我需要让 iOS “相信”我的数据是实际的原始图像数据。

I tried using CreatePixelBufferWithBytes in Swift and then CIImage from pixelbuffer but to no avail.我尝试在 Swift 中使用 CreatePixelBufferWithBytes,然后从像素缓冲区使用 CIImage,但无济于事。 The CIImage.cgimage is not an RGB color image. CIImage.cgimage 不是 RGB 彩色图像。

Is there a simple way to create a raw or DNG image in Swift from raw numerical data?有没有一种简单的方法可以从原始数值数据在 Swift 中创建原始或 DNG 图像?

Here is what I tried with the CVPixelBuffer approach, but I do not get any color image out of this:这是我尝试使用 CVPixelBuffer 方法的方法,但我没有从中得到任何彩色图像:

imgRawData is a [Int32] or [Float32] array with width*height number of elements. imgRawData 是一个 [Int32] 或 [Float32] 数组,其元素个数为 width*height。

var pixelBuffer: CVPixelBuffer?
let attrs = [kCVPixelBufferCGImageCompatibilityKey: kCFBooleanTrue,
            kCVPixelBufferCGBitmapContextCompatibilityKey: kCFBooleanTrue ]
    
CVPixelBufferCreateWithBytes(kCFAllocatorDefault, width, height, kCVPixelFormatType_14Bayer_RGGB, &imgRawData, 2*width, nil, nil, attrs as CFDictionary, &pixelBuffer)

let dummyImg = UIImage(systemName: "star.fill")?.cgImage
    
let ciiraw = CIImage(cvPixelBuffer: pixelBuffer!)
    
let cif = CIFilter.lanczosScaleTransform()
cif.scale = 0.25
cif.inputImage  = ciiraw
let cii = cif.outputImage
    
let context: CIContext = CIContext.init(options: nil)
guard let cgi = context.createCGImage(cii!, from: cii!.extent) else { return dummyImg! }

Quickview of Xcode shows me only black&white or grayscale images. Xcode 的快速浏览只显示黑白或灰度图像。 So does the SwiftUI View of the CGImage... CGImage 的 SwiftUI 视图也是如此...

You can use CGContext and pass your raw values in as bitmapinfo , see init:您可以使用CGContext并将原始值作为bitmapinfo ,请参见 init:

init?(data: UnsafeMutableRawPointer?, width: Int, height: Int, bitsPerComponent: Int, bytesPerRow: Int, space: CGColorSpace, bitmapInfo: UInt32)

And for space parameter, which takes CGColorSpace you would use CGColorSpaceCreateDeviceRGB() .对于采用CGColorSpacespace参数,您将使用CGColorSpaceCreateDeviceRGB()

You will then use your image with a code similar to this one:然后,您将使用与此类似的代码来使用您的图像:

let imageRef = CGContext.makeImage(context!)
let imageRep = NSBitmapImageRep(cgImage: imageRef()!)

Play around with it for a bit, I think you will find what you are looking for.玩一下,我想你会找到你想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM