소스 뷰어

행렬 크기 및 위상 연산

import numpy as np, cv2
x = np.array([1, 2, 3, 5, 10], np.float32)            # 리스트를 numpy array로 변환
y = np.array([2, 5, 7, 2, 9]).astype("float32")
mag = cv2.magnitude(x, y)                   # 크기 계산
ang = cv2.phase(x, y)                       # 각도(방향) 계산
p_mag, p_ang  = cv2.cartToPolar(x, y)  # 극좌표로 변환
x2, y2 = cv2.polarToCart(p_mag, p_ang)  # 직교좌표로 변한
print("[x] 형태: %s 원소: %s" % ( x.shape, x))
print("[mag] 형태: %s 원소: %s" % ( mag.shape, mag))
[x] 형태: (5,) 원소: [ 1.  2.  3.  5. 10.]
[mag] 형태: (5, 1) 원소: [[ 2.236068 ]
 [ 5.3851647]
 [ 7.6157737]
 [ 5.3851647]
 [13.453624 ]]
print(">>>열벡터를 1행에 출력하는 방법")
print("[m_mag] = %s" % mag.T)
print("[p_mag] = %s" % np.ravel(p_mag))
print("[p_ang] = %s" % np.ravel(p_ang))
print("[x_mat2] = %s" % x2.flatten())
print("[y_mat2] = %s" % y2.flatten())
>>>열벡터를 1행에 출력하는 방법
[m_mag] = [[ 2.236068   5.3851647  7.6157737  5.3851647 13.453624 ]]
[p_mag] = [ 2.236068   5.3851647  7.6157737  5.3851647 13.453624 ]
[p_ang] = [1.1071129 1.1902124 1.1658309 0.3805839 0.7329612]
[x_mat2] = [1.0000718 2.000388  3.000516  4.999845  9.998684 ]
[y_mat2] = [1.9999641 4.9998446 6.999779  2.0003877 9.00146  ]