1、通过简单例子-点进行演示;
clc;clear;
%%%1-等距变换示例-3自由度:旋转fai0、平移tx0/ty0
% 4个平面上的点的齐次坐标,列向量形式;
a=[1 2 2 1 1;
1 1 2 2 1;
1 1 1 1 1];
%旋转角度fai;
fai0=3.14/4;
%平移向量[tx,tx]
tx0=0;
ty0=0;
%m表示方向,m=1:保留方向并且是欧式变换,m=-1:方向
m=1;
Trans_Euc=[m*cos(fai0) -sin(fai0) tx0;
m*sin(fai0) cos(fai0) ty0;
0 0 1];
%等距变换,变换后点集,a_trans_euc
a_trans_euc=Trans_Euc*a;
%作图
plot(a(1,:),a(2,:))
xlim([-6 6.00]);
ylim([-6 6.0]);
hold on;
plot(a_trans_euc(1,:),a_trans_euc(2,:));legend('Original','Eucli');
%%%2-相似变换示例-四自由度:缩放系数s、旋转fai、平移tx/ty
% 4个平面上的点的齐次坐标,列向量形式;
%旋转角度fai;
fai=3.14/4;
%平移向量[tx,tx]
tx=0;
ty=0;
%各向均匀缩放系数
s=1;
Trans_sim=[s*cos(fai) -1*s*sin(fai) tx;
s*sin(fai) s*cos(fai) ty;
0 0 1];
%相似变换,变换后点集,a_trans_sim
a_trans_sim=Trans_sim*a;
%作图
hold on;
plot(a_trans_sim(1,:),a_trans_sim(2,:));legend('Original','Eucli','Similar');
%%%3-仿射变换示例-六自由度:各向异性缩放系数lamda1和lamda2、旋转fai1和theta1、平移tx/ty
fai1 = 3.14/3;
theta1 = 3.14/6;
lamda1 = 3;
lamda2 = 3;
tx1 = 0;
ty1 = 0;
nul=[0,0];
% 3x3平面仿射变换矩阵=[A t;
% 0' 1 ];
% A是2x2矩阵,t是2x1列向量,0'是1x2零向量(nul);
% A=R(theta1)R(-fai1)DR(fai1);表示先旋转fai1角度、然后缩放lamda1和lamda2、然后往回旋转fai1角度、最后旋转theta1
% D是以lamda1和lamda2为对角元素的矩阵;
R_theta1 = [ cos(theta1) sin(theta1);
sin(theta1) cos(theta1)];
R_fai1 = [ cos(fai1) sin(fai1);
sin(fai1) cos(fai1)];
R__fai1 = [ cos(-1*fai1) sin(-1*fai1);
sin(-1*fai1) cos(-1*fai1)];
D = [ lamda1 0;
0 lamda2];
A = R_theta1*R__fai1*D*R_fai1;
t=[tx1;ty1];
Trans_Aff= [ A t;
nul 1];
%仿射变换,变换后点集,a_trans_aff
a_trans_aff=Trans_Aff*a;
%作图
hold on
plot(a_trans_aff(1,:),a_trans_aff(2,:));
legend('Original','Eucli','Similar','Affine');
%%%4-投影变换示例-8自由度:
% 3x3平面仿射变换矩阵=[A t;
% v' v ];
% A是2x2矩阵,t是2x1列向量,v'是1x2向量(vv);
A=[2 3;
1 3]/3;
t=[0;0];
vv=[1 0];
v=3;
Trans_Pro=[A t;
vv v];
%投影变换,变换后点集,a_trans_pro
a_trans_pro=Trans_Pro*a;
%作图
hold on
plot(a_trans_pro(1,:),a_trans_pro(2,:));
legend('Original','Eucli','Similar','Affine','Projection');
2、通过复杂例子-图片进行演示;
clc;clear;
%%%1-等距变换示例-3自由度:旋转fai0、平移tx0/ty0
% 图像路径:path
path='D:\Program Files\Polyspace\R2019b\bin\ab.jpg';
img=imread(path); %读取图像
imshow(img) %显示图像
%旋转角度fai;
fai0=3.14/4;
%平移向量[tx,tx]
tx0=0;
ty0=0;
%m表示方向,m=1:保留方向并且是欧式变换,m=-1:方向
m=1;
Trans_Euc=[m*cos(fai0) -sin(fai0) tx0;
m*sin(fai0) cos(fai0) ty0;
0 0 1];
%等距变换
tform=projective2d(Trans_Euc); %生成投影变换矩阵
out=imwarp(img(:,:,2),tform); %投影变换矩阵作用于图像
imshow(out) %显示变换后图像
%%%2-相似变换示例-四自由度:缩放系数s、旋转fai、平移tx/ty
% 4个平面上的点的齐次坐标,列向量形式;
%旋转角度fai;
fai=3.14/4;
%平移向量[tx,tx]
tx=0;
ty=0;
%各向均匀缩放系数
s=2;
Trans_sim=[s*cos(fai) -1*s*sin(fai) tx;
s*sin(fai) s*cos(fai) ty;
0 0 1];
%相似变换
tform=projective2d(Trans_sim);
out=imwarp(img(:,:,2),tform);
imshow(out)
%%%3-仿射变换示例-六自由度:各向异性缩放系数lamda1和lamda2、旋转fai1和theta1、平移tx/ty
fai1 = 3.14/3;
theta1 = 3.14/6;
lamda1 = 3;
lamda2 = 3;
tx1 = 0;
ty1 = 0;
nul=[0,0];
% 3x3平面仿射变换矩阵=[A t;
% 0' 1 ];
% A是2x2矩阵,t是2x1列向量,0'是1x2零向量(nul);
% A=R(theta1)R(-fai1)DR(fai1);表示先旋转fai1角度、然后缩放lamda1和lamda2、然后往回旋转fai1角度、最后旋转theta1
% D是以lamda1和lamda2为对角元素的矩阵;
R_theta1 = [ cos(theta1) sin(theta1);
sin(theta1) cos(theta1)];
R_fai1 = [ cos(fai1) sin(fai1);
sin(fai1) cos(fai1)];
R__fai1 = [ cos(-1*fai1) sin(-1*fai1);
sin(-1*fai1) cos(-1*fai1)];
D = [ lamda1 0;
0 lamda2];
A = R_theta1*R__fai1*D*R_fai1;
t=[tx1;ty1];
Trans_Aff= [ A t;
nul 1];
%仿射变换
tform=projective2d(Trans_Aff);
out=imwarp(img(:,:,2),tform);
imshow(out)
%%%4-投影变换示例-8自由度:
% 3x3平面仿射变换矩阵=[A t;
% v' v ];
% A是2x2矩阵,t是2x1列向量,v'是1x2向量(vv);
A=[2 3;
1 3]/3;
t=[0;0];
vv=[1 0];
v=3;
Trans_Pro=[A t;
vv v];
%投影变换
tform=projective2d(Trans_Pro);
out=imwarp(img(:,:,2),tform);
imshow(out)
联系客服