Enhancing Image Contrast Tutorial


In this chapter we are going to enhance the contrast of an image using histogram equalization.

We are going to use OpenCV function equalizeHist. It can be found under Imgproc package. Its syntax is given below:

Imgproc.equalizeHist(source, destination);

The paramaters along with their description are listed below:

Sr.NoParamaters & description
Source 8-bit single channel image.
The destination image.

Apart from the equalizeHist method, there are other methods provide by the Imgproc class. They are listed below:

Sr.NoMethod & Description
1cvtColor(Mat src, Mat dst, int code, int dstCn)
Converts an image from one colour space to another.
2dilate(Mat src, Mat dst, Mat kernel)
Dilates an image by using a specific structuring element.
3equalizeHist(Mat src, Mat dst)
Equalizes the histogram of a grayscale image.
4filter2D(Mat src, Mat dst, int ddepth, Mat kernel, Point anchor, double delta)
Convolves an image with the kernel.
5GaussianBlur(Mat src, Mat dst, Size ksize, double sigmaX)
Blurs an image using a Gaussian filter.
6integral(Mat src, Mat sum)
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 )
         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());


Original Image

Enhancing Image Contrast Tutorial

Enhanced Contrast Image:

Enhancing Image Contrast Tutorial