2

抱歉,我重复了这个问题How to build AVDepthData manual,因为它没有我想要的答案,而且我没有足够的代表在那里发表评论。如果您不介意,我可以在以后删除我的问题,并请某人将未来的答案移至该主题。

所以,我的目标是创建深度数据并将其附加到任意图像。有一篇关于如何做到这一点的文章https://developer.apple.com/documentation/avfoundation/avdepthdata/creating_auxiliary_depth_data_manually,但我不知道如何实现它的任何步骤。我不会一次发布所有问题,而是从第一个问题开始。

作为第一步,深度图像必须按像素从灰度转换为深度或视差值。我从上述主题中获取了这个片段:

func buildDepth(image: UIImage) -> AVDepthData? {
        let width = Int(image.size.width)
        let height = Int(image.size.height)
        var maybeDepthMapPixelBuffer: CVPixelBuffer?
        let status = CVPixelBufferCreate(kCFAllocatorDefault, width, height, kCVPixelFormatType_DisparityFloat32, nil, &maybeDepthMapPixelBuffer)

        guard status == kCVReturnSuccess, let depthMapPixelBuffer = maybeDepthMapPixelBuffer else {
            return nil
        }

        CVPixelBufferLockBaseAddress(depthMapPixelBuffer, .init(rawValue: 0))

        guard let baseAddress = CVPixelBufferGetBaseAddress(depthMapPixelBuffer) else {
            return nil
        }

        let buffer = unsafeBitCast(baseAddress, to: UnsafeMutablePointer<Float32>.self)

        for i in 0..<width * height {
            buffer[i] = 0 // disparity must be calculated somehow, but set to 0 for testing purposes
        }

        CVPixelBufferUnlockBaseAddress(depthMapPixelBuffer, .init(rawValue: 0))

        let info: [AnyHashable: Any] = [kCGImagePropertyPixelFormat: kCVPixelFormatType_DisparityFloat32,
                                        kCGImagePropertyWidth: image.size.width,
                                        kCGImagePropertyHeight: image.size.height,
                                        kCGImagePropertyBytesPerRow: CVPixelBufferGetBytesPerRow(depthMapPixelBuffer)]

        let metadata = generateMetadata(image: image)
        let dic: [AnyHashable: Any] = [kCGImageAuxiliaryDataInfoDataDescription: info,
// I get an error when converting baseAddress to CFData
                                       kCGImageAuxiliaryDataInfoData: baseAddress as! CFData,
                                       kCGImageAuxiliaryDataInfoMetadata: metadata]

        guard let depthData = try? AVDepthData(fromDictionaryRepresentation: dic) else {
            return nil
        }

        return depthData
    }

然后文章说将像素缓冲区的基地址(其中是视差图)加载为 CFData 并将其作为kCGImageAuxiliaryDataInfoData值传递给 CFDictionary。但是在将 baseAddress 转换为 CFData 时出现错误。我也尝试转换像素缓冲区本身,但没有运气。我必须将什么作为 kCGImageAuxiliaryDataInfoData 传递?我是否首先正确创建了视差缓冲区?

除了这个问题,如果有人可以指导我参考一些关于如何完成整个事情的示例代码,那将是很酷的。

4

1 回答 1

0

你的问题真的帮助我从 cvPixelBuffer 到 AVDepthData 所以谢谢。大约是那里的95%。

为了解决您(和我的)问题,我添加了以下内容:

let bytesPerRow = CVPixelBufferGetBytesPerRow(depthMapPixelBuffer)
let size = bytesPerRow * height;
... code code code ...

CVPixelBufferLockBaseAddress(depthMapPixelBuffer!, .init(rawValue: 0))
let baseAddress = CVPixelBufferGetBaseAddressOfPlane(depthMapPixelBuffer!, 0)
let data = NSData(bytes: baseAddress, length: size);
... code code code ...

let dic: [AnyHashable: Any] = [kCGImageAuxiliaryDataInfoDataDescription: info,
                                   kCGImageAuxiliaryDataInfoData: data,
                                   kCGImageAuxiliaryDataInfoMetadata: metadata]
于 2021-08-24T17:06:47.730 回答