第二次写CSDN文档,上一篇的排版实在太烂了,于是决定认真学习一下markdown的语法。
好了,废话不多说,今天,我们学习一下图像(2维平面)到图像(2维平面)的四种变换,等距变换,相似变换,仿射变换,投影变换 首先介绍它的原理,最后介绍matlab的实现
射影变换矩阵
听了这么多群,不变量的数学概念,可能有点晕,下面我用最直观的语言解释。线性空间中的线性变换可以用矩阵来描述,因此我们用矩阵来刻画这四种变换。我们以数学系的经典代数入门教材北大版的《高等代数》为例,研究这些变换是如何进行的
等距变换(isometric transform),保持欧式距离不变,当图像中的点用齐次坐标表示时,变换矩阵如下所示:
clear;close all;clcI=imread('book1.jpg');figure,imshow(I);[w,h]=size(I);theta=pi/4;t=[100,100];s=0.5;% test Eucludian transformH_e=projective2d([cos(theta) -sin(theta) t(1); sin(theta) cos(theta) t(2); 0 0 1]');newimg=imwarp(I,H_e);figure,imshow(newimg);
相似变换(similarity transform):等距变换+均匀缩放,当图像中的点用齐次坐标表示时,变换矩阵如下所示:
clear;close all;clcI=imread('book1.jpg');figure,imshow(I);[w,h]=size(I);theta=pi/4;t=[100,100];s=0.5;%% test similar transformH_s=projective2d([s*cos(theta) -s*sin(theta) t(1); s*sin(theta) s*cos(theta) t(2); 0 0 1]');newimg=imwarp(I,H_s);figure,imshow(newimg);
仿射变换(affine transform):非奇异变换+均匀缩放,当图像中的点用齐次坐标表示时,变换矩阵如下所示:
clear;close all;clcI=imread('book1.jpg');figure,imshow(I);[w,h]=size(I);theta=pi/4;t=[100,100];s=0.5;%% test affine transformH_a=projective2d([1 0.5 t(1); 0 0.5 t(2); 0 0 1]');newimg=imwarp(I,H_a);figure,imshow(newimg);
射影变换(projection transform):当图像中的点的齐次坐标的一般非奇异线性变换。有些文献中把射影变换矩阵称为单应性矩阵变换矩阵如下所示:
clear;close all;clcI=imread('book1.jpg');figure,imshow(I);[w,h]=size(I);theta=pi/4;t=[100,100];s=0.5;%% test projective transformH_P=projective2d([0.765,-0.122,-0.0002; -0.174,0.916,9.050e-05; 105.018,123.780,1]);newimg=imwarp(I,H_P);figure,imshow(newimg);
说了这么多,下边举一个简单的小应用,就是把通过求两幅对应点的单应性矩阵(射影矩阵),把一种图片变换成另一张的形状。如图:
选择两幅图像对应的四个点
第一幅
第二幅
变换的结果
%% Initialclear;clc;img_num=2; %the number of imagecompress_scale=0.4; %define image compress scalepoints_p=[0 0;1 0;2 1;2 0]; %define cordinate of 2D plain in 3D space %% define a cell that load imageImage=cell(1,img_num);%% read the imageImage{1,1}=imread('book1.jpg');Image{1,2}=imread('book2.jpg');%% image compression, transform rgb to gray, and select feature points feature=[];for i=1:img_num Image{1,i}=imresize(Image{1,i},compress_scale); I{:,:,i}=Image{1,i}; Image{1,i}=rgb2gray(Image{1,i}); imshow(Image{1,i}); hold on; for j=1:4 [x,y]= ginput(1); %select the corner x=round(x); y=round(y); plot(x,y,'ro'); feature(j,2*i-1)=x; %feature is a matrix containing corner cordination feature(j,2*i)=y; end close all;end %% calculate homegraphy matrix for each matrixfeaturep1=feature(:,1:2);featurep2=feature(:,3:4);h = calc_homography(featurep2, featurep1);Im=I{:,:,2};[a,b]=size(I);tform=projective2d(h);J=imwarp(Im,tform); % matlab自带的处理图像变换的函数figure,imshow(I{:,:,1});figure,imshow(I{:,:,2});figure,imshow(J)
联系客服