DepthData - Get per-pixel depth data (CVPixelBuffer data analysis)

血红的双手。 提交于 2019-12-11 00:19:00

问题


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

  1. You need to make sure your AVCapturePhotoSettings() has isDepthDataDeliveryEnabled = true

  2. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!