首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >OPENCV PYTHON中的透视变换

OPENCV PYTHON中的透视变换
EN

Stack Overflow用户
提问于 2020-09-18 11:20:03
回答 2查看 9.8K关注 0票数 5

我正在尝试对sudoku拼图进行透视转换。预期的转换只发生在左侧。请帮我指出我的错误。

输入图像:

预期输出图像:

我得到的输出:

使用cv2.approxpolydp()发现的sudoku难题的角落如下:

代码语言:javascript
复制
top_left = [71,62]
top_right = [59, 418]
bottom_right = [443, 442]
bottom_left = [438, 29]

输出图像的形状为300,300。

相应的输出坐标是:

代码语言:javascript
复制
output_top_left = [0,0]
output_top_right = [0, 299]
output_bottom_right = [299, 299]
output_bottom_left = [299,0]

下面是用于透视转换的代码:

代码语言:javascript
复制
#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))

生成的转换矩阵是:

代码语言:javascript
复制
[[ 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]]
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-09-18 19:03:04

你的X,Y坐标颠倒了。Python/OpenCV需要将它们列为X、Y(即使您将它们定义为numpy值)。必须指定给getPerspectiveTransform的数组必须将它们列为X,Y。

输入:

代码语言:javascript
复制
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()

结果:

票数 11
EN

Stack Overflow用户

发布于 2021-05-20 22:09:35

你应该注意到:

height

  • Destination:
  1. 坐标反转:宽度,缩放:第一位比:宽/高第二位比: height/width
    1. Order点坐标不重要,但源点和目的地点应该是相互对应的。
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63954772

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档