例8-2
clear all;
I=imread('circuit.tif');
figure;
subplot(221);imshow(I);
subplot(222);imhist(I);
I1=histeq(I);
subplot(223);imshow(I1);
subplot(224);imhist(I1);
例8-4
I=imread('circuit.tif');
[M,N]=size(I);
for i=1:8:257
counts(i)=i;
end
Q=imread('circuit.tif');
N=histeq(Q,counts);
figure;
subplot(221);imshow(N);
subplot(222);imhist(N);
axis([0 256 0 5000]);
例8-5
clear;
a=imread('coins.png'); %读取图像
anoised=imnoise(a,'gaussian',0.1,0.005); %对图像进行高斯加噪
%制定卷积核
h=ones(3,3)/5;
h(1,1)=0;
h(1,3)=0;
h(3,1)=0;
h(1,3)=0;
%平滑运算
a2=imfilter(anoised,h);
subplot(131);
imshow(a); %显示原始图像
subplot(132);
imshow(anoised); %加有高斯噪声的图像
subplot(133);
imshow(a2); %经过平滑后的图像
例8-6
clear all;
I=imread('eight.tif');
J=imnoise(I,'salt & pepper',0.02);
subplot(231);imshow(I);
subplot(232);imshow(J);
K1=filter2(fspecial('average',3),J); %进行3×3
平滑滤波
K2=filter2(fspecial('average',5),J); %%进行5×5模板平滑滤波
K3=filter2(fspecial('average',7),J); %%进行7×7模板平滑滤波
K4=filter2(fspecial('average',9),J); %%进行9×9模板平滑滤波
subplot(233);imshow(uint8(K1));
subplot(234);imshow(uint8(K2));
subplot(235);imshow(uint8(K3));
subplot(236);imshow(uint8(K4));
例8-7
a=imread('coins.png'); %读取图像
b=imnoise(a,'salt & pepper',0.02); %对图像增加椒盐噪声
k=medfilt2(b); %中值滤波去噪
subplot(131);
imshow(a); xlabel('原始图像');
subplot(132);
imshow(b);xlabel('含有椒盐噪声图像');
subplot(133);
imshow(k);xlabel('中值滤波去噪图像');
例8-9
I=imread('\lena.bmp');
J=im2double(I);
subplot(2,2,1),imshow(J,[])
h1=[0 -1 0, -1 5 -1,0 -1 0];
h2=[-1 -1 -1, -1 9 -1,-1 -1 -1];
h3=[1 -2 0, -2 5 -2,1 -2 1];
A=conv2(J,h1,'same');
subplot(2,2,2), imshow(A,[])
B=conv2(J,h2,'same');
subplot(2,2,3),imshow(B,[])
C=conv2(J,h3,'same');
subplot(2,2,4),imshow(C,[])
例8-10
clear all;
[I,map]=imread('lena.bmp');
figure(1),imshow(I,map);
I=double(I);
[IX,IY]=gradient(I);
GM=sqrt(IX.*IX+IY.*IY);
meth1=GM;
figure(2),imshow(meth 1,map);
meth 2=I;
J=find(GM>10);
meth 2(J)=GM(J);
figure(3),imshow(meth 2,map);
meth 3=I;
J=find(GM>10);
meth 3(J)=255;
figure(4),imshow(meth 3,map);
meth 4=I;
J=find(GM<10);
meth 4(J)=255;
figure(5),imshow(meth 4,map);
meth 5=I;
J=find(GM>10);
meth 5(J)=255;
Q=find(GM<10);
OUTS(Q)=0;
figure(6),imshow(meth 5,map);
例8-11
I=imread('cat.jpg');
h1=[0,-1,0;-1,5,-1;0,-1,0];
h2=[-1,-1,-1;-1,9,-1;-1,-1,-1];
BW1=imfilter(I,h1);
BW2=imfilter(I,h2);
figure;
subplot(131);imshow(I);
subplot(132);imshow(BW1);
subplot(133);imshow(BW2);
例8-12
clear;
I=imread('lena.bmp');
I1=double(I);
h1=fspecial('sobel');
I2=filter2(h1,I);
figure;
subplot(121);imshow(I);
I3=I1-I2;
subplot(122);imshow(I3,[]);
例8-13
clear all;
%实现巴特沃斯低通滤波器
I=imread('saturn.png');
J=imnoise(I,'salt & pepper',0.02); %给原图像加入椒盐噪声
figure;
subplot(121);imshow(J);
J=double(J);
%采用傅里叶变换
f=fft2(J);
%数据矩阵平衡
g=fftshift(f);
[m,n]=size(f);
N=3;
d0=20;
n1=floor(m/2);
n2=floor(n/2);
for i=1:m
for j=1:n
d=sqrt((i-n1)^2+(j-n2)^2);
h=1/(1+(d/d0)^(2*N));
g(i,j)=h*g(i,j);
end
end
g=ifftshift(g);
g=uint8(real(ifft2(g)));
subplot(122);imshow(g);
例8-14
clear all;
% 频域高通滤波法对图像进行增强
[I,map]=imread('lena.bmp');
noisy=imnoise(I,'gaussian',0.01); %原图中加入高斯噪声
[M N]=size(I);
F=fft2(noisy);
fftshift(F);
Dcut=100;
D0=250;
D1=150;
for u=1:M
for v=1:N
D(u,v)=sqrt(u^2+v^2);
%巴特沃斯高通滤波器传递函数
BUTTERH(u,v)=1/(1+(sqrt(2)-1))*(Dcut/D(u,v))^2;
EXPOTH(u,v)=exp(log(1/sqrt(2))*(Dcut/D(u,v))^2); %指数高通滤波器传递函数
if D(u,v)
=1 & yr(i)<=m)
if f(yr(i),xr(i))==1
if xmin==0
xmin=i;
end
xmax=i;
end
end
end
if tetadetect(n1)~=0
x=xmin-1:xmax-1;
y=y(x+1);
else
y=xmin-1:xmax-1;
x=x(y+1);
end
y=m-1-y;
plot(x,y,'linewidth',1);
hold on;
end
axis([0,m-1,0,n-1]);
title('Hough变换检测出的直线');
例9-5
f=imread('woman.bmp');
subplot(121);
imshow(f);
title('原始图像');
f=double(f);
T=(min(f(:))+max(f(:)))/2;
done=false;
i=0;
while ~done
r1=find(f<=T);
r2=find(f>T);
Tnew=(mean(f(r1))+mean(f(r2)))/2;
done=abs(Tnew-T)<1;
T=Tnew;
i=i+1;
end
f(r1)=0;
f(r2)=1;
subplot(122);
imshow(f);
title('迭代阈值二值化图像');
例9-6
f=imread('lena.bmp');
subplot(121);
imshow(f);
title('原始图像');
T=graythresh(f);
g=im2bw(f,T);
subplot(122);
imshow(g);
title('Otsu方法二值化图像');
例9-7
clear;
I=imread('eight.tif');
subplot(221);
imshow(I);
title('原始图像');
subplot(222);
I=double(I);
%计算距离函数
hv=fspecial('prewitt');
hh=hv.';
gv=abs(imfilter(I,hv,'replicate'));
gh=abs(imfilter(I,hh,'replicate'));
g=sqrt(gv.^2+gh.^2);
% watershed算法分割
subplot(222);
L=watershed(g);
wr=L==0;
imshow(wr);
title('分水岭');
I(wr)=255;
subplot(223);
imshow(uint8(I));
title('分割结果');
%取出梯度图中局部极小值点
rm=imregionalmin(g);
subplot(224);
imshow(rm);
title('局部极小值');
例9-8
clear;
I=imread('eight.tif');
subplot(231);
imshow(I);
%计算梯度图
I=double(I);
hv=fspecial('prewitt');
hh=hv.';
gv=abs(imfilter(I,hv,'replicate'));
gh=abs(imfilter(I,hh,'replicate'));
g=sqrt(gv.^2+gh.^2);
%计算距离函数
subplot(232);
df=bwdist(I);
imshow(uint8(df*8));
%计算外部约束
L=watershed(df);
em=L==0;
subplot(233);
imshow(em);
% 计算内部约束
im=imextendedmax(I,20);
subplot(234);
imshow(im);
%重构梯度图
g2=imimposemin(g,im|em);
subplot(235);
imshow(g2);
% watershed算法分割
L2=watershed(g2);
wr2=L2==0;
subplot(236);
I(wr2)=255;
imshow(uint8(I));
例9-9
clear all;
%假设下面的I是一幅灰度图像,取阈值0.5进行四叉树分解
I=[1 1 1 1 2 3 6 6;
1 1 10 1 4 5 6 8;
1 1 1 1 10 15 7 7;
1 1 1 1 10 25 7 7;
10 22 10 22 1 2 3 4;
10 22 22 10 5 6 7 8;
10 22 10 10 9 10 11 12;
22 22 10 10 13 14 15 16];
figure;
image(I,'Cdatamapping','scaled');
axis ij;colormap gray
S=qtdecomp(I,0.5);
full(S)
例9-12
I=imread('image\duck.png');
subplot(221);
imshow(I);
title('原始图像');
b=imread('background.png');
subplot(222);
imshow(b);
title('背景图像');
df=im2double(I);
db=im2double(b);
c=df-db;
d=im2uint8(c);
subplot(223);
imshow(d);
title('差值图像');
T=50;
T=T/255;
i=find(abs(c)>=T);
c(i)=1;
i=find(abs(c)