如何规范iOS中的差异数据?

Jim*_*mmy 10 depth ios swift

在WWDC会议"深度图像编辑"中,他们提到了几次,normalizedDisparity并且normalizedDisparityImage:

"我们的基本想法是将标准化的差异值映射到0到1之间的值"

"因此,一旦你知道最小值和最大值,就可以将0和1之间的深度或差异标准化."

我试着先得到这样的混淆图像:

let disparityImage = depthImage.applyingFilter(
    "CIDepthToDisparity", withInputParameters: nil)
Run Code Online (Sandbox Code Playgroud)

然后我尝试获取depthDataMap并进行规范化,但它不起作用.我是在正确的轨道上吗?我会欣赏一些关于该怎么做的提示.

编辑:

这是我的测试代码,对不起质量.我得到了minmax然后我试图遍历所有的数据正常化它(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
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我所期望的是数据将反映用户点击图像但不匹配的差异.找到用户点击的差异的代码在这里:

// 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
Run Code Online (Sandbox Code Playgroud)

Fer*_*heh 6

raywenderlich.com 上有一篇名为“ 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))
  }
}  
Run Code Online (Sandbox Code Playgroud)

在处理深度数据时要记住,它们的分辨率低于实际图像的分辨率,因此您需要放大(博客和WWDC 视频中的更多信息)