ios - 如何获取kCVPixelFormatType_DepthFloat16(半点浮点数)的值?
问题描述
我正在使用 swift 处理 iOS 前置深度摄像头的项目。根据苹果文档,媒体类型为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
推荐阅读
- windows - 批处理脚本解释评论中的内容?
- wordpress - 多站点上的 WordPress ACF Pro 用户元
- docker - 使用 gcp 云构建创建 dockerfile 时出错
- excel - Excel VBA:当一个字符串是单元格中包含的文本值时,如何在 While 语句中使用 StrComp?
- php - 使用 phpWord 和 cakephp3 找不到类“Zend\Stdlib\StringUtils”
- android - Android Studio中colors.xml文件中的奇怪错误
- mysql - 如何使用 GROUP BY 并获取 MySQL 中的最后一个字符串?
- python - Tensorflow Custom Estimator: How to implement a `input_fn` function that returns a list of labels, and a list of features ?
- javascript - 如何为我的测验使用外部 json 文件
- angular - Angular 表单提交仅具有更改的字段 - 不是完整的表单值