Introduction
This tutorial is about one of the morphological operations on images – dilation. Previously we saw another morphological operation – blurring. Again, we will use a ready OpenCV function for the application of dilation on an image. Thanks to the already developed functions in OpenCV you don’t need to write all the code by yourself and this saves a lot of time and effort.
First I will start with a quick overview of the dilation process. I won’t go into much details and mathematical equations. Then I will introduce a short source code, which is used to apply a dilation on a given image. For the purpose of this tutorial I will be using black and white binary images. We can apply dilation to a color images as well.
The dilation process overview
The dilation is a morphological operation which enlarges the boundaries of regions of foreground pixels – typically the white pixels. In case of a binary images, the dilation will enlarge all the white regions, while shrinking the black ones. Thus, we can remove any noises like small black pixels and fill any black holes inside the white regions.
The dilation uses a so called structuring element – a set of points. They are applied on the input image and based on their size the effect of the dilation could be stronger or weaker. There are several types of structuring elements, that we could use. The most common one is a rectangle or in most cases a square. But OpenCV also supports a cross and an ellipse. It’s really up to you to find the perfect structuring element. You will need to play with its sizes and type until you get the desired result.
Demonstration on how the dilation works
Input image
Structuring element
Result
Here we have one input image with three white pixels and one structuring element in the form of cross. We can think of the dilation as performing the following operations. It finds one white pixel in the input image, then on top of that white pixel it pastes the cross element. The center of the cross is on top of the white pixel. What will happen next is that the other white pixels of the cross will cover some of the black pixels of the input image. The black pixels, which are covered by the structuring element’s white pixels will be turned into white pixels. This will go for each white pixel in input image.
Here is very important to understand that the new white pixels are applied to the result image and not on the input image. This way the input image will remain the same and we will go only over the original three white pixels, not over the newly added ones. Look at the above example and try to perform the operations by yourself.
I hope the example is understandable. In case you have troubles, write a comment in the comments section.
This was some simple overview about the dilation process. More information you can find here and here.
The source code for the dilation operation
Next, let’s see some source code. As I said, OpenCV already has a function to perform the dilation. That is why the source code is simple and so is our developer’s life.
using namespace cv; void dilateImage() { // Path to the input image std::string l_pathToInputImage{ "../Resources/hazard.jpg" }; // Create an object to hold the image data of the first image Mat l_image; // Read the image date from a file with no change to color scheme l_image = imread(l_pathToInputImage, IMREAD_UNCHANGED); // Check if we have read the first image data correctly if (!l_image.data) { std::cout << "No image data \n"; return; } // Create the output image matrix Mat l_outputImage = l_image.clone(); // Creating the structuring element for the morphological operation int l_dilationSize = 3; Mat l_dilatingElement = getStructuringElement( MORPH_RECT, Size( 2*l_dilationSize + 1, 2*l_dilationSize+1 ), Point( l_dilationSize, l_dilationSize ) ); // Dilate the image dilate( l_image, l_outputImage, l_dilatingElement ); // Display the input image namedWindow("Input", WINDOW_NORMAL); cv::imshow("Input", l_image); // Display the result image namedWindow("Result", WINDOW_NORMAL); cv::imshow("Result", l_outputImage); }
Firstly, we read the input image and check if we have read the image data correctly. Then we create a clone of the input image data, which will hold the result of the dilation. Next thing we do is to create the structuring element. Luckily in OpenCV there is the getStructuringElement() function which does this for us. The function takes the shape of the element we want to create as a first argument. Here is more information about the available shapes. In our case I am choosing a rectangle. Next, I am setting the size of the structuring element. The third argument is the anchor or what will be considered as a center of the structuring element.
After we have the structuring element it is time to call the function to dilate the image. The OpenCV function dilate() is responsible for applying the dilation over the input image. As you can see from the documentation it takes as a first argument the input image. Following is the output image and then we provide the structuring element. There are four more arguments which we use with their default values, because they work for us as they are.
The result of the dilation operation
Finally we display the input image and the result of the dilation operation. One thing that is worth mentioning is that OpenCV treats the black color of the binary image as a background and the white color as a foreground. That is why if you have seen in some books or articles about the dilation, they sometimes use the black color as a foreground so dilating a binary image might show the black color growing. In the case of OpenCV the white color is the foreground. So when dilating an image in OpenCV the white color is growing.
Here is my result with one binary image:

What you need to see here is the black holes which appear on the left input image are almost gone on the right (dilated) image. This is the effect of dilation. Also, here is another image with a bigger kernel size. See how the symbol looks grown and bolded.

Also here is a result of dilating a grayscale image:

As you can see the white pixels has grown and the image is somehow blurred.
Conclusion
In this tutorial we saw how to apply the dilation operation on an image using a ready OpenCV function. We had a brief overview on what is dilation and how can we use it. The source code we have so far is available for download below and also on GitHub.
Next we will get familiar with the opposite process of dilation – erosion.
OpenCV Training
Passionate developer, loving husband and caring father. As an introvert programming is my life. I work as a senior software engineer in a big international company. My hobbies include playing computer games (mostly World of Warcraft), watching TV series and hiking.