python open cv를 이용하여 이미지를 정사각형으로 변환하는 방법
조회수 516회
아래와 같은 이미지가 있고, p0
, p1
, p2
, p3
의 좌표값은 모두 알고 있습니다.
파이썬 opencv를 활용하여 아래와 같이 변환하고 싶습니다.
아래와 같은 코드를 작성하였으나 원하는 결과가 나오지 않습니다.
import math
import cv2
import numpy as np
path = 'img/path.jpg'
img = cv2.imread(path, cv2.IMREAD_COLOR)
height, width, ch = img.shape
points = some_magical_point_detector(img)
temp = []
for idx, p in enumerate(points):
p1 = points[idx]
p2 = points[(idx+1) % 4]
d = math.sqrt( ((p1[0]-p2[0])**2)+((p1[1]-p2[1])**2) )
temp.append(d)
size = max(temp)
dst_points = np.float32([[0, 0], [size, 0], [0, size], [size, size]])
M = cv2.getPerspectiveTransform(points, dst_points)
dst = cv2.warpPerspective(img, M, (width, height))
plt.imshow(dst)
plt.title('Affine')
plt.show()
어떤 부분을 잘못한걸까요...
-
(•́ ✖ •̀)
알 수 없는 사용자
댓글 입력