OpenCV Colour Space Conversion Tutorial

Advertisements


In order to change colour space of one image to another using OpenCV, first thing you need to do is to 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 colour conversion types , all of which can be found in the Imgproc class. Some of the types are listed below:

Sr.NoColour Conversion Type
1COLOR_RGB2BGR
2COLOR_RGB2BGRA
3COLOR_RGB2GRAY
4COLOR_RGB2HLS
5COLOR_RGB2HSV
6COLOR_RGB2Luv
7COLOR_RGB2YUV
8COLOR_RGB2Lab

From any of the colour 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 colour conversion type.

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

Sr.NoMethod & Description
1cvtColor(Mat src, Mat dst, int code, int dstCn)
Converts an image from one colour space to another.
2dilate(Mat src, Mat dst, Mat kernel)
Dilates an image by using a specific structuring element.
3equalizeHist(Mat src, Mat dst)
Equalizes the histogram of a grayscale image.
4filter2D(Mat src, Mat dst, int ddepth, Mat kernel, Point anchor, double delta)
Convolves an image with the kernel.
5GaussianBlur(Mat src, Mat dst, Size ksize, double sigmaX)
Blurs an image using a Gaussian filter.
6integral(Mat src, Mat sum)
Calculates the integral of an image.

Example

The following example demonstrates the use of Imgproc class to convert an image from one colour 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());
         }
   }
}

Output

When you run the above example, it would convert an image name digital_image_processing.jpg to its equivalent HSV color space image and write it on hard disk with name hsv.jpg.

Original Image (RGB)

OpenCV Colour Space Conversion Tutorial

Converted Image(HSV)

OpenCV Colour Space Conversion Tutorial

Advertisements
Advertisements