Open In App
Related Articles

Image Processing in Java – Contrast Enhancement

Improve Article
Improve
Save Article
Save
Like Article
Like

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

 

If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. 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.

 


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 24 Jan, 2022
Like Article
Save Article
Similar Reads
Related Tutorials