OpenCV
3.0.0-rc1
Open Source Computer Vision
|
In this tutorial you will learn how to:
We will explain dilation and erosion briefly, using the following image as an example:
As the kernel \(B\) is scanned over the image, we compute the maximal pixel value overlapped by \(B\) and replace the image pixel in the anchor point position with that maximal value. As you can deduce, this maximizing operation causes bright regions within an image to "grow" (therefore the name dilation). Take as an example the image above. Applying dilation we can get:
The background (bright) dilates around the black regions of the letter.
Analagously to the example for dilation, we can apply the erosion operator to the original image (shown above). You can see in the result below that the bright areas of the image (the background, apparently), get thinner, whereas the dark zones (the "writing") gets bigger.
This tutorial code's is shown lines below. You can also download it from here
Most of the stuff shown is known by you (if you have any doubt, please refer to the tutorials in previous sections). Let's check the general structure of the program:
Let's analyze these two functions:
element: This is the kernel we will use to perform the operation. If we do not specify, the default is a simple 3x3
matrix. Otherwise, we can specify its shape. For this, we need to use the function cv::getStructuringElement :
We can choose any of three shapes for our kernel:
Then, we just have to specify the size of our kernel and the anchor point. If not specified, it is assumed to be in the center.
dilation:
The code is below. As you can see, it is completely similar to the snippet of code for erosion. Here we also have the option of defining our kernel, its anchor point and the size of the operator to be used.
Compile the code above and execute it with an image as argument. For instance, using this image:
We get the results below. Varying the indices in the Trackbars give different output images, naturally. Try them out! You can even try to add a third Trackbar to control the number of iterations.