728x90
반응형
이번 포스팅에서는 opencv의 getPerspectiveTransform과 warpPerspective 함수를 이용한 이미지 warping 방법에 대해 알아보겠습니다.
먼저 필요한 라이브러리를 import 합니다.
import cv2
import numpy as np
warping하고자 하는 이미지를 읽어줍니다.
image = cv2.imread('test.jpg')
변환하기 전 좌표와 변환 후의 좌표 값을 설정합니다.
src_pts = np.array([[pts[0][0], pts[0][1]], [pts[1][0], pts[1][1]], [pts[2][0], pts[2][1]], [pts[3][0], pts[3][1]]], dtype=np.float32)
dst_pts = np.array([[0, 0], [w, 0], [w, h], [0, h]], dtype=np.float32)
getPerspectiveTransform 함수를 이용해 변환 행렬을 계산합니다.
trans_mat = cv2.getPerspectiveTransform(src_pts, dst_pts)
계산된 변환행렬을 이용해 원근 변환을 적용합니다.
new_image = cv2.warpPerspective(image, trans_mat, (w, h))
아래는 warping 전후의 이미지입니다.
728x90
반응형
'OpenCV' 카테고리의 다른 글
[OpenCV] OpenCV dnn을 이용해 딥러닝 모델 사용하기 (1) | 2024.02.08 |
---|