@Pedro Batista post incude many good idea to solve this problem too.
I propose this method (and need to be tested). Shape mustn't have hole before processing : opening and closing are necessary.
Mat imOriginal = imread("test.png", IMREAD_GRAYSCALE);
Mat imOri = imOriginal > 1;
Mat img = imOri.clone();
Mat kernel;
kernel=getStructuringElement(CV_SHAPE_CROSS, Size(3, 3));
int nbC = 2;
int nbIter = 0;
Mat dst,labels;
Mat prevErode;
while (nbC == 2)
{
erode(img, dst, kernel);
nbC = connectedComponents(dst, labels);
nbIter++;
if (nbC == 2)
{
prevErode = img.clone();
img = dst;
}
imshow("erode", dst);
waitKey(0);
}
Mat shape1 = labels == 1,dst1;
Mat shape2 = labels == 2,dst2;
Mat inter(imOri.size(), CV_8UC1, Scalar(0));
for (int i = 0;i <= nbIter; i++)
{
dilate(shape1, dst1, kernel);
dilate(shape2, dst2, kernel);
add(inter, Scalar(1), inter, dst1&dst2);
shape1 = dst1;
shape2 = dst2;
}
imshow("Boundaries", inter>0);
cvtColor(imOri, imOri, CV_GRAY2BGR);
imOri.setTo(Scalar(0, 255, 0), inter > 0);
imshow("Split shape",imOri);
waitKey(0);
imwrite("bilan.png",imOri);
Result is
erode 5 times objects. Then separate object in Region of interest and dilate 5 times all roi
@LBerger Objective is to find the count and the approx dimension. I would be losing on the dimension if I
erode
only afteriterate
is more then 5, the objects are being split.5 dilates and 5 erodes. you can find number 5 using connectedComponents. With a mask you can use roi and original image to find intersection points. Of course if you need a real time program this method is not good
I've had and solved this particular problem.
Here is a description of what I did. Unfortunately the links to the images I posted before became broken, so probably it will be harder to understand, but I do describe the solution.
http://answers.opencv.org/question/71...
@LBerger I did get what you meant dilate, erode and connected components. About the time taken, I will check. I have few ideas, will try those then post the steps.
@Pedro Batista Just went through your post as well. I have a bit of a different approach to separate these two blobs, will post as soon as I'm done.