Explain morphological Closing in OpenCV using Java.

Morphological operations are the set of operations that process images according to the given shapes. Erosion and dilation are the two basic morphological operations.

  • During dilation, additional pixels are added to the image boundaries.

  • During erosion, additional pixels are removed from the image boundaries.

The total number of pixels added/removed depends on the dimensions of the structuring element used. You can perform erosion and dilation operations using the erode() and dilate() methods respectively.

In addition to dilation, OpenCV provides more morphological transformations such as Opening, Closing, Morphological Gradient, Top Hat, Black Hat.

Morphological Closing

This is an operation that is equivalent to applying dilation on an image and then eroding the resultant image. Using this, you can remove/fill small holes in an image. In short, Morphological Closing is used to remove noise from an image.

You can apply this to an image using the morphologyEx() method. This method accepts −

  • Two Mat objects representing the source and destination images.

  • An integer variable representing the type of the Morphological operation.

  • A Mat object representing the kernel matrix.

To apply Morphological opening operation to an image you need to invoke the above-specified method by passing Imgproc.MORPH_CLOSE as (3rd) a parameter, along with source destination and kernel matrices.


import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.stage.Stage;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
public class MorphologicalClosing extends Application {
   public void start(Stage stage) throws IOException {
      //Loading the OpenCV core library
      System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
      //Reading image data
      String file ="D:\Images\morph_input2.jpg";
      Mat src = Imgcodecs.imread(file);
      //Creating destination matrix
      Mat dst = new Mat(src.rows(), src.cols(), src.type());
      //Preparing the kernel matrix object
      Mat kernel = Mat.ones(5,5, CvType.CV_32F);
      //Applying dilate on the Image
      Imgproc.morphologyEx(src, dst, Imgproc.MORPH_CLOSE, kernel);
      //Converting matrix to JavaFX writable image
      Image img = HighGui.toBufferedImage(dst);
      WritableImage writableImage= SwingFXUtils.toFXImage((BufferedImage) img, null);
      //Setting the image view
      ImageView imageView = new ImageView(writableImage);
      //Setting the Scene object
      Group root = new Group(imageView);
      Scene scene = new Scene(root, 595, 400);
      stage.setTitle("Dilation Example");
   public static void main(String args[]) {

Input Image


On executing, the above program generates the following output −

Updated on: 13-Apr-2020


Kickstart Your Career

Get certified by completing the course

Get Started