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
package ocv;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
public class GFG {
public static void main(String[] args)
{
try {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
Mat source = Imgcodecs.imread(
"E:\\input.jpg" ,
Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE);
Mat destination
= new Mat(source.rows(), source.cols(),
source.type());
Imgproc.equalizeHist(source, destination);
Imgcodecs.imwrite( "E:\\output.jpg" ,
destination);
System.out.print(
"Image Successfully Contrasted" );
}
catch (Exception e) {
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!