实验三 图像的算术运算与空间域增强
使用与图像算数运算的MATLAB程序
f1=imread('picture.jpg');%读入图像1 f2=imread('picture1.jpg');%读入图像2
I=imsubtract(f1,f2);%两幅图像相减,结果得出两幅图像的不同之处 figure,imshow(I)
利用MATLAB软件实现空域滤波的程序: I=imread('electric.tif');
J = imnoise(I,'gauss',0.02); %添加高斯噪声
%J = imnoise(I,'salt & pepper',0.02); %添加椒盐噪声 ave1=fspecial('average',3); %产生3×3的均值模版 ave2=fspecial('average',5); %产生5×5的均值模版 K = filter2(ave1,J)/255; %均值滤波3×3 L = filter2(ave2,J)/255; %均值滤波5×5 M = medfilt2(J,[3 3]); %中值滤波3×3模板 N = medfilt2(J,[5 5]); %中值滤波5×5模板 imshow(I); figure,imshow(J); figure,imshow(K); figure,imshow(L); figure,imshow(M); figure,imshow(N);
6
1. 调入并显示摄像头生成的二幅的图像。
obj = videoinput('winvideo',1,'YUY2_640x480');% 创建视频输入对象 preview(obj);%′打开视频预览窗口
frame1 = getsnapshot(obj);% 获取视频的一帧(注该帧并不是RGB格式) figure,imshow(frame1);%显示获取的那帧图像 frame=ycbcr2rgb(frame1);%图像格式转换 figure,imshow(frame);% 显示图像 imwrite(frame,1.jpg','jpg');
frame1 = getsnapshot(obj); figure,imshow(frame1); frame=ycbcr2rgb(frame1); figure,imshow(frame);
imwrite(frame,2.jpg','jpg');
2. 考虑使用适当的图像算术运算方法,找出二幅图像中不同的地方及其所处的位置;
f1=imread(1.jpg'); f2=imread(2.jpg'); I=imsubtract(f1,f2); figure,imshow(I)
3. 显示/记录处理结果并做出报告。 a) 调入并显示原始图像Sample2-1.jpg 。
A = imread('huangs.jpg'); C = rgb2gray(A); figure,imshow(C);
b) 利用imnoise 命令在图像Sample2-1.jpg 上加入高斯(gaussian) 噪声
B = imnoise(C,'gauss',0.1); figure,imshow(B); title('加噪后');
7
c)利用预定义函数fspecial 命令产生平均(average)滤波器
??1?1?1???19?1??????1?1?1??
d = (1/17).*[1 1 1; 1 9 1; 1 1 1]; Y = filter2(d,B)/255; figure,imshow(Y,[]); title('均值去噪后');
d)采用3x3的模板,分别用平均滤波器以及中值滤波器,对加入噪声的图像进行处理并观察不同噪声水平下,上述滤波器处理的结果;
M = medfilt2(B,[3,3]); figure,imshow(M);
title('中值去噪后'); %中值滤波
3×3模板
e)选择不同大小的模板,对加入某一固定噪声水平噪声的图像进行处理,观察上述滤波器处理的结果。
ave2=fspecial('average',5); %产生5×5的均值模版 L = filter2(ave2,B)/255; %均值滤波5×5; %Y = filter2(d,B)/255; figure,imshow(L,[]); M = medfilt2(B,[5,5]); figure,imshow(M);
title('中值去噪后'); %中值滤波5*5模板
f)利用imnoise 命令在图像Sample2-1.jpg 上加入椒盐噪声(salt & pepper)
g)重复c)~ e)的步骤 h)输出全部结果并进行讨论。
A = imread('huangs.jpg'); C = rgb2gray(A); subplot(3 ,2 ,1); imshow(C);
8
B = imnoise(C,'salt & pepper',0.02); subplot(3 ,2 ,2); imshow(B); title('加噪后');
d = fspecial('average',3); M = medfilt2(B,[3,3]); subplot(3 ,2 ,3); imshow(M);
title('中值去噪后[3,3]'); N = medfilt2(B,[5,5]); subplot(3 ,2 ,4); imshow(N);
title('中值去噪后[5,5]'); Y = filter2(d,B)/255; subplot(3 ,2 ,5); imshow(Y,[]);
title('均值去噪后[3,3]');
ave2=fspecial('average',5); %L = filter2(ave2,B)/255; %subplot(3 ,2 ,6); imshow(L,[]);
title('均值去噪后[5,5]');
产生5×5的均值模版均值滤波5×5; 9
实验四 图像的傅立叶变换与频域滤波
利用MATLAB软件实现数字图像傅立叶变换的程序:
I=imread(‘原图像名.gif’); %读入原图像文件 imshow(I); %显示原图像
fftI=fft2(I); %二维离散傅立叶变换 sfftI=fftshift(fftI); %直流分量移到频谱中心 RR=real(sfftI); %取傅立叶变换的实部 II=imag(sfftI); %取傅立叶变换的虚部 A=sqrt(RR.^2+II.^2);%计算频谱幅值
A=(A-min(min(A)))/(max(max(A))-min(min(A)))*225; %归一化 figure; %设定窗口 imshow(A); %显示原图像的频谱 利用MATLAB实现频域滤波的程序
f=imread('room.tif');
F=fft2(f); %对图像进行傅立叶变换
S=fftshift(log(1+abs(F)));%对变换后图像进行队数变化,并对其坐标平移,使其中心化
S=gscale(S); %将频谱图像标度在0-256的范围内 imshow(S) %显示频谱图像 h=special('sobel'); %产生空间‘sobel’模版 freqz2(h) %查看相应频域滤波器的图像
10