1、第七章 图像的空间变换,空间变换,MATLAB空间变换方法,MATLAB的图像匹配,MATLAB的图像投影,一.空间变换 空间变换主要是用来保持图像中的连续性和物体的连通性,一般都用数学函数形式来描述输入输出图像相应像素间的空间关系。空间变换的一般表达式为: g(x,y)=f(x,y)=fa(x,y),b(x,y) g(x,y) 输出图像 (x,y) 空间变换后的坐标 a(x,y),b(x,y) 空间变换函数,7.1 空间变换,二.简单变换,简单变换用来实现图像的平移、旋转、剪切等简单操作。如:,a(x,y)=x+x0,b(x,y)=y+y0,2)旋转变换,3)平移放大,水平变换,a(x,y)
2、=xcos-ysin,垂直变换,b(x,y)=y,a(x,y)=x,b(x,y)=(xsin+y)/cos,1)平移变换,a(x,y)=xcos-ysin,b(x,y)=xsin+ycos,1)多项式卷绕 利用控制点序列为a(x,y)和b(x,y)找到一个近似的多项式描述。该多项式的参数能够使多项式的取值与控制点及其位移量吻合。,三.利用控制点进行变换:,2)图像分割卷绕 将控制点形成一个矩形输入栅格,通过映射将栅格变换为输出图像中连同的水平输出栅格,控制点为每一个输出栅格对应的顶点,输入栅格内的各点影射为相应输出栅格内的点。 通常采用的栅格插值方法是双线性空间变换方法,其一般表达式为: G(
3、x,y)=F(ax+by+cxy+d,e+fy+gxy+h),%7_3_aI=imread(flowers.tif);subplot(331),imshow(I);%7_3_bI=imread(flowers.tif);scale=1.2;angle=20*pi/180;tx=0;ty=0;sc=scale*cos(angle);ss=scale*sin(angle);T=sc ss; ss sc; tx ty;,t_lc=maketform(affine,T);I_linearconformal=imtransform(I,t_lc,FillValues,.3);%subplot(332),
4、imshow(I_linearconformal);imshow(I_linearconformal);%7_3_cI=imread(flowers.tif);T=1 0.1 ;1 1;0 0 ;t_aff=maketform(affine,T);I_affine=imtransform(I,t_aff,FillValues,.3);%subplot(333),imshow(I_affine)imshow(I_affine);%7_3_dI=imread(flowers.tif);T=1 0 0.008; 1 1 0.01; 0 0 1;,t_proj=maketform(projective
5、,T);I_projective=imtransform(I,t_proj,FillValues,.3);%subplot(334),imshow(I_projective)iptsetpref(ImshowTruesize,manual);imshow(I_projective);%7_3_eI=imread(flowers.tif);xybase=reshape(randn(12,1),6,2);t_poly=cp2tform(xybase,xybase,polynomial,2);T=0 0;1 0;0 1;0.001 0;0.02 0;0.01 0;t_poly.tdata=T;I_p
6、olynomial=imtransform(I,t_poly,FillValues,.3);%subplot(335),imshow(I_polynomial);iptsetpref(ImshowTruesize,manual);,imshow(I_polynomial);%7_3_fI=imread(flowers.tif);imid=round(size(I,2)/2);I_left=I(:,1:imid);stretch=1.5;size_right=size(I,1)round(stretch*imid);I_right=I(:,imid+1:end);I_right_stretche
7、d=imresize(I_right,size_right);I_piecewiselinear=I_left I_right_stretched;%subplot(336),imshow(I_piecewiselinear);iptsetpref(ImshowTruesize,manual);imshow(I_piecewiselinear);%7_3_g,I=imread(flowers.tif);nrows,ncols=size(I);xi,yi=meshgrid(1:ncols,1:nrows);a1=5;a2=3;u=xi+a1*sin(pi*xi/imid);v=yi-a2*sin
8、(pi*yi/imid);tmap_B=cat(3,u,v);resamp=makeresampler(linear,fill);I_sinusoid=tformarray(I, ,resamp,2 1,1 2, ,tmap_B,.3);%subplot(337),imshow(I_sinusoid);iptsetpref(ImshowTruesize,manual);imshow(I_sinusoid);%7_3_h,I=imread(flowers.tif);xt=xi(:)-imid;yt=yi(:)-imid;theta,r=cart2pol(xt,yt);a=.00001;s=r+a
9、*r.3;ut,vt=pol2cart(theta,s);u=reshape(ut,size(xi)+imid;v=reshape(vt,size(yi)+imid;tmap_B=cat(3,u,v);I_barrel=tformarray(I, ,resamp,2 1,1 2, ,tmap_B,.3);%subplot(338),imshow(I_barrel);%iptsetpref(ImshowTruesize,manual);imshow(I_barrel);,%7_3_iI=imread(flowers.tif);xt=xi(:)-imid;yt=yi(:)-imid;theta,r
10、=cart2pol(xt,yt);a=-0.0005;s=r+a*r.3;ut,vt=pol2cart(theta,s);u=reshape(ut,size(xi)+imid;v=reshape(vt,size(yi)+imid;tmap_B=cat(3,u,v);I_pin=tformarray(I, ,resamp,2 1,1 2, ,tmap_B,.3);%subplot(339),imshow(I_pin);iptsetpref(ImshowTruesize,manual);,imshow(I_pin)%7.5.6orthophoto=imread(westconcordorthoph
11、oto.png);subplot(1,2,1),imshow(orthophoto)unregistered=imread(westconcordaerial.png);subplot(1,2,2),imshow(unregistered)%cpselect(unregistered(:,:,1),orthophoto)%input_points=120.7086 93.9772 319.2222 78.9202 127.9838 291.6312 352.0729 281.1445;%,input_points_corr=cpcorr(input_points,base_points,. u
12、nregistered(:,:,1),orthophoto) input_points_corr=120.7086 93.9772 319.2222 78.9202 127.1064 289.8935 352.0729 281.1445;%mytform=cp2tform(input_points,base_points,projective);%registered=imtransform(unregistered,mytform)%7.7clcClear%ch7_8_11lily=imread(lily.tif);flowers=imread(flowers.tif);,subplot(1
13、,2,1),imshow(lily);subplot(1,2,2),imshow(flowers);%rect_lily=93 13 81 69;rect_flowers=190 68 235 210;sub_lily=imcrop(lily,rect_lily);sub_flowers=imcrop(flowers,rect_flowers);sub_lily1,rect_lily=imcrop(lily);sub_flowers1,rect_flowers=imcrop(flowers);subplot(1,2,1),imshow(sub_lily1)subplot(1,2,2),imsh
14、ow(sub_flowers1)% c=normxcorr2(sub_lily(:,:,1),sub_flowers(:,:,1);figure,surf(c),shading flat,%sub_lily,rect_lily=imcrop(lily);sub_flowers,rect_flowers=imcrop(flowers);%max_c,imax=max(abs(c(:);ypeak,xpeak=ind2sub(size(c),imax(1);corr_offset=(xpeak-size(sub_lily,2)(ypeak-size(sub_lily,1);rect_offset=
15、(rect_flowers(1)-rect_lily(1) (rect_flowers(2)-rect_lily(2);offset=corr_offset+rect_offset;xoffset=offset(1);yoffset=offset(2);%,xbegin=xoffset + 1;xend=xoffset + size(lily,2);ybegin=yoffset+1;yend=yoffset+size(lily,1);extracted_lily=flowers(ybegin:yend,xbegin:xend,:);if isequal(lily,extracted_lily)
16、 disp(lily.tif was extracted from flowers.tif)end %recovered_lily=uint8(zeros(size(flowers);recovered_lily(ybegin:yend,xbegin:xend,:)=lily;m,n,p=size(flowers)mask=ones(m,n);i=find(recovered_lily(:,:,1)=0);,mask(i)=.2;figure,imshow(flowers(:,:,1)hold onh=imshow(recovered_lily);set(h,AlphaData,mask)%c
17、h7_8_11lily=imread(lily.tif);flowers=imread(flowers.tif);subplot(1,2,1),imshow(lily);subplot(1,2,2),imshow(flowers);%rect_lily=93 13 81 69;rect_flowers=190 68 235 210;sub_lily=imcrop(lily,rect_lily);,sub_flowers=imcrop(flowers,rect_flowers);sub_lily,rect_lily=imcrop(lily);sub_flowers,rect_flowers=im
18、crop(flowers);subplot(1,2,1),imshow(sub_lily)subplot(1,2,2),imshow(sub_flowers)% c=normxcorr2(sub_lily(:,:,1),sub_flowers(:,:,1);figure,surf(c),shading flat%max_c,imax=max(abs(c(:);ypeak,xpeak=ind2sub(size(c),imax(1);corr_offset=(xpeak-size(sub_lily,2)(ypeak-size(sub_lily,1);rect_offset=(rect_flower
19、s(1)-rect_lily(1),(rect_flowers(2)-rect_lily(2);offset=corr_offset+rect_offset;xoffset=offset(1);yoffset=offset(2);%xbegin=xoffset + 1;xend=xoffset + size(lily,2);ybegin=yoffset+1;yend=yoffset+size(lily,1);extracted_lily=flowers(ybegin:yend,xbegin:xend,:);if isequal(lily,extracted_lily) disp(lily.ti
20、f was extracted from flowers.tif)end %,recovered_lily=uint8(zeros(size(flowers);recovered_lily(ybegin:yend,xbegin:xend,:)=lily;m,n,p=size(flowers)mask=ones(m,n);i=find(recovered_lily(:,:,1)=0);mask(i)=.2;figure,imshow(flowers(:,:,1)hold onh=imshow(recovered_lily);set(h,AlphaData,mask)%ch7xiti%7-1I=i
21、mread(greens.jpg);ring=maketform(affine,1 0 0; .5 1 0; 0 0 1);,%J = imtransform(I,ring); % imshow(I), figure, imshow(J) uData =0 1; vData=0 1; D=imtransform(I,ring,bicubic,. UData,uData,VData,vData,. XData,-2 2,YData,-2 2,. Size,400 400,FillValues,255); imshow(I); figure, imshow(D); %7-2 truesizewar
22、ning=iptgetpref(TruesizeWarning); iptsetpref(TruesizeWarning,off); load mri;,M1=D(:,64,:,:);size(M1); M2=reshape(M1,128 27);size(M2); T0=maketform(affine,0 -2.5;1 0;0 0); imtransform(M2,T0,cubic) R2=makeresampler(bicubic,nearest,fill); M3=imfransform(M2,T0,R2); T1=makeform(affine,-2.5 0;0 1;68.5 0);
23、 T2=makeform(custom,3,2,ipex003,64); T1=makeform(composite,T1,T2); R3=makeresampler(cubic,nearest,nearest,fill); M4=tformarray(D,Tc,R3,4 1 2,1 2,66 128,0); T3=maketform(affine,-2.5 0 0;0 1 0;0 0 0.5;68.5 0 -14); S=tformarray(D,T3,R3,4 1 2,1 2 4,66 128 35,0); figure;,immovie(S,map); S2=padarray(S,6 0 0 0,0,both); montage(S2,map); functionU=ipex003(X,t) U=X repmat(T.tdata,size(X,1) 1);,