圖像仿射的基本原理-旋轉變換、平移、縮放

旋轉是圖像處理的常用技巧。今天介紹一下旋轉,平移以及尺度放縮的基本原理。

點的旋轉

給定一個點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

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