Data science/Computer Vision

[5분 컷 이해] Rotation matrix(회전 메트릭스) 구하기, 유도

연금(Pension)술사 2024. 3. 18. 10:53

 

 

 

회전변환시에 필요한 matrix을 roration matrix라고하며, 2D와 같이 표현할 수 있습니다.

$M(\theta)=
\begin{bmatrix}
    cos\theta & -sin\theta \\
    -sin\theta & cos\theta
\end{bmatrix}$

이 공식의 유도과정을 이해해보겠습니다.

그림1.

위 그림과 같이 구하고자하는 평면에 두 벡터가 있습니다. 이 그림의 요소들은 다음과 같습니다.

  • G(x, y): 회전시키기 전 벡터
  • G'(x',y'): G을 $\theta$만큼 회전시킨 벡터, 
  • r:G벡터와 G'벡터 길이
  • $\theta$: G을 G'으로 반시계방향(counter-clockwise)으로 회전한 벡터

위 그림에 따라 x, y은 아래와 같이 표현할 수 있습니다.

  • $ x=r cos v $  
  • $ y=r sin v $
  • $ x'=rcos(v+\theta)$
  • $ y'=rsin(v+\theta)$

3번 째, 4번째 식의 $ v+\theta$은 삼각함수의 덧셈정리로 풀면 아래와 같습니다.

  • $x'=rcos(v+\theta) = r(cos(v)\cdot cos (\theta) -sin(v)\cdot sin (\theta))$  (코코마사사)
  • $y'=rsin(v+\theta) = r(sin(v) \cdot cos (\theta) + cos(v) \cdot sin(\theta))$ (싸코코)

위 식에서 $cosv$ 와 $sinv$은 이미 알려져 있으니, 대입하여 알 수 있습니다. 

  • $x'=r(x \cdot cos(\theta) - y \cdot sin(\theta))$
  • $y'=r(x \cdot sin(\theta) + y \cdot cos(\theta))$  (x을 앞으로 정렬)

위 식에서 r이 1인 경우는 단순히 선형결합형태이므로, 선형결합 형태로 나타낼 수 있는 메트릭스로 표현 할 수 있습니다.

$\begin{bmatrix} x' \\ y' \end{bmatrix}= 
\begin{bmatrix}
    cos\theta & -sin\theta \\
    sin\theta & cos\theta
\end{bmatrix} \begin{bmatrix} x \\ y \end{bmatrix}$

 

Python implementation


파이썬으로 위의 방법을 구하면 아래와 같이 작성할 수 있습니다. 주의할 것은 여기서 는 각도이며, 여기서는 30도를 라디안 단위로 변환하여 사용해야 합니다.

import numpy as np

# 각도를 라디안으로 변환
theta = np.deg2rad(30)

# 회전 행렬 생성
R = np.array([[np.cos(theta), -np.sin(theta)],
              [np.sin(theta), np.cos(theta)]])

# G 벡터 생성 (임의의 값으로 설정)
G = np.array([1, 0])  # 예를 들어, G = (1, 0)으로 설정

# G' 벡터 계산
G_prime = np.dot(R, G)

print("G' 벡터:", G_prime)
print("회전 행렬:")
print(R)

 

References


https://www.cuemath.com/algebra/rotation-matrix/

반응형