OPENCV PYTHON의 관점 변환

Sep 18 2020

스도쿠 퍼즐의 원근 변환을 수행하려고합니다. 예상되는 변환은 왼쪽에서만 발생합니다. 제 실수를 지적 해주세요.

입력 이미지 :

예상 출력 이미지 :

내가 얻는 출력 :

cv2.approxpolydp ()를 사용하여 찾은 스도쿠 퍼즐의 모서리는 다음과 같습니다.

top_left = [71,62]
top_right = [59, 418]
bottom_right = [443, 442]
bottom_left = [438, 29]

출력 이미지의 모양은 [300,300]입니다.

해당 출력 좌표는 다음과 같습니다.

output_top_left = [0,0]
output_top_right = [0, 299]
output_bottom_right = [299, 299]
output_bottom_left = [299,0]

다음은 원근 변환에 사용한 코드입니다.

#corners = [[71,62], [59, 418], [443, 442], [438, 29]]
new = np.float32([[0,0], [0,299], [299,299], [299,0]])
M = cv2.getPerspectiveTransform(np.float32(corners), new)
dst = cv2.warpPerspective(gray, M, (300,300))

생성 된 변환 매트릭스는 다음과 같습니다.

[[ 9.84584842e-01  3.31882531e-02 -7.19631955e+01]
 [ 8.23993265e-02  9.16380389e-01 -6.26659363e+01]
 [ 4.58051741e-04  1.45318012e-04  1.00000000e+00]]

답변

4 fmw42 Sep 19 2020 at 02:03

X, Y 좌표가 반전되었습니다. Python / OpenCV에는 X, Y로 나열되어야합니다 (numpy 값으로 정의하더라도). getPerspectiveTransform에 지정해야하는 배열은 X, Y로 나열해야합니다.

입력:

import numpy as np
import cv2

# read input
img = cv2.imread("sudoku.jpg")

# specify desired output size 
width = 350
height = 350

# specify conjugate x,y coordinates (not y,x)
input = np.float32([[62,71], [418,59], [442,443], [29,438]])
output = np.float32([[0,0], [width-1,0], [width-1,height-1], [0,height-1]])

# compute perspective matrix
matrix = cv2.getPerspectiveTransform(input,output)

print(matrix.shape)
print(matrix)

# do perspective transformation setting area outside input to black
imgOutput = cv2.warpPerspective(img, matrix, (width,height), cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT, borderValue=(0,0,0))
print(imgOutput.shape)

# save the warped output
cv2.imwrite("sudoku_warped.jpg", imgOutput)

# show the result
cv2.imshow("result", imgOutput)
cv2.waitKey(0)
cv2.destroyAllWindows()

결과 :