Open In App

Image Processing in Java – Contrast Enhancement

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Prerequisites:

Contrast Enhancement 

At first, we need to set up OpenCV for Java, we recommend using eclipse for the same since it is easy to use and set up. Now let us understand some of the methods required for enhancing the color of an image. Now let us understand some of the methods required for contrast enhancement. 

Method 1: equalizeHist(source, destination): This method resides in Imgproc package of opencv.source parameter is an 8-bit single-channel source image, and the destination parameter is the destination image

Method 2: Imcodecs.imread() or Imcodecs.imwrite(): These methods are used to read and write images as Mat objects which are rendered by OpenCV.

Implementation: Let us take an arbitrary input image which is as follows: 

Example:

Java




// Java Program to Demonstrate Contrast Enhancement
// Using OpenCV Library
 
// Importing package module
package ocv;
 
// Importing required classes
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
 
// Main class
public class GFG {
 
    // Try block to check for exceptions
    public static void main(String[] args)
    {
 
        // Try block to check for exceptions
        try {
 
            // For proper execution of native libraries
            // Core.NATIVE_LIBRARY_NAME must be loaded
            // before calling any of the opencv methods
            System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
 
            // Reading image from local directory by
            // creating object of Mat class
            Mat source = Imgcodecs.imread(
                "E:\\input.jpg",
                Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE);
            Mat destination
                = new Mat(source.rows(), source.cols(),
                          source.type());
 
            // Applying histogram equalization
            Imgproc.equalizeHist(source, destination);
 
            // Writing output image to some other directory
            // in local system
            Imgcodecs.imwrite("E:\\output.jpg",
                              destination);
 
            // Display message on console for successful
            // execution of program
            System.out.print(
                "Image Successfully Contrasted");
        }
 
        // Catch block to handle exceptions
        catch (Exception e) {
 
            // Print the exception on the console
            // using getMessage() method
            System.out.println("error: " + e.getMessage());
        }
    }
}


 
 

Output: On console

 

Image Successfully Contrasted

 

 



Last Updated : 24 Jan, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads