2

I am trying to code histogram equalization by my self, but the results are different from the built in function in matlab histeq. Any suggestions why is this happening?. I attached the code and the results.output images output histograms

%% Histogram Equalization
close all
clear all
%%
I = imread('Iris_virginica.jpg');
grayI = rgb2gray(I);
[m,n] = size(grayI);
L = 256;
[counts,x] = imhist(grayI);
myCDF = cumsum(counts)/(m*n);
equalizedI = (L-1)*myCDF(double(grayI)+1);
equalizedI = uint8(equalizedI);
histMyOut = imhist(equalizedI);
builtInEqualizationI = histeq(grayI);
histBuiltInEqu = imhist(builtInEqualizationI);
%%
figure
subplot(1,3,1), stem(x,counts), title('Histogram');
subplot(1,3,2), stem(x,myCDF), title('Commulative Distribution');
subplot(1,3,3), stem(x,imhist(equalizedI)), title('Equalized');

figure
subplot(1,3,1),imshow(grayI), title('Input image');
subplot(1,3,2), imshow(equalizedI), title('Equalized image (mine)');
subplot(1,3,3), imshow(builtInEqualizationI), title('Equalized image (matlab)');

figure
subplot(1,3,1), stem(x,counts), title('Histogram');
subplot(1,3,2), stem(x,histMyOut), title('Equalized Histogram (mine)');
subplot(1,3,3), stem(x,histBuiltInEqu), title('Equalized Histogram (matlab)');

difference = abs(equalizedI-builtInEqualizationI);
figure, imshow(difference,[]);
Minhas Kamal
  • 20,752
  • 7
  • 62
  • 64
Fadwa
  • 1,717
  • 5
  • 26
  • 43

1 Answers1

2

histeq by default uses 64 bins to equalize your histogram. You are using the default implementation of histeq. Try doing:

builtInEqualizationI = histeq(grayI, 256);

... to specify 256 bins for equalization as your manual code is using that many bins for the equalization. The second parameter overrides the default and informs histeq to use that many bins to perform the equalization.

rayryeng
  • 102,964
  • 22
  • 184
  • 193
  • Yes, the results a re very close now, but the difference between the two images still not zero. – Fadwa Jul 23 '16 at 22:22
  • Probably due to floating point errors. Update your post then. – rayryeng Jul 23 '16 at 22:23
  • I counted the non zero bins in the histogram from the matlab function and they are not 32!. how can i add the number of bins in my implementation?. – Fadwa Jul 23 '16 at 22:26
  • 32 bins spaced in an 8 bit container so you will nonzero bins every multiple of 4. It's still going to give you 256 bins regardless of how many bins you specify. You should probably read up on what histogram equalization is doing before you ask more questions. – rayryeng Jul 23 '16 at 22:27
  • Yeah i got it now, we specify the number of gray levels. And the default is 64 not 32, the count of the non zero bins in the histogram from the matlab function is 64. Thanks for your help. – Fadwa Jul 23 '16 at 22:33
  • That changed. It used to be 32. In any case you're welcome. Are you still getting nonzero values in the difference? – rayryeng Jul 23 '16 at 22:35
  • Ah. Yeah probably floating point errors probably in the CDF. I'm glad it looks almost the same though. If you no longer need help your question, consider accepting my answer. Good luck! – rayryeng Jul 23 '16 at 22:40