
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
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.
Example
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); imageView.setX(10); imageView.setY(10); imageView.setFitWidth(575); imageView.setPreserveRatio(true); //Setting the Scene object Group root = new Group(imageView); Scene scene = new Scene(root, 595, 400); stage.setTitle("Dilation Example"); stage.setScene(scene); stage.show(); } public static void main(String args[]) { launch(args); } }
Input Image
Output
On executing, the above program generates the following output −
- Related Articles
- Explain Morphological Opening in OpenCV using Java.
- Performing a closing operation on an image using OpenCV
- How to compute the morphological gradient of an image using OpenCV in Python?
- Explain Top Hat and Black hat morphological operations in Java.
- Explain OpenCV Laplacian Transformation using java Example
- Explain OpenCV Distance Transformation using java Example
- Explain OpenCV Simple Threshold using Java Example
- Explain OpenCV Adaptive Threshold using Java Example
- Explain Otsu threshold technique in OpenCV using a Java Example
- Explain the Mat class in Java OpenCV library
- Java Image Translation example using OpenCV.
- How to draw polylines in OpenCV using Java?
- OpenCV Hough Line Transform implementation using Java.
- How to draw a line in OpenCV using Java?
- How to draw an ellipse in OpenCV using Java?
