Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App

Related Articles

Image Processing in Java – Contrast Enhancement

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article


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: 



// 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
            // Reading image from local directory by
            // creating object of Mat class
            Mat source = Imgcodecs.imread(
            Mat destination
                = new Mat(source.rows(), source.cols(),
            // Applying histogram equalization
            Imgproc.equalizeHist(source, destination);
            // Writing output image to some other directory
            // in local system
            // Display message on console for successful
            // execution of program
                "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


This article is contributed by Pratik Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


My Personal Notes arrow_drop_up
Last Updated : 24 Jan, 2022
Like Article
Save Article
Similar Reads
Related Tutorials