Date:

Share:

How to Dilate Images with OpenCV

Related Articles

Last updated: March 31, 2023


Welcome, computer vision geeks! This guide is a complete guide to image dilation, a key component of computer vision preprocessing. This is important for computer vision because it highlights features and makes objects more visible in binary images.

Note: Here is a link to Full source code.

Looking for Grinding in OpenCV?

Expansion: Expanding the horizons of image processing

Before diving in, let’s discuss image dilation and why it’s an essential technique in computer vision. Image dilation is a morphological operation that expands or thickens the white areas of a binary image (introduction). It is used to fill small gaps, connect disjointed components or highlight certain features in the image. In fact, dilation makes image features more salient and easier for algorithms to process and analyze.

The mechanics of expansion

Now that we’ve covered the basics, let’s explore the inner workings of image enlargement. Dilation is a local operation that compares each pixel in a binary image with its neighbors using a structuring element (also known as a kernel). The structure element is a small binary matrix that defines the neighborhood of a pixel.

During dilation, the kernel is placed on top of the image, and the central pixel is replaced by the maximum value of its neighbors covered by the structure element. In binary images this means that if one of the adjacent pixels is white (1), the central pixel will also become white (1).

OpenCV: the cornerstone of computer vision

OpenCV is a powerful open source computer vision library that offers a wealth of tools and techniques for image processing, including image dilation. Combined with Python, a versatile and beginner-friendly programming language, OpenCV becomes an incredibly powerful tool for computer vision tasks.

You can learn more about it with these great ones OpenCV tutorials.

Let’s get rolling!

To get started with image dilation in OpenCV and Python, let’s create a new Python environment:

python -m venv dilationdemo
source dilationdemo/bin/activate

Next, we need to install the OpenCV library. If you haven’t already done so, you can do so by running the following command in a terminal or command prompt:

pip install opencv-python

With OpenCV installed, let’s jump into the code!

We are going to use this image in our example:

You can Download it here

Let’s start by importing the necessary libraries and reading in our binary image.

import cv2
import numpy as np
image = cv2.imread('images/mustang.png', cv2.IMREAD_GRAYSCALE)

Next, we will create a structural element. For this example, we will use a 5×5 rectangular kernel. We will experiment with different shapes and sizes later.

kernel = np.ones((5, 5), np.uint8)

Now that we have our structure element, it’s time to do some extension using OpenCV’s dilate() function:

dilated_image = cv2.dilate(image, kernel, iterations=1)

Finally, let’s present the original and enlarged images side by side:

cv2.imshow('Original Image', image)
cv2.imshow('Dilated Image', dilated_image)

cv2.waitKey(0)
cv2.destroyAllWindows()

Now, let’s run the script:

Wow! Successfully extending an image using OpenCV and Python.

As you can see, the white lines are much more defined in the second image. You can adjust the thickness of these lines, as you will see in the next section.

Extension adjustment

You can adjust the expansion by playing with the size of the matrix and the iterations.

matrix size

for example. You can get more of the final details if you adjust the matrix size. A smaller matrix size will have less effect and cover a smaller area. But it is good for extracting details:

kernel = np.ones((3, 3), np.uint8)

It’s not that noticeable, because we were already at a fairly small matrix size. Look what happens when we increase it. The subtle details are not as noticeable, and the effect is more significant:

Each of these methods has an advantage depending on your application.

iterations

You can also change the iterations to increase the effect.

Change this line of code back to 5×5:

kernel = np.ones((5, 5), np.uint8)

And we see this result again:

Now increase the iterations to 2:

dilated_image = cv2.dilate(image, kernel, iterations=2)

And you see a more noticeable effect.

These are the ways you can tweak the images until they work best for you.

A key takeaway

  1. Image dilation is an operation that expands the white areas in a binary image, helping to highlight features and make them more prominent for processing and analysis.

  2. The expansion process includes using a structuring element (kernel) to compare each pixel with its neighbors and replace the central pixel with the maximum value of its neighbors covered by the structuring element.

  3. God dilate() The function in OpenCV allows easy extension of images with user-defined structure elements.

Mazel Tov! You know how to work with extension in OpenCV and Python. But this is just the beginning. Dilation is a fundamental technique that paves the way for countless other exciting computer vision applications, such as edge detection, feature extraction, and object segmentation.

So, keep exploring and experimenting with different actions and techniques to improve your computer vision skills. Bookmark my blog, because I have many more OpenCV Stuff here!

Happy coding, and see you in the next tutorial.

– Jeremy

I have a lot OpenCV demos On GitHub you can download and run. I build them on mine Computer Vision live broadcast

Questions, comments? Yell at me!


Source

Popular Articles