OStack程序员社区-中国程序员成长平台

标题: ios - xcode CVpixelBuffer 显示负值 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 05:00
标题: ios - xcode CVpixelBuffer 显示负值

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

部分代码如下

- (void)captureOutputAVCaptureOutput*)captureOutput didOutputSampleBuffer:
(CMSampleBufferRef)sampleBuffer fromConnectionAVCaptureConnection*)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(@"ixel value: %d",Temporary->imageData[i]);
        //where i try to print the pixels
    }
}



Best Answer-推荐答案


问题是 IplImageimageData 是签名的 char。因此,任何大于 127 的都将显示为负数。

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

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

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

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

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






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4