Reading a colored image as grey scale using Java OpenCV library.



The imread() method of the Imgcodecs class accepts a string value representing a file name as a parameter. This method reads the contents of the specified file into a matrix object and returns it. Using this method you can read the contents of an image.

In addition to this, the Imgcodecs class also provides another variant of this method which accepts an integer value representing a flag specifying the required reading mode.

The following are the various fields of the Imgcodecs class that can be used as flag values.

  • IMREAD_COLOR βˆ’ If the flag is set to this value, the loaded image will be converted to a 3-channel BGR (Blue Green Red) color image.

  • IMREAD_GRAYSCALE βˆ’ If the flag is set to this value, the loaded image will be converted to a single-channel grayscale image.

  • IMREAD_LOAD_GDAL βˆ’ If the flag is set to this value, you can load the image using the GDAL driver.

  • IMREAD_ANYCOLOR βˆ’ If the flag is set to this value, the image is read in any possible color format.

  • IMREAD_REDUCED_COLOR_2 or,
     IMREAD_REDUCED_COLOR_4 or,
    IMREAD_REDUCED_COLOR_8
    βˆ’ If the flag is set to this value, the image is read as three-channel BGR, and the size of the image is reduced to Β½, ΒΌth or ?th of the original size of the image with respect to the field used.

  • IMREAD_REDUCED_GRAYSCALE_2 or,
     IMREAD_REDUCED_GRAYSCALE_4 or,
     IMREAD_REDUCED_GRAYSCALE_8
    βˆ’ If the flag is set to this value, the image is read as a single-channel grayscale image, and the size of the image is reduced to Β½, ΒΌth or ?th of the original size of the image with respect to the field used.

  • IMREAD_UNCHANGED βˆ’ If the flag is set to this value, the loaded image is returned as it is.

Therefore, if you need to read a colored image as a greyscale image you should pass IMREAD_GRAYSCALE or, IMREAD_REDUCED_GRAYSCALE_X as parameters.

Example 1

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
public class ReadAsGreyScale {
   public static void main(String args[]) {
      //Loading the OpenCV core library
      System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
      //Reading the Image from the file
      Mat matrix = Imgcodecs.imread("D://images//sunset.jpg", Imgcodecs.IMREAD_GRAYSCALE);
      //Writing the image
      Imgcodecs.imwrite("D://images//sunset_greyscale.jpg", matrix);
      System.out.println("Image re-Saved");
   }
}

Input

Output

Example 2

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
public class ReadingAsGrey2 {
   public static void main(String args[]) {
      //Loading the OpenCV core library
      System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
      String input = "D://images//sunset.jpg";
      //Reading the Image from the file
      Mat matrix = Imgcodecs.imread(input, Imgcodecs.IMREAD_REDUCED_GRAYSCALE_4 );
      //Writing the image
      Imgcodecs.imwrite("D://images//sunset_grey_8.jpg", matrix);
      System.out.println("Image re-Saved");
   }
}

Output

Updated on: 2020-04-08T14:00:57+05:30

352 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements