【数字图像处理】-- 弄懂等距变换(刚性变换)、相似变换、仿射变换、透视变换(投影变换)

概述(Introduction)

图像处理中有许多空间变换,如等距变换(刚性变换)、相似变换、仿射变换、透视变换(投影变换)。本文介绍了各种变换的变换矩阵,并以matlab实现相关实验,展示示意图,从而来帮助读者理解各个变换的区别与联系。

等距变换(Euclidean Transformation)

Euclidean transformation,欧式变换,也叫等距变换、刚性变换,其包括平移变换和旋转变换。

平移变换(Translation Transformation)

平移变换矩阵:
[ x ′ y ′ 1 ] = [ 1 0 t x 0 1 t y 0 0 1 ] [ x y 1 ] \left[ \begin{matrix} {{x}^{'}} \\ {{y}^{'}} \\ 1 \\ \end{matrix} \right]=\left[ \begin{matrix} 1 & 0 & {{t}_{x}} \\ 0 & 1 & {{t}_{y}} \\ 0 & 0 & 1 \\ \end{matrix} \right]\left[ \begin{matrix} x \\ y \\ 1 \\ \end{matrix} \right] xy1=100010txty1xy1

Matlab代码:

cb = checkerboard(25);
cb_ref = imref2d(size(cb));
tx = 100;
ty = 50;
m = [1 0 tx;
     0 1 ty;
     0 0 1]';
tform = affine2d(m);
[out,out_ref] = imwarp(cb,tform);
figure;
subplot(1,2,1),imshowpair(cb,cb_ref,background,imref2d(size(background))),title("Original");
subplot(1,2,2),imshowpair(out,out_ref,background,imref2d(size(background))),title("Translation");

结果图:
沿x轴平移100px,沿y轴平移50px

旋转变换(Rotation Transformation)

平移变换矩阵:
[ x ′ y ′ 1 ] = [ cos ⁡ ( θ ) sin ⁡ ( θ ) 0 − sin ⁡ ( θ ) cos ⁡ ( θ ) 0 0 0 1 ] [ x y 1 ] \left[ \begin{matrix} {{x}^{'}} \\ {{y}^{'}} \\ 1 \\ \end{matrix} \right]=\left[ \begin{matrix} \cos (\theta ) & \sin (\theta ) & 0 \\ -\sin (\theta ) & \cos (\theta ) & 0 \\ 0 & 0 & 1 \\ \end{matrix} \right]\left[ \begin{matrix} x \\ y \\ 1 \\ \end{matrix} \right] xy1=cos(θ)sin(θ)0sin(θ)cos(θ)0001xy1

Matlab代码:

cb = checkerboard(25);
cb_ref = imref2d(size(cb));
theta = 50;
m = [cos(theta), sin(theta), 0;
     -sin(theta),cos(theta), 0;
     0,0,1];
tform = affine2d(m);
[out,out_ref] = imwarp(cb,tform);
figure;
subplot(1,2,1),imshowpair(cb,cb_ref,background,imref2d(size(background))),title("Original");
subplot(1,2,2),imshowpair(out,out_ref,background,imref2d(size(background))) ,title("Rotation");

结果图:
逆时针旋转50°

等距变换(Euclidean Transformation)

等距变换相当于是平移变换和旋转变换的复合。
等距变换矩阵为:
[ x ′ y ′ 1 ] = [ cos ⁡ ( θ ) sin ⁡ ( θ ) t x − sin ⁡ ( θ ) cos ⁡ ( θ ) t y 0 0 1 ] [ x y 1 ] \left[ \begin{matrix} {{x}^{'}} \\ {{y}^{'}} \\ 1 \\ \end{matrix} \right]=\left[ \begin{matrix} \cos (\theta ) & \sin (\theta ) & {{t}_{x}} \\ -\sin (\theta ) & \cos (\theta ) & {{t}_{y}} \\ 0 & 0 & 1 \\ \end{matrix} \right]\left[ \begin{matrix} x \\ y \\ 1 \\ \end{matrix} \right] xy1=cos(θ)sin(θ)0sin(θ)cos(θ)0txty1xy1
可以看出,等距变换有 t x , t y , θ t_x,t_y,\theta tx,ty,θ三个变量,即自由度为3。
θ = 0 \theta=0 θ=0时退化为平移变换;
t x = t y = 0 t_x=t_y=0 tx=ty=0时退化为旋转变换。
Matlab代码:

cb = checkerboard(25);
cb_ref = imref2d(size(cb));
tx = 100;
ty = 50;
m_t = [1 0 tx;
       0 1 ty;
       0 0 1]';
theta = 50;
m_r = [cos(theta), sin(theta), 0;
     -sin(theta),cos(theta), 0;
     0,0,1];
m = m_t * m_r;
tform = affine2d(m);
[out,out_ref] = imwarp(cb,tform);
figure;
subplot(1,2,1),imshowpair(cb,cb_ref,background,imref2d(size(background))),title("Original");
subplot(1,2,2),imshowpair(out,out_ref,background,imref2d(size(background))),title("Translation & Rotation");

结果图:
逆时针旋转50°,沿x轴平移100px,沿y轴平移50px

相似变换(Similarity Transformation)

Similarity transformation,相似变换,包括平移变换、旋转变换和各向同性缩放变换。

各向同性缩放变换(Isotropic Scaling Transformation)

各向同性缩放只有一个缩放因子 s s s,即沿x轴和y轴缩放的因子相同,缩放后比例与原图一致。
各向同性缩放变换矩阵:
[ x ′ y ′ 1 ] = [ s 0 0 0 s 0 0 0 1 ] [ x y 1 ] \left[ \begin{matrix} {{x}^{'}} \\ {{y}^{'}} \\ 1 \\ \end{matrix} \right]=\left[ \begin{matrix} s & 0 & 0 \\ 0 & s & 0 \\ 0 & 0 & 1 \\ \end{matrix} \right]\left[ \begin{matrix} x \\ y \\ 1 \\ \end{matrix} \right] xy1=s000s0001xy1

Matlab代码:

cb = checkerboard(25);
cb_ref = imref2d(size(cb));
s = 0.5;
mscale = [s,0,0;
          0,s,0;
          0,0,1];
tform = affine2d(mscale);
[out,out_ref] = imwarp(cb,tform);
figure;
subplot(1,2,1),imshowpair(cb,cb_ref,background,imref2d(size(background))),title("Original");
subplot(1,2,2),imshowpair(out,out_ref,background,imref2d(size(background))),xlim([0,200]),ylim([0,200]),title("Scale");

结果图:
缩放因子为0.5

相似变换(Similarity Transformation)

相似变换可以由前文提到的平移、旋转加上各向同性缩放变换复合而成。
相似变换矩阵:
[ x ′ y ′ 1 ] = [ s cos ⁡ ( θ ) s sin ⁡ ( θ ) t x − s sin ⁡ ( θ ) s cos ⁡ ( θ ) t y 0 0 1 ] [ x y 1 ] \left[ \begin{matrix} {{x}^{'}} \\ {{y}^{'}} \\ 1 \\ \end{matrix} \right]=\left[ \begin{matrix} s\cos (\theta ) & s\sin (\theta ) & {{t}_{x}} \\ -s\sin (\theta ) & s\cos (\theta ) & {{t}_{y}} \\ 0 & 0 & 1 \\ \end{matrix} \right]\left[ \begin{matrix} x \\ y \\ 1 \\ \end{matrix} \right] xy1=scos(θ)ssin(θ)0ssin(θ)scos(θ)0txty1xy1

可以看出,相似变换在等距变换 t x , t y , θ t_x,t_y,\theta tx,ty,θ三个变量的基础上,增加了一个缩放因子 s s s,自由度为4。
s = 1 s=1 s=1时退化为等距变换。

Matlab代码:

cb = checkerboard(25);
cb_ref = imref2d(size(cb));
tx = 100;
ty = 50;
m_t = [1 0 tx;
       0 1 ty;
       0 0 1]';
theta = 50;
m_r = [cos(theta), sin(theta), 0;
       -sin(theta),cos(theta), 0;
       0,0,1];
s = 0.5;
m_s = [s,0,0;
       0,s,0;
       0,0,1];
m = m_t * m_r * m_s;
tform = affine2d(m);
[out,out_ref] = imwarp(cb,tform);
figure;
subplot(1,2,1),imshowpair(cb,cb_ref,background,imref2d(size(background))),title("Original");
subplot(1,2,2),imshowpair(out,out_ref,background,imref2d(size(background))),xlim([0,200]),ylim([0,200]),title("Scale");

结果图:
在这里插入图片描述

仿射变换(Affine Transformation)

Affine transformation,仿射变换,包括平移变换、旋转变换和各向不同性缩放变换(Non-inotropic Scaling)。

不同性缩放变换(Non-inotropic Scaling)

同性缩放变换只有一个缩放因子 s s s,而不同性缩放变换有两个缩放因子 s x s_x sx s y s_y sy
不同性缩放变换矩阵:
[ x ′ y ′ 1 ] = [ s x 0 0 0 s y 0 0 0 1 ] [ x y 1 ] \left[ \begin{matrix} {{x}^{'}} \\ {{y}^{'}} \\ 1 \\ \end{matrix} \right]=\left[ \begin{matrix} s_x & 0 & 0 \\ 0 & s_y & 0 \\ 0 & 0 & 1 \\ \end{matrix} \right]\left[ \begin{matrix} x \\ y \\ 1 \\ \end{matrix} \right] xy1=sx000sy0001xy1
其中, s x s_x sx控制沿x轴缩放, s y s_y sy控制沿y轴倾斜。特别地,

  1. s x = s y s_x=s_y sx=sy时,为各向同性缩放变换。
  2. s x = − 1 , s y = 1 s_x=-1,s_y=1 sx=1,sy=1时,为垂直翻转。
  3. s x = 1 , s y = − 1 s_x=1,s_y=-1 sx=1,sy=1时,为水平翻转。
  4. s x = − 1 , s y = − 1 s_x=-1,s_y=-1 sx=1,sy=1时,为水平垂直翻转。

Matlab代码:

cb = checkerboard(25);
cb_ref = imref2d(size(cb));
sx = -1;
sy = -1;
mx = [sx,0,0;
     0,1,0;
     0,0,1];
tform = affine2d(mx);
[out,out_ref] = imwarp(cb,tform);
figure;
subplot(1,4,1),imshowpair(cb,cb_ref,background,imref2d(size(background))),title("Original");
subplot(1,4,2),imshowpair(out,out_ref,background,imref2d(size(background))),title("Vertical Flipping");
my = [1,0,0;
     0,sy,0;
     0,0,1];
tform = affine2d(my);
[out,out_ref] = imwarp(cb,tform);
subplot(1,4,3),imshowpair(out,out_ref,background,imref2d(size(background))),title("Horizontal Flipping");
m = [sx,0,0;
     0,sy,0;
     0,0,1];
tform = affine2d(m);
[out,out_ref] = imwarp(cb,tform);
subplot(1,4,4),imshowpair(out,out_ref,background,imref2d(size(background))),title("Vertical and Horizontal Flipping");

示意图:
在这里插入图片描述

剪切变换(Shear Transformation)

剪切变换矩阵:
[ x ′ y ′ 1 ] = [ 1 α x 0 α y 1 0 0 0 1 ] [ x y 1 ] \left[ \begin{matrix} {{x}^{'}} \\ {{y}^{'}} \\ 1 \\ \end{matrix} \right]=\left[ \begin{matrix} \text{1} & {{\alpha }_{x}} & 0 \\ {{\alpha }_{y}} & 1 & 0 \\ 0 & 0 & 1 \\ \end{matrix} \right]\left[ \begin{matrix} x \\ y \\ 1 \\ \end{matrix} \right] xy1=1αy0αx10001xy1
剪切变换由两个变量 α x , α y \alpha_x,\alpha_y αx,αy。其中, α x \alpha_x αx控制沿x轴倾斜, α y \alpha_y αy控制沿y轴倾斜。
α x = − α y = t a n ( θ ) \alpha_x=-\alpha_y=tan(\theta) αx=αy=tan(θ)时,剪切变换退化为旋转变换。
Matlab代码:

cb = checkerboard(25);
cb_ref = imref2d(size(cb));
ax = 0.3;
ay = 0.5;
mx = [1,ax,0;
     1,1,0;
     0,0,1];
tform = affine2d(mx);
[out,out_ref] = imwarp(cb,tform);
figure;
subplot(1,4,1),imshowpair(cb,cb_ref,background,imref2d(size(background))),title("Original");
subplot(1,4,2),imshowpair(out,out_ref,background,imref2d(size(background))),title("Shear in x Direction");
my = [1,1,0;
     ay,1,0;
     0,0,1];
tform = affine2d(my);
[out,out_ref] = imwarp(cb,tform);
subplot(1,4,3),imshowpair(out,out_ref,background,imref2d(size(background))),title("Shear in y Direction");
m = [1,ax,0;
     ay,1,0;
     0,0,1];
tform = affine2d(m);
[out,out_ref] = imwarp(cb,tform);
subplot(1,4,4),imshowpair(out,out_ref,background,imref2d(size(background))),title("Shear in both x and y Direction");

示意图:
剪切示意图

仿射变换(Affine Transformation)

仿射变换可以由前文的平移、剪切变换和非同性缩放变换复合而成。
仿射变换矩阵:
[ x ′ y ′ 1 ] = [ s x α x t x α y s y t y 0 0 1 ] [ x y 1 ] \left[ \begin{matrix} {{x}^{'}} \\ {{y}^{'}} \\ 1 \\ \end{matrix} \right]=\left[ \begin{matrix} {{s}_{x}} & {{\alpha }_{x}} & {{t}_{x}} \\ {{\alpha }_{y}} & {{s}_{y}} & {{t}_{y}} \\ 0 & 0 & 1 \\ \end{matrix} \right]\left[ \begin{matrix} x \\ y \\ 1 \\ \end{matrix} \right] xy1=sxαy0αxsy0txty1xy1
可见,仿射变换矩阵有六个变量 s x , s y , α x , α y , t x , t y s_x,s_y,\alpha_x,\alpha_y,t_x,t_y sx,sy,αx,αy,tx,ty,即自由度为6,其中 s x , s y s_x,s_y sx,sy控制缩放、翻转, α x , α y \alpha_x,\alpha_y αx,αy控制旋转、倾斜, t x , t y t_x,t_y tx,ty控制平移。

透视变换(Perspective Transformation)

Perspective Transformation,透视变换,或称投影变换(Projective Transformation)。

前面所讲的欧式变换、相似变换、仿射变换,其实都是在2维平面上的变换,他们的变换矩阵的第三行恒为 [ 0 , 0 , 1 ] [0,0,1] [0,0,1]
这里把欧式变换矩阵、相似变换矩阵和仿射变换矩阵统一写作:
[ x ′ y ′ 1 ] = [ a 11 a 12 b 1 a 21 a 22 b 2 0 0 1 ] [ x y 1 ] \left[ \begin{matrix} {{x}^{'}} \\ {{y}^{'}} \\ 1 \\ \end{matrix} \right]=\left[ \begin{matrix} {{a}_{11}} & {{a}_{12}} & {{b}_{1}} \\ {{a}_{21}} & {{a}_{22}} & {{b}_{2}} \\ 0 & 0 & 1 \\ \end{matrix} \right]\left[ \begin{matrix} x \\ y \\ 1 \\ \end{matrix} \right] xy1=a11a210a12a220b1b21xy1
透视变换则是在三维空间上的变换,其变换矩阵可以写作:
[ x ′ y ′ z ′ ] = [ a 11 a 12 a 13 a 21 a 22 a 23 a 31 a 32 a 33 ] [ x y 1 ] \left[ \begin{matrix} {{x}^{'}} \\ {{y}^{'}} \\ {{z}^{'}} \\ \end{matrix} \right]=\left[ \begin{matrix} {{a}_{11}} & {{a}_{12}} & {{a}_{13}} \\ {{a}_{21}} & {{a}_{22}} & {{a}_{23}} \\ {{a}_{31}} & {{a}_{32}} & {{a}_{33}} \\ \end{matrix} \right]\left[ \begin{matrix} x \\ y \\ 1 \\ \end{matrix} \right] xyz=a11a21a31a12a22a32a13a23a33xy1

可得
x ′ = a 11 x + a 12 y + a 13 {{x}^{'}}={{a}_{11}}x+{{a}_{12}}y+{{a}_{13}} x=a11x+a12y+a13

y ′ = a 21 x + a 22 y + a 23 {{y}^{'}}={{a}_{21}}x+{{a}_{22}}y+{{a}_{23}} y=a21x+a22y+a23

z ′ = a 31 x + a 32 y + a 33 {{z}^{'}}={{a}_{31}}x+{{a}_{32}}y+{{a}_{33}} z=a31x+a32y+a33

变换得,
x ′ ′ = x ′ z ′ = a 11 x + a 12 y + a 13 a 31 x + a 32 y + a 33 = k 11 x + k 12 y + k 13 k 31 x + k 32 y + 1 {{x}^{''}}=\frac{x'}{z'}=\frac{{{a}_{11}}x+{{a}_{12}}y+{{a}_{13}}}{{{a}_{31}}x+{{a}_{32}}y+{{a}_{33}}}=\frac{{{k}_{11}}x+{{k}_{12}}y+{{k}_{13}}}{{{k}_{31}}x+{{k}_{32}}y+1} x=zx=a31x+a32y+a33a11x+a12y+a13=k31x+k32y+1k11x+k12y+k13

y ′ ′ = y ′ z ′ = a 21 x + a 22 y + a 23 a 31 x + a 32 y + a 33 = k 21 x + k 22 y + k 23 k 31 x + k 32 y + 1 {{y}^{''}}=\frac{y'}{z'}=\frac{{{a}_{21}}x+{{a}_{22}}y+{{a}_{23}}}{{{a}_{31}}x+{{a}_{32}}y+{{a}_{33}}}=\frac{{{k}_{21}}x+{{k}_{22}}y+{{k}_{23}}}{{{k}_{31}}x+{{k}_{32}}y+1} y=zy=a31x+a32y+a33a21x+a22y+a23=k31x+k32y+1k21x+k22y+k23

z ′ ′ = z ′ z ′ = 1 z''=\frac{z'}{z'}=1 z=zz=1
因此,透视变换由8个变量控制,即自由度为8,选定图片的四个不同坐标点即可进行透视变换。

取原图的四个顶点,并用鼠标选取四个变换目标点,进行透视变换。
Matlab代码:

cb = checkerboard(25);
cb_ref = imref2d(size(cb));
fixedPoints = [1,1; 200,1;200,200;1,200];
movingPoints = ginput(4)*200;
hold on
for i=1:length(movingPoints)
    plot(movingPoints(i,1),movingPoints(i,2),'LineStyle','none','Marker','*');
    if(i+1<=length(movingPoints))
        line(movingPoints(i:i+1,1),movingPoints(i:i+1,2));
    else
        line([movingPoints(end,1);movingPoints(1,1)],[movingPoints(end,2);movingPoints(1,2)]);
    end
    text(movingPoints(i,1),movingPoints(i,2),sprintf("%d",i));
end
title("Moving Points");

tform = fitgeotrans(movingPoints,fixedPoints,'projective');
[out,out_ref] = imwarp(cb,tform,'OutputView',imref2d(size(cb)));
figure;
subplot(1,2,1),imshowpair(cb,cb_ref,background,imref2d(size(background))),title("Original");
subplot(1,2,2),imshowpair(out,out_ref,background,imref2d(size(background))),title("Projective Transformation");

示意图:
变换的点透视变换

  • 13
    点赞
  • 97
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值