问题
I'm trying to implement the histogram equalization using this code:
clc
A=input('please enter image adress','s');
Iimg=imread(A);
Iimg1=double(Iimg);
histi=imhist(Iimg);
mmax=max(Iimg1(:));
h=histi/numel(Iimg1)
cdf=cumsum(h)
cdf=cdf*double(mmax);
c=uint8(cdf);
subplot(1,3,1)
bar(c)
subplot(1,3,2)
imhist(Iimg)
subplot(1,3,3)
imhist(histeq(Iimg))
Is my code wrong? I don't get expected results.
回答1:
I get this from your code:
What is wrong? unless your image is rgb, the code works (I used cameraman).
回答2:
i find correct code for this and write here for others
clc
A=input('please enter image adress: ','s');
GIm=imread(A);
[x, y ,m]=size(GIm);
if m==3
GIm=rgb2gray(GIm);
end
inf=whos('GIm');
Isize=0;
if inf.class=='uint8'
Isize=256;
else if inf.class=='uint68'
Isize=65565;
end
end
HIm=uint8(zeros(size(GIm,1),size(GIm,2)));
freq=zeros(256,1);
probf=zeros(256,1);
probc=zeros(256,1);
cum=zeros(256,1);
output=zeros(256,1);
freq=imhist(GIm);%histogram
sum=0;
no_bins=255;
probc=cumsum(freq)/numel(GIm);
output=round(probc*no_bins);
HIm(:)=output(GIm(:)+1);
%show
figure
subplot(2,2,1)
imshow(GIm);
title('original image');
subplot(2,2,2)
imshow(HIm);
title('Image equalization');
subplot(2,2,3)
imhist(GIm);
title('origina');
subplot(2,2,4)
imhist(HIm);
title('histogram equalization');
figure
subplot(2,2,1)
imshow(histeq(GIm));
title('matlab equalization');
subplot(2,2,2)
imshow(HIm);
title('my code equalization');
subplot(2,2,3)
imhist(histeq(GIm));
title('hist matlab eq');
subplot(2,2,4)
imhist(HIm);
title('hist my code eq');
figure
subplot(2,2,1)
imshow(GIm);
title('origina');
subplot(2,2,2)
imshow(HIm);
title('Image equalization');
x=(1:Isize);
subplot(2,2,3)
plot(x,output);
title('transform function');
subplot(2,2,4)
plot(x,freq);
title('transform function');
来源:https://stackoverflow.com/questions/26560037/histogram-equalization-function