Ignacious
Ignacious

Reputation: 119

Histogram Equalization Problems

I'm working on a project in which I'm trying to use histogram equalization to do something like going from this image

http://zerocool.is-a-geek.net/wp-content/uploads/2011/12/input-300x200.jpg

"http://zerocool.is-a-geek.net/wp-content/uploads/2011/12/hist_before.png"

to this image

http://zerocool.is-a-geek.net/wp-content/uploads/2011/12/output-300x200.jpg

"http://zerocool.is-a-geek.net/wp-content/uploads/2011/12/hist_after.png"

but I can't seem to figure it out.

This is my enhanced image code which should implement the same type of adjustment..

public void EnhancedImage (File fileName) {

           double sumc = 0;
            for(int r = 0; r < array.length; r++){
                for(int c = 0; c < array[r].length; c++){ 
                    sumc = r+c;
                    if (sumc <= array[r][c]) {
                     sumc = array[r][c];
                    }
                        newc = Math.round(maxshade * ((double)sumc / pixtot));
                        array[r][c] = (int) (newc);
                        if (array[r][c] > 255) {
                            array[r][c] = 255;
                    }
                }

                }

The algorithm that i would like to use is below where maxShade is the maximum shade of the image (usually 255) sumc is the total number of pixels in the image with a value less than or equal to c and pixtot is the total number of pixels in the picture:

newc := round(maxShade * ((double)sumc / pixtot))

but im not sure if i did it right...currently my image just turns really dark.

Any help would be appreciated!! Thanks.

Also my pixtot routine:

 pixtot = 0;
           for(int y = 0; y < imageArray.length; y++)
                for(int x = 0; x < imageArray[0].length; x++)
                    pixtot = x+y;

Upvotes: 1

Views: 1540

Answers (1)

Keith Randall
Keith Randall

Reputation: 23265

Your problem is here:

pixtot = x+y;

First, you want +=, not =. Second, this is adding up the indexes of the pixels, not the value of the pixels. You want something like

pixtot += imageArray[y][x];

You make the same conceptual error with sumc.

Edit:

There's lots of other problems with your code. If you want to stretch the dynamic range, you want to compute min and max, the minimum and maximum of all the pixel values, then compute each pixel value as value = maxshade * (value - min) / (max - min). That gives you a result pixel value of 0 if value==min and maxshade if value==max.

This doesn't really give you histogram equalization, however. For that you need to compute a histogram of the input pixel values and compute quantiles in that histogram to figure out the output values, it isn't easy.

Upvotes: 1

Related Questions