您好,这是一个示例实现!请使用这个!
void findNonZero( InputArray _src, InputArray _Mask, OutputArray _idx )
{
Mat src = _src.getMat();
Mat msk = _Mask.getMat();
CV_Assert( src.type() == CV_8UC1 );
CV_Assert( src.size() == msk.size());
int n = countNonZero(src);
if( n == 0 )
{
_idx.release();
return;
}
if( _idx.kind() == _InputArray::MAT && !_idx.getMatRef().isContinuous() )
_idx.release();
_idx.create(n, 1, CV_32SC2);
Mat idx = _idx.getMat();
CV_Assert(idx.isContinuous());
Point* idx_ptr = idx.ptr<Point>();
for( int i = 0; i < src.rows; i++ )
{
const uchar* bin_ptr = src.ptr(i);
const uchar* msk_ptr = msk .ptr(i);
for( int j = 0; j < src.cols; j++ )
if( bin_ptr[j] && msk_ptr[j])
*idx_ptr++ = Point(j, i);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
string sFileNameEx="F:\\Balaji\\Image Samples\\Test.jpg";
size_t lastindex = sFileNameEx.find_last_of(".");
String sFileName = sFileNameEx.substr(0, lastindex);
bool bSaveImages=true;
Mat mSrc_Gray,mSrc_Mask,mResult_Bgr;
mSrc_Gray= imread(sFileNameEx,0);
mSrc_Mask= Mat(mSrc_Gray.size(),CV_8UC1,Scalar(0));
cvtColor(mSrc_Gray,mResult_Bgr,COLOR_GRAY2BGR);
if(mSrc_Gray.empty())
{
cout<<"[Error]! Invalid Input Image";
return 0;
}
threshold(mSrc_Gray,mSrc_Gray,10,255,THRESH_BINARY);
imshow("mSrc_Gray",mSrc_Gray);
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
/// Find contours
findContours( mSrc_Gray.clone(), contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE, Point(0, 0) );
for( size_t i = 0; i < contours.size(); i++ )
{
if(contourArea(contours[i])>500)
{
drawContours(mSrc_Mask,contours,i,Scalar(255),-1);
}
}
//May be further Filtering like erode needed?
imshow("mSrc_Mask",mSrc_Mask);
vector<Point> locations; // output, locations of non-zero pixels
findNonZero(mSrc_Gray,mSrc_Mask,locations);
for( size_t i = 0; i < locations.size(); i++ )
{
circle(mResult_Bgr,locations[i],1,Scalar(0,255,0),1);
}
imshow("mResult_Bgr",mResult_Bgr);
waitKey(0);
return 0;
}