300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > 【数字图像处理】-- 弄懂等距变换(刚性变换) 相似变换 仿射变换 透视变换(投影变换)

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

时间:2020-06-02 08:16:58

相关推荐

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

目录

概述(Introduction)等距变换(Euclidean Transformation)平移变换(Translation Transformation)旋转变换(Rotation Transformation)等距变换(Euclidean Transformation) 相似变换(Similarity Transformation)各向同性缩放变换(Isotropic Scaling Transformation)相似变换(Similarity Transformation) 仿射变换(Affine Transformation)不同性缩放变换(Non-inotropic Scaling)剪切变换(Shear Transformation)仿射变换(Affine Transformation) 透视变换(Perspective Transformation)

概述(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] ⎣⎡​x′y′1​⎦⎤​=⎣⎡​100​010​tx​ty​1​⎦⎤​⎣⎡​xy1​⎦⎤​

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");

结果图:

旋转变换(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] ⎣⎡​x′y′1​⎦⎤​=⎣⎡​cos(θ)−sin(θ)0​sin(θ)cos(θ)0​001​⎦⎤​⎣⎡​xy1​⎦⎤​

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");

结果图:

等距变换(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] ⎣⎡​x′y′1​⎦⎤​=⎣⎡​cos(θ)−sin(θ)0​sin(θ)cos(θ)0​tx​ty​1​⎦⎤​⎣⎡​xy1​⎦⎤​

可以看出,等距变换有 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");

结果图:

相似变换(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] ⎣⎡​x′y′1​⎦⎤​=⎣⎡​s00​0s0​001​⎦⎤​⎣⎡​xy1​⎦⎤​

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");

结果图:

相似变换(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] ⎣⎡​x′y′1​⎦⎤​=⎣⎡​scos(θ)−ssin(θ)0​ssin(θ)scos(θ)0​tx​ty​1​⎦⎤​⎣⎡​xy1​⎦⎤​

可以看出,相似变换在等距变换 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] ⎣⎡​x′y′1​⎦⎤​=⎣⎡​sx​00​0sy​0​001​⎦⎤​⎣⎡​xy1​⎦⎤​

其中, s x s_x sx​控制沿x轴缩放, s y s_y sy​控制沿y轴倾斜。特别地,

当 s x = s y s_x=s_y sx​=sy​时,为各向同性缩放变换。当 s x = − 1 , s y = 1 s_x=-1,s_y=1 sx​=−1,sy​=1时,为垂直翻转。当 s x = 1 , s y = − 1 s_x=1,s_y=-1 sx​=1,sy​=−1时,为水平翻转。当 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] ⎣⎡​x′y′1​⎦⎤​=⎣⎡​1αy​0​αx​10​001​⎦⎤​⎣⎡​xy1​⎦⎤​

剪切变换由两个变量 α 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] ⎣⎡​x′y′1​⎦⎤​=⎣⎡​sx​αy​0​αx​sy​0​tx​ty​1​⎦⎤​⎣⎡​xy1​⎦⎤​

可见,仿射变换矩阵有六个变量 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] ⎣⎡​x′y′1​⎦⎤​=⎣⎡​a11​a21​0​a12​a22​0​b1​b2​1​⎦⎤​⎣⎡​xy1​⎦⎤​

透视变换则是在三维空间上的变换,其变换矩阵可以写作:

[ 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] ⎣⎡​x′y′z′​⎦⎤​=⎣⎡​a11​a21​a31​​a12​a22​a32​​a13​a23​a33​​⎦⎤​⎣⎡​xy1​⎦⎤​

可得

x ′ = a 11 x + a 12 y + a 13 {{x}^{'}}={{a}_{11}}x+{{a}_{12}}y+{{a}_{13}} x′=a11​x+a12​y+a13​

y ′ = a 21 x + a 22 y + a 23 {{y}^{'}}={{a}_{21}}x+{{a}_{22}}y+{{a}_{23}} y′=a21​x+a22​y+a23​

z ′ = a 31 x + a 32 y + a 33 {{z}^{'}}={{a}_{31}}x+{{a}_{32}}y+{{a}_{33}} z′=a31​x+a32​y+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′′=z′x′​=a31​x+a32​y+a33​a11​x+a12​y+a13​​=k31​x+k32​y+1k11​x+k12​y+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′′=z′y′​=a31​x+a32​y+a33​a21​x+a22​y+a23​​=k31​x+k32​y+1k21​x+k22​y+k23​​

z ′ ′ = z ′ z ′ = 1 z''=\frac{z'}{z'}=1 z′′=z′z′​=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 onfor 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));elseline([movingPoints(end,1);movingPoints(1,1)],[movingPoints(end,2);movingPoints(1,2)]);endtext(movingPoints(i,1),movingPoints(i,2),sprintf("%d",i));endtitle("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");

示意图:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。