1 | initial version |
Something like this should work :
Mat img=imread("g:/lib/opencv/samples/data/lena.jpg",IMREAD_GRAYSCALE);
Mat imBin;
threshold(img,imBin,180,255,THRESH_BINARY);
imshow("Result", imBin);
waitKey();
Mat stats, centroids, labelImage;
int nLabels = connectedComponentsWithStats(imBin, labelImage, stats, centroids, 8, CV_32S);
Mat mask(labelImage.size(), CV_8UC1, Scalar(0));
for (int i = 1; i < nLabels; i++) // 0 is background may be you should scan it
{
int s= stats.at<int>(i, 4);
cout<<s<<"\t";;
if (stats.at<int>(i, 4) > 2000)
{
mask = mask | (labelImage==i);
}
}
Mat r(img.size(), CV_8UC1, Scalar(0));
img.copyTo(r,mask);
imshow("Result", r);
waitKey();
2 | No.2 Revision |
Something like this should work :
Mat img=imread("g:/lib/opencv/samples/data/lena.jpg",IMREAD_GRAYSCALE);
Mat imBin;
threshold(img,imBin,180,255,THRESH_BINARY);
imshow("Result", imBin);
waitKey();
Mat stats, centroids, labelImage;
int nLabels = connectedComponentsWithStats(imBin, labelImage, stats, centroids, 8, CV_32S);
Mat mask(labelImage.size(), CV_8UC1, Scalar(0));
for (int i = 1; i < nLabels; i++) // 0 is background may be you should scan it
{
int s= stats.at<int>(i, 4);
cout<<s<<"\t";;
if (stats.at<int>(i, 4) > 2000)
2000) // 2000 is surface threshold
{
mask = mask | (labelImage==i);
}
}
Mat r(img.size(), CV_8UC1, Scalar(0));
img.copyTo(r,mask);
imshow("Result", r);
waitKey();
3 | No.3 Revision |
Something like this should work :
Mat img=imread("g:/lib/opencv/samples/data/lena.jpg",IMREAD_GRAYSCALE);
Mat imBin;
threshold(img,imBin,180,255,THRESH_BINARY);
imshow("Result", imBin);
waitKey();
Mat stats, centroids, labelImage;
int nLabels = connectedComponentsWithStats(imBin, labelImage, stats, centroids, 8, CV_32S);
Mat mask(labelImage.size(), CV_8UC1, Scalar(0));
Mat surfSup=stats.col(4)>2000;
for (int i = 1; i < nLabels; i++) // 0 is background may be you should scan it
i++)
{
int s= stats.at<int>(i, 4);
cout<<s<<"\t";;
if (stats.at<int>(i, 4) > 2000) // 2000 is surface threshold
(surfSup.at<uchar>(i, 0))
{
mask = mask | (labelImage==i);
}
}
Mat r(img.size(), CV_8UC1, Scalar(0));
img.copyTo(r,mask);
imshow("Result", r);
waitKey();