Eroding and Dilating Tutorial

Advertisements


In this chapter we are going to apply two very common morphology operators: Dilation and Erosion.

We are going to use OpenCV function erode and dilate. They can be found under Imgproc package. Its syntax is given below:

Imgproc.erode(source, destination, element);
Imgproc.dilate(source, destination, element);				

The parameters along with their description are listed below:

Sr.NoParameters & description
1source
The Source image.
2destination
The destination image.
3element
structuring element used for erosion and dilation, if element=Mat() , a 3 x 3 rectangular structuring element is used.

Apart from the erode and dilate method, there are other methods provide by the Imgproc class. They are listed below:

Sr.NoMethod & Description
1cvtColor(Mat src, Mat dst, int code, int dstCn)
Converts an image from one colour space to another.
2dilate(Mat src, Mat dst, Mat kernel)
Dilates an image by using a specific structuring element.
3equalizeHist(Mat src, Mat dst)
Equalizes the histogram of a grayscale image.
4filter2D(Mat src, Mat dst, int ddepth, Mat kernel, Point anchor, double delta)
Convolves an image with the kernel.
5GaussianBlur(Mat src, Mat dst, Size ksize, double sigmaX)
Blurs an image using a Gaussian filter.
6integral(Mat src, Mat sum)
Calculates the integral of an image.

Example

The following example demonstrates the use of Imgproc class to perform erosion and dilation to an image.

import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.highgui.Highgui;
import org.opencv.imgproc.Imgproc;

public class main {
   public static void main( String[] args )
   {
      try{	
         System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
         Mat source = Highgui.imread("digital_image_processing.jpg", 
         Highgui.CV_LOAD_IMAGE_COLOR);
         Mat destination =new Mat(source.rows(),source.cols(),source.type());
         destination = source;

         int erosion_size = 5;
         int dilation_size = 5;
         Mat element = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, 
         new Size(2*erosion_size + 1, 2*erosion_size+1));
         Imgproc.erode(source, destination, element);
         Highgui.imwrite("erosion.jpg", destination);

         source = Highgui.imread("digital_image_processing.jpg", 
         Highgui.CV_LOAD_IMAGE_COLOR);
         destination = source;
         Mat element1 = Imgproc.getStructuringElement(Imgproc.MORPH_RECT,
         new Size(2*dilation_size + 1, 2*dilation_size+1));
         Imgproc.dilate(source, destination, element1);
         Highgui.imwrite("dilation.jpg", destination);
         }catch (Exception e) {
            System.out.println("error: " + e.getMessage());
         }
  }
}

Output

Original Image

Eroding and Dilating Tutorial

On the above original image, some erosion and dilation operations have been performed which have been shown in the output below:

Erosion

Eroding and Dilating Tutorial

Dilation

Eroding and Dilating Tutorial

Advertisements
Advertisements