数字图像处理作业:
1.数字图像的基本操作联系,如MATLAB读入、显示等 2.灰度直方图的使用 3.图像增强的练习 4.图像的几何变换
一.数字图像的基本操作练习 1.matlab读入图像
1.1函数imread
Imread函数可以将指定位置的图像文件读入工作区。对于除索引图像以外的情况,其原型如下。
A=imread(FILENAME,FMT);
对于索引图像,情况有所不同,此时imread的调用形式如下。 [X,MAP]=imread(FILENAME,FMT); 1.2函数imwrite
Inwrite将指定的图像数据写入文件中,通过指定不同的保存文件扩展名可以起到图像格式转换的作用。其调用格式如下。
Imwrite(A,FILENAME,FMT);
2.matlab图像显示
2.1函数imshow
Imshow函数用于显示工作区或图像文件中的图像,在显示的同时可控制部分效果,常用的调用形式如下。
Imshow(I,[low high],param1,value1,param2,value2,...); Imshow(I,MAP); Imshow(filename);
二.灰度直方图的使用
Matlab中的imhist函数可以进行图像的灰度直方图运算,调用语法如下。 imhist(I); imhist(I,n); [counts,s]=imhist(...);
4.一般直方图
下面使用了Matlab中的一张内置示例图片演示灰度直方图的生成与显示,程序如下。
I=imread('pout.tif'); figure;
imshow(I);title('Source'); figure;
imhist(I);title('Histogram');
上述程序的运行结果如图所示。
5.归一化直方图
在imhist函数的返回值中,counts保存了落入每个区间的像素个数,通过计算counts与图像中像素总数的商可以得到归一化直方图。
绘制有32个灰度区间的归一化直方图的Matlab程序如下。
I=imread('pout.tif'); figure;
[M,N]=size(I);
[counts,x]=imhist(I,32); counts=counts/M/N; stem(x,counts);
三.图像增强的练习: 3.1空间域图像增强
3.1.1线性平滑滤波器
例题:对一个图像进行不同大小模板的均值滤波,并比较结果。
I=imread('eight.tif');
J=imnoise(I,'salt & pepper',0.02); subplot(2,2,1)
imshow(J);title('噪声图像');
K1=filter2(fspecial('average',3),J)/255; K2=filter2(fspecial('average',5),J)/255; K3=filter2(fspecial('average',7),J)/255; subplot(2,2,2) imshow(K1);
title('3x3模板均值滤波'); subplot(2,2,3) imshow(K2);
title('5x5模板均值滤波'); subplot(2,2,4) imshow(K3);
title('7x7模板均值滤波');
其显示结果如图所示。
3.1.2非线性平滑滤波器
例题:对一个图像实现不同模板的中值滤波,并比较结果。
I=imread('eight.tif');
J=imnoise(I,'salt & pepper',0.02); subplot(2,2,1)
imshow(J);title('噪声图像'); K1=medfilt2(J,[3 3]); K2=medfilt2(J,[5 5]); K3=medfilt2(J,[7 7]); subplot(2,2,2)
imshow(K1);
title('3x3模板中值滤波'); subplot(2,2,3) imshow(K2);
title('5x5模板中值滤波'); subplot(2,2,4) imshow(K3);
title('7x7模板中值滤波'); 其显示结果如图所示。
3.1.3线性锐化滤波器
例题:对图像pout.tif进行线性高通滤波。 I=imread('pout.tif'); h=fspecial('laplacian'); I2=filter2(h,I); subplot(1,2,1);
imshow(I);title('原始图像'); subplot(1,2,2);
imshow(I2);title('滤波后图像');
其显示结果如图所示。
3.1.4非线性锐化滤波器
例题:sobel算子,prewitt算子,log算子对图像滤波。 I=imread('cameraman.tif'); subplot(2,2,1);
imshow(I);title('原始图像'); h1=fspecial('sobel'); I1=filter2(h1,I); subplot(2,2,2);
imshow(I1);title('sobel算子滤波'); h1=fspecial('prewitt'); I1=filter2(h1,I); subplot(2,2,3);
imshow(I1);title('prewitt算子滤波'); h1=fspecial('log'); I1=filter2(h1,I); subplot(2,2,4);
imshow(I1);title('log算子滤波');
其显示结果如图所示。
3.2频域图像增强 3.2.1低通滤波
例题:对图像eight.tif加入椒盐噪声后,实现Butterworth低通滤波。
clear
I1=imread('eight.tif'); subplot(2,2,1);
imshow(I1);title('原始图像'); I2=imnoise(I1,'salt & pepper'); subplot(2,2,2);
imshow(I2);title('噪声图像'); f=double(I2); g=fft2(f); g=fftshift(g);
[N1,N2]=size(g); n=2; d0=50;
n1=fix(N1/2); n2=fix(N2/2); for i=1:N1 for j=2:N2
d=sqrt((i-n1)^2+(j-n2)^2); h=1/(1+0.414*(d/d0)^(2*n)); result1(i,j)=h*g(i,j); if(g(i,j)>50) result2(i,j)=0; else
result2(i,j)=g(i,j); end end end
result1=ifftshift(result1); result2=ifftshift(result2); X2=ifft2(result1); X3=uint8(real(X2)); subplot(2,2,3);
imshow(X3);title('Btterworth滤波图像'); X4=ifft2(result2); X5=uint8(real(X4)); subplot(2,2,4);
imshow(X5);title('理想低通滤波图像');
其显示结果如图所示。
3.2.2高通滤波
例题:对图像eight.tif实现Butterworth高通滤波。 clear
I1=imread('eight.tif'); subplot(2,2,1);
imshow(I1);title('原始图像'); I2=imnoise(I1,'salt & pepper'); subplot(2,2,2);
imshow(I2);title('噪声图像'); f=double(I2); g=fft2(f); g=fftshift(g); [N1,N2]=size(g); n=2; d0=50;
n1=fix(N1/2);
n2=fix(N2/2); for i=1:N1 for j=2:N2
d=sqrt((i-n1)^2+(j-n2)^2); if d==0 h=0; else
h=1/(1+(d0/d)^(2*n)); end
result1(i,j)=h*g(i,j); if(g(i,j)<50) result2(i,j)=0; else
result2(i,j)=g(i,j); end end end
result1=ifftshift(result1); result2=ifftshift(result2); X2=ifft2(result1); X3=uint8(real(X2)); subplot(2,2,3);
imshow(X3);title('Btterworth滤波图像'); X4=ifft2(result2); X5=uint8(real(X4)); subplot(2,2,4);
imshow(X5);title('理想高通滤波图像');
其显示结果如图所示。
四. 图像的几何变换
4.1图像平移 程序:
function I_out=imMove(I,Tx,Ty)
tform=maketform('affine',[1 0 0;0 1 0;Tx Ty 1]);
I_out=imtransform(I,tform,'XData',[1 size(I,2)],'YData',[1 size(I,1)] );
subplot(1,2,1),imshow(I); title('原图像');
subplot(1,2,2),imshow(I_out); title('平移图像');
其中 I=imread('pout.tif');
其显示结果如图所示。
4.2图像镜像 程序:
A=imread('pout.tif'); [height,width,dim]=size(A);
tform=maketform('affine',[-1 0 0;0 1 0;width 0 1]); B=imtransform(A,tform,'nearest');
tform2=maketform('affine',[1 0 0;0 -1 0;0 height 1]); C=imtransform(A,tform2,'nearest'); subplot(1,3,1),imshow(A); title('原图像');
subplot(1,3,2),imshow(B); title('水平镜像');
subplot(1,3,3),imshow(C); title('竖直镜像');
其显示结果如图所示。
4.3图像转置 程序:
clear
A=imread('pout.tif');
tform=maketform('affine',[0 1 0;1 0 0;0 0 1]); B=imtransform(A,tform,'nearest'); subplot(1,2,1),imshow(A); title('原图像');
subplot(1,2,2),imshow(B); title('图像转置'); 其显示结果如图所示。
4.4图像缩放 程序:
clear
A=imread('pout.tif');
B=imresize(A,1.2,'nearest'); figure,imshow(A); title('原图像'); figure,imshow(B); title('图像缩放'); 其显示结果如图所示。
4.5图像旋转 程序:
clear
A=imread('pout.tif');
B=imrotate(A,30,'nearest','crop'); subplot(1,2,1),imshow(A); title('原图像');
subplot(1,2,2),imshow(B); title('逆时针旋转30度'); 其显示结果如图所示。
因篇幅问题不能全部显示,请点此查看更多更全内容