[英]Perspective Transform in OPENCV PYTHON
I am trying to perform a perspective transform of a sudoku puzzle.我正在尝试对数独谜题进行透视变换。 The expected transformation is happening only on the left side.预期的转换仅发生在左侧。 Please help me by pointing out my mistake.请帮助我指出我的错误。
Input Image:输入图像:
Expected Output Image:预期输出图像:
The output I am getting:我得到的输出:
The corners of the sudoku puzzle found using cv2.approxpolydp() are as follows:使用cv2.approxpolydp()找到的数独游戏的角落如下:
top_left = [71,62]
top_right = [59, 418]
bottom_right = [443, 442]
bottom_left = [438, 29]
The shape of the output image is [300,300].输出图像的形状为 [300,300]。
The corresponding output coordinates are :对应的输出坐标为:
output_top_left = [0,0]
output_top_right = [0, 299]
output_bottom_right = [299, 299]
output_bottom_left = [299,0]
The following is the code I used for perspective transform:以下是我用于透视变换的代码:
#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))
The generated Transformation Matrix is :生成的变换矩阵为:
[[ 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]]
You have your X,Y coordinates reversed.你的 X、Y 坐标颠倒了。 Python/OpenCV requires them listed as X,Y (even though you define them as numpy values). Python/OpenCV 要求将它们列为 X、Y(即使您将它们定义为 numpy 值)。 The array you must specify to getPerspectiveTransform must list them as X,Y.您必须为 getPerspectiveTransform 指定的数组必须将它们列为 X、Y。
Input:输入:
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()
Results:结果:
You should note that:你应该注意:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.