Explain Dilation of an image How to dilate an image in OpenCV

This recipe explains what is the Dilation of an image and how to dilate an image with OpenCV. This method takes two inputs in which one is our input image and the second is called the structuring element

Recipe Objective: What is the Dilation of an image? How to dilate an image using OpenCV?

In this recipe, let us understand the Dilation of an image and how to dilate an image using OpenCV.

Step 1: Import the libraries and read the image.

Let us first import the necessary libraries and read the image. The image that we are using here is the one shown below.

Input Image

import numpy as np
import cv2
from matplotlib import pyplot as plt
image = cv2.imread('letter_A.jpg',0)

Step 2: Converting Grayscale image to binary image.

Dilation is one of the two fundamental Morphological transformation techniques. Morphological transformations generally work only on binary images. Hence, let us convert our input image into a binary image. It is also recommended to keep the foreground in white, and the background is black. This can be achieved by using the cv2.threshold() function.

retVal,masked_image = cv2.threshold(image,155,255,cv2.THRESH_BINARY_INV)

The cv2.THRESH_BINARY_INV performs inverse binary thresholding which maintains the foreground in white and the background in black

Step 3: Dilation of an image

Dilation is a morphological transformation operator used to increase the size or thickness of the foreground object in an image. In most cases, Dilation is used to connect two broken objects of an image. To dilate an image, we define a kernel matrix which is made of ones and slide the kernel through the image. A kernel is nothing but a small matrix used for sharpening, blurring, embossing, edge detection, and much more. It is also sometimes called a convolution matrix, a mask, or a filter. Each pixel element is assigned a one if any of the pixels under the kernel neighborhood is 1. This increases the size of the white region in the image, which in turn increases the size of the foreground object in an image. Dilation is usually performed after the image is eroded using another morphological transformation operator called Erosion. This process helps in removing the white noise from the image.

We can dilate an image in OpenCV using the cv2.dilate() function, which takes the following inputs.

  • src: The image which is to be dilated
  • kernel: The kernel matrix
  • iterations : (Optional) The number of iterations that specify how many times the operation will be performed. The default value is 1

Before we dilate the image using the cv2.dilate() function, let us first create a kernel matrix using the np.ones() function of the NumPy package.

kernel = np.ones((7,7),np.uint8)
dilated_image = cv2.dilate(masked_image,kernel,iterations = 3)

Step 4: Displaying the output

Let us display the output using matplotlib subplots for a better comparison of results.

titles = ['Original Image',"Binary Image",'Dilated Image']
images = [image,masked_image, dilated_image]
plt.figure(figsize=(13,5))
for i in range(3):
    plt.subplot(1,3,i+1)
    plt.imshow(images[i],'gray')
    plt.title(titles[i])
    plt.xticks([])
    plt.yticks([])
plt.tight_layout()
plt.show()

Output:

Dilated Image

We can see the thickness of A being increased in the dilated image.

Download Materials

What Users are saying..

profile image

Ed Godalle

Director Data Analytics at EY / EY Tech
linkedin profile url

I am the Director of Data Analytics with over 10+ years of IT experience. I have a background in SQL, Python, and Big Data working with Accenture, IBM, and Infosys. I am looking to enhance my skills... Read More

Relevant Projects

Build a Multi-Class Classification Model in Python on Saturn Cloud
In this machine learning classification project, you will build a multi-class classification model in Python on Saturn Cloud to predict the license status of a business.

Langchain Project for Customer Support App in Python
In this LLM Project, you will learn how to enhance customer support interactions through Large Language Models (LLMs), enabling intelligent, context-aware responses. This Langchain project aims to seamlessly integrate LLM technology with databases, PDF knowledge bases, and audio processing agents to create a comprehensive customer support application.

Build a Speech-Text Transcriptor with Nvidia Quartznet Model
In this Deep Learning Project, you will leverage transfer learning from Nvidia QuartzNet pre-trained models to develop a speech-to-text transcriptor.

Machine Learning Project to Forecast Rossmann Store Sales
In this machine learning project you will work on creating a robust prediction model of Rossmann's daily sales using store, promotion, and competitor data.

Time Series Project to Build a Multiple Linear Regression Model
Learn to build a Multiple linear regression model in Python on Time Series Data

Build OCR from Scratch Python using YOLO and Tesseract
In this deep learning project, you will learn how to build your custom OCR (optical character recognition) from scratch by using Google Tesseract and YOLO to read the text from any images.

Skip Gram Model Python Implementation for Word Embeddings
Skip-Gram Model word2vec Example -Learn how to implement the skip gram algorithm in NLP for word embeddings on a set of documents.

Learn How to Build PyTorch Neural Networks from Scratch
In this deep learning project, you will learn how to build PyTorch neural networks from scratch.

OpenCV Project for Beginners to Learn Computer Vision Basics
In this OpenCV project, you will learn computer vision basics and the fundamentals of OpenCV library using Python.

Loan Default Prediction Project using Explainable AI ML Models
Loan Default Prediction Project that employs sophisticated machine learning models, such as XGBoost and Random Forest and delves deep into the realm of Explainable AI, ensuring every prediction is transparent and understandable.