我正在使用 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-推荐答案 strong>
问题是 IplImage 的 imageData 是签名的 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/
|