How to alter the brightness of a grey scale image?

JavaObject Oriented ProgrammingProgramming

The equalizeHist() method of the Imgproc class accepts a greyscale image and equalizes its histogram, which will, in turn, normalizes the brightness and increases the contrast of the given image. This method accepts two parameters −

  • A Mat object representing the source image (greyscale).

  • A Mat object to save the result.

Example

Following Java program reads a colored image as greyscale, saves it, normalizes the brightness and increases the contrast of the given image and saves it.

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
public class HstExample {
   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 source = Imgcodecs.imread(input, Imgcodecs.IMREAD_GRAYSCALE );
      //Creating an empty matrix to store the result
      Mat dst = new Mat(source.rows(),source.cols(),source.type());
      Imgcodecs.imwrite("D://images//Grey_scale.jpg", source);
      //Increasing the contrast
      Imgproc.equalizeHist(source, dst);
      //Writing the image
      Imgcodecs.imwrite("D://images//increasing_contrast.jpg", dst);
      HighGui.imshow("output image", dst);
   }
}

Greyscale image

Resultant image

raja
Published on 09-Apr-2020 09:17:10
Advertisements