Rectangle 27 0

Output

When you execute the given example, it converts an image name digital_image_processing.jpg to its equivalent Grayscale image and writes it on hard disk with name grayscale.jpg.

Rectangle 27 0

Output

When you execute the given example, it converts an image name digital_image_processing.jpg to its equivalent HSV color space image and writes it on hard disk with name hsv.jpg.

Rectangle 27 0

Example

The following example demonstrates the use of Imgproc class to convert an image from one color space to another.

import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;

import java.io.File;
import javax.imageio.ImageIO;

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

public class Main {
   public static void main( String[] args ) {
   
      try {
         System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
         File input = new File("digital_image_processing.jpg");
         BufferedImage image = ImageIO.read(input);	
         byte[] data = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
         Mat mat = new Mat(image.getHeight(),image.getWidth(), CvType.CV_8UC3);
         mat.put(0, 0, data);

         Mat mat1 = new Mat(image.getHeight(), image.getWidth(), CvType.CV_8UC3);
         Imgproc.cvtColor(mat, mat1, Imgproc.COLOR_RGB2HSV);

         byte[] data1 = new byte[mat1.rows()*mat1.cols()*(int)(mat1.elemSize())];
         mat1.get(0, 0, data1);
         BufferedImage image1 = new BufferedImage(mat1.cols(), mat1.rows(), 5);
         image1.getRaster().setDataElements(0, 0, mat1.cols(), mat1.rows(), data1);

         File ouptut = new File("hsv.jpg");
         ImageIO.write(image1, "jpg", ouptut);
         
      } catch (Exception e) {
         System.out.println("Error: " + e.getMessage());
      }
   }
}
Rectangle 27 0

Example

The following example demonstrates the use of Imgproc class to convert an image to Grayscale:

import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;

import java.io.File;
import javax.imageio.ImageIO;

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

public class Main {
   public static void main( String[] args ) { 
   
      try {
         System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
         File input = new File("digital_image_processing.jpg");
         BufferedImage image = ImageIO.read(input);	

         byte[] data = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
         Mat mat = new Mat(image.getHeight(), image.getWidth(), CvType.CV_8UC3);
         mat.put(0, 0, data);

         Mat mat1 = new Mat(image.getHeight(),image.getWidth(),CvType.CV_8UC1);
         Imgproc.cvtColor(mat, mat1, Imgproc.COLOR_RGB2GRAY);

         byte[] data1 = new byte[mat1.rows() * mat1.cols() * (int)(mat1.elemSize())];
         mat1.get(0, 0, data1);
         BufferedImage image1 = new BufferedImage(mat1.cols(),mat1.rows(), BufferedImage.TYPE_BYTE_GRAY);
         image1.getRaster().setDataElements(0, 0, mat1.cols(), mat1.rows(), data1);

         File ouptut = new File("grayscale.jpg");
         ImageIO.write(image1, "jpg", ouptut);
         
      } catch (Exception e) {
         System.out.println("Error: " + e.getMessage());
      }
   }
}
Rectangle 27 0

Java DIP - OpenCV Color Space Conversion

In order to change color space of one image to another using OpenCV, we read image into BufferedImage and convert it into Mat Object. Its syntax is given below:

File input = new File("digital_image_processing.jpg");
BufferedImage image = ImageIO.read(input);
//convert Buffered Image to Mat.

OpenCv allows many color conversion types, all of which can be found in the Imgproc class. Some of the types are described briefly:

Sr.No.Color Conversion Type
COLOR_RGB2BGRCOLOR_RGB2BGRA
COLOR_RGB2GRAYCOLOR_RGB2HLS
COLOR_RGB2HSVCOLOR_RGB2Luv
COLOR_RGB2YUVCOLOR_RGB2Lab

From any of the color conversion type, just pass the appropriate one into method cvtColor() in the Imgproc class. Its syntax is given below:

Imgproc.cvtColor(source mat, destination mat1, Color_Conversion_Code);

The method cvtColor() takes three parameters which are the source image matrix, the destination image matrix and the color conversion type.

Apart from the cvtColor() method, there are other methods provide by the Imgproc class. They are described briefly:

cvtColor(Mat src, Mat dst, int code, int dstCn)

It converts an image from one color space to another.

dilate(Mat src, Mat dst, Mat kernel)

It dilates an image by using a specific structuring element.

equalizeHist(Mat src, Mat dst)

It equalizes the histogram of a grayscale image.

filter2D(Mat src, Mat dst, int ddepth, Mat kernel, Point anchor, double delta) It convolves an image with the kernel.

GaussianBlur(Mat src, Mat dst, Size ksize, double sigmaX)

It blurs an image using a Gaussian filter.

integral(Mat src, Mat sum)

It calculates the integral of an image.

Rectangle 27 0

Java DIP - Introduction to OpenCV

OpenCV is released under a BSD license and hence it is free for both academic and commercial use. It has C++, C, Python, and Java interfaces, and it supports Windows, Linux, Mac OS, iOS, and Android.

OpenCV was designed for computational efficiency and with a strong focus on real-time applications. Written in optimized C/C++, the library can take advantage of multi-core processing.

Some of the basic features of OpenCV are described below:

Smoothing Images

This involves applying Blur, GaussianBlur, medianBlur, and bilateral Filter.

Eroding and Dilating

It can apply two very common morphology operators: Dilation and Erosion.

Morphology Transformations

OpenCV function morphologyEx to apply Morphological Transformation such as opening, closing, TopHat, and BlackHat etc.

Image Pyramids

OpenCV functions pyrUp and pyrDown to down sample or up sample a given image.

Basic Thresholding Operations

It can perform basic thresholding operations using OpenCV function threshold.

Adding borders to your images

OpenCV function copyMakeBorder is used to set the borders(extra padding to your image).

Remapping

In OpenCV, the function remap offers a simple remapping implementation.

Histogram Calculation

For simple purposes, OpenCV implements the function calcHist, which calculates the histogram of a set of arrays (usually images or image planes). It can operate with up to 32 dimensions.

Rectangle 27 0

Integrating OpenCV

These following steps explain how to integrate OpenCV into your applications.

You can download OpenCV from their official Website here.

Further, we create a user library of OpenCV, so that we can use it as a future project.

Launch Eclipse

Select Window -> Preferences from the menu.

Navigate under Java -> Build Path -> User Libraries and click New.

Now enter the name for your library. For example, OpenCV-2.4.6.

After that, select your new user library(i.e. OpenCV-2.4.6) and click on Add External JARs.

Browse through C:\OpenCV-2.4.6\build\java\ and select opencv-246.jar. After adding the jar, extend the opencv-246.jar and select Native library location and press Edit.

Select External Folder... and browse to select the folder C:\OpenCV-2.4.6\build\java\x64. If you have a 32-bit system, you need to select the x86 folder instead of x64.

Press Ok and you are done.

Now your user library is created. Now you can reuse this configuration in any of the project.

Create a new java project in eclipse.

On the Java Settings step, under Libraries tab, select Add Library... and select OpenCV-2.4.6, then click Finish.

Click finish and you are done.

Rectangle 27 0

Java DIP - OpenCV GrayScale Conversion

In order to convert a color image to Grayscale image using OpenCV, we read the image into BufferedImage and convert it into Mat Object. Its syntax is given below:

File input = new File("digital_image_processing.jpg");
BufferedImage image = ImageIO.read(input);
//convert Buffered Image to Mat.

Then you can transform the image from RGB to Grayscale format by using method cvtColor() in the Imgproc class. Its syntax is given below:

Imgproc.cvtColor(source mat, destination mat1, Imgproc.COLOR_RGB2GRAY);

The method cvtColor() takes three parameters which are the source image matrix, the destination image matrix, and the color conversion type.

Apart from the cvtColor method, there are other methods provided by the Imgproc class. They are listed below:

cvtColor(Mat src, Mat dst, int code, int dstCn)

It converts an image from one color space to another.

dilate(Mat src, Mat dst, Mat kernel)

It dilates an image by using a specific structuring element.

equalizeHist(Mat src, Mat dst)

It equalizes the histogram of a grayscale image.

filter2D(Mat src, Mat dst, int ddepth, Mat kernel, Point anchor, double delta) It convolves an image with the kernel.

GaussianBlur(Mat src, Mat dst, Size ksize, double sigmaX)

It blurs an image using a Gaussian filter.

integral(Mat src, Mat sum)

It calculates the integral of an image.