The two principal morphological operations are dilation and erosion. Dilation allows objects to expand, thus potentially filling in small holes and connecting disjoint objects. Erosion shrinks objects by etching away (eroding) their boundaries. These operations can be customized for an application by the proper selection of the structuring element, which determines exactly how the objects will be dilated or eroded.
Dilation and Erosion
- Using the output from previous tutorial, Sbin, we are going to try the morphological operator.
- First, we create a structure element using “imcreatese” command.
–>se = imcreatese(‘ellipse’,15,15);
- Using the “imdilate” and “imerode” command, we perform dilation and erosion on the binary image. Show and compare the results with the original binary image.
–>Sd = imdilate(Sbin,se);
–>Se = imerode(Sbin,se);
Opening and Closing
Dilation and Erosion would increase and reduce the components size. In order to preserved the size of the components, these 2 operations are always used together to preserve the size.
Opening operation is the process of erosion followed by dilation. The usage of this operation is to remove smaller components or components which are not the desired shape.
- Create a structure element.
–>se = imcreatese(‘ellipse’,35,35);
- Perform opening operation and show the result.
–>So = imopen(Sbin,se);
Find Number of Objects in An Image
- Numbers of objects could be found by using “imlabel” function.
–>[S_labeled,n] = imlabel(So);
- S_labeled is the labelled components, for example, first component pixels value are all marked as ‘1’, second marked as ‘2’, etc. n is the numbers of labelled objects in the image. We could visualize them using following extra parameter in imshow command.
- Function “imblobprop” is used to find the area and bounding box for all components in labelled image.
–>[A, BB] = imblobprop(S_labeled);
- Use the bounding boxes to show the detected components on the original image.
–>imrects(BB,[255 0 0]);