数字图像处理关于直方图线性拉伸、直方图均衡化、直方图匹配的代码,matlab编写。
读入一张RGB影像,进行直方图直方图线性拉伸、直方图均衡化、直方图匹配(以另一张影像为基准),展示出处理过的图像及直方图。
function F=Histogram_trans(image)
%调用代码--------------------------------
% imageR=imread('eye.jpg');
% image=rgb2gray(imageR); Histogram_trans(image);
%调用代码--------------------------------
[row,column]=size(image);
x=imhist(image)';
y=zeros(1,256);
for i=1:256
for j=1:i
y(i)=y(i)+x(j)/(row*column);
end
end %计算灰度值的累计频率
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%(1) 直方图线性拉伸,图像变换前亮度值为[a1,a2],变换后的亮度值为[b1,b2]
%A=(b2-b1)/(a2-a1),B=(a2b1-a1b2)/(a2-a1),假设变换前[Min,Max]后的亮度值为[0,255]
n=1;
for i=1:row
for j=1:column
h(n)=image(i,j); %???
n=n+1;
end
end
Max=double(max(h));
Min=double(min(h));
A=(255-0)/(Max-Min);B=(Max*0-255*Min)/(Max-Min);
Image1=image;
for i=1:row;
for j=1:column
Image1(i,j)=round(image(i,j)*A+B);
end
end
subplot(1,4,1);imshow(image);
title('原图')
subplot(1,4,2);imshow(Image1);
title('线性拉伸后')
subplot(1,4,3);imhist(image);
title('原图直方图')
subplot(1,4,4);imhist(Image1);
title('线性拉伸后直方图')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%(2) 直方图均衡化,y=(max-min)*Pk(累计)+min;
for a=1:256
Y(a)=round((255-0)*y(a)+0); %均衡化后第a灰度级的新的灰度值
end
Image2=image;
for i=1:row
for j=1:column
Image2(i,j)=Y(image(i,j)+1); %均衡化后的图像,第ij个像元灰度值为%原像元灰度级+1%均衡化后的值
end
end
z=zeros(1,256);
for i=1:row
for j=1:column
k=Image2(i,j);
z(k+1)=z(k+1)+1; %计算均衡化后的图像每个灰度级对应的像素数
end
end
figure,
subplot(1,4,1);imshow(image);
title('原图')
subplot(1,4,2);imshow(Image2);
title('均衡化后')
subplot(1,4,3);imhist(image);
title('原图直方图')
subplot(1,4,4);imhist(Image2);
title('均衡化后直方图')
%%%%%%%%%%%%%%%%%均衡化后的累计概率密度
e=zeros(1,256);
for i=1:256
for j=1:i
e(i)=e(i)+z(j)/(row*column);
end
end %计算均衡化后灰度值的累计频率
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%(3) 直方图匹配
Refer=imread('eye_refer.jpg');
refer=rgb2gray(Refer);
[row2,column2]=size(refer);
b=imhist(refer)';
c=zeros(1,256);
for i=1:256
for j=1:i
c(i)=c(i)+b(j)/(row2*column2);
end
end %计算参考图像灰度值的累计频率
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%均衡化参考图像
for a=1:256
C(a)=round((255-0)*c(a)+0); %均衡化后第a灰度级的新的灰度值
end
refer2=refer;
for i=1:row2
for j=1:column2
refer2(i,j)=C(refer(i,j)+1); %均衡化后的图像,第ij个像元灰度值为%原像元灰度级+1%均衡化后的值
end
end
z2=zeros(1,256);
for i=1:row2
for j=1:column2
k=refer2(i,j);
z2(k+1)=z2(k+1)+1; %计算均衡化后的图像每个灰度级对应的像素数
end
end
f=zeros(1,256);
for i=1:256
for j=1:i
f(i)=f(i)+z2(j)/(row2*column2);
end
end %计算均衡化后参考图像灰度值的累计概率
%%%%%%%%%%%%%%%%%%%%%%%%%%
M=zeros(1,256);
for i=1:256
for j=1:256
t(j)=abs(e(i)-f(j)); %找距离最近的点,e,f分别为均衡化后的累计概率密度
end
[a,index]=min(t); %a代表数组t中的最小值,index为最小值的位置
M(i)=index-1; %像元灰度值为最小值的位置减1
end
Image3=image;
for i=1:row
for j=1:column
Image3(i,j)=M(image(i,j)+1);
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure;
subplot(1,6,1),imshow(image);
title('原图')
subplot(1,6,2);imshow(refer);
title('参考图像');
subplot(1,6,3);imshow(Image3);
title('匹配后');
subplot(1,6,4);imhist(image);
title('原图直方图');
subplot(1,6,5);imhist(refer);
title('匹配直方图');
subplot(1,6,6);imhist(Image3);
title('匹配后直方图');
end