如何获取kCVPixelFormatType_DepthFloat16(半点浮点数)的值?
How to get the value of kCVPixelFormatType_DepthFloat16 (half-point float)?
我正在使用 swift 处理 iOS 前置深度相机的项目。根据 Apple 文档,媒体类型为 kCVPixelFormatType_DepthFloat16
,半点浮动,尺寸为 640*360,帧率为 30fps。我被困在如何进一步逐个像素地检索和处理值。
let buffer:CVPixelBuffer = depthData.depthDataMap //depthData is AVDepthData type
CVPixelBufferLockBaseAddress(buffer, CVPixelBufferLockFlags(rawValue: 0))
let width = CVPixelBufferGetWidth(buffer)
let height = CVPixelBufferGetHeight(buffer)
for y in 0 ..< height {
for x in 0 ..< width {
let pixel = ?? //what should I do here?
}
}
我已经解决了我的问题。这可以通过两种方式完成。
- 使用
kCVPixelFormatType_DepthFloat32
而不是kCVPixelFormatType_DepthFloat16
,它将具有与之前的深度图相同的维度和fps。然后你可以将其转换为 Swift Float
类型,如下所示:
let width = CVPixelBufferGetWidth(buffer)
let height = CVPixelBufferGetHeight(buffer)
CVPixelBufferLockBaseAddress(buffer, CVPixelBufferLockFlags(rawValue: 0))
let floatBuffer = unsafeBitCast(CVPixelBufferGetBaseAddress(buffer), to: UnsafeMutablePointer<Float>.self)
for y in 0 ..< height {
for x in 0 ..< width {
let pixel = floatBuffer[y*width+x]
}
}
CVPixelBufferUnlockBaseAddress(self, CVPixelBufferLockFlags(rawValue: 0))
- 第二种方式是先转成
UInt16
,再在前面加两个零字节
// to access the point height = y, width = x, thanks to this project https://github.com/edvardHua/Articles/tree/master/%5BAR:MR%20%E5%9F%BA%E7%A1%80%5D%20%E5%88%A9%E7%94%A8%20iPhone%20X%20%E7%9A%84%E6%B7%B1%E5%BA%A6%E7%9B%B8%E6%9C%BA(TruthDepth%20Camera)%E8%8E%B7%E5%BE%97%E5%83%8F%E7%B4%A0%E7%82%B9%E7%9A%84%E4%B8%89%E7%BB%B4%E5%9D%90%E6%A0%87/Obtain3DCoordinate
let rowData = CVPixelBufferGetBaseAddress(buffer)! + Int(y) * CVPixelBufferGetBytesPerRow(buffer)
var f16Pixel = rowData.assumingMemoryBound(to: UInt16.self)[x]
var f32Pixel = Float(0.0)
var src = vImage_Buffer(data: &f16Pixel, height: 1, width: 1, rowBytes: 2)
var dst = vImage_Buffer(data: &f32Pixel, height: 1, width: 1, rowBytes: 4)
vImageConvert_Planar16FtoPlanarF(&src, &dst, 0)
let depth = f32Pixel //depth in cm
我正在使用 swift 处理 iOS 前置深度相机的项目。根据 Apple 文档,媒体类型为 kCVPixelFormatType_DepthFloat16
,半点浮动,尺寸为 640*360,帧率为 30fps。我被困在如何进一步逐个像素地检索和处理值。
let buffer:CVPixelBuffer = depthData.depthDataMap //depthData is AVDepthData type
CVPixelBufferLockBaseAddress(buffer, CVPixelBufferLockFlags(rawValue: 0))
let width = CVPixelBufferGetWidth(buffer)
let height = CVPixelBufferGetHeight(buffer)
for y in 0 ..< height {
for x in 0 ..< width {
let pixel = ?? //what should I do here?
}
}
我已经解决了我的问题。这可以通过两种方式完成。
- 使用
kCVPixelFormatType_DepthFloat32
而不是kCVPixelFormatType_DepthFloat16
,它将具有与之前的深度图相同的维度和fps。然后你可以将其转换为 SwiftFloat
类型,如下所示:
let width = CVPixelBufferGetWidth(buffer)
let height = CVPixelBufferGetHeight(buffer)
CVPixelBufferLockBaseAddress(buffer, CVPixelBufferLockFlags(rawValue: 0))
let floatBuffer = unsafeBitCast(CVPixelBufferGetBaseAddress(buffer), to: UnsafeMutablePointer<Float>.self)
for y in 0 ..< height {
for x in 0 ..< width {
let pixel = floatBuffer[y*width+x]
}
}
CVPixelBufferUnlockBaseAddress(self, CVPixelBufferLockFlags(rawValue: 0))
- 第二种方式是先转成
UInt16
,再在前面加两个零字节
// to access the point height = y, width = x, thanks to this project https://github.com/edvardHua/Articles/tree/master/%5BAR:MR%20%E5%9F%BA%E7%A1%80%5D%20%E5%88%A9%E7%94%A8%20iPhone%20X%20%E7%9A%84%E6%B7%B1%E5%BA%A6%E7%9B%B8%E6%9C%BA(TruthDepth%20Camera)%E8%8E%B7%E5%BE%97%E5%83%8F%E7%B4%A0%E7%82%B9%E7%9A%84%E4%B8%89%E7%BB%B4%E5%9D%90%E6%A0%87/Obtain3DCoordinate
let rowData = CVPixelBufferGetBaseAddress(buffer)! + Int(y) * CVPixelBufferGetBytesPerRow(buffer)
var f16Pixel = rowData.assumingMemoryBound(to: UInt16.self)[x]
var f32Pixel = Float(0.0)
var src = vImage_Buffer(data: &f16Pixel, height: 1, width: 1, rowBytes: 2)
var dst = vImage_Buffer(data: &f32Pixel, height: 1, width: 1, rowBytes: 4)
vImageConvert_Planar16FtoPlanarF(&src, &dst, 0)
let depth = f32Pixel //depth in cm