Love this tutorial and the code.
I have one question though. Iβd like to save the image with the manipulated depth data back to the camera roll. But not as the finished processed image. Iβd like it to show up in the photos app as an image with portrait mode. Where you can change the focal length etc
In short: is this possible? Load an image with depth data. Manipulate depth data. Save it back as an image with depth information.
CgdestinationAddAuxData etc but I wasnβt able to do it. Would be cool if the tutorial covered this topic too! Thanks for your great work!
Hi Owen, very nice tutorial. One suggestion for the normalize function would be to use the Accelerate framework vector based functions. Itβs much faster than stepping pixel by pixel. And also something people should learn about.
I replaced your normalize() with the following two functions.
best regards
Will L-B
import Accelerate
extension CVPixelBuffer {
func vectorNormalize( targetVector: UnsafeMutableBufferPointer) β [Float] {
// range = max - min
// normalized to 0β¦1 is (pixel - minPixel) / range
// see Documentation "Using vDSP for Vector-based Arithmetic" in vDSP under system "Accelerate" documentation
// see also the Accelerate documentation section 'Vector extrema calculation'
// Maximium static func maximum<U>(U) -> Float
// Returns the maximum element of a single-precision vector.
//static func minimum<U>(U) -> Float
// Returns the minimum element of a single-precision vector.
let maxValue = vDSP.maximum(targetVector)
let minValue = vDSP.minimum(targetVector)
let range = maxValue - minValue
let negMinValue = -minValue
let subtractVector = vDSP.add(negMinValue, targetVector)
// adding negative value is subtracting
let result = vDSP.divide(subtractVector, range)
return result