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|
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.