实验四图像增强
信息工程学院实验报告
课程名称:数字图像处理Array
实验项目名称:实验四图像增强实验时间:2016.11.08
班级::学号:
一、实验目的
1.了解图像增强的目的及意义,加深对图像增强的感性认识,巩固所学理论知识。
2. 掌握图像空域增强算法的基本原理。
3. 掌握图像空域增强的实际应用及MATLAB实现。
4. 掌握频域滤波的概念及方法。
5. 熟练掌握频域空间的各类滤波器。
6.掌握怎样利用傅立叶变换进行频域滤波。
7. 掌握图像频域增强增强的实际应用及MATLAB实现。
二、实验步骤及结果分析
1. 基于幂次变换的图像增强
程序代码:
clear all;
close all;
I{1}=double(imread('fig534b.tif'));
I{1}=I{1}/255;
figure,subplot(2,4,1);imshow(I{1},[]);hold on
I{2}=double(imread('room.tif'));
I{2}=I{2}/255;
subplot(2,4,5);imshow(I{2},[]);hold on
for m=1:2
Index=0;
for lemta=[0.5 5]
Index=Index+1;
F{m}{Index}=I{m}.^lemta;
subplot(2,4,(m-1)*4+Index+1),imshow(F{m}{Index},[])
end
end
执行结果:
图1 幂次变换增强结果
实验结果分析:
由实验结果可知,当r<1时,黑色区域被扩展,变的清晰;当r>1时,黑色区域被压缩,变的几乎不可见。
2.直方图规定化处理
程序代码:
clear all
clc
close all
%0.读图像
I=double(imread('lena.tiff'));
subplot(2,4,1);
imshow(I,[]);
title('原图')
N=32;
Hist_image=hist(I(:),N);
Hist_image=Hist_image/sum(Hist_image);
Hist_image_cumulation=cumsum(Hist_image);%累
计直方图
subplot(245);
stem(0:N-1,Hist_image);
title('原直方图');
%1.设计目标直方图
Index=0:N-1;
%正态分布直方图
Hist{1}=exp(-(Index-N/2).^2/N);
Hist{1}=Hist{1}/sum(Hist{1});
Hist_cumulation{1}=cumsum(Hist{1});
subplot(242);
title('规定化直方图1');
%倒三角形状直方图
Hist{2}=abs(2*N-1-2*Index);
Hist{2}=Hist{2}/sum(Hist{2});
Hist_cumulation{2}=cumsum(Hist{2});
subplot(246);
stem(0:N-1,Hist{2});
title('规定化直方图2');
%2. 规定化处理
Project{1}=zeros(N);
Project{2}=zeros(N);
Hist_result{1}=zeros(N);
Hist_result{2}=zeros(N);
for m=1:2
Image=I;
%SML处理(SML,Single Mapping Law单映射规则
for k=1:N
Temp=abs(Hist_image_cumulation(k)-Hist_cumulati
on{m});
[Temp1,Project{m}(k)]=min(Temp);
end
%2.2 变换后直方图
for k=1:N
if isempty(Temp)
Hist_result{m}(k)=0;
else Hist_result{m}(k)=sum(Hist_image(Temp)); end
end
subplot(2,4,(m-1)*4+3);
stem(0:N-1,Hist_result{m});
title(['变换后的直方图',num2str(m)]);
%2.3结果图Step=256/N;
for K=1:N
Index=find(I>=Step*(k-1)&I Image(Index)=Project{m}(k); end subplot(2,4,(m-1)*4+4),imshow(Image,[]); title(['变换后的结果图',num2str(m)]); end 执行结果: 原图 规定化直方图 2 变换后的直方图1 变换后的结果图1 变换后的直方图2 变换后的结果图2图2 直方图规定化 实验结果分析: 由实验结果可知,采用直方图规定化技术后,原图的直方图逼近规定化的直方图,从而有相应的变换后的结果图1和变换后的结果图2。 3.灰度图像常用平常、锐化滤波 程序代码: clear all; close all; %0.原图 I=double(imread('lena.tiff')); subplot(2,4,1);imshow(I,[]);title('原图');H=fspecial('average',5); F{1}=double(filter2(H,I)); subplot(2,4,2);imshow(F{1},[]);title('均值低通滤波'); %2.gaussian 低通滤波 F{2}=double(filter2(H,I)); subplot(2,4,3);imshow(F{2},[]);title('高斯低通滤波'); %3.增强图像=原图-均值低通滤波 F{3}=2*I-F{1}; subplot(2,4,4);imshow(uint8(F{3}),[]);title('原图-均值低通滤波'); %4.增强图像=原图-高斯低通滤波 F{4}=2*I-F{2}; subplot(2,4,5);imshow(uint8(F{4}),[]);title('原图-高斯低通滤波'); %5.'prewitt'边缘算子增强 H=fspecial('prewitt'); F{5}=uint8(I+filter2(H,I)); subplot(2,4,6);imshow(F{5},[]);title('prewitt 边缘算子增强'); %6.'soble'边缘算子增强 H=fspecial('sobel'); F{6}=uint8(I+filter2(H,I)); subplot(2,4,7);imshow(F{6},[]);title('sobel 边缘算子增强'); 执行结果: 原 图 均值低通滤 波 高斯低通滤 波 原图-均值低通滤 波 原图-高斯低通滤 波prewitt 边缘算子增 强sobel 边缘算子增强 图3 灰色图像平滑、锐化 实验结果分析: 由实验结果可知,均值和高斯滤波都使原图模糊,而采用原图减去低通滤波图像方法、prewitt 算子、sobel 算子都可以增强图像边缘。 4. 频率域滤波:对于给定图像+噪声,使用不同的频域滤波器对图像进行滤波处理。 (1)用Butterworth 低通滤波器实现图像信号的滤波运算。 程序代码: clear all ; close all ; %(a)读入并显示图像electric.tif ; I=imread('electric.tif'); subplot(2,3,1),imshow(I);title('原图像'); %(b)利用imnoise 命令在图electric.tif 上加入高 J=imnoise(I,'gaussian',0,0.01); subplot(2,3,2),imshow(J);title('加入高斯噪声的图像'); %(c)用Butterworth 低通滤波器实现图像信号的滤波运算,变换不同的截止频率d I1=fftshift(fft2(J)); N=2;d1=30;d2=50;d3=70;d4=90; n1=floor(m/2);n2=floor(n/2); for i=1:m for j=1:n d=sqrt((i-n1)^2+(j-n2)^2); H1=1/(1+(d/d1)^(2*N)); H2=1/(1+(d/d2)^(2*N)); H3=1/(1+(d/d3)^(2*N)); H4=1/(1+(d/d4)^(2*N)); I3(i,j)=H1*I1(i,j); I5(i,j)=H2*I1(i,j); I7(i,j)=H3*I1(i,j); I9(i,j)=H4*I1(i,j); end end I3=ifftshift(I3); I4=real(ifft2(I3)); I5=ifftshift(I5); I6=real(ifft2(I5)); I7=ifftshift(I7); I8=real(ifft2(I7)); I9=ifftshift(I9); I10=real(ifft2(I9)); subplot(2,3,3),imshow(I4,[]),title('Butterworth 低通滤波器d1=30'); subplot(2,3,4),imshow(I6,[]),title('Butterworth 低通滤波器d2=50'); subplot(2,3,5),imshow(I8,[]),title('Butterworth 低通滤波器d3=70'); subplot(2,3,6),imshow(I10,[]),title('Butterworth 低通滤波器d4=90'); 执行结果: 原图 像 加入高斯噪声的图 像 Butterworth 低通滤波器 d1=30 Butterworth 低通滤波器 d2=50Butterworth 低通滤波器 d3=70Butterworth 低通滤波器d4=90 图4 Butterworth 低通滤波器滤波结果 实验结果分析: 由实验结果可知,采用Butterworth 低通滤波器对加噪声的图像进行滤波运算,滤波器的截止频率d 越小,滤波后的图像越模糊。 (2)用理想低通滤波器实现图像信号的滤波运算 程序代码: clear all; close all; I=imread('electric.tif'); subplot(2,3,1),imshow(I);title('原图像'); F=imnoise(I,'gaussian',0,0.01); subplot(2,3,2),imshow(F);title('加入高斯噪声的图像'); %傅里叶变换并把频谱中心移到中点 F1=fft2(F); I1=fftshift(F1); %构建理想低通滤波器 [m,n]=size(I1); d1=30;d2=50;d3=70;d4=90; n1=floor(n/2);n2=floor(m/2); for u=1:m for v=1:n if sqrt((u-n1)^2+(v-n2)^2)<=d1 H1(u,v)=1; else H1(u,v)=0; end end end for u=1:m for v=1:n if sqrt((u-n1)^2+(v-n2)^2)<=d2 H2(u,v)=1; else H2(u,v)=0; end end end for u=1:m for v=1:n if sqrt((u-n1)^2+(v-n2)^2)<=d3 H3(u,v)=1;else H3(u,v)=0; end end end for u=1:m for v=1:n if sqrt((u-n1)^2+(v-n2)^2)<=d4 H4(u,v)=1; else H4(u,v)=0; end end end J1=I1.*H1; J2=I1.*H2; J3=I1.*H3; J4=I1.*H4; K1=ifft2(ifftshift(J1)); K2=ifft2(ifftshift(J2)); K3=ifft2(ifftshift(J3)); K4=ifft2(ifftshift(J4)); C1=real(K1); C2=real(K2); C3=real(K3); C4=real(K4); subplot(2,3,3);imshow(C1,[]);title('滤波结果图像,d1=30'); subplot(2,3,4);imshow(C2,[]);title('滤波结果图像,d2=50'); subplot(2,3,5);imshow(C3,[]);title('滤波结果图像,d3=70'); subplot(2,3,6);imshow(C4,[]);title('滤波结果图像,d4=90'); 执行结果: 原图 像加入高斯噪声的图 像滤波结果图像 ,d1=30 滤波结果图像 ,d2=50滤波结果图像 ,d3=70滤波结果图像,d4=90 图5 理想低通滤波器滤波结果 实验结果分析: 由实验结果可知,采用理想低通滤波器对加噪声的图像进行滤波运算,滤波器的滤波半径d 越小,滤波后的图像越模糊,且出现明显的振铃现象,相比于巴特沃斯低通滤波器,理想低通滤波器的滤波效果会比较差。 (3)采用巴特沃斯高通滤波器对room.tif 图像进行锐化滤波,并显示滤波结果图像。 程序代码: clear all ; close all ; %(d)读入并显示原始图像room.tif; I=imread('room.tif'); subplot(1,3,1),imshow(I);title('原图像room'); %(e)采用巴特沃斯高通滤波器对room.tif 图像进行锐化滤波,变换不同的滤波半径 I1=fftshift(fft2(I)); [m,n]=size(I1); N=2;d1=10;d2=50; n1=floor(m/2);n2=floor(n/2); for i=1:m for j=1:n d=sqrt((i-n1)^2+(j-n2)^2); H1=1/(1+(d1/d)^(2*N)); H2=1/(1+(d2/d)^(2*N)); I3(i,j)=H1*I1(i,j); I5(i,j)=H2*I1(i,j); end end I3=ifftshift(I3); I4=real(ifft2(I3)); I5=ifftshift(I5); I6=real(ifft2(I5)); subplot(1,3,2),imshow(I4,[]),title('Butterworth 高通滤波器d1=30'); subplot(1,3,3),imshow(I6,[]),title('Butterworth 高通滤波器d2=50'); 执行结果: 原图像room Butterworth高通滤波器d1=30Butterworth高通滤波器d2=50 图6 巴特沃斯高通滤波器锐化滤波结果 实验结果分析: 由实验结果可知,采用Butterworth高通滤波器对图像进行滤波运算,滤波后图像的边缘和细节变的更加突出,是一种对图像的锐化处理。 三、实验中遇到问题及解决方法 实验中遇到的问题有:初期对图像在频域空间的滤波不太了解,不懂得如何利用傅立叶变换进行频域滤波;解决的方法:通过参考课本中例题及参考程序,逐步分析,加深理解。 四、实验心得体会 通过此次试验,初步掌握图像空域增强算法的基本原理并能在实际应用及MATLAB中实现;通过熟悉各类滤波器对图像处理的应用;加深对图像增强的感性认识,巩固所学理论知识。 五、源程序清单 %% %1.基于幂次变换的图像增强 clear all; close all; I{1}=double(imread('fig534b.tif')); I{1}=I{1}/255; figure,subplot(2,4,1);imshow(I{1},[]);hold on I{2}=double(imread('room.tif')); I{2}=I{2}/255; subplot(2,4,5);imshow(I{2},[]);hold on for m=1:2 Index=0; for lemta=[0.5 5] Index=Index+1; F{m}{Index}=I{m}.^lemta; subplot(2,4,(m-1)*4+Index+1),imshow(F{m}{Index},[]) end end %% %2. 直方图规定化处理 clear all clc close all %0.读图像 I=double(imread('lena.tiff')); subplot(2,4,1); imshow(I,[]); title('原图') N=32; Hist_image=hist(I(:),N); Hist_image=Hist_image/sum(Hist_image); Hist_image_cumulation=cumsum(Hist_image);%累计直方图subplot(245); stem(0:N-1,Hist_image); title('原直方图'); %1.设计目标直方图 Index=0:N-1; %正态分布直方图 Hist{1}=exp(-(Index-N/2).^2/N); Hist{1}=Hist{1}/sum(Hist{1}); Hist_cumulation{1}=cumsum(Hist{1}); subplot(242); stem([0:N-1],Hist{1}); title('规定化直方图1'); %倒三角形状直方图 Hist{2}=abs(2*N-1-2*Index); Hist{2}=Hist{2}/sum(Hist{2}); Hist_cumulation{2}=cumsum(Hist{2}); subplot(246); stem(0:N-1,Hist{2}); title('规定化直方图2'); %2. 规定化处理 Project{1}=zeros(N); Project{2}=zeros(N); Hist_result{1}=zeros(N); Hist_result{2}=zeros(N); for m=1:2 Image=I; %SML处理(SML,Single Mapping Law单映射规则 for k=1:N Temp=abs(Hist_image_cumulation(k)-Hist_cumulation{m}); [Temp1,Project{m}(k)]=min(Temp); end for k=1:N Temp=find(Project{m}==k); if isempty(Temp) Hist_result{m}(k)=0; else Hist_result{m}(k)=sum(Hist_image(Temp)); end end subplot(2,4,(m-1)*4+3); stem(0:N-1,Hist_result{m}); title(['变换后的直方图',num2str(m)]); %2.3结果图 Step=256/N; for K=1:N Index=find(I>=Step*(k-1)&I Image(Index)=Project{m}(k); end subplot(2,4,(m-1)*4+4),imshow(Image,[]); title(['变换后的结果图',num2str(m)]); end %% %3. 灰度图像常用平常、锐化滤波 clear all; close all; %0.原图 I=double(imread('lena.tiff')); subplot(2,4,1);imshow(I,[]);title('原图'); %1.均值低通滤波 H=fspecial('average',5); F{1}=double(filter2(H,I)); subplot(2,4,2);imshow(F{1},[]);title('均值低通滤波'); %2.gaussian 低通滤波 H=fspecial('gaussian',7,3); F{2}=double(filter2(H,I)); subplot(2,4,3);imshow(F{2},[]);title('高斯低通滤波'); %3.增强图像=原图-均值低通滤波 F{3}=2*I-F{1}; subplot(2,4,4);imshow(uint8(F{3}),[]);title('原图-均值低通滤波'); %4.增强图像=原图-高斯低通滤波 F{4}=2*I-F{2}; subplot(2,4,5);imshow(uint8(F{4}),[]);title('原图-高斯低通滤波'); %5.'prewitt'边缘算子增强 F{5}=uint8(I+filter2(H,I)); subplot(2,4,6);imshow(F{5},[]);title('prewitt边缘算子增强'); %6.'soble'边缘算子增强 H=fspecial('sobel'); F{6}=uint8(I+filter2(H,I)); subplot(2,4,7);imshow(F{6},[]);title('sobel边缘算子增强'); %% %4.(1)Butterworth低通滤波器实现图像信号的滤波运算 clear all; close all; %(a) 读入并显示图像electric.tif; I=imread('electric.tif'); subplot(2,3,1),imshow(I);title('原图像'); %(b) 利用imnoise 命令在图像electric.tif 上加入高斯(gaussian) 噪声; J=imnoise(I,'gaussian',0,0.01); subplot(2,3,2),imshow(J);title('加入高斯噪声的图像'); %(c) 用Butterworth低通滤波器实现图像信号的滤波运算,变换不同的截止频率d:d1=30;d2=50;d3=70;d4=90 I1=fftshift(fft2(J)); [m,n]=size(I1); N=2;d1=30;d2=50;d3=70;d4=90; n1=floor(m/2);n2=floor(n/2); for i=1:m for j=1:n d=sqrt((i-n1)^2+(j-n2)^2); H1=1/(1+(d/d1)^(2*N)); H2=1/(1+(d/d2)^(2*N)); H3=1/(1+(d/d3)^(2*N)); H4=1/(1+(d/d4)^(2*N)); I3(i,j)=H1*I1(i,j); I5(i,j)=H2*I1(i,j); I7(i,j)=H3*I1(i,j); I9(i,j)=H4*I1(i,j); end end I3=ifftshift(I3); I4=real(ifft2(I3)); I5=ifftshift(I5); I6=real(ifft2(I5)); I7=ifftshift(I7); I8=real(ifft2(I7)); I9=ifftshift(I9); I10=real(ifft2(I9)); subplot(2,3,3),imshow(I4,[]),title('Butterworth低通滤波器d1=30'); subplot(2,3,5),imshow(I8,[]),title('Butterworth低通滤波器d3=70'); subplot(2,3,6),imshow(I10,[]),title('Butterworth低通滤波器d4=90'); %% % 4.(2)用理想低通滤波器实现图像信号的滤波运算 clear all; close all; I=imread('electric.tif'); subplot(2,3,1),imshow(I);title('原图像'); F=imnoise(I,'gaussian',0,0.01); subplot(2,3,2),imshow(F);title('加入高斯噪声的图像'); %傅里叶变换并把频谱中心移到中点 F1=fft2(F); I1=fftshift(F1); %构建理想低通滤波器 [m,n]=size(I1); d1=30;d2=50;d3=70;d4=90; n1=floor(n/2);n2=floor(m/2); for u=1:m for v=1:n if sqrt((u-n1)^2+(v-n2)^2)<=d1 H1(u,v)=1; else H1(u,v)=0; end end end for u=1:m for v=1:n if sqrt((u-n1)^2+(v-n2)^2)<=d2 H2(u,v)=1; else H2(u,v)=0; end end end for u=1:m for v=1:n if sqrt((u-n1)^2+(v-n2)^2)<=d3 H3(u,v)=1; else H3(u,v)=0; end end for u=1:m for v=1:n if sqrt((u-n1)^2+(v-n2)^2)<=d4 H4(u,v)=1; else H4(u,v)=0; end end end J1=I1.*H1; J2=I1.*H2; J3=I1.*H3; J4=I1.*H4; K1=ifft2(ifftshift(J1)); K2=ifft2(ifftshift(J2)); K3=ifft2(ifftshift(J3)); K4=ifft2(ifftshift(J4)); C1=real(K1); C2=real(K2); C3=real(K3); C4=real(K4); subplot(2,3,3);imshow(C1,[]);title('滤波结果图像,d1=30'); subplot(2,3,4);imshow(C2,[]);title('滤波结果图像,d2=50'); subplot(2,3,5);imshow(C3,[]);title('滤波结果图像,d3=70'); subplot(2,3,6);imshow(C4,[]);title('滤波结果图像,d4=90'); %% %4(*)用理想低通滤波器和Butterworth低通滤波器对加噪图像进行滤波对比clear all; close all; I=imread('electric.tif'); subplot(2,2,1),imshow(I);title('原图像'); F=imnoise(I,'gaussian',0,0.01); subplot(2,2,2),imshow(F);title('加入高斯噪声的图像'); %傅里叶变换并把频谱中心移到中点 F1=fft2(F); I1=fftshift(F1); %构建理想低通滤波器(传递函数) [m,n]=size(I1); N=2;d1=90;d2=90; n1=floor(n/2);n2=floor(m/2); for u=1:m for v=1:n if sqrt((u-n1)^2+(v-n2)^2)<=d1 H1(u,v)=1; H1(u,v)=0; end end end J1=I1.*H1; K1=ifft2(ifftshift(J1)); C1=real(K1); subplot(2,2,3);imshow(C1,[]);title('理想低通滤波结果图像'); for i=1:m for j=1:n d=sqrt((i-n1)^2+(j-n2)^2); H2=1/(1+(d/d2)^(2*N)); I3(i,j)=H2*I1(i,j); end end I3=ifftshift(I3); I4=real(ifft2(I3)); subplot(2,2,4),imshow(I4,[]),title('Butterworth低通滤波器'); %% %4.(3)采用巴特沃斯高通滤波器对room.tif图像进行锐化滤波,并显示滤波结果图像clear all; close all; %(d)读入并显示原始图像room.tif; I=imread('room.tif'); subplot(1,3,1),imshow(I);title('原图像room'); %(e)采用巴特沃斯高通滤波器对room.tif图像进行锐化滤波,变换不同的滤波半径 I1=fftshift(fft2(I)); [m,n]=size(I1); N=2;d1=10;d2=50; n1=floor(m/2);n2=floor(n/2); for i=1:m for j=1:n d=sqrt((i-n1)^2+(j-n2)^2); H1=1/(1+(d1/d)^(2*N)); H2=1/(1+(d2/d)^(2*N)); I3(i,j)=H1*I1(i,j); I5(i,j)=H2*I1(i,j); end end I3=ifftshift(I3); I4=real(ifft2(I3)); I5=ifftshift(I5); I6=real(ifft2(I5)); subplot(1,3,2),imshow(I4,[]),title('Butterworth高通滤波器d1=30');