Ask Your Question

Revision history [back]

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);

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, 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);

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.