Techno Blender
Digitally Yours.

Morphological Operations to Remove Image Distortion

0 27


Morphological Operations in Detail

Morphological operation is a technique to process an image based on its shape. It works on comparing the neighboring pixels to structure an image. The process is preferable for binary images ({0,1} or {0,255}).

How does the process work?

Before getting familiar with morphological operations, we need to have knowledge about some basic terminologies — Structuring Element, Miss, Hit and Fit.

Figure 1: Morphological Operation’s Elements (Image By Author)

Structuring Element

It’s a small piece of shape or template that analyze every pixel of an image with the neighborhood pixels under the element. The structuring element is shown in the above image marked with blue color.

Different Structuring Elements —

Figure 2: Different Structuring Elements (Image By Author)

Structuring elements are designed according to the shape of the image. The size of structuring elements can be of different sizes (2x2, 3x3, 5x1, 5x5, etc.). Structuring elements contains the intensity value of foreground and background (namely 0 or 1). It can also hold the don’t care value. One pixel of the structuring element is considered an origin. In the above image, I have mentioned the origin pixel with a black dot. There is no hard and fast rule to define the origin. It is depended on you. But conventionally, the origin is considered the center pixel.

Morphological operation is done by propagating the structuring elements through the image. The pixel value is changed in the origin position of the image by comparing the pixels under the structuring elements.

Miss: If no pixel of the image matches with the structuring element, then it is called miss. Shown in figure 1.

Hit: When at least one pixel of structuring element overlaps with the image pixel, it is called hit. Shown in figure 1.

Fit: If all the pixels of structuring elements match with the image, then it is called fit. Shown in figure 1.

Basically, there are two types of morphological operations —

  1. Erosion
  2. Dilation

Another two compound operations can be derived from these two operations — Closing and Opening.

Different Types of Morphological Operations with Implementation

The binary image is suitable for applying morphological operations.

Erosion

The operation is simple. The structuring element convolutes the object’s each pixel. If all the pixels of the structuring element overlap with object image pixels (satisfy the fit condition), the object image pixel will be filled with foreground pixel intensity value. Otherwise, it will be filled with background pixel intensity value.

Let’s assume 0 for the background and 1 for the foreground.

if Fit -> 1
else -> 0

I have created a simulation for better intuition. For demonstration purposes, I have used a 2×2 structuring element with an intensity of 1.

Figure-3: Structuring Element (Image By Author)

I have taken an image with 6×6 pixels. Where the white color elements are considered as 0 intensity value and sky-blue color pixels are considered as intensity value of 1. Now carefully observe the simulation given below.

Erosion Operation (Gif By Author)

The structuring element convolutes every pixel of the given image. If it satisfies the miss or hit condition, it will change the pixel to 0 in the origin’s location of the structuring element. In the simulation, I have shown where the pixel is changed from 1 to 0 with red color. Finally, we get the following result.

Figure-4: Erosion Result (Image by Author)

So, it shows that the main image pixels have been reduced by erosion.

Practical Implementation with OpenCv

We will use the OpenCV library to implement the morphological operations. In the OpenCV library, the structuring element is known as the kernel.

First of all, we import the necessary libraries.

I will show how the image gets changed with different structuring elements/ kernel sizes.

Our image is black on white background. But OpenCV expects black background with a white image object for morphological operation. So, we convert the white background to black and the black object to white, with inverse binary thresholding. Next, we apply 5x5, 9x9 and 11x11 kernels on the inverse binary threshold image. As we have applied morphological operation on the inverse binary threshold image, we have applied inverse binary thresholding again after the morphological operation to retain the white background and black object. And finally, we have plotted the images.

The code is given below with the comments.

We can also apply more than one iteration on the image with the same kernel size. Output is shown for 1, 2 and 3 iterations with a kernel size of 5×5.

Properties of Erosion with Coding Example

  1. It can be used to remove the extra noise of an object.

Loading the image —

Result after removing the external noise —

The steps are similar to the previous code (inverse binary thresholding → morphological operation → inverse thresholding to retain the original image background and foreground color).

2. Disjoint the joint images.

Coding example —

Loading a joint image —

It disjoints the images.

Dilation

In dilation, structuring element convolute on the object image. If any of the structuring element’s pixel value overlaps with the object image (satisfy the hit condition), the object image pixel will be filled with foreground pixel intensity value. Otherwise, the pixel intensity value will remain the same.

We assume 0 for the background and 1 for the foreground.

If Hit -> 1
else -> 0

For better intuition, I have created a simulation of the dilation operation. As the structuring element, we have used the previous structuring element of the figure-3 and kept the same image as shown in the erosion operation.

Now, carefully observe the dilation operation shown below.

Dilation Operation (Gif by Author)

The structuring element is convoluting in every pixel of the object image from left to right and top to bottom. When it satisfies the hit or miss condition, the pixel in the origin’s location of the structuring element changes from 0 to 1. Otherwise, it remains the same. After completing the operation, it produces the result shown below.

Figure-5: Dilation result (Image By Author)

So, dilation increases the pixels of the object image.

Practical Implementation with OpenCv

I have used the same image shown in the erosion section. The code is also identical to the previous section. In this section, we will apply dilation instead of erosion operation and show the results for different kernel sizes and iterations.

With different iteration values and the same kernel size of 5×5, we can see how the shape of the object image is changing. For implementation with OpenCV, we have followed the same steps as described in the erosion section (inverse binary thresholding → morphological operation → inverse thresholding to retain the original image background and foreground color).

Properties of Dilation Operation with Example

  1. With dilation, we can reduce/repair the breaks of an image.

Coding Example —

We have loaded the image of ‘H’ with breaks.

Next, we applied dilation to repair the breaks.

And we have successfully done the job.

2. We can remove the internal noise of an image with dilation.

Coding Example —

Load an image with internal noise.

After applying dilation on the above image, we can easily get an output image with no noise.

Compound Operations

There are some other compound morphological operations. Among them, opening and closing are two widely used operations. The following diagram shows the operations at a glance.

Figure-6: Opening and Closing Operation (Image by Author)[1]

The opening operation is done by performing erosion and then dilation. It removes the joint on the objects keeping the shape identical to the main object. Shown in Figure 6. It is useful to remove background noise[2].

We can apply opening by using OpenCV with the following syntax.

opening = cv.morphologyEx(img, cv.MORPH_OPEN, kernel)

If we first apply dilation and erosion on the next, the process is known as closing. The process is shown in figure-6. It helps to remove the noise of the foreground image[2]. The syntax for closing operations with OpenCV is given below.

closing = cv.morphologyEx(img, cv.MORPH_CLOSE, kernel)

Some other compound operations are available, like morphological gradient, top hat, and black hat.


Morphological Operations in Detail

Morphological operation is a technique to process an image based on its shape. It works on comparing the neighboring pixels to structure an image. The process is preferable for binary images ({0,1} or {0,255}).

How does the process work?

Before getting familiar with morphological operations, we need to have knowledge about some basic terminologies — Structuring Element, Miss, Hit and Fit.

Figure 1: Morphological Operation’s Elements (Image By Author)

Structuring Element

It’s a small piece of shape or template that analyze every pixel of an image with the neighborhood pixels under the element. The structuring element is shown in the above image marked with blue color.

Different Structuring Elements —

Figure 2: Different Structuring Elements (Image By Author)

Structuring elements are designed according to the shape of the image. The size of structuring elements can be of different sizes (2x2, 3x3, 5x1, 5x5, etc.). Structuring elements contains the intensity value of foreground and background (namely 0 or 1). It can also hold the don’t care value. One pixel of the structuring element is considered an origin. In the above image, I have mentioned the origin pixel with a black dot. There is no hard and fast rule to define the origin. It is depended on you. But conventionally, the origin is considered the center pixel.

Morphological operation is done by propagating the structuring elements through the image. The pixel value is changed in the origin position of the image by comparing the pixels under the structuring elements.

Miss: If no pixel of the image matches with the structuring element, then it is called miss. Shown in figure 1.

Hit: When at least one pixel of structuring element overlaps with the image pixel, it is called hit. Shown in figure 1.

Fit: If all the pixels of structuring elements match with the image, then it is called fit. Shown in figure 1.

Basically, there are two types of morphological operations —

  1. Erosion
  2. Dilation

Another two compound operations can be derived from these two operations — Closing and Opening.

Different Types of Morphological Operations with Implementation

The binary image is suitable for applying morphological operations.

Erosion

The operation is simple. The structuring element convolutes the object’s each pixel. If all the pixels of the structuring element overlap with object image pixels (satisfy the fit condition), the object image pixel will be filled with foreground pixel intensity value. Otherwise, it will be filled with background pixel intensity value.

Let’s assume 0 for the background and 1 for the foreground.

if Fit -> 1
else -> 0

I have created a simulation for better intuition. For demonstration purposes, I have used a 2×2 structuring element with an intensity of 1.

Figure-3: Structuring Element (Image By Author)

I have taken an image with 6×6 pixels. Where the white color elements are considered as 0 intensity value and sky-blue color pixels are considered as intensity value of 1. Now carefully observe the simulation given below.

Erosion Operation (Gif By Author)

The structuring element convolutes every pixel of the given image. If it satisfies the miss or hit condition, it will change the pixel to 0 in the origin’s location of the structuring element. In the simulation, I have shown where the pixel is changed from 1 to 0 with red color. Finally, we get the following result.

Figure-4: Erosion Result (Image by Author)

So, it shows that the main image pixels have been reduced by erosion.

Practical Implementation with OpenCv

We will use the OpenCV library to implement the morphological operations. In the OpenCV library, the structuring element is known as the kernel.

First of all, we import the necessary libraries.

I will show how the image gets changed with different structuring elements/ kernel sizes.

Our image is black on white background. But OpenCV expects black background with a white image object for morphological operation. So, we convert the white background to black and the black object to white, with inverse binary thresholding. Next, we apply 5x5, 9x9 and 11x11 kernels on the inverse binary threshold image. As we have applied morphological operation on the inverse binary threshold image, we have applied inverse binary thresholding again after the morphological operation to retain the white background and black object. And finally, we have plotted the images.

The code is given below with the comments.

We can also apply more than one iteration on the image with the same kernel size. Output is shown for 1, 2 and 3 iterations with a kernel size of 5×5.

Properties of Erosion with Coding Example

  1. It can be used to remove the extra noise of an object.

Loading the image —

Result after removing the external noise —

The steps are similar to the previous code (inverse binary thresholding → morphological operation → inverse thresholding to retain the original image background and foreground color).

2. Disjoint the joint images.

Coding example —

Loading a joint image —

It disjoints the images.

Dilation

In dilation, structuring element convolute on the object image. If any of the structuring element’s pixel value overlaps with the object image (satisfy the hit condition), the object image pixel will be filled with foreground pixel intensity value. Otherwise, the pixel intensity value will remain the same.

We assume 0 for the background and 1 for the foreground.

If Hit -> 1
else -> 0

For better intuition, I have created a simulation of the dilation operation. As the structuring element, we have used the previous structuring element of the figure-3 and kept the same image as shown in the erosion operation.

Now, carefully observe the dilation operation shown below.

Dilation Operation (Gif by Author)

The structuring element is convoluting in every pixel of the object image from left to right and top to bottom. When it satisfies the hit or miss condition, the pixel in the origin’s location of the structuring element changes from 0 to 1. Otherwise, it remains the same. After completing the operation, it produces the result shown below.

Figure-5: Dilation result (Image By Author)

So, dilation increases the pixels of the object image.

Practical Implementation with OpenCv

I have used the same image shown in the erosion section. The code is also identical to the previous section. In this section, we will apply dilation instead of erosion operation and show the results for different kernel sizes and iterations.

With different iteration values and the same kernel size of 5×5, we can see how the shape of the object image is changing. For implementation with OpenCV, we have followed the same steps as described in the erosion section (inverse binary thresholding → morphological operation → inverse thresholding to retain the original image background and foreground color).

Properties of Dilation Operation with Example

  1. With dilation, we can reduce/repair the breaks of an image.

Coding Example —

We have loaded the image of ‘H’ with breaks.

Next, we applied dilation to repair the breaks.

And we have successfully done the job.

2. We can remove the internal noise of an image with dilation.

Coding Example —

Load an image with internal noise.

After applying dilation on the above image, we can easily get an output image with no noise.

Compound Operations

There are some other compound morphological operations. Among them, opening and closing are two widely used operations. The following diagram shows the operations at a glance.

Figure-6: Opening and Closing Operation (Image by Author)[1]

The opening operation is done by performing erosion and then dilation. It removes the joint on the objects keeping the shape identical to the main object. Shown in Figure 6. It is useful to remove background noise[2].

We can apply opening by using OpenCV with the following syntax.

opening = cv.morphologyEx(img, cv.MORPH_OPEN, kernel)

If we first apply dilation and erosion on the next, the process is known as closing. The process is shown in figure-6. It helps to remove the noise of the foreground image[2]. The syntax for closing operations with OpenCV is given below.

closing = cv.morphologyEx(img, cv.MORPH_CLOSE, kernel)

Some other compound operations are available, like morphological gradient, top hat, and black hat.

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! Techno Blender is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – [email protected]. The content will be deleted within 24 hours.
Leave a comment