数字图像处理上机实验.docx
《数字图像处理上机实验.docx》由会员分享,可在线阅读,更多相关《数字图像处理上机实验.docx(45页珍藏版)》请在冰点文库上搜索。
数字图像处理上机实验
人民武装学院
学生姓名:
周云
学号:
PB102027115
专业:
电子信息科学与技术
年级:
2010级
扌旨导老师:
陈其松老师
时间:
2011年12月24日
第二章:
纸看起
一张纸,当放在桌子上时看上去似乎比较白,当用纸来遮蔽眼睛直视明亮的天空时,
来总是黑的
>>colormap(gray);
>>dark=zeros(256,256);
>>dark(64:
192,64:
192)=0.5;
>>subplot(1,3,1);imshow(dark)
>>middle(1:
256,1:
256)=0.7;
>>middle(64:
192,64:
192)=0.5;
>>subplot(1,3,2);imshow(bright)
>>subplot(1,3,2);imshow(middle)
>>bright=ones(256,256);
>>bright(64:
192,64:
192)=0.5;
>>subplot(1,3,3);imshow(bright)
1.二值图像
>>clear>>x=zeros(10,10);
>>x(2:
2:
10,2:
2:
10)=1;
>>imshow(x)
>>xx=
Columns1through8
000
010
000
010
000
010
000
000
101000
101
000
101
000
0
0
0
0
0
0
0
0
1
0
1
0
1
0
010
1
0
1
0
1
000
0
0
0
0
0
010
1
0
1
0
1
Columns9through10
00
01
00
01
00
01
00
01
00
01
□da«鸣札,#圧/l
二值图像2•亮度图像:
>>X=imread('D:
\lena.bmp');>>imshow(X);
>>Y=X(128:
138,128:
138);
>>figure,imshow(Y);
>>Y
Y=
Columns1through8
194
183
173
160
145
127
113
111
186
172
175
171
153
128
112
110
185
178
173
162
145
128
117
114
188
181
168
150
136
128
123
118
192
176
162
145
133
128
125
120
192
165
156
144
135
129
124
119
187
157
152
143
136
130
124
118
181
157
149
139
133
129
125
118
179
159
148
135
129
129
126
118
122
123
172153142130127129129
168151143135133133131
Columns9through11
115
116
116
116
118
118
115
119
120
115
120
120
115
118
118
115
115
115
114
111
112
112
109
110
110
109
110
115
108
112
115
113
115
(a)原始图像
(b)获取的部分灰度图像3•索引图像:
>>RGB=imread('D:
\flowers.tif);
>>[X,map]=rgb2ind(RGB,128);
>>imshow(X,map)>>X
(1)
ans=
127
>>X
(2)
ans=
127
>>map(127,:
:
:
)ans=
0.76470.61180.6549
>>whos
NameSize
RGB362x500x3
X362x500
ans1x3
BytesClass
543000uint8array181000uint8array
24doublearray
3072doublearray
map128x3
Grandtotalis724387elementsusing727096bytes
索引图像
4.RGB图像:
>>[X,map]=imread('D:
\xiangjiao.bmp');
>>Y=X(90:
95,90:
95);
>>imshow(Y)
>>R=X(90:
95,90:
95,1);
>>G=X(90:
95,90:
95,2);
>>B=X(90:
95,90:
95,3);
>>R,G,B
R=
175
181
185
189
181
155
171
176
185
188
175
146
171
173
182
186
176
161
171
176
184
186
181
178
170
181
187
186
183
182
170
180
180
181
183
180
175181185189181155
171
176
185
188
175
146
171
173
182
186
176
161
171
176
184
186
181
178
170
181
187
186
183
182
170
180
180
181
183
180
B=
175
181
185
189
181
155
171
176
185
188
175
146
171
173
182
186
176
161
171
176
184
186
181
178
170
181
187
186
183
182
170
180
180
181
183
180
(a)
(b)
第三章
例3.1把一幅图加上高斯噪声,再通过100次相加求平均的方法去除噪声
l=imread('D:
eight.tif);
>>J=imnoise(l,'gaussian',0,0.02);
>>subplot(1,2,1),imshow(l);
>>subplot(1,2,2),imshow(J);
>>K=zeros(242,308);
>>fori=1:
100
J=imnoise(l,'gaussian',0,0.02);
J仁im2double(J);
K=K+J1;
end
>>K=K/100;
>>figure;imshow(K);
(a)原图
(b)加噪图
(c)求平均后的图
例3.3图像的乘法运算:
I=imread('D:
\moon.tif);
>>J=immultiply(l,1.2);
>>K=immultiply(l,2);
>>subplot(1,3,1),imshow(l);
>>subplot(1,3,2),imshow(J);
>>subplot(1,3,3),imshow(K);
|>liIk.-fin.taraaIrilitawi
分别为:
原图、乘以1.2、乘以2
例3.4除法运算
moon=imread('moon.tif);
>>I=double(moon);
>>J=l*0.43+90;
>>K=I*0.1+90;
>>L=l*0.01+90;
>>moon2=uint8(J);
>>moon3=uint8(K);
>>moon4=uint8(L);
>>J=imdivide(moon,moon2);
>>K=imdivide(moon,moon3);
>>L=imdivide(moon,moon4);
>>subplot(2,2,1),imshow(moon);
>>subplot(2,2,2),imshow(J,[]);
>>subplot(2,2,3),imshow(K,[]);
>>subplot(2,2,4),imshow(L,[]);
分别为:
原图、J=l*0.43+90、K=l*0.1+90、L=l*0.01+90
例3.5逻辑运算:
>>l=imread('D:
\moon.tif);
>>A=zeros(128);
>>A(40:
67,60:
100)=1;
>>figure
(1);
>>imshow(A);
>>B=zeros(128);
>>B(50:
80,40:
70)=1;
>>figure
(2);
>>imshow(B);
>>C=and(A,B);
>>figure(3);
>>imshow(C);
>>D=or(A,B);
>>figure(4);
>>imshow(D);
>>E=not(A);
>>figure(5);
(a)A图
(b)B图
(c)A、B相与结果图
(d)A、B相或结果图
(c)A取反结果图
例3.6实现把一副图像旋转60度,并分别采用把转出显示区域的图像截去和扩大显示区域
范围以显示图像的全部两种方式
>>l=imread('D:
\9.jpg');
>>J=imrotate(l,60,'bilinea。
;
>>imshow(J);
>>K=imrotate(I,60,'bilinear','crop');
>>imshow(K);
>>I=imread('D:
\9.jpg');
>>J=imrotate(I,60,'bilinea。
;
>>K=imrotate(I,60,'bilinear','crop');
>>subplot(1,3,1),imshow(I);
>>subplot(1,3,2),imshow(J);
>>subplot(1,3,3),imshow(K);
分别为:
原图、旋转图(部分)、旋转图(全部)
例3.7采用三种不同插值法进行图片的放大比较:
>>I=imread('D:
\\lena.jpg');
>>J1=imresize(I,10,'nearest');
>>J2=imresize(I,10,'bilinea。
;
>>J3=imresize(l,10,'bicubic');
>>figure
>>imshow(I);
>>title('原图');
>>figure
>>imshow(J1);
>>title('最近邻法');
>>figure
>>imshow(J2);
>>title('双线性插值法');
>>figure
>>imshow(J3);
>>title('三次内插法');
第四章
例4.2图像的二维离散傅里叶频谱:
>>l=imread('D:
\inst_bkgrnd.bmp');
>>imshow(I)
>>J=fftshift(fft2(I));
>>figure
(2);
>>imshow(log(abs(J)),[8,10])
(a)原始图像
(b)散傅里叶频谱
例4.4二维离散傅里叶变换的旋转性:
>>l=zeros(256,256);
>>1(28:
228,108:
148)=1;
>>imshow(I)
>>J=fft2(I);
>>F=abs(J);
>>J1=fftshift(F);figure
>>imshow(J1,[550])
>>I=zeros(256,256);|
>>1(28:
228,108:
148)=1;
>>J=imrotate(I,315,'bilinear','crop');
>>figure
>>imshow(J)
>>J1=fft2(J);
>>F=abs(J1);
>>J2=fftshift(F);figure
>>imshow(J2,[550])
(a)原始图像
(b)原图像的傅里叶频谱
(c)旋转后的图像
(d)旋转后图像的傅里叶频谱
例4.5比例尺度展宽:
>>clear;
>>l=zeros(256,256);
>>1(8:
248,110:
136)=5;
>>figure⑴
>>imshow(I)
>>a=0.1;
>>J3=fft2(I);
>>F2=abs(J3);
>>J4=fftshift(F2);
>>figure
(2)
>>imshow(J4,[530])
>>fori=1:
256
forj=1:
256
I(i,j)=I(i,j)*a;
end
end
>>J2=fft2(I);
>>F1=abs(J2);
>>J3=fftshift(F1);
>>figure(3)
>>imshow(J3,[530])
aa.
(a)原始图像
(b)比例尺度展宽前的频谱
(c)比例尺度a=0.1,b=1,展宽后的频谱
例4.7(a)图乘以一指数,将图像亮度整体变暗,并求其中心移到零点的频谱图
>>l=imread('D:
\inst_bkgrnd.bmp');
>>figure
(1);
>>imshow(I)
>>P=l&exp(-1);
>>figure
(2);
>>imshow(P)
>>P仁fftshift(fft2(P));
>>figure(3);
>>imshow(log(abs(P1)),[8,10])
(a)原图
(b)变暗后的图
(c)中心移到零点的频谱图
例4.8(a)加入高斯噪声,得出一个有颗粒噪声的图,并求其中心移到零点的频谱图。
>>l=imread('D:
/cs_assault.bmp');
>>l=imnoise(l,'gaussian',0,0.01);
>>figure;
>>imshow(I);
>>P=fftshift(fft2(I));
>>figure;
>>imshow(log(abs(P)),[8,10])
(a)原图
(b)有颗粒噪声
(c)有颗粒噪声中心移到零点的频谱图
例4.9
>>l=imread('E:
\img4.bmp');
>>figure⑴
>>imshow(real(I));
>>I=I(:
:
3);>>fftI=fft2(I);
>>sfftl=fftshift(fftl);
>>RRfdp仁real(sfftl);
>>Ilfdp1=imag(sfftl);
>>a=sqrt(RRfdp1.A2+llfdp1.A2);
>>a=(a-min(min(a)))/(max(max(a))-min(min(a)))*225;
>>figure
(2)
>>imshow(real(a));
>>b=angle(fftl);
>>figure(3)
>>imshow(real(b));
>>theta=30;
>>RR1=a*cos(theta);
>>II1=a*sin(thets);
>>II1=a*sin(theta);
>>fftl1=RR1+i.*II1;
>>C=ifft2(fftI1)*225;
>>figure(4)
>>imshow(real(C));
>>MM=150;
>>RR2=MM*cos(angle(fftl));
>>II2=MM*sin(angle(fftl));
>>fftI2=RR2+i.*II2;
>>D=ifft2(fftI2);
>>figure(5)
>>imshow(real(D));
(a)幅值普
(b)相位谱
(c)幅值谱重构图像
(d)相位谱重构图像
例4.10说明二维余弦正反变换在MATLAB中的实现:
>>RGB=imread(i_lean.tif');
l=rgb2gray(RGB);
Imshow(l);
Figure
(2);
J=dct2(l);imshow(log(abs(J),[]));
Figure(3);
J(abs(J)<10)=0;
K=idct2(J)/255;
Imshow(K);
第五章
例5.1采用如下程序对图像进行反转
l=imread('H:
\img2.bmp');
>>figure,imshow(I);title('originalimage');
>>J=imcomplement(l);
>>figure,imshow(J);title('negativeimage');
LI¥11舍■禹2#*
MW心■血
3T*nhx
Iat:
Mi*•T
丄・At二•即“•M
UAH$•■!
$■/■!
■』:
.•
例5.3对灰度图像进行均衡化,灰度等级为8级:
>>匸imread('D:
\\lena.jpg');
>>匸rgb2gray(l);
>>K=16;
>>H=histeq(l,K);
>>figure,subplot(2,2,1),imshow(l,[]);
>>subplot(2,2,2),imshow(H,[]),holdon
>>subplot(2,2,3),hist(double(l),16),subplot(2,2,4),hist(double(H),16)
Lt!
!
'+■
例5.4对图像进行平滑
Img=rgb2gray(imread(D:
/lmage.bmp7));
Figure;imshow(img);
img_noise=double(imnoise(img,'salt&pepper',0.06));figure,imshow(img_noise,[]);
img_smoothed=imfilter(img_noise,fspecial(average',5));figure;imshow(img_smoothed,[]);
(a)原图像
(b)加椒盐噪声的图像
(c)平滑
第八早例6.1
l=imread('H:
\img2.bmp');
>>figure,imshow(l),figure,hist(double(l),10)
>>J=imnoise(l,'gaussian',0.05);
>>figure,imshow(J),figure,hist(double(J),10)
>>J=imnoise(l,'speckle',0.05);
>>figure,imshow(J),figure,hist(double(J),10)
>>J=imnoise(l,'salt&pepper',0.05);
>>figure,imshow(J),figure,hist(double(J),10)
11-I*1z-AS-H
vk?
i
-■'
(a)原图
(b)高斯噪声图
(c)均匀分布噪声
P*F-子-I--I1-*-H
UD-U#kkJf■■>-■
(d)椒盐噪声
例6.2
img=imread('H:
\img2.bmp');
>>figure;imshow(img);
>>img_noise=double(imnoise(img,'gaussian',0.06));
>>figure,imshow(img_noise,[]);
>>img_mean=imfilter(img_noise,fspecial('average',3));
>>figure;imshow(img_mean,[]);title('de_noisebymeanfilter');
>>img_mean=exp()imfilter(log(img_noise),fspecial('average',3)));
™-*
(a)输入图像
(b)高斯噪声污染图像
(c)用均值滤波结果
(d)几何均值滤波
(e)Q=-1.5的逆谐波滤波
(f)Q=1.5滤波的结果