gpt4 book ai didi

ios - 如何在 Swift 中解释从 CMSampleBuffer 派生的像素数组

转载 作者:行者123 更新时间:2023-12-01 16:12:34 27 4
gpt4 key购买 nike

也许这是一个非常愚蠢的问题。
我在我的应用程序中使用 AVFoundation 并且能够获取帧(32BGRA 格式)。
帧的宽度为 1504,高度为 1128,每行字节数为 6016。
当我从这个样本缓冲区创建一个 UInt8 像素数组时,这个数组的长度(array.count)是 1696512,恰好等于宽度 * 高度。

我没有得到的是为什么数组长度是宽度 * 高度。不应该是宽*高*4。

我在这里想念什么?

编辑 - 1:代码

func BufferToArray(sampleBuffer: CMSampleBuffer) -> ([UInt8], Int, Int, Int) {

var rgbBufferArray = [UInt8]()

//Get pixel Buffer from CMSSampleBUffer
let pixelBuffer: CVPixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)!

//Lock the base Address
CVPixelBufferLockBaseAddress(pixelBuffer, CVPixelBufferLockFlags.readOnly)

let width = CVPixelBufferGetWidth(pixelBuffer)
let height = CVPixelBufferGetHeight(pixelBuffer)
//get pixel count
let pixelCount = CVPixelBufferGetWidth(pixelBuffer) * CVPixelBufferGetHeight(pixelBuffer)

//Get base address
let baseAddress = CVPixelBufferGetBaseAddress(pixelBuffer)

//Get bytes per row of the image
let bytesPerRow = CVPixelBufferGetBytesPerRow(pixelBuffer)

//Cast the base address to UInt8. This is like an array now
let frameBuffer = baseAddress?.assumingMemoryBound(to: UInt8.self)


rgbBufferArray = Array(UnsafeMutableBufferPointer(start: frameBuffer, count: pixelCount))


//Unlock and release memory
CVPixelBufferUnlockBaseAddress(pixelBuffer, CVPixelBufferLockFlags(rawValue: 0))

return (rgbBufferArray, bytesPerRow, width, height)

}

最佳答案

罪魁祸首是数据类型( UInt8 )与 count 的组合。 :

您假设内存包含 UInt8 assumingMemoryBound(to: UInt8.self) 的值 ( pixelCount )数数。但是,正如您正确得出的结论,它应该是该数字的四倍。

我会推荐你​​import simd并使用 simd_uchar4作为数据类型。这是一个包含 4 UInt8 的结构类型.然后您的数组将包含 pixelCount 4 元组像素值的值。您可以通过 array[index].x 访问 channel , .y , .z , 和 .w分别。

关于ios - 如何在 Swift 中解释从 CMSampleBuffer 派生的像素数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61015358/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com