First time here? Check out the FAQ!

Ask Your Question
0

Objects Separation

asked Sep 17 '13

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. https://dl.dropboxusercontent.com/u/522026/foto/elaborated.jpg

Preview: (hide)

1 answer

Sort by » oldest newest most voted
0

answered Sep 17 '13

updated Sep 18 '13

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.

Preview: (hide)

Comments

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: https://dl.dropboxusercontent.com/u/522026/foto/rect/elab_six_rect.jpg

Using 6 times cvDilate with CV_SHAPE_ELLIPSE (which can better keep the round shape): https://dl.dropboxusercontent.com/u/522026/foto/ell/elab_six_ell.jpg

Fabio gravatar imageFabio (Sep 17 '13)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 (Sep 17 '13)edit

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

StevenPuttemans gravatar imageStevenPuttemans (Sep 18 '13)edit
1

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 (Sep 18 '13)edit

Question Tools

Stats

Asked: Sep 17 '13

Seen: 729 times

Last updated: Sep 18 '13