Javatpoint Logo
Javatpoint Logo

OpenCV Erosion and Dilation

Erosion and Dilation are morphological image processing operations. OpenCV morphological image processing is a procedure for modifying the geometric structure in the image. In morphism, we find the shape and size or structure of an object. Both operations are defined for binary images, but we can also use them on a grayscale image. These are widely used in the following way:

  • Removing Noise
  • Identify intensity bumps or holes in the picture.
  • Isolation of individual elements and joining disparate elements in image.

In this tutorial, we will explain the erosion and dilation briefly.

Dilation

Dilation is a technique where we expand the image. It adds the number of pixels to the boundaries of objects in an image. The structuring element controls it. The structuring element is a matrix of 1's and 0's.

Structuring Element

The size and shape of the structuring element define how many numbers of the pixel should be added or removed from the objects in an image.

It is a matrix of 1's and 0's. The center pixel of the image is called the origin.

It contains an image A with some kernel (B), which can have any shape or size, generally a square or circle. Here the kernel B has a defined anchor point. It is the center of the kernel.

In the next step, the kernel is overlapped over the image to calculate maximum pixel values. When the computation is completed, the image is replaced with an anchor at the center. The brighter areas increase in size that made increment in image size.

OpenCV Erosion and Dilation

For example, the size of the object increases in the white shade; on the other side, the size of an object in black shades automatically decreases.

The dilation operation is performed by using the cv2.dilate() method. The syntax is given below:

Parameters: The dilate() function accepts the following argument:

  • src - It represents the input image.
  • dst - It represents the output image.
  • kernel - It represents the kernel.

Consider the following example:

Output

OpenCV Erosion and Dilation

Erosion

Erosion is much similar to dilation. The difference is that the pixel value calculated minimum rather than the maximum in dilation. The image is replaced under the anchor point with that calculated minimum pixel. Unlikely dilation, the regions of darker shades increase. While it decreases in white shade or brighter side.

OpenCV provides cv2.erode() function to perform this operation. The syntax of the function is the following:

Parameters:

  • src - It represents the source(input) image.
  • dst - It represents the destination (output) image.
  • kernel - It represents the Kernel.

Consider the following example:

Output

The above program will give the following output. We can see the different between both images.

OpenCV Erosion and Dilation

Erosion operation applied to the input image.

OpenCV Erosion and Dilation




Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA