반응형

warpAffine함수를 사용하여 이미지를 이동 및 회전하는 방법을 다루고 있습니다.



마지막 업데이트 - 2018. 10. 3





다음 OpenCV Python 튜토리얼을 참고하여 강좌를 비정기적로 포스팅하고 있습니다.

https://docs.opencv.org/3.4.3/d6/d00/tutorial_py_root.html




translation matrix를 사용하여 x축 방향 및 y축 방향으로 이미지를 이동시킵니다.

 




rotation matrix를 사용하여 이미지를 회전시킵니다.

 

OpenCV의 getRotationMatrix2D 함수는 배율 및 회전 중심 좌표가 추가된 rotation matrix를 사용합니다.

 

 




테스트에 사용한 전체 코드입니다.

 

import numpy as np
import cv2


# 원본 이미지
img_source = cv2.imread('cat.jpg')
cv2.imshow("original", img_source)

cv2.waitKey(0)


# 이미지 이동
height, width = img_source.shape[:2]
M = np.float32([[1, 0, 100], [0, 1, 25]]) # 이미지를 오른쪽으로 100, 아래로 25 이동시킵니다.
img_translation = cv2.warpAffine(img_source, M, (width,height))
cv2.imshow("translation", img_translation)

cv2.waitKey(0)


# 이미지 회전
M = cv2.getRotationMatrix2D((width/2.0, height/2.0), #회전 중심
                            45, # 회전각도(양수 반시계방향, 음수 시계방향)
                            1) # 이미지 배율
img_rotation = cv2.warpAffine(img_source, M, (width, height))
cv2.imshow("rotation", img_rotation)

cv2.waitKey(0)


cv2.destroyAllWindows()

 

반응형

문제 발생시 지나치지 마시고 댓글 남겨주시면 가능한 빨리 답장드립니다.

도움이 되셨다면 토스아이디로 후원해주세요.
https://toss.me/momo2024


제가 쓴 책도 한번 검토해보세요 ^^

+ Recent posts