OpenCV Python Morphological Transformations

In this OpenCV Python article we are going to talk about Morphological Transformations in

OpenCV, so according to OpenCV Documentation Morphological transformations are some simple

operations based on the image shape. It is normally performed on binary images. It needs two

inputs, one is our original image, second one is called structuring element or kernel which

decides the nature of operation. Two basic morphological operators are Erosion and Dilation.

Then its variant forms like Opening, Closing, Gradient etc also comes into play.

 

 

 

Read Image Smoothing Techniques in OpenCV

1: OpenCV Averaging Image Blurring in Python

2: OpenCV Gaussian Blurring for Images in Python

3: OpenCV Median Blurring for Images in Python

4: OpenCV Smooth Image with Bilateral Filtering

 

 

 

1: Erosion 

The basic idea of erosion is just like soil erosion only, it erodes away the boundaries of foreground

object (Always try to keep foreground in white). So what does it do? The kernel slides through the

image (as in 2D convolution). A pixel in the original image (either 1 or 0) will be considered 1 only

if all the pixels under the kernel is 1, otherwise it is eroded (made to zero).

 

 

 

2: Dilation

It is just opposite of erosion. Here, a pixel element is ‘1’ if at least one pixel under the kernel is ‘1’.

So it increases the white region in the image or size of foreground object increases. Normally, in

cases like noise removal, erosion is followed by dilation. Because, erosion removes white noises,

but it also shrinks our object.

 

 

 

 

3: Opening 

Opening is just another name of erosion followed by dilation. It is useful in removing noise,

as we explained above. Here we use the function, cv2.morphologyEx().

 

 

 

4: Closing 

Closing is reverse of Opening, Dilation followed by Erosion. It is useful in closing small

holes inside the foreground objects, or small black points on the object.

 

 

 

 

5: Morphological Gradient

It is the difference between dilation and erosion of an image.

 

 

Also there are two more that you can use we have Top Hat and Black Hat.

 

 

 

 

So now this is the code for  OpenCV Python Morphological Transformations 

 

 

 

 

 

Run the complete code and this will be the result 

OpenCV Python Morphological Transformations
OpenCV Python Morphological Transformations

Subscribe and Get Free Video Courses & Articles in your Email

 

Share via
Copy link
Powered by Social Snap
×