• Home
  • Despre MMDB
  • Articole
    • Algoritmi prelucrare imagini
    • Algoritmi extragere caracteristici imagini
      • Caracteristici globale
      • Caracteristicile matricii de co-ocurenta
      • Caracteristici color
      • Filtre Gabor
    • Metode de indexare imagini in baze de date
    • Algortmi de cautare in baze de imagini
  • Cod JAVA
    • Algoritmi prelucrare imagini
    • Algoritmi extragere caracteristici imagini
      • Caracteristici globale
      • Caracteristicile matricii de co-ocurenta
      • Caracteristici color
    • Metode de indexare imagini in baze de date
    • Algortmi de cautare in baze de imagini
  • Baze de date de imagini
    • Grayscale Images Databases
    • Color/Hyperspectral Images
    • Biomedical Images
    • 3D Scanning
    • Biometric Images
  • Solutii implementate
  • Contact

GrayScale Conversion

Categorie: Caracteristici color Publicat: 01 Iulie 2015
Scris de Alex Accesări: 937
  • Tipărire
  • Email

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

File input =newFile("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 methodcvtColor() 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:

Sr.No.Methods
1

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

It converts an image from one color space to another.

2

dilate(Mat src, Mat dst, Mat kernel)

It dilates an image by using a specific structuring element.

3

equalizeHist(Mat src, Mat dst)

It equalizes the histogram of a grayscale image.

4

filter2D(Mat src, Mat dst, int ddepth, Mat kernel, Point anchor, double delta)

It convolves an image with the kernel.

5

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

It blurs an image using a Gaussian filter.

6

integral(Mat src, Mat sum)

It calculates the integral of an image.

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;

publicclassMain{
publicstaticvoid main(String[] args ){try{System.loadLibrary(Core.NATIVE_LIBRARY_NAME );
File input =newFile("digital_image_processing.jpg");
BufferedImage image =ImageIO.read(input);
byte[] data =((DataBufferByte) image.getRaster().getDataBuffer()).getData();
Mat mat =newMat(image.getHeight(), image.getWidth(),CvType.CV_8UC3);
mat.put(0,0, data);
Mat mat1 =newMat(image.getHeight(),image.getWidth(),CvType.CV_8UC1);
Imgproc.cvtColor(mat, mat1,Imgproc.COLOR_RGB2GRAY);
byte[] data1 =newbyte[mat1.rows()* mat1.cols()*(int)(mat1.elemSize())];
mat1.get(0,0, data1);
BufferedImage image1 =newBufferedImage(mat1.cols(),mat1.rows(),BufferedImage.TYPE_BYTE_GRAY); image1.getRaster().setDataElements(0,0, mat1.cols(), mat1.rows(), data1);
File ouptut =newFile("grayscale.jpg");
ImageIO.write(image1,"jpg", ouptut);
}
catch(Exception e){
System.out.println("Error: "+ e.getMessage());
}
}
}

Output

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

Original Image

OpenCV GrayScale Conversion Tutorials

Grayscale Image

OpenCV GrayScale Conversion Tutorials

 

https://drive.google.com/file/d/0Bw7015w4SfZmMVZOcExzbDctOEE/view?usp=sharing

  • Contact
  • Termeni si conditii
Copyright © MMDB - Multimedia DataBase 2025 All rights reserved. Custom Design by Youjoomla.com
Caracteristici color