ios - xcode CVpixelBuffer 显示负值
<p><p>我正在使用 xcode,目前正尝试使用以下代码从像素缓冲区中提取像素值。但是,当我打印出像素值时,它由负值组成。有人遇到过这样的问题吗?</p>
<p>部分代码如下</p>
<pre><code>- (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);
//where i try to print the pixels
}
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>问题是 <code>IplImage</code> 的 <code>imageData</code> 是签名的 <code>char</code>。因此,任何大于 127 的都将显示为负数。</p>
<p>您可以简单地将其分配给 <code>unsigned char</code>,然后将其打印出来,您将看到 0 到 255 之间的值,正如您可能预期的那样:</p>
<pre><code>for (int i = 0; i < Temporary->width * Temporary->height; ++i) {
unsigned char c = Temporary->imageData;
NSLog(@"Pixel value: %u", c);
}
</code></pre>
<p>或者你可以用十六进制打印:</p>
<pre><code>NSLog(@"Pixel value: %02x", c);
</code></pre></p>
<p style="font-size: 20px;">关于ios - xcode CVpixelBuffer 显示负值,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/28150512/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/28150512/
</a>
</p>
页:
[1]