Monday, November 18, 2024
Google search engine
HomeLanguagesJavaImage Processing in Java – Contrast Enhancement

Image Processing in Java – Contrast Enhancement

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

 

This article is contributed by Pratik Agarwal. If you like Lazyroar 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 Lazyroar 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.

 

RELATED ARTICLES

Most Popular

Recent Comments