Thread Subject: Histogram equalization

Subject: Histogram equalization

From: Matman86 Di

Date: 7 Jun, 2009 08:28:01

Message: 1 of 3

Hello again,I wanted to know if there's a way to revert the equalization of an image Y,returning to the original image X.
I know how the equalization works:

p=hist(X(:),0:255)/(M*N) %pmf of the image X
C=cumsum(p); %cumulative distribution function

Y=C(X+1); % equalization

In short for each level of gray contained in X matrix I assign the element in C whose index is just the gray level,so in Y i find C values.
Teorically,in order to revert the process I should find Y values in C and for each value return the index,the original gray level but there's a problem:values in C are not integers so the istruction

find(C==0.6541)

returns an empty matrix,because I can't give every significative digit.

Is there a way to solve this problem?Sorry for bothering you so often lately:)

Subject: Histogram equalization

From: ImageAnalyst

Date: 8 Jun, 2009 01:37:08

Message: 2 of 3

On Jun 7, 4:28 am, "Matman86 Di"
You can invert the equalization only if you keep everything floating
point. If you quantize through use of histogram, then sometimes two
bins get combined into one bin. To split that one bin up into two
bins again is impossible because you don't know the relative
porportion of the two bins that went into the one bin, unless you kept
the original histogram. (But if you kept that then why don't you just
keep the original image also?)

You can get somewhat close except for the bins that got combined will
not get split up. The following code illustrates this perfectly:
% Demo macro to ...
% by ImageAnalyst
clc;
close all;
originalImage = imread('cameraman.tif');
[rows columns numberOfColorBands] = size(originalImage);
subplot(3, 3, 1);
imshow(originalImage, []);
title('Histogram');

[pixelCounts grayLevels] = imhist(originalImage);
subplot(3,3,2);
bar(pixelCounts);
title('Histogram');

cdf = uint8(255 * cumsum(pixelCounts) / (double(rows) * double
(columns)));
subplot(3,3,3);
plot(cdf);
title('CDF');

equalizedimage = intlut(originalImage, cdf); % equalization
subplot(3,3,4);
imshow(equalizedimage, []);
title('Equalized Image');

[pixelCountsEq grayLevelsEq] = imhist(equalizedimage);
subplot(3,3,5);
bar(pixelCountsEq);
title('Equalized Histogram');

cdfEq = uint8(255 * cumsum(pixelCountsEq) / (double(rows) * double
(columns)));
subplot(3,3,6);
plot(cdfEq);
title('Equalized CDF');

% Invert the cdf
inverseCDF2 = -50 *ones(256,1);
inverseindexes = 1:256;
for gl = 0:255
outputGL = cdf(gl+1);
inverseCDF2(outputGL + 1) = gl;
end
missing = inverseCDF2 < 0;
inverseCDF2(missing) = [];
inverseindexes(missing) = [];
% Fill in missing points
inverseCDF = uint8(interp1(inverseindexes, inverseCDF2, 1:256));
inverseCDF(256) = inverseCDF(255);
subplot(3,3,7);
plot(inverseCDF);
title('Inverse CDF');

% Tranform equalized image with the inverse transform.
restoredimage = intlut(equalizedimage, inverseCDF); % equalization
subplot(3,3,8);
imshow(restoredimage, []);
title('Equalized Image');

% See how it looks - it's histogram should be similar to the original
% but not exact.
[pixelCountsR grayLevelsR] = imhist(restoredimage);
subplot(3,3,9);
bar(pixelCountsR);
title('Restored Histogram');

set(gcf, 'Position', get(0, 'ScreenSize'));


Note from the plots that the input range of 25-100 got mapped into
about 50-75 (75 bins got squished into only 25 bins). Now those 25
bins need to get expanded out into 75 bins again but you can't. It
just spaces out the 25 bins (see the gaps in the final restored
histogram). So the final restored histogram looks similar to the
original histogram but is not exact due to gaps in it.

Hope this helps explain things a bit.
Good luck,
ImageAnalyst

Subject: Histogram equalization

From: Matman86 Di

Date: 9 Jun, 2009 10:20:17

Message: 3 of 3

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <e019a063-585a-4206-af29-f78e3b3c70c3@o30g2000vbc.googlegroups.com>...
> On Jun 7, 4:28?am, "Matman86 Di"
> You can invert the equalization only if you keep everything floating
> point. If you quantize through use of histogram, then sometimes two
> bins get combined into one bin. To split that one bin up into two
> bins again is impossible because you don't know the relative
> porportion of the two bins that went into the one bin, unless you kept
> the original histogram. (But if you kept that then why don't you just
> keep the original image also?)
>
> You can get somewhat close except for the bins that got combined will
> not get split up. The following code illustrates this perfectly:
> % Demo macro to ...
> % by ImageAnalyst
> clc;
> close all;
> originalImage = imread('cameraman.tif');
> [rows columns numberOfColorBands] = size(originalImage);
> subplot(3, 3, 1);
> imshow(originalImage, []);
> title('Histogram');
>
> [pixelCounts grayLevels] = imhist(originalImage);
> subplot(3,3,2);
> bar(pixelCounts);
> title('Histogram');
>
> cdf = uint8(255 * cumsum(pixelCounts) / (double(rows) * double
> (columns)));
> subplot(3,3,3);
> plot(cdf);
> title('CDF');
>
> equalizedimage = intlut(originalImage, cdf); % equalization
> subplot(3,3,4);
> imshow(equalizedimage, []);
> title('Equalized Image');
>
> [pixelCountsEq grayLevelsEq] = imhist(equalizedimage);
> subplot(3,3,5);
> bar(pixelCountsEq);
> title('Equalized Histogram');
>
> cdfEq = uint8(255 * cumsum(pixelCountsEq) / (double(rows) * double
> (columns)));
> subplot(3,3,6);
> plot(cdfEq);
> title('Equalized CDF');
>
> % Invert the cdf
> inverseCDF2 = -50 *ones(256,1);
> inverseindexes = 1:256;
> for gl = 0:255
> outputGL = cdf(gl+1);
> inverseCDF2(outputGL + 1) = gl;
> end
> missing = inverseCDF2 < 0;
> inverseCDF2(missing) = [];
> inverseindexes(missing) = [];
> % Fill in missing points
> inverseCDF = uint8(interp1(inverseindexes, inverseCDF2, 1:256));
> inverseCDF(256) = inverseCDF(255);
> subplot(3,3,7);
> plot(inverseCDF);
> title('Inverse CDF');
>
> % Tranform equalized image with the inverse transform.
> restoredimage = intlut(equalizedimage, inverseCDF); % equalization
> subplot(3,3,8);
> imshow(restoredimage, []);
> title('Equalized Image');
>
> % See how it looks - it's histogram should be similar to the original
> % but not exact.
> [pixelCountsR grayLevelsR] = imhist(restoredimage);
> subplot(3,3,9);
> bar(pixelCountsR);
> title('Restored Histogram');
>
> set(gcf, 'Position', get(0, 'ScreenSize'));
>
>
> Note from the plots that the input range of 25-100 got mapped into
> about 50-75 (75 bins got squished into only 25 bins). Now those 25
> bins need to get expanded out into 75 bins again but you can't. It
> just spaces out the 25 bins (see the gaps in the final restored
> histogram). So the final restored histogram looks similar to the
> original histogram but is not exact due to gaps in it.
>
> Hope this helps explain things a bit.
> Good luck,
> ImageAnalyst

You were very nice,thanks much for help and patiente:D

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Contact us at files@mathworks.com