Saturday, 29 September 2018

Displaying Depth Map from PHAsset

I am trying to read the depth map and overlay it over the UIImageView of a PHAsset.

So far I have this:

PHImageManager.default().requestImageData(for: self.asset!, options: nil, resultHandler:{ (data, responseString, orientation, info) -> Void in
    if let imageData: Data = data {
        if let imageSource = CGImageSourceCreateWithData(imageData as CFData, nil) {

            let imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil)! as NSDictionary

            let auxDataType = kCGImageAuxiliaryDataTypeDisparity
            let auxDataInfo = CGImageSourceCopyAuxiliaryDataInfoAtIndex(imageSource, 0, auxDataType)
            if auxDataInfo == nil {
                print ("NO DEPTH")
            } else {
                do {
                    var depthData = try AVDepthData(fromDictionaryRepresentation: auxDataInfo as! [AnyHashable : Any])
                    if depthData.depthDataType != kCVPixelFormatType_DisparityFloat16 {
                        depthData = depthData.converting(toDepthDataType: kCVPixelFormatType_DisparityFloat16)
                    }
                    let depthMap : CVPixelBuffer = depthData.depthDataMap
                    var ciImage = CIImage(cvPixelBuffer: depthMap)
                    ciImage = ciImage.applyingFilter("CIDisparityToDepth", parameters: [:])
                    DispatchQueue.main.async {
                        self.imageView.image = UIImage(ciImage: ciImage)
                    }
                }
                catch { }

            }
        }

    }
})

Issues:

  1. Depth map is incorrect and seems to be vague.
  2. Orientation of depth map is incorrect
  3. Depth map color is red for some reason


from Displaying Depth Map from PHAsset

No comments:

Post a Comment