直方图均衡:将直方图转换为32个二进制位

时间:2014-09-14 02:10:19

标签: matlab image-processing computer-vision

我正在进行图像直方图均衡化。该代码适用于具有256 bin直方图的图像,但是,当我尝试将直方图转换为32 bin时,图像质量会有所不同。任何想法如何解决?

我的代码:

I=imread('D:\came.jpg');
subplot(2,2,1); 
imshow(I);
title('Original Image');
subplot(2,2,2); 
input=rgb2gray(I);
imhist(input);
title('Histogram: Before Histogram equalization');
for j=1:3
R=I(:,:,j);
channel=j;
GIm=R;
numofpixels=size(GIm,1)*size(GIm,2);
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 counts the occurrence of each pixel value.
%The probability of each occurrence is calculated by probf.
for i=1:size(GIm,1)
    for j=1:size(GIm,2)
        value=GIm(i,j);
        freq(value+1)=freq(value+1)+1;
        probf(value+1)=freq(value+1)/numofpixels;
    end

end

sum=0;
no_bins=32;% it works fine for 256 values but not for 32 values
%The cumulative distribution probability is calculated. 
for i=1:size(probf)
   sum=sum+freq(i);
   cum(i)=sum;
   probc(i)=cum(i)/numofpixels;
   output(i)=round(probc(i)*no_bins);
end

for i=1:size(GIm,1)
    for j=1:size(GIm,2)
            HIm(i,j)=output(GIm(i,j)+1);
    end
end

if(channel==1) HIm1=uint8(HIm); 
elseif(channel==2) HIm2=uint8(HIm);
elseif(channel==3) HIm3=uint8(HIm);    
end


end

subplot(2,2,3); 
finalimage = cat(3,HIm1,HIm2, HIm3);
imshow(finalimage);
title('After Histogram equalization');
subplot(2,2,4); 
input=rgb2gray(finalimage);
imhist(input);
title('Histogram: After Histogram equalization');


end

我的输出: enter image description here

0 个答案:

没有答案