【数字图像处理】-- 弄懂等距变换(刚性变换)、相似变换、仿射变换、透视变换(投影变换)
目录
- 概述(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⎦⎤=⎣⎡100010txty1⎦⎤⎣⎡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(θ)0sin(θ)cos(θ)0001⎦⎤⎣⎡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(θ)0sin(θ)cos(θ)0txty1⎦⎤⎣⎡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⎦⎤=⎣⎡s000s0001⎦⎤⎣⎡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(θ)0ssin(θ)scos(θ)0txty1⎦⎤⎣⎡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⎦⎤=⎣⎡sx000sy0001⎦⎤⎣⎡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αy0αx10001⎦⎤⎣⎡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αy0αxsy0txty1⎦⎤⎣⎡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⎦⎤=⎣⎡a11a210a12a220b1b21⎦⎤⎣⎡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′⎦⎤=⎣⎡a11a21a31a12a22a32a13a23a33⎦⎤⎣⎡xy1⎦⎤
可得
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′′=z′x′=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′′=z′y′=a31x+a32y+a33a21x+a22y+a23=k31x+k32y+1k21x+k22y+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 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));elseline([movingPoints(end,1);movingPoints(1,1)],[movingPoints(end,2);movingPoints(1,2)]);endtext(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");
示意图:
【数字图像处理】-- 弄懂等距变换(刚性变换)、相似变换、仿射变换、透视变换(投影变换)相关推荐
- python离散余弦变换_数字图像处理(三)—— 离散余弦变换
离散余弦变换(Discrete Cosine Transform)本质上也是离散傅里叶变换(Discrete Fourier Transform),但是只有实数部分.有这样一个性质:如果信号 在给定区 ...
- 数字图像处理与Python实现-离散余弦变换
离散余弦变换 离散余弦变换 1. 前言 2.数学表达 3. 代码实现 1. 前言 离散余弦变换(DCT for Discrete Cosine Transform)是与傅里叶变换相关的一种变换 ...
- 数字图像处理 线性系统、卷积、傅立叶变换
第九章 线性系统.卷积.傅立叶变换 目录 1. 线性系统 2. 二维卷积 3. Fourier变换 作业 1. 线性系统 线性系统理论在"系统分析与控制"及&qu ...
- 图像处理怎么学matlab,Matlab数字图像处理学习(1)-亮度变换
亮度变换函数: 1.imadjust(f,[low_in high_in],[low_out high_out],gamma) gamma = 1时是线性映射,gamma < 1时高亮度区域被压 ...
- dct变换的主要优点有哪些_数字图像处理(三)—— 离散余弦变换
离散余弦变换(Discrete Cosine Transform)本质上也是离散傅里叶变换(Discrete Fourier Transform),但是只有实数部分.有这样一个性质:如果信号 在给定区 ...
- C语言数字图像处理---1.7HSV颜色空间与颜色变换
本篇给大家介绍HSV颜色空间变换与对应的色相.饱和度和明度调节算法.图像颜色空间有很多,比如HSV.HSI.YUV.LAB.CMYK.NTSC等等,HSV颜色空间是一种比较常用的图像颜色空间,本文将学 ...
- 《精通Matlab数字图像处理与识别》一6.2 傅立叶变换基础知识
本节书摘来自异步社区<精通Matlab数字图像处理与识别>一书中的第6章,第6.2节,作者 张铮 , 倪红霞 , 苑春苗 , 杨立红,更多章节内容可以访问云栖社区"异步社区&qu ...
- 【自动驾驶】15.一文读懂图像中点的坐标变换(刚体变换,相似变换,仿射变换,投影变换)
0 前言 现在的人脸图像识别流程中有一个步骤叫人脸对齐,现在的一般方法是采用人脸上的关键点坐标,进行相似变换来实现人脸校正.多次在人脸识别的论文中看到 similarity transform,由于在 ...
- MATLAB说明图像增强,MATLAB数字图像处理(二)图像增强
1 图像增强 1.1 直方图均衡化 对于灰度图像,可以使用直方图均衡化的方法使得原图像的灰度直方图修正为均匀的直方图. 代码如下: 1 2 3I2=histeq( ...
最新文章
- 天堂向左,六级考场向右
- python socket清空接受区_原始Python服务器
- QT —— 应用程序发布
- 【算法】合并两个有序数组【LeetCode】
- 使用cglib实现数据库框架的级联查询
- 技术人员如何持续不断的成长?
- DDD(Domain-Driven Design)领域驱动设计-(一)整体概述
- Linux下GCC报:extern “C“ _declspec(dllexport)相关错误
- 计算机怎么通过网线共享网络,怎样用一根网线联接两台电脑实现网络共享?
- KKB:二进制知识:15瓶水,其中只有一瓶水有毒,请问至少需要几只小白鼠可以一次性喝出来?
- 如何在线绘制简单又漂亮的思维导图
- CSS实现空心三角指示箭头
- Debezium系列之:使用Debezium接入PostgreSQL数据库数据到Kafka集群的详细技术文档
- 批量部署服务器系统,自动化运维之cobbler批量部署服务器系统 | opengers
- GAMES101课程学习笔记—Lec 02:Linear Algebra 线性代数回顾
- DO=MOSI DI=MISO
- 奥利给!有了这么豪横的指南,还愁不会逛 GitHub?!
- html5 加速球 效果,css 渐隐渐现、echarts 圆环图、百度地图覆盖物、echarts水球图(360加速球效果)...
- 测试人员必备数据库技术之数据库约束(五)
- 业务需求——Excel转 Json 以及相关优化