如何在Swift中将数值数据数组转换为RAW图像数据? [英] How to convert a numerical data array into RAW image data in Swift?

查看:87
本文介绍了如何在Swift中将数值数据数组转换为RAW图像数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Int16或Int32数值的数据数组,它们是来自具有RGGB像素布局(CFA)的11MP相机芯片的原始图像数据.数据由相机驱动程序导出为FITS数据,在我的情况下,该数据基本上是向量或长字节字符串或16bit/pixel数据.

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

我喜欢在Swift中将这些数据转换为原始图像格式,以便在iOS/Swift中使用强大的去拜耳和去马赛克功能以及算法.我不打算自我贬低,因为iOS已经为此提供了一个很棒的库(请参阅WWDC2016关于使用核心映像进行原始处理的主题演讲").

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. 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相信"我的数据是实际的 raw 图片数据.

I need to make iOS "believe" my data are actual raw image data.

我尝试在Swift中使用CreatePixelBufferWithBytes,然后从pixelbuffer中使用CIImage,但无济于事.CIImage.cgimage不是RGB彩色图像.

I tried using CreatePixelBufferWithBytes in Swift and then CIImage from pixelbuffer but to no avail. The CIImage.cgimage is not an RGB color image.

有没有一种简单的方法可以根据原始数值数据在Swift中创建原始或DNG图像?

Is there a simple way to create a raw or DNG image in Swift from raw numerical data?

这是我使用CVPixelBuffer方法尝试过的方法,但是我没有从中得到任何彩色图像:

Here is what I tried with the CVPixelBuffer approach, but I do not get any color image out of this:

imgRawData是一个[Int32]或[Float32]数组,具有宽度*高度的元素数.

imgRawData is a [Int32] or [Float32] array with width*height number of elements.

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! }

Xcode的快速浏览仅显示黑白图像或灰度图像.CGImage的SwiftUI视图也是如此...

Quickview of Xcode shows me only black&white or grayscale images. So does the SwiftUI View of the CGImage...

推荐答案

您可以使用 CGContext 并将原始值作为 bitmapinfo 传入,请参阅init:

You can use CGContext and pass your raw values in as bitmapinfo, see init:

init?(数据:UnsafeMutableRawPointer ?,宽度:Int,高度:Int,bitsPerComponent:Int,bytesPerRow:Int,空间:CGColorSpace,bitmapInfo:UInt32)

对于需要 CGColorSpace space 参数,您将使用 CGColorSpaceCreateDeviceRGB().

And for space parameter, which takes CGColorSpace you would use 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.

这篇关于如何在Swift中将数值数据数组转换为RAW图像数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆