TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

OpenCV Erosion and Dilation

OpenCV Erosion and Dilation with What is OpenCV, History, Installation, Reading Images, Writing Images, Resize Image, Image Rotation, Gaussian Blur, Blob Detection, Face Detection and Face Recognition etc.

<< Back to OPENCV

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:

cv2.dilate(src, dst, kernel)

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:

import cv2
import numpy as np
img = cv2.imread(r'C:\Users\DEVANSH SHARMA\jtp_flower.jpg, 0)

kernel = np.ones((5,5), np.uint8)
img_erosion = cv2.erode(img, kernel, iterations=1)
img_dilation = cv2.dilate(img, kernel, iterations=1)
cv2.imshow('Input', img)
cv2.imshow('Dilation', img_dilation)
cv2.waitKey(0)

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:

cv2.erode(src, dst, kernel)

Parameters:

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

Consider the following example:

import cv2
import numpy as np
img = cv2.imread(r'C:\Users\DEVANSH SHARMA\baloon.jpg', 1)
kernel = np.ones((5,5), np.uint8)
img_erosion = cv2.erode(img, kernel, iterations=1)
img_dilation = cv2.dilate(img, kernel, iterations=1)
cv2.imshow('Input', img)
cv2.imshow('Erosion', img_erosion)
cv2.waitKey(0)

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




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf