Ask Your Question

Objects Separation

asked 2013-09-17 04:51:14 -0600

Fabio gravatar image

Hi to all, I'm a beginner of OpenCV and I'm doing a little project for my study. After a binarization, I need to separate object which are connected. I have tried using erosion o dilatation, but these operators seem inappropriate for the purpose.

Do you have any suggestion?

This is the image which I need to use.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2013-09-17 07:20:09 -0600

updated 2013-09-18 03:05:57 -0600

Actually, if used properly then erosion and dilations is exactly what you need. You use it to peel off or to add one layer of pixels around white pixels. Check this tutorial for more information

Something like this should seperate the two elements, after whicgh you can apply a findCountours, to find the individual objects.

Mat binary_image;
Mat output_image;
// you would like to apply a dilation of the white pixels, in order to erode the black ones.
// start with one iteration and increase when needed
int dilation_type = MORPH_RECT;
int dilation_size = 3;
Mat element = getStructuringElement( dilation_type, Size( 2*dilation_size + 1, 2*dilation_size+1 ), Point( dilation_size, dilation_size ) );
dilate(binary_image, output_image, element);

EDIT1 : extra remarks based on comments

You should start by switching to the C++ interface which is more clean and more robust than the C interface, for which support will be dropped towards the end of the year.

To solve your problem, you need to enlarge your structuring element. For now you are looking in a 3 to 3 region, which will indeed take time to remove both tools next to eachother. However, if you enlarge it to, lets say 10x10 you will see a huge difference. You should play around more with the parameters.

edit flag offensive delete link more


I have tried to use these operators without success. Using more than one time the same operator, such as cvDilate, it could separate the objects only when I have lost all the object's structure. I can show you which is the effect of this approach:

Using 6 times cvDilate with CV_SHAPE_RECT:

Using 6 times cvDilate with CV_SHAPE_ELLIPSE (which can better keep the round shape):

Fabio gravatar imageFabio ( 2013-09-17 16:44:02 -0600 )edit

To be thorough I attach my code: < IplImage* image_out=cvCreateImage(cvGetSize(image_in), IPL_DEPTH_8U, 1);

  IplConvKernel* element_ell = cvCreateStructuringElementEx(3,3,0,0,CV_SHAPE_ELLIPSE,NULL);
  IplConvKernel* element_rect = cvCreateStructuringElementEx(3,3,0,0,CV_SHAPE_RECT,NULL);

   cvDilate(image_in,image_out,element_ell,1); ...


Fabio gravatar imageFabio ( 2013-09-17 16:45:46 -0600 )edit

I will post some edits in my answer above, since the formatting is nicer there.

StevenPuttemans gravatar imageStevenPuttemans ( 2013-09-18 03:03:01 -0600 )edit

Actually, it doesn't seem to improve the separation. Using a larger kernel, gives the same result as using more times a little one.

I have used different kind of kernel, in size and stencil, but I can't find a useful algorith which could be general.

I'll investigate to find a solution. Thank you for the help!!

Fabio gravatar imageFabio ( 2013-09-18 17:10:11 -0600 )edit

Question Tools


Asked: 2013-09-17 04:51:14 -0600

Seen: 631 times

Last updated: Sep 18 '13