Morphological filters

Similar to smoothening filters, morphology filters are the algorithms that change the value of each pixel based on the value of the neighboring pixels, although the obvious difference is that they do not have a blurring effect and are mostly used to produce some form of erosion or dilation effect on images. This will be clarified with a few hands-on examples further in this section, but for now, let's see how morphological operations (also called transformations) are performed using OpenCV functions.

You can use the morphologyEx function to perform morphological operations on images. This function can be provided with an entry from the MorphTypes enum to specify the morphological operation. Here are the values that can be used:

  • MORPH_ERODE: For erosion operation
  • MORPH_DILATE: For dilation operation
  • MORPH_OPEN: For opening operation, or the dilation of eroded images
  • MORPH_CLOSE: For closing operation, or the erosion of dilated images
  • MORPH_GRADIENT: For morphological gradient operation, or subtraction of the eroded image from the dilated image
  • MORPH_TOPHAT: For Top-hat operation, or subtraction of the opening operation result from the source image
  • MORPH_BLACKHAT: For Black-hat operation, or subtraction of the source image from the result of the closing operation

To understand all of the possible morphological operations mentioned in the previous list, it's important to first understand the effect of erosion and dilation (the first two entries in the list) since the rest are simply different combinations of these two morphological operations. Let's first try erosion with an example and, at the same time, learn how the morphologyEx function is used:

MorphTypes op = MORPH_ERODE; 
MorphShapes shape = MORPH_RECT; 
Size ksize(3,3); 
Point anchor(-1, -1); 
Mat kernel = getStructuringElement(shape, 
                                   ksize, 
                                   anchor); 
int iterations = 3; 
BorderTypes borderType = BORDER_CONSTANT; 
Scalar borderValue = morphologyDefaultBorderValue(); 
morphologyEx(image, 
             result, 
             op, 
             kernel, 
             anchor, 
             iterations, 
             borderType, 
             borderValue); 

op, or the operation, is an entry from the MorphTypes enum, as mentioned before. kernel, or the structuring element, is the kernel matrix used for the morphological operation, which itself is either created manually or by using the getStructuringElement function. You must provide the getStructuringElement with a shape morph, kernel size (ksize), and anchor. shape can be a rectangle, cross, or ellipse, and is simply an entry from the MorphShapes enum. The iterations variable refers to the number of times the morphological operation is performed on an image. borderType is interpreted exactly the same way as with all the functions we've seen so far for the extrapolation of pixels. In case a constant border type value is used, the morphologyEx function must also be provided with a border value, which can be retrieved by using the morphologyDefaultBorderValue function, or specified manually.

Here is the result of our preceding code (for erosion), when executed on our sample image:

Dilation, on the other hand, is performed by simply replacing the op value with MORPH_DILATE in the previous example. Here's the result of the dilation operation:

A highly simplified description of what erosion and dilation operations do is that they cause the neighboring pixels of darker pixels to become darker (in case of erosion) or the neighboring pixels of brighter pixels to become brighter (in case of dilations), which, after more iterations, will cause a stronger and easily visible effect.

As was mentioned before, all of the other morphological operations are simply a combination of erosion and dilation. Here are the results of opening and closing operations when they are executed on our example images:

Make sure you try the rest of the morphological operations by yourself to see their effects. Create user interfaces that have trackbars on them and try changing the value of iteration and other parameters to see how they affect the result of morphological operations. If used carefully and wisely, morphological operations can have very interesting results and further simplify an operation that you want to perform on an image.

Before proceeding to the next section, it is worth noting that you can also use the erode and dilate functions to perform exactly the same operation. Although these functions do not require an operation parameter (since the operation is in their name already), the rest of the parameters are exactly the same as with the morphologyEx function.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.219.236.70