
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Explain Morphological Opening 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 opening
This is an operation that is equivalent to applying erosion on an image and then dilating the resultant image. Using this, you can remove small objects from the foreground an image while preserving the features of the large object.
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_OPEN 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 MorphologicalOpening 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_OPEN, 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 −