BBHE

将直方图分成两段,各自均衡化
% clc;
% clear all
% close all;
% I0=imread('d:\faceData\AR\m-027-01.pgm');                 %读入JPG彩色图像文件
function pic=BBHE(I0,meanv);
if size(I0,3)==3
    I=rgb2gray(I0);
else 
    I=I0;
end
% figure,imshow(I);
% title('灰度图')
% meanv=floor(mean(I(:)));
[m,n]=size(I);  %测量图像尺寸参数
pl=zeros(1,meanv);   
pu=zeros(1,256);%预创建存放灰度出现概率的向量
nl=0;
nu=0;
for i=1:m
    for j=1:n
        if I(i,j)<meanv
            pl(I(i,j) + 1) = pl(I(i,j) + 1)  + 1;
            nl=nl+1;
        else
            pu(I(i,j)+1)=pu(I(i,j)+1)+1;
            nu=nu+1;
        end
    end
end
pl=pl./nl;
pu=pu./nu;
cl=zeros(1,meanv+1);
cu=zeros(1,256);
cl(1)=pl(1);
cu(meanv+1)=pu(meanv+1);
for i=2:meanv
     cl(i)=pl(i) + cl(i-1); %统计图像中<每个灰度级像素的累积个数,s(i):0,1,```,i-1
end
for i=meanv+2:256
    cu(i)=pu(i)+cu(i-1);
end
for i=1:meanv
    cl(i)=cl(i)*meanv;   
end
for i=meanv+1:256
    cu(i)=meanv+1+cu(i)*(256-meanv);
end
%图像均衡化
I_equal = I;
for i=1:m
    for j=1:n
        if I(i,j)<meanv
           I_equal(i,j) = cl( I(i,j) + 1);
        else
            I_equal(i,j)=cu(I(i,j)+1);
        end
    end
end
pic=I_equal;
% figure,imshow(I_equal);                         %显示均衡化后的图像 
% title('均衡化后图像')
% 
% q=zeros(1,256);                            %预创建存放灰度出现概率的向量
% for i=1:m
%     for j=1:n
%      q(I_equal(i,j) + 1) = q(I_equal(i,j) + 1)  + 1;
%     end
% end
% q=q/(m*n);
% x=0:255;
% figure(3);
% plot(x,q,'g');

你可能感兴趣的:(BBHE)