DepthData-获取每个像素的深度数据(CVPixelBuffer数据分析) [英] DepthData - Get per-pixel depth data (CVPixelBuffer data analysis)
问题描述
现在,我运行的 AVDepthPhotoFilter
可以从iPhone7Plus的立体摄像机渲染深度Deta.
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.
推荐答案
如何获取DepthData和分析CVPixelBuffer数据
-
您需要确保AVCapturePhotoSettings()的isDepthDataDeliveryEnabled = true
You need to make sure your AVCapturePhotoSettings() has isDepthDataDeliveryEnabled = true
您必须使用函数 func photoOutput(_输出:AVCapturePhotoOutput,didFinishProcessingPhoto照片:AVCapturePhoto,错误:错误?)
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)]
}
如果您想了解有关CVPixelBuffer分析的更多信息,请参阅以下有用的帖子-> 详细信息
If you want more informations about CVPixelBuffer analysis, here is a useful post -> details
这篇关于DepthData-获取每个像素的深度数据(CVPixelBuffer数据分析)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!