使用来自 AVDepthData 的差异的 iPhone X 后置摄像头的绝对深度?

Wes*_* W. 6 disparity-mapping stereo-3d depth ios swift

我正在尝试AVDepthData根据以下方程估计对象的绝对深度(以米为单位):depth = baseline x focus_length / (disparity + d_offset)。我有来自 的所有参数cameraCalibrationData,但是这是否仍然适用于使用 iPhone X 在人像模式下拍摄的图像,因为两个摄像头垂直偏移?同样基于WWDC 2017 Session 507,视差图是相对的,但AVDepthData文档指出视差值在 1/m 中。那么我可以按原样将方程应用于深度数据中的值,还是需要事先进行一些额外的处理?

var depthData: AVDepthData

do {
  depthData = try AVDepthData(fromDictionaryRepresentation: auxDataInfo)

} catch {
  return nil
}

// Working with disparity
if depthData.depthDataType != kCVPixelFormatType_DisparityFloat32 {
  depthData = depthData.converting(toDepthDataType: kCVPixelFormatType_DisparityFloat32)
}

CVPixelBufferLockBaseAddress(depthData.depthDataMap, CVPixelBufferLockFlags(rawValue: 0))

// Scale Intrinsic matrix to be in depth image pixel space
guard var intrinsicMatrix = depthData.cameraCalibrationData?.intrinsicMatrix else{ return nil}

let referenceDimensions = depthData.cameraCalibrationData?.intrinsicMatrixReferenceDimensions

let depthWidth = CVPixelBufferGetWidth(depthData.depthDataMap)
let depthHeight = CVPixelBufferGetHeight(depthData.depthDataMap)

let depthSize = CGSize(width: depthWidth, height: depthHeight)

let ratio: Float =  Float(referenceDimensions.width) / Float(depthWidth)

intrinsicMatrix[0][0] /= ratio;
intrinsicMatrix[1][1] /= ratio;
intrinsicMatrix[2][0] /= ratio;
intrinsicMatrix[2][1] /= ratio;

// For converting disparity to depth    
let baseline: Float = 1.45/100.0 // measured baseline in m

// Prepare for lens distortion correction
let lut = depthData.cameraCalibrationData?.lensDistortionLookupTable

let center = depthData.cameraCalibrationData?.lensDistortionCenter
let centerX: CGFloat = center!.x / CGFloat(ratio)
let centerY: CGFloat = center!.y / CGFloat(ratio)
let correctedCenter = CGPoint(x: centerX, y: centerY);            

// Build point cloud
var pointCloud = Array<Any>()

for dataY in 0 ..< depthHeight{
  let rowData = CVPixelBufferGetBaseAddress(depthData.depthDataMap)! + dataY * CVPixelBufferGetBytesPerRow(depthData.depthDataMap)
  let data = UnsafeBufferPointer(start: rowData.assumingMemoryBound(to: Float32.self), count: depthWidth)

  for dataX in 0 ..< depthWidth{
    let dispZ = data[dataX] 
    let pointZ = baseline * intrinsicMatrix[0][0] / dispZ 

    let currPoint: CGPoint = CGPoint(x: dataX,y: dataY)
    let correctedPoint: CGPoint = lensDistortionPoint(for: currPoint, lookupTable: lut!, distortionOpticalCenter: correctedCenter,imageSize: depthSize)

    let pointX = (Float(correctedPoint.x) - intrinsicMatrix[2][0]) * pointZ / intrinsicMatrix[0][0];
    let pointY = (Float(correctedPoint.y) - intrinsicMatrix[2][1]) * pointZ / intrinsicMatrix[1][1];

    pointCloud.append([pointX,pointY,pointZ])
  }
}

CVPixelBufferUnlockBaseAddress(depthData.depthDataMap, CVPixelBufferLockFlags(rawValue: 0))
Run Code Online (Sandbox Code Playgroud)