C ++,使用OpenCV的像素的负RGB值 [英] C++, Negative RGB values of pixels using OpenCV
问题描述
我使用OpenCV迭代图像并找到每个像素的颜色,这里是我使用的一些代码:
IplImage * img = cvLoadImage(c:\\test.png);
int pixels = img-> height * img-> width;
int channels = img-> nChannels;
for(int i = 0; i {
unsigned char red = img-> imageData [i + 2];
unsigned char green = img-> imageData [i + 1];
unsigned char blue = img-> imageData [i];
outputRGBValues(red,green,blue);
if(red == REDCOLOUR&& green == GREENCOLOUR&& blue == BLUECOLOUR)
{
count ++;
}
}
cvReleaseImage(& img);
当我运行它时,outputRGBValues输出负值。最常见的R,G,B = -1,但偶尔也有其他负数和少数正数。我听说过关于未初始化的内存内容,以及像素没有正确分配给内存。但我真的不明白它,绝对不知道如何解决它。
UPDATE
修正代码后(如上所述) )与fschmitt的变化,我有点接近。 这是我使用的图片,如果它有任何帮助。很难看到,但它只是一个5 * 3'V'的黑色像素,一个绿色的底部。
运行代码,我得到这个输出:
0 0 0
255 255 255
255 255 255
255 255 255
0 0 0
255 255 186
0 0 255
255 255 0
它继续
前5行很好,它们应该是什么。这是图像的顶行。下一行,第6行向上是错误的。应为:
255 255 255
255 255 255
0 0 0
我不知道是什么原因造成的。它如何工作的顶线而不是第二?
尝试这样:
IplImage * img = cvLoadImage(c:\\test.png);
for(int i = 0; i {
for(int j = 0; j< img-> width ; j + = img-> nChannels)
{
unsigned char red = img-> imageData [i * img-> widthStep + j + 2];
unsigned char green = img-> imageData [i * img-> widthStep + j + 1];
unsigned char blue = img-> imageData [i * img-> widthStep + j];
outputRGBValues(red,green,blue);
if(red == REDCOLOUR&& green == GREENCOLOUR&& blue == BLUECOLOUR)
{
count ++;
}
}
}
cvReleaseImage(& img);
I'm using OpenCV to iterate through an image and find the colour of each pixel, here's some of the code I'm using:
IplImage* img = cvLoadImage("c:\\test.png");
int pixels = img->height * img->width;
int channels = img->nChannels;
for (int i = 0; i < pixels*channels; i+= channels)
{
unsigned char red = img->imageData[i + 2];
unsigned char green = img->imageData[i + 1];
unsigned char blue = img->imageData[i];
outputRGBValues(red, green, blue);
if (red == REDCOLOUR && green == GREENCOLOUR && blue == BLUECOLOUR)
{
count++;
}
}
cvReleaseImage(&img);
When I run it, it outputRGBValues outputs negative values. Most often R, G, B = -1, but occasionally other negative numbers and a handful of positive numbers. I've heard something about uninitialized memory contents, and pixels not being allocated to the memory properly. But I'm don't really understand it and definitely don't know how to fix it. What am I doing wrong and how can I fix it?
UPDATE
After fixing the code (as above) with fschmitt's changes, I'm a bit closer. This is the image I'm using, if it's any help. Quite hard to see, but it's just a 5*3 'V' of black pixels with one green one at the bottom.
Running the code on it, I get this output:
0 0 0
255 255 255
255 255 255
255 255 255
0 0 0
255 255 186
0 0 255
255 255 0
And it continues
The first 5 lines are fine, exactly what they should be. That's the top row of the image. The next row, 6th line onwards is wrong. It should be:
255 255 255
255 255 255
0 0 0
I'm not sure what's causing this. How can it work for the top line and not the second? Is there some sort of mismatch, and its taking the value one bit to the left of where it should?
Try this:
IplImage* img = cvLoadImage("c:\\test.png");
for (int i = 0; i < img->height; i++)
{
for (int j = 0; j < img->width; j += img->nChannels)
{
unsigned char red = img->imageData[i * img->widthStep + j + 2];
unsigned char green = img->imageData[i * img->widthStep + j + 1];
unsigned char blue = img->imageData[i * img->widthStep + j];
outputRGBValues(red, green, blue);
if (red == REDCOLOUR && green == GREENCOLOUR && blue == BLUECOLOUR)
{
count++;
}
}
}
cvReleaseImage(&img);
这篇关于C ++,使用OpenCV的像素的负RGB值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!