Re: Histogram equalization



Second try: I'm not sure why Google didn't post this the first time
yesterday, so here it is again:
=============================
This has been discussed before:
http://groups.google.com/group/comp.soft-sys.matlab/browse_frm/thread/f4ee8aae3372b32f/19b153470634fea6?lnk=gst&q=equalization&rnum=2&hl=en#19b153470634fea6

There is nothing in MATLAB that does accurate histogram matching. All
it does is redistribute the gray levels to very approximately match
the reference image. That's because their histogram equalization
routine just uses the simple, canonical by-the-book mindless way of
doing it, which is to basically take the pdf (histogram) of one image,
calculate the cdf, and then use that to do a gray level intensity
transform of the image to be changed. This WILL NOT FLATTEN the
histogram of the first image, and if that is not flat, then you can't
transform it accurately to the histogram of the second image. Here's
a demo to prove it:
close all;
image1 = imread('tire.tif');
figure; imshow(image1, []);
[counts1, GLs1] = imhist(image1);
figure; plot(GLs1, counts1);
image2 = imread('westconcordorthophoto.png');
figure; imshow(image2, []);
[counts2, GLs2] = imhist(image2);
figure; plot(GLs2, counts2);
transformedImage1 = histeq(image1, counts2);
[counts3, GLs3] = imhist(transformedImage1);
figure; plot(GLs3, counts3);

Notice that the histogram of image3 doesn't look anything like the
histogram of image2.


The way to do this properly is to add noise to each pixel as you
process it one pixel at a time. You still use the cdf to transform
the image but if you added noise, you won't have quantization into
certain bins with other bins completely empty. Just look at the hist
of any image using "dumb" histogram equalization and you'll see a non-
flat spiky histogram with the bins spread out from 0 to 255 but with
lots of empty bins and the number of bins it had before unchanged.
For example, let's say you had an image with a histogram between 101
and 116 gray levels. After equalization, that will get spread out so
that you will still have only 16 bins but they're spread out. So
you'll have the same number of pixels that used to be in bin 101 but
now they're at 0, and the same number of pixels that used to be in bin
102 but now they're in bin 16, etc. The bins from 1 to 15 are all
empty. BUT if you don't do a gray level transformation but process
each pixel instead (after adding +/- 0.5 gray level of noise) you'll
get a very flat histogram and ALL the bins will be filled up. Imagine
this: If I have a gray level of 101, that got quantized. It really
might have been anywhere from 100.5 to 101.5 but because we can't
quantize that precisely, we just put it into the 100 bin. So you can
add plus or minus a half gray level to the image and you'd still have
the same image. But the beauty and magic of it is that if you now run
that noisy pixel value through your cdf, you get a completely filled
and flat histogram. However you don't use the quantized cdf because
it's just a simple lookup table with only 256 possible input values.
So you need to turn your cdf into some kind of analytical formula.
Cubic splines work nicely. Now you just run your pixel with +/- 0.5
Gl noise added to it through your cubic spline representation of your
cdf, and you now have a valid output value for that pixel. It will go
into the nearest bin, and not into some far off bin, like the one
closest to a multiple of 16 like in my example.

Now, to match two images, you need to transform both images to get a
flat histogram. If you know the transform, you also know the inverse
transform - just flip the axes of the cdf. So this is what you do to
match image 1 to image 2:
1. Do a precise histogram equalization of image 1 to produce an image
3 with a truly flat and evenly distributed histogram (as per above
method). Note the transform (spine fit of count vs. gray level).
2. Do a precise histogram equalization of image 2 to produce an image
4 with a truly flat and evenly distributed histogram (as per above
method). Calculate the INVERSE transform from the regular
forwardtransform. That is, the transform that would take you from the
flat histogram to the original (like I said, just flip the axes and do
the spline fit again). You don't need image 4 anymore, you can throw
it away.
3. Take image 3 (the flat one) and apply the inverse transform of
step 2 to take it from the flat histogram image to an image that will
have the (nearly exact) histogram of image 2.

This is probably more detail than you wanted, but if you want it done
precisely, this is how you do it. Maybe it will help some people who
want to do it accurately. Several papers on the subject. Search
sci.image.processing in Google Groups for "equalization" or
"histogram matching." I think I gave some paper references over
there.
Regards,
ImageAnalyst

==================================================

On Apr 24, 7:14 am, Invisible <k0mb1n4...@xxxxxxxxx> wrote:
hi!

i need to write this function in matlab:

HistEqual(X, Y)
Histogram equalization. After running this function, the histogram of X should be
as similar to the histogram of Y as possible, without changing the histogram of Y.
Return the new image after histogram equalization.

Any one can help ?


.



Relevant Pages

  • Re: The Promise of Forth
    ... Returns the computed median value of a list of numbers, ... number of bins to use for the histogram (more bins brings the computed value ... Association for Computing Machinery Inc., New York, ...
    (comp.lang.forth)
  • Problem overlaying data on .gif
    ... I'm plotting some satellite data (latitude, ... every time step) in a 2d histogram (using hist2d from mathworks) so I ... % spaced bins in both dimensions ...
    (comp.soft-sys.matlab)
  • Re: Histogram and Normality test
    ... but the data always fails the normality ... When plotting the histogram with 1000 bins, ... various spikes in the figure. ...
    (comp.soft-sys.matlab)
  • Re: Number of bins in a histogram
    ... density from a histogram. ... bins are better, and for the second, large bins ... A histogram concentrates on the CENTER of a distribution. ... graphed pdfs which one is NORMAL, ...
    (sci.stat.math)
  • Re: Number of bins in a histogram
    ... density from a histogram. ... bins are better, and for the second, large bins ... to use for the assessment of continuous distributions. ... Herman Rubin, Department of Statistics, Purdue University ...
    (sci.stat.math)