DepthData - 获取每像素深度数据(CVPixelBuffer数据分析)

时间:2017-10-17 16:08:12

标签: swift ios11 cvpixelbuffer iphone7plus

现在我从iPhone7Plus的立体相机中运行渲染Depth Deta的AVDepthPhotoFilter

所以,我想访问每像素深度数据,但是,我不知道该怎么做。请指教。

1 个答案:

答案 0 :(得分:6)

如何获取DepthData和分析CVPixelBuffer数据

  1. 您需要确保您的AVCapturePhotoSettings()具有isDepthDataDeliveryEnabled = true

  2. 你必须使用函数 func photoOutput(_ output:AVCapturePhotoOutput,didFinishProcessingPhoto photo:AVCapturePhoto,error:Error?)

    func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
    
        //## Convert Disparity to Depth ##
    
        let depthData = (photo.depthData as AVDepthData!).converting(toDepthDataType: kCVPixelFormatType_DepthFloat32)
        let depthDataMap = depthData.depthDataMap //AVDepthData -> CVPixelBuffer
    
        //## Data Analysis ##
    
        // Useful data
        let width = CVPixelBufferGetWidth(depthDataMap) //768 on an iPhone 7+
        let height = CVPixelBufferGetHeight(depthDataMap) //576 on an iPhone 7+
        CVPixelBufferLockBaseAddress(depthDataMap, CVPixelBufferLockFlags(rawValue: 0))
    
        // Convert the base address to a safe pointer of the appropriate type
        let floatBuffer = unsafeBitCast(CVPixelBufferGetBaseAddress(depthDataMap), to: UnsafeMutablePointer<Float32>.self)
    
        // Read the data (returns value of type Float)
        // Accessible values : (width-1) * (height-1) = 767 * 575
    
        let distanceAtXYPoint = floatBuffer[Int(x * y)]
    
    }
    
  3. 如果您想了解有关CVPixelBuffer分析的更多信息,这里有一个有用的帖子 - &gt; details