1、例 4.1 代码及结果如下:I=imread(pout.tif);imshow(I);I=double(I);M,N=size(I);for i=1:Mfor j=1:Nif I(i,j)=s1h2=1+0.5;elseh1=0;h2=0.5;endg1(i,j)=h1*g(i,j);g2(i,j)=h2*g(i,j);endendg1=ifftshift(g1); g1=uint8(real(ifft2(g1); subplot(221);imshow(g1);title(理想高通滤波结果)g2=ifftshift(g2);g2=uint8(real(ifft2(g2);subplot(22
2、2);imshow(g2); title(理想高通加强滤波结果)n=2;d0=20;for i=1:M for j=1:Nd=sqrt(i-n1)2+(j-n2)2);if d=0h1=0;h2=0.5;elseh1=1/(1+(d0/d)(2*n);h2=1/(1+(d0/d)(2*n)+0.5;endgg1(i,j)=h1*g(i,j);gg2(i,j)=h2*g(i,j);endendgg1=ifftshift(gg1);gg1=uint8(real(ifft2(gg1);subplot(223);imshow(gg1); title(巴特沃斯高通滤波结果)gg2=ifftshift(g
3、g2);gg2=uint8(real(ifft2(gg2);subplot(224);imshow(gg2); title(巴特沃斯高通加强滤波结果)例 4.16 代码及结果如下:J=imread(eight.tif); subplot(121);imshow(J);J=double(J);f=fft2(J); g=fftshift(f);M,N=size(f);d0=10;r1=0.5;rh=2;c=4;n1=floor(M/2);n2=floor(N/2);for i=i:Mfor j=1:Nd=sqrt(i-n1)2+(j-n2)2);h=(rh-r1)*(1-exp(-c*(d.2/d
4、0.2)+r1;g(i,j)=h*g(i,j);endendg=ifftshift(g);g=uint8(real(ifft2(g);subplot(122);imshow(g);例 5.1 代码及结果如下:C=imread(e.bmp);subplot(1,2,1);imshow(C);LEN=30;THETA=45;PSF=fspecial(motion,LEN,THETA);MF=imfilter(C,PSF,circular,conv);subplot(1,2,2),imshow(MF);imwrite(MF,canoe-MF.tif);例 5.2 代码及结果如下:MF,map=imr
5、ead(f.bmp);figure(1);imshow(MF);LEN=30;THETA=45;INITPSF=fspecial(motion,LEN,THETA);J,P=deconvblind(MF,INITPSF,30);figure(2);imshow(J);figure(3);imshow(P,notruesize);例 5.3 代码及结果如下:F=checkerboard(8);figure(1);imshow(F,);PSF=fspecial(motion,7,45);MF=imfilter(F,PSF,circular);noise=imnoise(zeros(size(F),
6、gaussian,0,0.001);MFN=MF+noise;figure(2);imshow(MFN,);NSR=sum(noise(:).2)/sum(MFN(:).2);figure(3);imshow(deconvwnr(MFN,PSF),);figure(4);imshow(deconvwnr(MFN,PSF,NSR),);例 5.7 代码及结果如下:f=imread(cameraman.tif);figure(1);imshow(f);title(-);g=imnoise(f,salt figure(2);imshow(g);title();g1=double(g)/255;j1=
7、medfilt2(g1,symmetric);figure(3);imshow(j1);title();j2=ordfilt2(g1,median(1:3*3),ones(3,3),symmetric);figure(4);imshow(j2);title();j3=ordfilt2(g1,1,ones(3,3);figure(5);imshow(j3);title();j4=ordfilt2(g1,9,ones(3,3);figure(6);imshow(j4);title();例 5.8 代码及结果如下:f=checkerboard(24); figure(1):imshow(f);s=0
8、.7;theta=pi/6;T=s*cos(theta) s*sin(theta) 0 -s*sin(theta) s*cos(theta) 0 0 0 1;tform=maketform(affine,T);g1=imtransform(f,tform,nearest); figure(2):imshow(g1);g2=imtransform(f,tform); figure(3):imshow(g2);g3=imtransform(f,tform,FillValue,0.5); figure(4):imshow(g3);例 6.1 代码及结果如下:iR=zeros(128,128);iR(
9、1:64,1:64)=1;iG=zeros(128,128);iG(65:128,1:64)=1;iB=zeros(128,128);iB(1:64,65:128)=1;I=cat(3,iR,iG,iB);imshow(I);例 6.2 代码及结果如下:rgb_I=imread(g.bmp);cmy_I=imcomplement(rgb_I);imshow(rgb_I);figureimshow(cmy_I);clc例 6.3 代码及结果如下:rgb=imread(h.bmp);subplot(221);imshow(rgb);rgb=im2double(rgb);r=rgb(:,:,1);g
10、=rgb(:,:,2);b=rgb(:,:,3); I=(r+g+b)/3;tmp1=min(min(r,g),b);tmp2=r+g+b;tmp2(tmp2=0)=eps;S=1-3.*tmp1./tmp2;tmp1=0.5*(r-g)+(r-b);tmp2=sqrt(r-g).2+(r-b).*(g-b);theta=acos(tmp1./(tmp2+eps);H=theta;H(bg)=2*pi-H(bg);H=H/(2*pi);H(S=0)=0;hsi=cat(3,H,S,I);subplot(222);imshow(H);subplot(223);imshow(S);例 6.4 代码
11、及结果如下:rgb=imread(i.bmp);imshow(rgb);R=rgb(:,:,1);G=rgb(:,:,2);B=rgb(:,:,3);subplot(232);figure,imshow(R);figure,imshow(G);figure,imshow(B);m=fspecial(average);R_filtered=imfilter(R,m);G_filtered=imfilter(G,m);B_filtered=imfilter(B,m);rgb_filtered=cat(3,R_filtered,G_filtered,B_filtered);figure,imshow
12、(rgb_filtered);例 6.5 代码及结果如下:I=imread(i.bmp );imshow(I);lapMatrix=1 1 1;1 -8 1;1 1 1;I_tmp=imfilter(I,lapMatrix,replicate);I_sharped=imsubtract(I,I_tmp);imshow(I_sharped);例 6.6 代码及结果如下:I=imread(i.bmp);subplot(221);imshow(I);title(RGB 原图像);m=fspecial(motion,20,45);I2=imfilter(I,m,circular);noise=imno
13、ise(zeros(size(I),gaussian,0,0.005);subplot(222);imshow(noise);title(noise);I3=double(I2)+noise;I3=uint8(I3);subplot(223);imshow(I3);title(运动模糊并加入噪声图像 );I3_recovered=deconvwnr(I3,m);subplot(224);imshow(I3_recovered);title(维纳滤波复原后图像);clc例 6.7 代码及结果如下:I=imread(j.bmp);subplot(121);imshow(I);s0pt=fspeci
14、al(sobel);Rx=imfilter(double(I(:,:,1),s0pt,replicate);Ry=imfilter(double(I(:,:,1),s0pt,replicate);Gx=imfilter(double(I(:,:,2),s0pt,replicate);Gy=imfilter(double(I(:,:,2),s0pt,replicate);Bx=imfilter(double(I(:,:,3),s0pt,replicate);By=imfilter(double(I(:,:,3),s0pt,replicate);gxx=Rx.2+Gx.2+Bx.2;gyy=Ry.
15、2+Gy.2+By.2;gxy=Rx.*Ry+Gx.*Gy+Bx.*By;theta=0.5*(atan(2*gxy./(gxx-gyy+eps);G1=0.5*(gxx+gyy)+(gxx-gyy).*cos(2*theta)+2*gxy.*sin(2*theta);theta=theta+pi/2;G2=0.5*(gxx+gyy)+(gxx-gyy).*cos(2*theta)+2*gxy.*sin(2*theta);G1=G1.0.5G2=G2.0.5;gradiant=mat2gray(max(G1,G2);subplot(122);imshow(gradiant);例 6.8 代码及
16、结果如下:I=imread(cameraman.tif);imshow(I);G2C=grayslice(I,8);figureimshow(G2C,hot(8);例 6.9 代码及结果如下:I=imread(lena.bmp);imshow(I);I=double(I);M,N=size(I);L=256;for i=1:Mfor j=1:Nif I(i,j)L/4R(i,j)=0;G(i,j)=4*I(i,j);B(i,j)=L;else if I(i,j)=L/2R(i,j)=0;G(i,j)=L;B(i,j)=-4*I(i,j)+2*L;else if I(i,j)=3*L/4;R(i
17、,j)=4*I(i,j)-2*L;G(i,j)=L;B(i,j)=0;elseR(i,j)=L;G(i,j)=-4*I(i,j)+4*L;B(i,j)=0;endendend end end for i=1:Mfor j=1:NG2C(i,j,1)=R(i,j);G2C(i,j,2)=G(i,j);G2C(i,j,3)=B(i,j);endendG2C=G2C/256;figureimshow(G2C);clc例 7.10 代码及结果如下:X=imread(pout.tif,tif);X=double(X);Y=LPCencode(X);XX=LPCdecode(Y);figure(1); i
18、mshow(mat2gray(Y);e=double(X)-double(XX);m,n=size(e);erms=sqrt(sum(e(:).2)/(m*n)figure(2);h,x=hist(X(:);subplot(121);bar(x,h,k);h,x=hist(Y(:); subplot(122);bar(x,h,k);例 7.11 代码及结果如下:X=14 15 14 15 13 15 15 14 20 26 27 28 27 27 29 37 47 62 75 77 78 79 80 81 81 82 82t=entropy(X)a=1;delt=6.5;y=DMencode(
19、X,a,delt);XX=DMdecode(y);ax=0:1:26X2,Y2=STAIRS(ax,XX)plot(X2,Y2,k,ax,X,k);例 7.14 代码及结果如下:cr=0.5;I1=imread(lena.bmp);I1=double(I1)/255;figure(1);imshow(I1);fftcoe=blkproc(I1,8 8,fft2(x);coevar=im2col(fftcoe,8 8,distinct);coe=coevar;y,ind=sort(coevar);m,n=size(coevar);snum=64-64*cr;for i=1:ncoe(ind(1:
20、snum),i)=0;endB2=col2im(coe,8 8,512 512,distinct);I2=blkproc(B2,8 8,ifft2(x);figure(2);imshow(I2);e=double(I1)-double(I2);m,n=size(e);erms=sqrt(sum(e(:).2)/(m*n)例 7.15 代码及结果如下:cr=0.5;initialimage=imread(lena.bmp);initialimage=double(initialimage)/255;figure(1);imshow(initialimage);T=dctmtx(8);dctCoe
21、=blkproc(initialimage,8 8,P1*x*P2,T,T);CoeVar=im2col(dctCoe,8 8,distinct);Coe=CoeVar;Y,Ind=sort(CoeVar);m,n=size(CoeVar);Snum=64-64*cr;for i=1:nCoe(Ind(1:Snum),i)=0;endB2=col2im(Coe,8 8,512 512,distinct);I2=blkproc(B2,8 8,P1*x*P2,T,T);figure(2);imshow(I2);e=double(initialimage)-double(I2);m,n=size(e
22、);erms=sqrt(sum(e(:).2)/(m*n)例 7.16 代码及结果如下:cr=0.5;initialimage=imread(lena.bmp);initialimage=double(initialimage)/255;figure(1);imshow(initialimage);T=dctmtx(8);dctCoe=blkproc(initialimage,8 8,P1*x*P2,T,T);CoeVar=im2col(dctCoe,8 8,distinct);Coe=CoeVar;Y,Ind=sort(CoeVar);m,n=size(CoeVar);Snum=64-64*
23、cr;for i=1:nCoe(Ind(1:Snum),i)=0;endB2=col2im(Coe,8 8,512 512,distinct);I2=blkproc(B2,8 8,P1*x*P2,T,T);figure(2);imshow(I2);e=double(initialimage)-double(I2);m,n=size(e);erms=sqrt(sum(e(:).2)/(m*n)例 9.1 代码及结果如下:f=imread(pout.tif);subplot(2,2,1);imshow(f);title(原始图像);g,t=edge(f,roberts,both);subplot(
24、2,2,2);imshow(g);title(Roberts算子分割结果 );g,t=edge(f,sobel,both);subplot(2,2,3);imshow(g);title(Sobel算子分割结果);g,t=edge(f,prewitt,both);subplot(2,2,4);imshow(g);title(Prewitt算子分割结果 );例 9.2 代码及结果如下:f=imread(pout.tif);subplot(1,2,1);imshow(f);title(原始图像);g,t=edge(f,log);subplot(1,2,2);imshow(g);title(LOG算子
25、分割结果 );例 9.3 代码及结果如下:f=imread(lena.bmp);subplot(1,2,1);imshow(f);title( 原始图像);g,t=edge(f,canny);subplot(1,2,2);imshow(g);title(Canny 算子分割结果);例 9.6 代码及结果如下:f=imread(k.bmp);subplot(2,2,1);imshow(f);title(原始图像 );T=graythresh(f);f=im2bw(f,T);subplot(2,2,2);imshow(f);title(二值化图像);subplot(2,2,3);f=bwmorph(f,skel,Inf);f=bwmorph(f,spur,8);imshow(f);title(细化图像 );rodetect,tetadetect,Accumulator=houghtrans(f,0.25,1,20);subplot(2,2,4);m,n=size(f);for ln=1:length(rodetect);if tetadetect(ln)=0x=0:n-1;y=-cot(tetadetect(ln)*pi/180)*x+rodetect(ln)/sin(tetadetect(ln)*pi/180)elsex=rodetect(ln)*ones(1,n);