In this article we will learn how to enhance contrast of an image using OpenCV library. In order to enhance contrast histogram equalization techniques are used.
For more information about histogram equalization techniques refer to this paper.
At first we need to setup OpenCV for Java, we recommend to use eclipse for the same since it is easy to use and setup. For installation refer to http://docs.opencv.org/2.4/doc/tutorials/introduction/java_eclipse/java_eclipse.html
Now lets understand some of the methods required for contrast enhancement.
- equalizeHist(source, destination) – This method resides in Imgproc package of OpenCv. source parameter is 8-bit single channel source image, and the destination parameter is the destination image
- Imcodecs.imread()/Imcodecs.imwrite() – These methods are used to read and write images as Mat objects which are rendered by OpenCV.
// Java program to demonstrate contrast enhancement package ocv; import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; public class Main { public static void main( String[] args ) { 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 ); //input image 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 Imgcodecs.imwrite( "E:\\output.jpg" , destination); } catch (Exception e) { System.out.println( "error: " + e.getMessage()); } } } |
Note: The code will not work in online ide since it requires image in hard drive.
Input.jpg
Output.jpg
This article is contributed by Pratik Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@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.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.