Our Feeds

Thursday, 23 February 2017

AJITH KP

Histogram Equalization Algorithm and Implementation in Java

Histogram equalization 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 varying 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 equalization is used in digital image processing is to generate an image with equally distributed brightness level over the whole brightness scale.

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

ALGORITHM

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 scan every pixel of image and increment 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 4: Rescan image and create new image with gray level value,
            NewImg[x][y] = T[OldImg[x][y]]

SCREENSHOTS

Histogram Equalisation
Histogram Equalisation in Java: 1

Histogram Equalisation in Java
Histogram Equalisation in Java: 2

SOURCE CODE

import java.awt.GridLayout;
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


/**
 *
 * @author AJITH KP (http://fb.com/ajithkp560)
 * http://www.terminalcoders.blogspot.com
 * 
 */
public class HistogramEqualization extends JFrame{
    HistogramEqualization(String in, String out){
        super("..:: Histogram Equalization ::..");
        try {
            this.setLayout(new GridLayout(1, 2, 10, 10));
            JPanel img1 = new JPanel();
            JPanel img2 = new JPanel();
            File f1 = new File(in);
            File f2 = new File(out);
            BufferedImage image1 = getGrayscaleImage(ImageIO.read(f1));
            img1.add(new JLabel(new ImageIcon(image1)));
            BufferedImage image2 = equalize(image1);
            ImageIO.write(image2, "png", f2);
            img2.add(new JLabel(new ImageIcon(image2)));
            this.add(img1);
            this.add(img2);
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }
    BufferedImage equalize(BufferedImage src){
        BufferedImage nImg = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
        WritableRaster wr = src.getRaster();
        WritableRaster er = nImg.getRaster();
        int totpix= wr.getWidth()*wr.getHeight();
        int[] histogram = new int[256];

        for (int x = 1; x < wr.getWidth(); x++) {
            for (int y = 1; 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);
            }
        }
        nImg.setData(er);
        return nImg;
    }
    BufferedImage getGrayscaleImage(BufferedImage src) {
        BufferedImage gImg = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
        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));
            }
        }
        gImg.setData(gr);
        return gImg;
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        System.out.println("#! TERMINALCODERS ::..\nhttp://www.terminalcoders.blogspot.com");
        if(args.length<2){
            System.out.println("Usage: java HistogramEqualization <input file name> <output file name>");
        }
        else{
            HistogramEqualization he = new HistogramEqualization(args[0], args[1]);
            he.setSize(1024, 500);
            he.setVisible(true);
            he.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    }
    
}

31 comments

Write comments
Anonymous
AUTHOR
24 February 2017 at 08:53 delete

Thanks for Java code.

Reply
avatar
Ajith KP
AUTHOR
24 February 2017 at 08:54 delete

Thanks... Please share post with your friends... :)

Reply
avatar
Anonymous
AUTHOR
24 February 2017 at 08:57 delete

Is histogram equalization used for increase clarity of image?

Reply
avatar
Ajith KP
AUTHOR
24 February 2017 at 09:00 delete

To create image with equally distributed brightness level... You can see what happened in above images... :)

Reply
avatar
2 May 2017 at 21:14 delete This comment has been removed by a blog administrator.
avatar
Unknown
AUTHOR
28 August 2018 at 06:39 delete This comment has been removed by a blog administrator.
avatar
sai
AUTHOR
4 September 2018 at 00:44 delete

It was worth visiting your blog and I have bookmarked your blog. Hope to visit again
Click here:
angularjs training in chennai
Click here:
angularjs training in velarchery

Reply
avatar
Unknown
AUTHOR
7 September 2018 at 07:00 delete

I am really happy with your blog because your article is very unique and powerful for new reader.
Click here:
Selenium Training in Chennai | Selenium Training in Bangalore | Selenium Training in Pune

Reply
avatar
pooja
AUTHOR
10 September 2018 at 23:27 delete

Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging.
Blueprism training in tambaram

Blueprism training in annanagar

Blueprism training in velachery

Reply
avatar
6 March 2019 at 23:18 delete

I really like the dear information you offer in your articles. I’m able to bookmark your site and show the kids check out up here generally. Im fairly positive theyre likely to be informed a great deal of new stuff here than anyone
angularjs online training

apache spark online training

informatica mdm online training

devops online training

aws online training

Reply
avatar
jai
AUTHOR
12 March 2019 at 04:03 delete

Thanks a lot for sharing us about this update. Hope you will not get tired on making posts as informative as this. 
Microsoft Azure online training
Selenium online training
Java online training
Python online training
uipath online training

Reply
avatar
1 December 2019 at 21:44 delete

Very useful and information content has been shared out here, Thanks for sharing it.sap hr training in bangalore

Reply
avatar
nizam
AUTHOR
17 May 2020 at 19:52 delete

it is wonderful as always and do more and share more
BEST ANGULAR JS TRAINING IN CHENNAI WITH PLACEMENT

https://www.acte.in/angular-js-training-in-chennai
https://www.acte.in/angular-js-training-in-annanagar
https://www.acte.in/angular-js-training-in-omr
https://www.acte.in/angular-js-training-in-porur
https://www.acte.in/angular-js-training-in-tambaram
https://www.acte.in/angular-js-training-in-velachery

Reply
avatar
harini
AUTHOR
31 October 2020 at 03:49 delete

This article has great content. Thanks for sharing it across and making it available. It helps in all the way around. Very informative and interesting blog. Appreciate all your efforts.
Selenium Training in Chennai

Selenium Training in Velachery

Selenium Training in Tambaram

Selenium Training in Porur

Selenium Training in Omr

Selenium Training in Annanagar

Reply
avatar
Pushba
AUTHOR
11 November 2020 at 22:39 delete

I really like the dear information you offer in your articles. I’m able to bookmark your site and show the kids check out up here generally. Im fairly positive theyre likely to be informed a great deal of new stuff here than anyone
angularjs online training
IELTS Coaching in chennai

German Classes in Chennai

GRE Coaching Classes in Chennai

TOEFL Coaching in Chennai

Spoken english classes in chennai | Communication training

Reply
avatar
Lavanya
AUTHOR
8 December 2020 at 03:08 delete

This article has great content. Thanks for sharing it across and making it available. It helps in all the way around. Very informative and interesting blog. Appreciate all your efforts.
salesforce training in chennai

software testing training in chennai

robotic process automation rpa training in chennai

blockchain training in chennai

devops training in chennai

Reply
avatar