JAVA based Image Processing with OpenCV

Uvindu Dharmawardana
4 min readAug 6, 2020

Before we getting started with OpenCV let’s talk about what is Image Processing…?

Image Processing…?

Simply image processing is that enhancing an image or extracting useful information out of it using computer algorithms. digital image processing deals with developing a digital system that performs operations on a digital image.

OpenCV for JAVA…?

In order do Image processing there are several pre-defined image processing libraries and OpenCV is one of them. OpenCV is stand for Open Source Computer Vision Library. In simple words OpenCV is an open source computer vision and machine learning software library. you can download OpenCV from this link to your windows, Linux or Mac.

Set-up OpenCV with Intelij

After downloading the library you need to integrate it with your Idea. In this example I’m using Intelij. You can follow these steps to add the OpenCV jar file to your project.

Please follow these steps….

  1. Go the project structure and select Modules from the side panel and the select the Dependencies tab. Click the + icon on the right to add a dependency. Then select the Add Jar/Directory option.
  2. After that Then browse to the path where you installed the OpenCV and select build/java/opencv-***.jar and click OK.
  3. It will be shown as dependency in the window. Now, you also have to add the Native Library Location. To do that, double click on the opencv-***.jar, after that click on the + icon to add the library location.
  4. Next step is browse to the location where you installed OpenCV and select build/java/x64. You should select x64 or x86 based on your system specification. then click OK.

If these steps doesn't work go to project structure and select libraries and add your OpenCV java library.

Now you have completed the OpenCV set-up and let’s take look at a simple example and see how image processing is working with OpenCV.

OpenCV image erosion

Erosion and dilation are the two basic morphological operations. In this example I’m going to show you how to remove additional pixels in an image using OpenCV image erosion.

What happens in erosion is that it will remove that additional pixels are removed from image boundaries, total number of pixels removed during the erosion process depends on the dimensions of the structuring element used.

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.Mat;
import org.opencv.core.Size;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;


public class imageProcess extends Application {
public void start(Stage stage) throws IOException {
//Loading the OpenCV library
System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
//Reading image from your storage
String file ="D:\\Images\\valorant.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 = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size((4*2) + 1, (4*2)+1));
//Applying erosion on the Image
Imgproc.erode(src, dst, 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(80);
imageView.setY(80);
imageView.setFitWidth(675);
imageView.setPreserveRatio(true);
//Setting the Scene object
Group root = new Group(imageView);
Scene scene = new Scene(root, 595, 400);
stage.setTitle("OpenCV erosion example");
stage.setScene(scene);
stage.show();
}
public static void main(String args[]) {
launch(args);
}
}

You can try out this code and see what happens when you do the erosion to your image. you can always change values in kernel matrix to remove pixels as you like.

erode() method will be used to perform this operation and it will use Imgproc class, this method three mat objects representing source, destination, and kernel.

After compiling this code let’s see the difference….

Input Image

After the erosion output image

You can see a clear difference between two images. In the output image you can see that pixels are reduced than the input image. Just like that there are many image processing techniques in OpenCV try them out and have fun :).

--

--