MATLAB Answers


HISTEQ normalization range not clear

Asked by Jurgen
on 18 Nov 2012

When I tried to equalize an image using histeq I got an unexpected result. The original has values in the range 0-199 and the output has a range of 153-255, I was hoping for more contrast. So I wrote a few lines of code myself, to see if I could reproduce the results. If I apply 'faulty' normalization to the cumulative histogram of the original I got similar, but not identical, results. Leading me to believe histeq.m does not normalize properly.

Or am I doing something wrong? Heres the code to reproduce it:

im = imread('pout','tif'); %or cameraman if you like
% get original histogram:
graycount = imhist(im);
% Calculate CDF & Normalize:
graymap   = cumsum(graycount)/sum(graycount);
graymap   = uint8(graymap*255/max(graymap));
% Apply CDF as LUT & Get new histogram:
imEQ      = graymap(im+1);
EQcount   = imhist(imEQ); 
% Same using histeq():
imhisteq  = histeq(im,256);
histeqcount = imhist(histeq(im,256));

Then test for equality with isequal and some figures:

  disp('Histeq does something else!!')
  subplot(1,2,1);stem(histeqcount);title('hist. Histeq im');
  subplot(1,2,2);stem(cumsum(histeqcount));title('cum. hist. Histeq im');
  subplot(1,2,1);stem(EQcount);title('manual hist. EQ im');
  subplot(1,2,2);stem(cumsum(EQcount));title('manual cum. hist. EQ im');  
  hist(EQcount-histeqcount,100);title('Difference histeq and manual histograms')
figure;        subplot(2,2,1);imshow(histeq(im,256));subplot(2,2,3);imshow(vec2mat(histeqcount,20));       subplot(2,2,2);imshow(graymap(im+1));subplot(2,2,4);imshow(vec2mat(EQcount,20)) 

Very similar images, I think histeq normalized it differently but why? Also a better result is gotten if normalized CDF to 0-1:

graymap = cumsum(graycount)/sum(graycount); %raw CDF
% Using built-in functions:
graymap = im2uint8(mat2gray(graymap));
% Or explicitly:
graymap = uint8((graymap -min(graymap))*255/(max(graymap)-min(graymap)));


Log in to comment.

0 Answers

Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!