1 | initial version |
I have used this image
I'm sorry I know nothing in java so my answer in C++ is
int main( int argc, char** argv )
{
Mat m = imread( "c:/Users/Laurent.PC-LAURENT-VISI/Downloads/testindex.png", CV_LOAD_IMAGE_GRAYSCALE );
Mat mThresh;
Mat mBlur;
GaussianBlur(m, mBlur, Size(21,21),10.0,10.0);
Mat mFil=mBlur-m;
threshold(mFil,mThresh,5,255,THRESH_BINARY);
findContours(mThresh,contours,hierarchy, cv::RETR_CCOMP, cv::CHAIN_APPROX_NONE, cv::Point(0,0));
Mat mCtr = Mat::zeros(m.size(),CV_8UC1);
drawContours(mCtr,contours,-1,255,-1);
imshow("Threshold",mThresh);
imshow("Ctr",mCtr);
Mat labels;
Mat stats,centroids;
connectedComponentsWithStats(mCtr,labels,stats,centroids);
double mSize=0,sSize=0;
for (int i = 1; i < centroids.rows; i++)
{
cout << stats.at<int>(i, cv::CC_STAT_AREA) << "\t"<< stats.at<int>(i, CC_STAT_WIDTH) << "\t"<< stats.at<int>(i,CC_STAT_HEIGHT) << "\n";
mSize+=stats.at<int>(i, cv::CC_STAT_AREA);
sSize+=stats.at<int>(i, cv::CC_STAT_AREA)*stats.at<int>(i, cv::CC_STAT_AREA);
}
mSize /= (centroids.rows-1);
sSize /= (centroids.rows-1);
sSize = sqrt(sSize - mSize*mSize);
int nb=0;
for (int i = 1; i < centroids.rows; i++)
{
if (stats.at<int>(i, cv::CC_STAT_AREA)>mSize - (sSize/2))
{
cout << stats.at<int>(i, cv::CC_STAT_AREA) << "\t"<< stats.at<int>(i, CC_STAT_WIDTH) << "\t"<< stats.at<int>(i,CC_STAT_HEIGHT) << "\n";
nb++;
}
}
cout << "Drops of blood " << nb << " ("<<centroids.rows<<")\n";
waitKey();
return(0);
}