DepthData - Get per-pixel depth data (CVPixelBuffer data analysis)

3k Views Asked by At

Now I run AVDepthPhotoFilter that Rendering Depth Deta from a stereo camera of iPhone7Plus.

So, I want to access per-pixel depth data, but, I don’t know how to do it. Please advice.

1

There are 1 best solutions below

1
On

How to get DepthData and analysis CVPixelBuffer data

  1. You need to make sure your AVCapturePhotoSettings() has isDepthDataDeliveryEnabled = true

  2. You have to use the function 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)]
    
    }
    

If you want more informations about CVPixelBuffer analysis, here is a useful post -> details