gpt4 book ai didi

python - 使用cv2.HoughLines()的效果是错误的

转载 作者:太空宇宙 更新时间:2023-11-03 22:45:16 26 4
gpt4 key购买 nike

我指的是这个link用于实验。

这是原图:

Original

我的测试代码:

import cv2
import numpy as np

img = cv2.imread('E:/image/sudoku.png')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray,50,150,apertureSize = 3)

lines = cv2.HoughLines(edges,1,np.pi/180,200)
for rho,theta in lines[0]:
a = np.cos(theta)
b = np.sin(theta)
x0 = a*rho
y0 = b*rho
x1 = int(x0 + 1000*(-b))
y1 = int(y0 + 1000*(a))
x2 = int(x0 - 1000*(-b))
y2 = int(y0 - 1000*(a))

cv2.line(img,(x1,y1),(x2,y2),(0,0,255),2)

cv2.imwrite('E:/image/myhoughlines.jpg',img)
cv2.imshow('1',img)
cv2.waitKey(0)

我的代码运行结果:

my

但是我想要这样的效果:

want

哪里错了?

最佳答案

我知道哪里错了!官网的代码少了一个循环。代码改成这样:

import cv2
import numpy as np

img = cv2.imread('E:/image/sudoku.png')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray,50,150,apertureSize = 3)
lines = cv2.HoughLines(edges,1,np.pi/180,200)
for i in range(len(lines)):
for rho,theta in lines[i]:
a = np.cos(theta)
b = np.sin(theta)
x0 = a*rho
y0 = b*rho
x1 = int(x0 + 1000*(-b))
y1 = int(y0 + 1000*(a))
x2 = int(x0 - 1000*(-b))
y2 = int(y0 - 1000*(a))
cv2.line(img,(x1,y1),(x2,y2),(0,0,255),2)
cv2.imwrite('E:/image/myhoughlines.jpg',img)
cv2.imshow('1',img)
cv2.waitKey(0)

关于python - 使用cv2.HoughLines()的效果是错误的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48494568/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com