直方图均衡化结果

Histogram Equalization Results

我正在尝试自己编写直方图均衡代码,但结果与 matlab 中的内置函数不同 histeq。有什么建议为什么会这样吗?我附上了代码和结果。

%% 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,[]);

histeq 默认使用 64 个 bin 来均衡直方图。您正在使用 histeq 的默认实现。尝试做:

builtInEqualizationI = histeq(grayI, 256);

... 指定 256 个 bin 用于均衡,因为您的手动代码使用那么多 bin 进行均衡。第二个参数覆盖默认值并通知 histeq 使用那么多 bin 来执行均衡。