1 | initial version |
i trial code based on convexityDefects maybe you will improve it.
result image :
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
char* filename = argc >= 2 ? argv[1] : (char*)"Obj_Sep.png";
Mat src = imread(filename);
if (src.empty())
return -1;
Mat bw;
cvtColor( src, bw, COLOR_BGR2GRAY );
bw = bw > 127;
// Find contours
vector<vector<Point> > contours;
vector<int> contoursHull;
vector<Vec4i> defects;
findContours( bw, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE );
for ( size_t i = 0; i < contours.size(); i++)
{
if( contourArea(contours[i]) > 500 )
{
approxPolyDP(contours[i],contours[i],9,true);
convexHull(contours[i], contoursHull,true);
convexityDefects(contours[i], contoursHull,defects);
for ( size_t j = 0; j < defects.size(); j++)
{
Vec4i defpoint = defects[j];
circle(src,contours[i][defpoint[2]],3,Scalar(0,0,200),3);
}
imshow("bw", src);
waitKey();
}
}
return 0;
}