gpt4 book ai didi

ios - xcode CVpixelBuffer 显示负值

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:18:11 24 4
gpt4 key购买 nike

我正在使用 xcode,目前正在尝试使用以下代码从像素缓冲区中提取像素值。但是,当我打印出像素值时,它包含负值。有人遇到过这样的问题吗?

部分代码如下

- (void)captureOutput:(AVCaptureOutput*)captureOutput didOutputSampleBuffer:
(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection*)connection
{

CVImageBufferRef Buffer = CMSampleBufferGetImageBuffer(sampleBuffer);

CVPixelBufferLockBaseAddress(Buffer, 0);
uint8_t* BaseAddress = (uint8_t*)CVPixelBufferGetBaseAddressOfPlane(Buffer, 0);
size_t Width = CVPixelBufferGetWidth(Buffer);
size_t Height = CVPixelBufferGetHeight(Buffer);
if (BaseAddress)
{
IplImage* Temporary = cvCreateImage(cvSize(Width, Height), IPL_DEPTH_8U, 4);

Temporary->imageData = (char*)BaseAddress;
for (int i = 0; i < Temporary->width * Temporary->height; ++i) {
NSLog(@"Pixel value: %d",Temporary->imageData[i]);
//where i try to print the pixels
}
}

最佳答案

问题是 IplImageimageData 是一个带符号的 char。因此,任何大于 127 的值都将显示为负数。

您可以简单地将它分配给一个 unsigned char,然后打印它,您将看到 0 到 255 之间的值,正如您可能预期的那样:

for (int i = 0; i < Temporary->width * Temporary->height; ++i) {
unsigned char c = Temporary->imageData[i];
NSLog(@"Pixel value: %u", c);
}

或者你可以用十六进制打印:

NSLog(@"Pixel value: %02x", c);

关于ios - xcode CVpixelBuffer 显示负值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28150512/

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