python - 为什么函数cv2.HoughLinesP()有不同的效果?

标签 python python-3.x opencv

我在这个学习link .

这是原图: enter image description here

我的测试代码:

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 )
minLineLength = 100
maxLineGap = 10
lines = cv2.HoughLinesP( edges,1,np.pi/180,100,minLineLength,maxLineGap )
for line in lines:
    for x1,y1,x2,y2 in line:
        cv2.line( img,( x1,y1 ),( x2,y2 ),( 0,255,0 ),2 )
cv2.imwrite( 'E:/image/myhoughlinesp.jpg',img )
cv2.imshow( '1',img )
cv2.waitKey(0)

我的代码运行结果: enter image description here

但是官网生成的图片是这样的: enter image description here

如果不更改代码(使用该链接的代码),生成的图片是这样的: enter image description here

当我修改代码时,虽然出现了很多绿线,但是没有官网的好效果。

为什么我的图片和官网的不一样?

最佳答案

我知道为什么效果不同了。

python3.X中的函数cv2.HoughLinesP()有7个参数。

def HoughLinesP(image, rho, theta, threshold, lines=None, minLineLength=None, maxLineGap=None):

但是官网的代码只写了6个参数,所以你应该写参数的名字,像这样:

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 )
minLineLength = 100
maxLineGap = 10
lines = cv2.HoughLinesP( edges,1,np.pi/180,100,minLineLength=minLineLength,maxLineGap=maxLineGap )
for line in lines:
    for x1,y1,x2,y2 in line:
        cv2.line( img,( x1,y1 ),( x2,y2 ),( 0,255,0 ),2 )
cv2.imwrite( 'E:/image/myhoughlinesp.jpg',img )
cv2.imshow( '1',img )
cv2.waitKey(0)

结果图片:

enter image description here

关于python - 为什么函数cv2.HoughLinesP()有不同的效果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48496032/

相关文章:

python - 对列使用 groupby 后计算重复值的实例

python:(lambda)函数的字典

python - 在 Python 中加速双循环

python - 变长参数

multithreading - 在 Python 中使用多线程运行 Iperf 服务器和客户端会导致段错误

qt - 如何避免缩放图像中出现奇怪的结构伪影?

python - 从文本生成关键字的简单方法是什么?

python - 使用 Pandas 从字典列中提取值

windows - 跨平台的 OpenCV 视频

python - 使用opencv加载32位整数图像