Posts

Post not yet marked as solved
1 Replies
1.3k Views
Hi All,I am trying to rectify a AVDepthMap so I can translate the depth map into a list of real world XYZ coordinates.I am not entirely sure whether i am doing this correct. I am especially not sure about whether to use the inverseLensdistortionLookupTable or lensDistorationLookupTable. private func rectifyDepthData(avDepthData: AVDepthData) -> CVPixelBuffer? { guard // which lookup table to use here??? inverse or not? let distortionLookupTable = avDepthData.cameraCalibrationData?.inverseLensDistortionLookupTable, let distortionCenter = avDepthData.cameraCalibrationData?.lensDistortionCenter else { return nil } let originalDepthDataMap = avDepthData.depthDataMap let width = CVPixelBufferGetWidth(originalDepthDataMap) let height = CVPixelBufferGetHeight(originalDepthDataMap) let scaledCenter = CGPoint(x: (distortionCenter.x / CGFloat(PHOTO_WIDTH)) * CGFloat(width), y: (distortionCenter.y / CGFloat(PHOTO_HEIGHT)) * CGFloat(height)) CVPixelBufferLockBaseAddress(originalDepthDataMap, CVPixelBufferLockFlags(rawValue: 0)) var maybePixelBuffer: CVPixelBuffer? let status = CVPixelBufferCreate(nil, width, height, avDepthData.depthDataType, nil, &maybePixelBuffer) assert(status == kCVReturnSuccess && maybePixelBuffer != nil); guard let rectifiedPixelBuffer = maybePixelBuffer else { return nil } CVPixelBufferLockBaseAddress(rectifiedPixelBuffer, CVPixelBufferLockFlags(rawValue: 0)) guard let address = CVPixelBufferGetBaseAddress(rectifiedPixelBuffer) else { return nil } for y in 0 ..< height{ let rowData = CVPixelBufferGetBaseAddress(originalDepthDataMap)! + y * CVPixelBufferGetBytesPerRow(originalDepthDataMap) let data = UnsafeBufferPointer(start: rowData.assumingMemoryBound(to: Float32.self), count: width) for x in 0 ..< width{ let oldPoint = CGPoint(x: x, y: y) let newPoint = rectifyDepthMap.lensDistortionPoint(for: oldPoint, lookupTable: distortionLookupTable, distortionOpticalCenter: scaledCenter, imageSize: CGSize(width: width, height: height) ) let val = data[x] let newRow = address + Int(newPoint.y) * CVPixelBufferGetBytesPerRow(rectifiedPixelBuffer) let newData = UnsafeMutableBufferPointer(start: newRow.assumingMemoryBound(to: Float32.self), count: width) newData[Int(newPoint.x)] = val } } CVPixelBufferUnlockBaseAddress(rectifiedPixelBuffer, CVPixelBufferLockFlags(rawValue: 0)) CVPixelBufferUnlockBaseAddress(originalDepthDataMap, CVPixelBufferLockFlags(rawValue: 0)) return rectifiedPixelBuffer }any other remarks about code style or best practices are also appreciated!
Posted Last updated
.