How to get real world distance with/from iOS AVFoundation LiDAR?

254 views Asked by At

I am trying to use the depthmap data, but in a very simple way. I am reading data from the Lidar. The data is somehow unexpected. When the iPhone 14 Pro is parallel to a flat wall, say in a distance of 0.3 meter. I would expect to read that 0,3 meter (the one exactly opposite to the camera.) But do not. All data is more than 0,3 meter, even 1.2 meter and more. Any insight would be highly appreciated.

  func depthDataOutput(syncedDepthData: AVCaptureSynchronizedDepthData) {
        let depthData = syncedDepthData.depthData.converting(toDepthDataType: kCVPixelFormatType_DepthFloat16)
        let depthMapWidth = CVPixelBufferGetWidthOfPlane(depthData.depthDataMap, 0)
        let depthMapHeight = CVPixelBufferGetHeightOfPlane(depthData.depthDataMap, 0)
        print("Depth quality: \(depthData.depthDataQuality) accuracy: \(depthData.depthDataAccuracy) pixelSize: \(String(describing: depthData.cameraCalibrationData?.pixelSize))")
        CVPixelBufferLockBaseAddress(depthData.depthDataMap, .readOnly)
        if let rowData = CVPixelBufferGetBaseAddress(depthData.depthDataMap)?.assumingMemoryBound(to: Float16.self) {
            var depthArray = [Float16]()
            for y in 0...depthMapHeight-1 {
                var distancesLine = [Float16]()
                for x in 0...depthMapWidth-1 {
                    let distanceAtXYPoint = rowData[y * depthMapWidth + x]
                    distancesLine.append(distanceAtXYPoint)
                }
                let sum = distancesLine.reduce(0, +)
                let average = sum / Float16(distancesLine.count)
                let min_r = distancesLine.min()
                depthArray.append(Float16(min_r!))
            }
            let sum2 = depthArray.reduce(0, +)
            let average2 = sum2 / Float16(depthArray.count)
            let min = depthArray.min()            
            print("Depth value in meters avg: \(average2) min: \(min!) meters")
        }
        CVPixelBufferUnlockBaseAddress(depthData.depthDataMap, .readOnly)
    }
0

There are 0 answers