opencv识别物体上的黑色污点

#include "stdafx.h"
#include
#include
#include
#include

void thredChange(cv::InputArray src,cv::OutputArray dst)
{
cv::threshold(src,dst,180,255,CV_THRESH_BINARY_INV);
}

void filterPoints(cv::Mat image,cv::Mat result)
{
// allocate if necessary
//result.create(image.size(), image.type());
for(int j=0; j result.row(j).setTo(cv::Scalar(0));

int d=10;
for (int j= d; j // (except first and last)
const uchar* previous=
image.ptr(j-d); // previous row
const uchar* current=
image.ptr(j); // current row
const uchar* next=
image.ptr(j+d); // next row

uchar* output= result.ptr(j); // output row
for (int i=d; i if(current[i]!=255||
current[i-d]!=0||
current[i+d]!=0||
previous[i]!=0||
previous[i-d]!=0||
previous[i+d]!=0||
previous[i+1]!=0||
previous[i+2]!=0||
previous[i+3]!=0||
previous[i+4]!=0||
previous[i-4]!=0||
previous[i-3]!=0||
previous[i-2]!=0||
previous[i-1]!=0||
next[i-d]!=0||
next[i+d]!=0||
next[i-1]!=0||
next[i-2]!=0||
next[i-3]!=0||
next[i-4]!=0||
next[i+1]!=0||
next[i+2]!=0||
next[i+3]!=0||
next[i+4]!=0||
next[i]!=0
)
*output++=0;
else
*output++=255;
}
}
}
void removeLittle(cv::Mat image,cv::Mat result,int num)
{
// allocate if necessary
//result.create(image.size(), image.type());
for(int j=0; j result.row(j).setTo(cv::Scalar(0));

int d=1;
for (int j= d; j // (except first and last)
const uchar* previous=
image.ptr(j-d); // previous row
const uchar* current=
image.ptr(j); // current row
const uchar* next=
image.ptr(j+d); // next row

uchar* output= result.ptr(j); // output row
for (int i=d; i int a=current[i]+current[i-1]+current[i+1]
+previous[i-1]+previous[i]+previous[i+1]
+next[i-1]+next[i]+next[i+1];
if((current[i]==255)&&
(a>255*num))
*output++=255;
else
*output++=0;
}
}
}

int _tmain(int argc, _TCHAR* argv[])
{
cv::Mat image=cv::imread("./data/test1/points.jpg",CV_LOAD_IMAGE_GRAYSCALE);
cv::Mat p2;
cv::Mat p3=image.clone(); ;
cv::Mat p4=image.clone();
cv::Mat p5=image.clone();

thredChange(image,p2);
filterPoints(p2,p3);
removeLittle(p3,p4,5);
removeLittle(p4,p5,3);

cv::namedWindow("gui");
cv::imshow("gui",p5);
cv::waitKey(0);
return 0;
}

    推荐阅读