在iOS应用中将深度数据(具有16位位深度)存储为JPEG文件中的辅助数据

Dhr*_*gai 6 jpeg ios swift avkit arkit

我希望能够在iOS的捕获ARframe的深度数据存储在一个无损压缩的图像文件。

根据这次 WWDC演讲:

“在iOS 11中,我们支持两种深度图像。第一种是HEIF HEVC,这是一种新格式,也称为HEIC文件,并且那里对深度有一流的支持...第二种格式是JPEG。伙计,JPEG并不是要这样做,但无论如何,我们还是让它做到了,如果地图经过过滤,则为8位有损JPEG,或者如果其中没有数字,则使用16位无损JPEG编码保留所有非数字,我们将其存储为JPEG底部的第二个图像,因此,如果您熟悉它,它就像一个多图片对象。”

当我将原始深度缓冲区(16位)与从存储的图像像素明智检索的深度缓冲区进行比较时,将得到以下结果:

First Second 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)
        }
    }
Run Code Online (Sandbox Code Playgroud)

ARG*_*Geo 0

文件的性质JPEG使您的尝试变得毫无意义。iOS 支持 8 位 3 通道 JPEG 或 16 位 8 通道 JPEG。然而,允许存储真实深度信息的文件格式,例如无损OpenEXR文件)支持 1023 通道内的 32 位颜色数据。这样的颜色深度对于保持强大的场景深度是必要的。这样数量的频道对于存储额外的有用频道是必要的。

因此,在我看来,您的情况最简单的解决方案是使用 32 位未压缩或压缩EXR文件来存储深度、Alpha、视差、运动和其他通道,而不是 Apple“手工制作”的 16 位 JPEG 文件。