Thanks for being a part of WWDC25!

How did we do? We’d love to know your thoughts on this year’s conference. Take the survey here

How to get the actual distance of the depth map image subject from the true depth camera

I was able to obtain the depth map image using AVCapturePhotoOutput from the delegate method

func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: (any Error)?)

I convert the depth map to kCVPixelFormatType_DepthFloat32 format and get the pixel values of the depth map using the below code

 func convertDepthData(depthMap: CVPixelBuffer) -> [[Float32]] {
        let width = CVPixelBufferGetWidth(depthMap)
        let height = CVPixelBufferGetHeight(depthMap)
        var convertedDepthMap: [[Float32]] = Array(
            repeating: Array(repeating: 0, count: width),
            count: height
        )
        CVPixelBufferLockBaseAddress(depthMap, CVPixelBufferLockFlags(rawValue: 2))
        let floatBuffer = unsafeBitCast(
            CVPixelBufferGetBaseAddress(depthMap),
            to: UnsafeMutablePointer<Float32>.self
        )
        for row in 0 ..< height {
            for col in 0 ..< width {
                if floatBuffer[width * row + col].isFinite{
                    convertedDepthMap[row][col] = floatBuffer[width * row + col]
                }
            }
        }
        CVPixelBufferUnlockBaseAddress(depthMap, CVPixelBufferLockFlags(rawValue: 2))
        return convertedDepthMap
    }

Is this the right way of accessing the depth float values from a depth map. And what will be the unit for it. Because some times the depth values are in range of 0.7 when I keep the device close to the subject around 15 to 30 cm.

Hello @SaiBalaji,

Is this the right way of accessing the depth float values from a depth map.

Not really, but before I give a recommendation, I'd like to understand more about your use-case here. For many use-cases, it does not make sense to convert your depth map to a 2D-array.

And what will be the unit for it.

Meters.

Because some times the depth values are in range of 0.7 when I keep the device close to the subject around 15 to 30 cm.

Consider that the depth data returned to you comes with both an accuracy and a quality.

Best regards,

Greg

How to get the actual distance of the depth map image subject from the true depth camera
 
 
Q