I am trying to find depth data at a specific point in the captured image and return the distance to meters.
I included depth data and captured the data along with the image. I get the point from the X, Y coordinates of the center of the image (and when clicked) and convert it to a buffer index using
Int((width - touchPoint.x) * (height - touchPoint.y))
c WIDTHand HEIGHTare the dimensions of the captured image. I am not sure if this is the right method to achieve this.
I process depth data as such:
func handlePhotoDepthCalculation(point : Int) {
guard let depth = self.photo else {
return
}
let depthData = (depth.depthData as AVDepthData!).converting(toDepthDataType: kCVPixelFormatType_DepthFloat32)
let depthDataMap = depthData.depthDataMap
let accuracy = depthData.depthDataAccuracy
switch (accuracy) {
case .absolute:
self.accuracyLbl.text = "Absolute"
break
case .relative:
self.accuracyLbl.text = "Relative"
}
CVPixelBufferLockBaseAddress(depthDataMap, CVPixelBufferLockFlags(rawValue: 0))
let depthPointer = unsafeBitCast(CVPixelBufferGetBaseAddress(depthDataMap), to: UnsafeMutablePointer<Float32>.self)
let distanceAtXYPoint = depthPointer[point]
self.distanceLbl.text = "\(distanceAtXYPoint) m"
self.filteredLbl.text = "\(depthData.isDepthDataFiltered)"
}
I am not sure that I am getting the right position. My research also shows that precision returns only in .relativeor .absolute, and not in float / integer?