问题
Now I run AVDepthPhotoFilter
that Rendering Depth Deta from a stereo camera of iPhone7Plus.
So, I want to access per-pixel depth data, but, I don’t know how to do it. Please advice.
回答1:
How to get DepthData and analysis CVPixelBuffer data
You need to make sure your AVCapturePhotoSettings() has isDepthDataDeliveryEnabled = true
You have to use the function func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?)
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) { //## Convert Disparity to Depth ## let depthData = (photo.depthData as AVDepthData!).converting(toDepthDataType: kCVPixelFormatType_DepthFloat32) let depthDataMap = depthData.depthDataMap //AVDepthData -> CVPixelBuffer //## Data Analysis ## // Useful data let width = CVPixelBufferGetWidth(depthDataMap) //768 on an iPhone 7+ let height = CVPixelBufferGetHeight(depthDataMap) //576 on an iPhone 7+ CVPixelBufferLockBaseAddress(depthDataMap, CVPixelBufferLockFlags(rawValue: 0)) // Convert the base address to a safe pointer of the appropriate type let floatBuffer = unsafeBitCast(CVPixelBufferGetBaseAddress(depthDataMap), to: UnsafeMutablePointer<Float32>.self) // Read the data (returns value of type Float) // Accessible values : (width-1) * (height-1) = 767 * 575 let distanceAtXYPoint = floatBuffer[Int(x * y)] }
If you want more informations about CVPixelBuffer analysis, here is a useful post -> details
来源:https://stackoverflow.com/questions/46794561/depthdata-get-per-pixel-depth-data-cvpixelbuffer-data-analysis