我希望能够在iOS中以非有损压缩的图像文件中存储捕获的ARframe的深度数据。
根据这个WWDC演讲:
"在iOS 11中,我们支持两种深度图像。第一种是HEIF HEVC,即新格式,也称为HEIC文件,在那里,对深度有一流的支持...我们支持的第二种格式是JPEG。JPEG并不意味着要做这样的技巧,但我们仍然让它做到了。如果地图被过滤,则地图是8位有损JPEG,如果地图中没有数字,则我们使用16位无损JPEG编码来保留所有未知数字,并将其作为第二个图像存储在JPEG底部,因此就像多图片对象一样,如果您熟悉它的话。"
当我逐像素比较原始深度缓冲区(16位)和我从存储的图像检索到的深度缓冲区时,我得到以下结果:
第一 第二
0.61865234 0.6196289
0.62109375 0.6196289
0.6269531 0.6274414
0.6298828 0.63134766
0.6328125 0.63134766
nan 0.003921509
nan 0.0
nan 0.0
nan 0.007843018
nan 0.003921509
即使我有未经过滤的具有NAN的深度数据,存储的数据也无法保留它们,似乎也没有使用无损编码。
这是我编写的代码:
if let currentFrame = session.currentFrame, let depthData = currentFrame.capturedDepthData { // The session variable is an ARSession object
let outputURL: URL? = filePath(forKey: "test")
guard let cgImageDestination = CGImageDestinationCreateWithURL(outputURL! as CFURL, kUTTypeJPEG, 1, nil) else {
return
}
depthData.depthDataMap.normalize() // Normalizing depth data between 0.0 and 1.0
let sixteenBitDepthData = depthData.converting(toDepthDataType: kCVPixelFormatType_DepthFloat16)
let ciImage = CIImage(cvPixelBuffer: currentFrame.capturedImage)
let context = CIContext(options: nil)
let dict: NSDictionary = [
kCGImageDestinationLossyCompressionQuality: 1.0,
kCGImagePropertyIsFloat: kCFBooleanTrue,
]
if let cgImage: CGImage = context.createCGImage(ciImage, from: ciImage.extent) {
CGImageDestinationAddImage(cgImageDestination, cgImage, nil)
}
var auxDataType: NSString?
let auxData = sixteenBitDepthData.dictionaryRepresentation(forAuxiliaryDataType: &auxDataType)
CGImageDestinationAddAuxiliaryDataInfo(cgImageDestination, auxDataType!, auxData! as CFDictionary)
CGImageDestinationFinalize(cgImageDestination)
if let second = getDepthBufferFromFile(key: "test") {
self.compareBuffers(first: sixteenBitDepthData.depthDataMap, second: second)
}
}