Java DIP - Enhancing Image Contrast

In this chapter learn how to enhance the contrast of an image using histogram equalization.

We use the OpenCV function equalizeHist() method. It can be found under Imgproc package. Its syntax is given below −

Imgproc.equalizeHist(source, destination);

The parameters are described below −

Sr.No. Parameter & Description


It is 8-bit single channel source image.



It is the destination image.

Apart from the equalizeHist() method, there are other methods provided by the Imgproc class. They are described briefly −

Sr.No. Method & Description

cvtColor(Mat src, Mat dst, int code, int dstCn)

It converts an image from one color space to another.


dilate(Mat src, Mat dst, Mat kernel)

It dilates an image by using a specific structuring element.


equalizeHist(Mat src, Mat dst)

It equalizes the histogram of a grayscale image.


filter2D(Mat src, Mat dst, int ddepth, Mat kernel, Point anchor, double delta)

It convolves an image with the kernel.


GaussianBlur(Mat src, Mat dst, Size ksize, double sigmaX)

It blurs an image using a Gaussian filter.


integral(Mat src, Mat sum)

It calculates the integral of an image.


The following example demonstrates the use of Imgproc class to enhance contrast of an image −

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.highgui.Highgui;
import org.opencv.imgproc.Imgproc;

public class Main {

   static int width;
   static int height;
   static double alpha = 2;
   static double beta = 50;
   public static void main( String[] args ) {
      try {
         System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
         Mat source = Highgui.imread("grayscale.jpg", 
         Mat destination = new Mat(source.rows(),source.cols(),source.type());
         Imgproc.equalizeHist(source, destination);
         Highgui.imwrite("contrast.jpg", destination);
      } catch (Exception e) {
         System.out.println("error: " + e.getMessage());


When you execute the given code, the following output is seen −

Original Image

Enhancing Image Contrast Tutorial

Enhanced Contrast Image

Enhancing Image Contrast Tutorial
Kickstart Your Career

Get certified by completing the course

Get Started