openCV:Trying 查找二值图像中的黑色像素
openCV:Trying to find black pixels in binary image
我有一个 1 位深度的单色 BMP 文件。
http://i.stack.imgur.com/MrOsU.png
我正在尝试查找此图像中黑色像素的位置和数量。
这是我的代码:
#include<opencv2/opencv.hpp>
#include<opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat img = imread("/home/jordan/opencv/Test/edge_detection/img2.bmp",0);
cout<<"cols= "<<img.cols<<"row= "<<img.rows<<"\n";
int i,j,count=0;
for(i=0;i<=img.cols;i++)
{
for(j=0;j<=img.rows;j++)
{
int k=img.at<uchar>(j,i);
if(k==0)
{
count++;
cout<<"col="<<i<<"row="<<j<<"k= "<< k<<"\t\n";
}
}
}
cout<<"count="<<count<<"\n";
}
从下面的输出中可以看出,我在第 195 行得到了很多零,根据上图,这是不正确的。请帮我解决这个问题。
输出:
clo=98row=69 k=0 clo=98row=195 k=0 clo=99row=69 k=0 clo=99row=195
k=0 clo=100row=69 k=0 clo=100row=195 k=0 clo=101row=69 k=0
clo=101row=195 k=0 clo=102row=69 k=0 clo=102row=195 k=0
clo=103row=68 k=0 clo=103row=195 k=0 clo=104row=68 k=0
clo=104row=195 k=0 clo=105row=68 k=0 clo=105row=195 k=0
clo=106row=68 k=0 clo=106row=195 k=0 clo=107row=68 k=0
clo=107row=195 k=0 clo=108row=68 k=0 clo=108row=195 k=0
clo=109row=68 k=0 clo=109row=195 k=0 clo=110row=68 k=0
clo=110row=195 k=0 clo=111row=68 k=0 clo=111row=195 k=0
clo=112row=68 k=0 clo=112row=195 k=0 clo=113row=68 k=0
clo=113row=195 k=0 clo=114row=68 k=0 clo=114row=195 k=0
clo=115row=68 k=0 clo=115row=195 k=0 clo=116row=68 k=0
clo=116row=195 k=0 clo=117row=68 k=0 clo=117row=195 k=0
clo=118row=68 k=0 clo=118row=195 k=0 clo=119row=68 k=0
clo=119row=195 k=0 clo=120row=68 k=0 clo=120row=195 k=0
clo=121row=68 k=0 clo=121row=195 k=0 clo=122row=68 k=0
clo=122row=195 k=0 clo=123row=68 k=0 clo=123row=195 k=0
clo=124row=68 k=0 clo=124row=195 k=0 clo=125row=68 k=0
clo=125row=195 k=0 clo=126row=68 k=0 clo=126row=195 k=0
clo=127row=69 k=0 clo=127row=195 k=0 clo=128row=69 k=0
clo=128row=195 k=0 clo=129row=69 k=0 clo=129row=195 k=0
clo=130row=69 k=0 clo=130row=195 k=0 clo=131row=69 k=0
clo=131row=195 k=0 clo=132row=69 k=0 clo=132row=195 k=0
clo=133row=69 k=0 clo=133row=195 k=0 clo=134row=69 k=0
clo=134row=195 k=0 clo=135row=69 k=0 clo=135row=195 k=0
clo=136row=69 k=0 clo=136row=195 k=0 clo=137row=70 k=0
clo=137row=195 k=0 clo=138row=70 k=0 clo=138row=195 k=0
clo=139row=70 k=0 clo=139row=195 k=0 clo=140row=70 k=0
clo=140row=195 k=0 clo=141row=70 k=0 clo=141row=195 k=0
clo=142row=70 k=0 clo=142row=195 k=0 clo=143row=70 k=0
clo=143row=195 k=0 clo=144row=71 k=0 clo=144row=195 k=0
clo=145row=71 k=0 clo=145row=195 k=0 clo=146row=71 k=0
clo=146row=195 k=0 clo=147row=71 k=0 clo=147row=195 k=0
clo=148row=71 k=0 clo=148row=195 k=0 clo=149row=72 k=0
clo=149row=195 k=0 clo=150row=72 k=0 clo=150row=195 k=0
clo=151row=72 k=0 clo=151row=195 k=0 clo=152row=72 k=0
clo=152row=195 k=0 clo=153row=73 k=0 clo=153row=195 k=0
clo=154row=73 k=0 clo=154row=195 k=0 clo=155row=73 k=0
clo=155row=195 k=0 clo=156row=73 k=0 clo=156row=195 k=0
clo=157row=74 k=0 clo=157row=195 k=0 clo=158row=74 k=0
clo=158row=195 k=0 clo=159row=74 k=0 clo=159row=195 k=0
clo=160row=74 k=0 clo=160row=195 k=0 clo=161row=75 k=0
clo=161row=195 k=0 clo=162row=75 k=0 clo=162row=195 k=0
clo=163row=75 k=0 clo=163row=195 k=0 clo=164row=76 k=0
clo=164row=195 k=0 clo=165row=76 k=0 clo=165row=195 k=0
clo=166row=76 k=0 clo=166row=195 k=0 clo=167row=76 k=0
clo=167row=195 k=0 clo=168row=77 k=0 clo=168row=195 k=0
clo=169row=77 k=0 clo=169row=195 k=0 clo=170row=78 k=0
clo=170row=195 k=0 clo=171row=78 k=0 clo=171row=195 k=0
clo=172row=78 k=0 clo=172row=195 k=0 clo=173row=79 k=0
clo=173row=195 k=0 clo=174row=79 k=0 clo=174row=195 k=0
clo=175row=79 k=0 clo=175row=195 k=0 clo=176row=80 k=0
clo=176row=195 k=0 clo=177row=80 k=0 clo=177row=195 k=0
clo=178row=81 k=0 clo=178row=195 k=0 clo=179row=81 k=0
clo=179row=195 k=0 clo=180row=82 k=0 clo=180row=195 k=0
clo=181row=82 k=0 clo=181row=195 k=0 clo=182row=83 k=0
clo=182row=195 k=0 clo=183row=83 k=0 clo=183row=195 k=0
clo=184row=83 k=0 clo=184row=195 k=0 clo=185row=84 k=0
clo=185row=195 k=0 clo=186row=84 k=0 clo=186row=195 k=0
clo=187row=85 k=0 clo=187row=195 k=0 clo=188row=85 k=0
clo=188row=195 k=0 clo=189row=86 k=0 clo=189row=195 k=0
clo=190row=86 k=0 clo=190row=195 k=0 clo=191row=87 k=0
clo=191row=195 k=0 clo=192row=88 k=0 clo=192row=195 k=0
clo=193row=88 k=0 clo=193row=195 k=0 clo=194row=89 k=0
clo=194row=195 k=0 clo=195row=89 k=0 clo=195row=195 k=0
clo=196row=90 k=0 clo=196row=195 k=0 clo=197row=91 k=0
clo=197row=195 k=0 clo=198row=91 k=0 clo=198row=195 k=0
clo=199row=92 k=0 clo=199row=195 k=0 clo=200row=92 k=0
clo=200row=195 k=0 clo=201row=93 k=0 clo=201row=195 k=0
clo=202row=94 k=0 clo=202row=195 k=0 clo=203row=95 k=0
clo=203row=195 k=0 clo=204row=95 k=0 clo=204row=195 k=0
clo=205row=96 k=0 clo=205row=195 k=0 clo=206row=97 k=0
clo=206row=195 k=0 clo=207row=97 k=0 clo=207row=195 k=0
clo=208row=98 k=0 clo=208row=195 k=0 clo=209row=99 k=0
clo=209row=195 k=0 clo=210row=100 k=0 clo=210row=195 k=0
clo=211row=101 k=0 clo=211row=195 k=0 clo=212row=101 k=0
clo=212row=195 k=0 clo=213row=102 k=0 clo=213row=195 k=0
clo=214row=103 k=0 clo=214row=195 k=0 clo=215row=104 k=0
clo=215row=195 k=0 clo=216row=105 k=0 clo=216row=195 k=0
clo=217row=106 k=0 clo=217row=195 k=0 clo=218row=107 k=0
clo=218row=195 k=0 clo=219row=108 k=0 clo=219row=195 k=0
clo=220row=109 k=0 clo=220row=195 k=0 clo=221row=110 k=0
clo=221row=195 k=0 clo=222row=111 k=0 clo=222row=195 k=0
clo=223row=112 k=0 clo=223row=195 k=0 clo=224row=113 k=0
clo=224row=195 k=0 clo=225row=195 k=0 clo=226row=195 k=0
clo=227row=195 k=0 clo=228row=195 k=0 clo=229row=194 k=0
clo=229row=195 k=0
我不太习惯 C++ 中的 OpenCV,我现在无法对其进行测试,但根据文档,您应该像这样访问像素的强度:
Scalar intensity = img.at<uchar>(y, x);
intensity.val[0]则包含从0到255的灰度值。
也许有帮助
我认为这应该可以,但我还没有测试过
int k = (int)img.at<uchar>(j,i);
那是因为您超出了图像范围。您很不幸您的应用程序没有崩溃(这会让它更加明显)。你的循环不正确。它应该是 < 而不是 <=
for(i=0;i<img.cols;i++)
{
for(j=0;j<img.rows;j++)
{
我有一个 1 位深度的单色 BMP 文件。
http://i.stack.imgur.com/MrOsU.png
我正在尝试查找此图像中黑色像素的位置和数量。
这是我的代码:
#include<opencv2/opencv.hpp>
#include<opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat img = imread("/home/jordan/opencv/Test/edge_detection/img2.bmp",0);
cout<<"cols= "<<img.cols<<"row= "<<img.rows<<"\n";
int i,j,count=0;
for(i=0;i<=img.cols;i++)
{
for(j=0;j<=img.rows;j++)
{
int k=img.at<uchar>(j,i);
if(k==0)
{
count++;
cout<<"col="<<i<<"row="<<j<<"k= "<< k<<"\t\n";
}
}
}
cout<<"count="<<count<<"\n";
}
从下面的输出中可以看出,我在第 195 行得到了很多零,根据上图,这是不正确的。请帮我解决这个问题。
输出:
clo=98row=69 k=0 clo=98row=195 k=0 clo=99row=69 k=0 clo=99row=195 k=0 clo=100row=69 k=0 clo=100row=195 k=0 clo=101row=69 k=0
clo=101row=195 k=0 clo=102row=69 k=0 clo=102row=195 k=0 clo=103row=68 k=0 clo=103row=195 k=0 clo=104row=68 k=0
clo=104row=195 k=0 clo=105row=68 k=0 clo=105row=195 k=0 clo=106row=68 k=0 clo=106row=195 k=0 clo=107row=68 k=0
clo=107row=195 k=0 clo=108row=68 k=0 clo=108row=195 k=0 clo=109row=68 k=0 clo=109row=195 k=0 clo=110row=68 k=0
clo=110row=195 k=0 clo=111row=68 k=0 clo=111row=195 k=0 clo=112row=68 k=0 clo=112row=195 k=0 clo=113row=68 k=0
clo=113row=195 k=0 clo=114row=68 k=0 clo=114row=195 k=0 clo=115row=68 k=0 clo=115row=195 k=0 clo=116row=68 k=0
clo=116row=195 k=0 clo=117row=68 k=0 clo=117row=195 k=0 clo=118row=68 k=0 clo=118row=195 k=0 clo=119row=68 k=0
clo=119row=195 k=0 clo=120row=68 k=0 clo=120row=195 k=0 clo=121row=68 k=0 clo=121row=195 k=0 clo=122row=68 k=0
clo=122row=195 k=0 clo=123row=68 k=0 clo=123row=195 k=0 clo=124row=68 k=0 clo=124row=195 k=0 clo=125row=68 k=0
clo=125row=195 k=0 clo=126row=68 k=0 clo=126row=195 k=0 clo=127row=69 k=0 clo=127row=195 k=0 clo=128row=69 k=0
clo=128row=195 k=0 clo=129row=69 k=0 clo=129row=195 k=0 clo=130row=69 k=0 clo=130row=195 k=0 clo=131row=69 k=0
clo=131row=195 k=0 clo=132row=69 k=0 clo=132row=195 k=0 clo=133row=69 k=0 clo=133row=195 k=0 clo=134row=69 k=0
clo=134row=195 k=0 clo=135row=69 k=0 clo=135row=195 k=0 clo=136row=69 k=0 clo=136row=195 k=0 clo=137row=70 k=0
clo=137row=195 k=0 clo=138row=70 k=0 clo=138row=195 k=0 clo=139row=70 k=0 clo=139row=195 k=0 clo=140row=70 k=0
clo=140row=195 k=0 clo=141row=70 k=0 clo=141row=195 k=0 clo=142row=70 k=0 clo=142row=195 k=0 clo=143row=70 k=0
clo=143row=195 k=0 clo=144row=71 k=0 clo=144row=195 k=0 clo=145row=71 k=0 clo=145row=195 k=0 clo=146row=71 k=0
clo=146row=195 k=0 clo=147row=71 k=0 clo=147row=195 k=0 clo=148row=71 k=0 clo=148row=195 k=0 clo=149row=72 k=0
clo=149row=195 k=0 clo=150row=72 k=0 clo=150row=195 k=0 clo=151row=72 k=0 clo=151row=195 k=0 clo=152row=72 k=0
clo=152row=195 k=0 clo=153row=73 k=0 clo=153row=195 k=0 clo=154row=73 k=0 clo=154row=195 k=0 clo=155row=73 k=0
clo=155row=195 k=0 clo=156row=73 k=0 clo=156row=195 k=0 clo=157row=74 k=0 clo=157row=195 k=0 clo=158row=74 k=0
clo=158row=195 k=0 clo=159row=74 k=0 clo=159row=195 k=0 clo=160row=74 k=0 clo=160row=195 k=0 clo=161row=75 k=0
clo=161row=195 k=0 clo=162row=75 k=0 clo=162row=195 k=0 clo=163row=75 k=0 clo=163row=195 k=0 clo=164row=76 k=0
clo=164row=195 k=0 clo=165row=76 k=0 clo=165row=195 k=0 clo=166row=76 k=0 clo=166row=195 k=0 clo=167row=76 k=0
clo=167row=195 k=0 clo=168row=77 k=0 clo=168row=195 k=0 clo=169row=77 k=0 clo=169row=195 k=0 clo=170row=78 k=0
clo=170row=195 k=0 clo=171row=78 k=0 clo=171row=195 k=0 clo=172row=78 k=0 clo=172row=195 k=0 clo=173row=79 k=0
clo=173row=195 k=0 clo=174row=79 k=0 clo=174row=195 k=0 clo=175row=79 k=0 clo=175row=195 k=0 clo=176row=80 k=0
clo=176row=195 k=0 clo=177row=80 k=0 clo=177row=195 k=0 clo=178row=81 k=0 clo=178row=195 k=0 clo=179row=81 k=0
clo=179row=195 k=0 clo=180row=82 k=0 clo=180row=195 k=0 clo=181row=82 k=0 clo=181row=195 k=0 clo=182row=83 k=0
clo=182row=195 k=0 clo=183row=83 k=0 clo=183row=195 k=0 clo=184row=83 k=0 clo=184row=195 k=0 clo=185row=84 k=0
clo=185row=195 k=0 clo=186row=84 k=0 clo=186row=195 k=0 clo=187row=85 k=0 clo=187row=195 k=0 clo=188row=85 k=0
clo=188row=195 k=0 clo=189row=86 k=0 clo=189row=195 k=0 clo=190row=86 k=0 clo=190row=195 k=0 clo=191row=87 k=0
clo=191row=195 k=0 clo=192row=88 k=0 clo=192row=195 k=0 clo=193row=88 k=0 clo=193row=195 k=0 clo=194row=89 k=0
clo=194row=195 k=0 clo=195row=89 k=0 clo=195row=195 k=0 clo=196row=90 k=0 clo=196row=195 k=0 clo=197row=91 k=0
clo=197row=195 k=0 clo=198row=91 k=0 clo=198row=195 k=0 clo=199row=92 k=0 clo=199row=195 k=0 clo=200row=92 k=0
clo=200row=195 k=0 clo=201row=93 k=0 clo=201row=195 k=0 clo=202row=94 k=0 clo=202row=195 k=0 clo=203row=95 k=0
clo=203row=195 k=0 clo=204row=95 k=0 clo=204row=195 k=0 clo=205row=96 k=0 clo=205row=195 k=0 clo=206row=97 k=0
clo=206row=195 k=0 clo=207row=97 k=0 clo=207row=195 k=0 clo=208row=98 k=0 clo=208row=195 k=0 clo=209row=99 k=0
clo=209row=195 k=0 clo=210row=100 k=0 clo=210row=195 k=0 clo=211row=101 k=0 clo=211row=195 k=0 clo=212row=101 k=0 clo=212row=195 k=0 clo=213row=102 k=0 clo=213row=195 k=0 clo=214row=103 k=0 clo=214row=195 k=0 clo=215row=104 k=0 clo=215row=195 k=0 clo=216row=105 k=0 clo=216row=195 k=0 clo=217row=106 k=0 clo=217row=195 k=0 clo=218row=107 k=0 clo=218row=195 k=0 clo=219row=108 k=0 clo=219row=195 k=0 clo=220row=109 k=0 clo=220row=195 k=0 clo=221row=110 k=0 clo=221row=195 k=0 clo=222row=111 k=0 clo=222row=195 k=0 clo=223row=112 k=0 clo=223row=195 k=0 clo=224row=113 k=0 clo=224row=195 k=0 clo=225row=195 k=0 clo=226row=195 k=0 clo=227row=195 k=0 clo=228row=195 k=0 clo=229row=194 k=0 clo=229row=195 k=0
我不太习惯 C++ 中的 OpenCV,我现在无法对其进行测试,但根据文档,您应该像这样访问像素的强度:
Scalar intensity = img.at<uchar>(y, x);
intensity.val[0]则包含从0到255的灰度值。
也许有帮助
我认为这应该可以,但我还没有测试过
int k = (int)img.at<uchar>(j,i);
那是因为您超出了图像范围。您很不幸您的应用程序没有崩溃(这会让它更加明显)。你的循环不正确。它应该是 < 而不是 <=
for(i=0;i<img.cols;i++)
{
for(j=0;j<img.rows;j++)
{