【发布时间】:2016-04-14 10:13:18
【问题描述】:
我正在尝试制作一个简单的程序来读取输入图像的像素值 (BGR) 并制作相同大小的输出图像,但如果原始图片中的像素是白色的,则输出图像的像素(相同位置)为蓝色,如果像素不是白色,则使输出图像中的相同像素为黑色。但是我很难让它正常工作。这似乎很容易,但它的表现并不像我期望的那样。下面是我的代码:
Mat input;
Mat output;
Vec3b color;
input = imread("C:/temp/COLORTEST2.png", CV_LOAD_IMAGE_UNCHANGED);
output = input;
int rows = input.rows;
int cols = input.cols;
int Blue, Green, Red;
cout << "Rows: " << rows << "\nColumns: " << cols << "\nTotal Pixels: " << (rows*cols) << endl;
cout << "Calculating...\n\n";
for (j = 0; j < rows; j++)
{
for (i = 0; i < cols; i++)
{
color = input.at<Vec3b>(Point(j, i));
Blue = color.val[0];
Green = color.val[1];
Red = color.val[2];
if (Blue == 255 && Green == 255 && Red == 255){
W++; //White count +1
output.at<Vec3b>(Point(j, i))[0] = 255;
output.at<Vec3b>(Point(j, i))[1] = 0;
output.at<Vec3b>(Point(j, i))[2] = 0;
}
else{
NW++; //NonWhite count +1
output.at<Vec3b>(Point(j, i))[0] = 0;
output.at<Vec3b>(Point(j, i))[1] = 0;
output.at<Vec3b>(Point(j, i))[2] = 0;
//cin >> response;
}
}
}
cout << "White Pixels: " << W << "\nNon-White Pixels: " << NW << endl;
imwrite("C:/temp/Output.png", output);
waitKey();
就像我说的,看起来很简单,但是当我运行它时它崩溃了。如果我在 for 循环中切换行和列,它运行良好,但输出图像看起来不像它应该的(带有几个黑色像素的垂直条带)。函数“mat.at(Point(j, i))”似乎是问题所在,看起来它在某个点崩溃,好像它在寻找一个几乎不存在的点,就好像行和列是切换了,但这没有意义...我还通过制作 MS 绘画图片 (20 x 30) 并将单个黑色像素放在 1x1 上对其进行了测试,它说在 1x1 时它的 BGR 为 255, 0, 0 ...这意味着它认为它是蓝色的并且更没有意义...我真的很难过,如果有人对这个功能及其工作原理有任何建议,我将非常感激。
编辑:这是输出和输入图像。这是我所说的错误,输出被剥离甚至没有完成:http://imgur.com/a/DPRlO
规格:
视窗 8.1
视觉工作室 2013
OpenCV 2.4.10
C++
【问题讨论】:
-
请分享错误消息,它崩溃的行..任何有助于快速识别问题的信息
标签: c++ arrays opencv for-loop pixels