Click here to Skip to main content
15,919,931 members
Articles / Programming Languages / Java

Histogram Equalisation in Java

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
23 Feb 2017CPOL1 min read 22.9K   551  
Enhance Image using Histogram Equalization


Histogram equalisation is a technique used to enhance the contrast of image using the histogram of image. The histogram of image represents the frequency of gray levels in the image. The gray levels of image vary from 0 to 255, that is a gray scale image's pixel size is 8 bits(1 byte). So the histogram contains frequency of occurrence of values from 0 to 255.

The aim of histogram equalisation used in digital image processing is to generate an image with equally distributed brightness level over the whole brightness scale.

Histogram Equalization

Histogram equalisation can enhance contrast for brightness values close to histogram maxima and decrease contrast near minima.


Step 1. Image size: NxM, gray level from 0 to 255, create an array H of size 256 and initialise it with 0.

Step 2: Create image histogram by scanning every pixel of image and incrementing the relevant member of array.

H[grayval(pix)] = H[grayval(pix)]+1

Step 3: Form a cumulative histogram CH of size 256.

CH[0] = H[0]

CH[i] =  CH[i-1] + H[i], i=1,2,3,...255.

Step 4

Set T[i] = Round((255*CH[i])/(NxM))

Step 5: Rescan image and create new image with gray level value.

NewImg[x][y] = T[OldImg[x][y]]

Using the Code

The first thing to do is convert image to gray scale image. The function to convert image to gray scale image is defined.

BufferedImage getGrayscaleImage(BufferedImage src) {
    BufferedImage gImg = new BufferedImage(src.getWidth(), src.getHeight(),
    WritableRaster wr = src.getRaster();
    WritableRaster gr = gImg.getRaster();
    for(int i=0;i<wr.getWidth();i++){
        for(int j=0;j<wr.getHeight();j++){
            gr.setSample(i, j, 0, wr.getSample(i, j, 0));
    return gImg;

The second step is converting the gray scale image to enhanced, histogram equalised image. The algorithm stated above is implemented in the below function.

BufferedImage equalize(BufferedImage src){
    BufferedImage nImg = new BufferedImage(src.getWidth(), src.getHeight(),
    WritableRaster wr = src.getRaster();
    WritableRaster er = nImg.getRaster();
    int totpix= wr.getWidth()*wr.getHeight();
    int[] histogram = new int[256];

    for (int x = 0; x < wr.getWidth(); x++) {
        for (int y = 0; y < wr.getHeight(); y++) {
            histogram[wr.getSample(x, y, 0)]++;

    int[] chistogram = new int[256];
    chistogram[0] = histogram[0];
    for(int i=1;i<256;i++){
        chistogram[i] = chistogram[i-1] + histogram[i];

    float[] arr = new float[256];
    for(int i=0;i<256;i++){
        arr[i] =  (float)((chistogram[i]*255.0)/(float)totpix);

    for (int x = 0; x < wr.getWidth(); x++) {
        for (int y = 0; y < wr.getHeight(); y++) {
            int nVal = (int) arr[wr.getSample(x, y, 0)];
            er.setSample(x, y, 0, nVal);
    return nImg;


Histogram Equalisation

Histogram Equalisation in Java: 1

Histogram Equalisation in Java

Histogram Equalisation in Java: 2


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Systems Engineer Tata Consultancy Service Ltd.
India India
Name Ajith Kp. Currently working at Tata Consultancy Service Ltd. I completed MCA from School of Information Science and Techonolgy, Kannur University Campus, Mangattuparamba. I like programming as well as computer/network security analyzing. I'm concentrating programming in Android, PHP, Python, Ajax, JQuery, C# and JAVA.


Knowledge in Java, Python, PHP, and Android.

Comments and Discussions

-- There are no messages in this forum --