在 WWDC session “深度图像编辑”中,他们多次提到 normalizedDisparity
和 normalizedDisparityImage
:
"The basic idea is that we're going to map our normalized disparity values into values between 0 and 1"
"So once you know the min and max you can normalize the depth or disparity between 0 and 1."
我试图首先像这样获得视差图像:
let disparityImage = depthImage.applyingFilter(
"CIDepthToDisparity", withInputParameters: nil)
然后我尝试获取 depthDataMap并进行标准化,但没有用。我在正确的轨道上吗?将不胜感激一些关于该做什么的提示。
编辑:
这是我的测试代码,质量问题请见谅。我得到 min
和 max
然后我尝试遍历数据以对其进行归一化(let normalizedPoint = (point - min)/(max - min)
)
let depthDataMap = depthData!.depthDataMap
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)
var min = floatBuffer[0]
var max = floatBuffer[0]
for x in 0..<width{
for y in 0..<height{
let distanceAtXYPoint = floatBuffer[Int(x * y)]
if(distanceAtXYPoint < min){
min = distanceAtXYPoint
}
if(distanceAtXYPoint > max){
max = distanceAtXYPoint
}
}
}
我期望的是数据将反射(reflect)用户点击图像但不匹配的差异。查找用户点击的视差的代码是here :
// Apply the filter with the sampleRect from the user’s tap. Don’t forget to clamp!
let minMaxImage = normalized?.clampingToExtent().applyingFilter(
"CIAreaMinMaxRed", withInputParameters:
[kCIInputExtentKey : CIVector(cgRect:rect2)])
// A four-byte buffer to store a single pixel value
var pixel = [UInt8](repeating: 0, count: 4)
// Render the image to a 1x1 rect. Be sure to use a nil color space.
context.render(minMaxImage!, toBitmap: &pixel, rowBytes: 4,
bounds: CGRect(x:0, y:0, width:1, height:1),
format: kCIFormatRGBA8, colorSpace: nil)
// The max is stored in the green channel. Min is in the red.
let disparity = Float(pixel[1]) / 255.0
最佳答案
raywenderlich.com 上有一篇名为“Image Depth Maps Tutorial for iOS”的新博文,其中包含示例应用程序和与深度相关的详细信息。示例代码展示了如何使用 CVPixelBuffer
扩展规范化深度数据:
extension CVPixelBuffer {
func normalize() {
let width = CVPixelBufferGetWidth(self)
let height = CVPixelBufferGetHeight(self)
CVPixelBufferLockBaseAddress(self, CVPixelBufferLockFlags(rawValue: 0))
let floatBuffer = unsafeBitCast(CVPixelBufferGetBaseAddress(self), to: UnsafeMutablePointer<Float>.self)
var minPixel: Float = 1.0
var maxPixel: Float = 0.0
for y in 0 ..< height {
for x in 0 ..< width {
let pixel = floatBuffer[y * width + x]
minPixel = min(pixel, minPixel)
maxPixel = max(pixel, maxPixel)
}
}
let range = maxPixel - minPixel
for y in 0 ..< height {
for x in 0 ..< width {
let pixel = floatBuffer[y * width + x]
floatBuffer[y * width + x] = (pixel - minPixel) / range
}
}
CVPixelBufferUnlockBaseAddress(self, CVPixelBufferLockFlags(rawValue: 0))
}
}
处理深度数据时要记住,它们的分辨率低于实际图像,因此您需要按比例放大(更多信息在博客和 WWDC video 中)
关于ios - 如何规范化 iOS 中的视差数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47117001/