我正在使用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
}
}
答案 0 :(得分:1)
问题是imageData
IplImage
是签名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);