Post not yet marked as solved
Post marked as unsolved with 1 replies, 981 views
Hello,
I am currently working on figuring out how to export the TrueDepth camera Depth data from the sample code found here - https://developer.apple.com/documentation/avfoundation/cameras_and_media_capture/streaming_depth_data_from_the_truedepth_camera. I want to convert AVDepthData object to a list of real world XYZ coordinate, that i could export to a .txt file.
After searching the developer forum, I stumbled upon this arcticle - https://developer.apple.com/forums/thread/106438?answerId=345004022#345004022. The following code:
privatefunc getPoints(avDepthData: AVDepthData)->Array{
				let depthData = avDepthData.converting(toDepthDataType: kCVPixelFormatType_DepthFloat32)
				guard let intrinsicMatrix = avDepthData.cameraCalibrationData?.intrinsicMatrix,
				}
			
				CVPixelBufferLockBaseAddress(depthDataMap, CVPixelBufferLockFlags(rawValue: 0))
			
				let width = CVPixelBufferGetWidth(depthDataMap)
				let height = CVPixelBufferGetHeight(depthDataMap)
			
				var points = Array()
				let focalX = Float(width) * (intrinsicMatrix[0][0] / PHOTO_WIDTH)
				let focalY = Float(height) * ( intrinsicMatrix[1][1] / PHOTO_HEIGHT)
				let principalPointX = Float(width) * (intrinsicMatrix[2][0] / PHOTO_WIDTH)
				let principalPointY = Float(height) * (intrinsicMatrix[2][1] / PHOTO_HEIGHT)
				for y in 0 ..< height{
						for x in 0 ..< width{
								guard let Z = getDistance(at: CGPoint(x: x, y: y) , depthMap: depthDataMap, depthWidth: width, depthHeight: height) else {
										continue
								}
							
								let X = (Float(x) - principalPointX) * Z / focalX
								let Y = (Float(y) - principalPointY) * Z / focalY
								points.append(PointXYZ(x: X, y: Y, z: Z))
						}
				}
				CVPixelBufferUnlockBaseAddress(depthDataMap, CVPixelBufferLockFlags(rawValue: 0))
			
				return points
		}
The problem with this is, that the sample code doesn't implement PHOTOWIDTH and PHOTOHEIGHT. Also, the rectification of the depth map was not posted, in my scenario it is not needed at the moment.
Maybe someone can help to manipulate this code in order to get the values ?