图像仿射的基本原理-旋转变换、平移、缩放

旋转是图像处理的常用技巧。今天介绍一下旋转,平移以及尺度放缩的基本原理。

点的旋转

给定一个点P(x,y),以及一个角度θ\theta,求逆时针旋转θ\theta之后新的点座标PP'的位置。
我们用极座标表示P
x=Rcosϕx = Rcos\phi
y=Rsinϕy = R sin\phi
逆时针旋转thetatheta之后,
x=Rcos(θ+ϕ);y=Rsin(θ+ϕ)x' = Rcos(\theta + \phi); y' = Rsin(\theta+\phi)
把cos和sin拆开。
x=R(cosθcosϕsinθsinϕ)=xcosθysinθx' =R(cos\theta cos\phi - sin\theta sin\phi)\\ = xcos\theta - ysin\theta
y=R(sinθcosϕ+cosθsinϕ)=xsinθ+ycosθy' = R(sin\theta cos\phi + cos\theta sin\phi)\\ =xsin\theta + ycos\theta
然后写成矩阵形式
[xy]=[cosθsinθsinθcosθ][xy]\left[ \begin{matrix} x' \\ y' \end{matrix} \right] = \left[ \begin{matrix} cos\theta & -sin\theta \\ sin\theta & cos\theta \end{matrix} \right] \left[ \begin{matrix} x \\ y \end{matrix} \right]

齐次座标

图像的仿射变换是要在齐次座标系下进行的。齐次座标能把旋转,缩放以及平移都用相同大小的矩阵表示,而且他们的组合是线性且可逆的。
范式如下:
[x,y,1]T=A[x,y,1]T[x',y',1]^T = A[x,y,1]^T

  • 旋转矩阵
    A=[cosθsinθ0sinθcosθ0001]A = \left[ \begin{matrix} cos\theta & -sin\theta & 0 \\ sin\theta & cos\theta & 0 \\ 0 & 0 & 1 \end{matrix} \right]

  • 平移矩阵
    A=[10tx01ty001]A=\left[ \begin{matrix} 1&0&t_x \\ 0&1&t_y \\ 0&0&1 \end{matrix} \right]

  • 缩放矩阵
    A=[sx000sy0001]A=\left[ \begin{matrix} s_x &0&0 \\ 0&s_y&0 \\ 0&0&1 \end{matrix} \right]

使用这些矩阵,我们就能获得每个座标在仿射变换之后的位置。

如果我们想让一张图片围绕中心旋转。我们应该把中心点移到原点座标上,然后旋转,然后在平移回去。

OpenCV中的仿射API

  • cv2.getRotationMatrix2D(center, angle, scale)
  • cv2.warpAffine(image, A, (width, height))

第一个函数用来获得仿射矩阵。你可以设置这个三个自由度。angle用角度值(不是弧度制)
第二个函数是做仿射变换。其中A就是仿射矩阵。

另外numpy中有角度转弧度的函数 np.radians 以及弧度转角度的函数 np.rad2deg

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章