为什么函数 cv2.HoughLinesP() 有不同的效果? [英] Why the function cv2.HoughLinesP() has different effects?

查看:41
本文介绍了为什么函数 cv2.HoughLinesP() 有不同的效果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这个

我的测试代码:

导入 cv2将 numpy 导入为 npimg = cv2.imread('E:/image/sudoku.png')灰色 = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)边缘 = cv2.Canny(灰色,50,150,apertureSize = 3)最小线长度 = 100最大线距 = 10线 = cv2.HoughLinesP(edges,1,np.pi/180,100,minLineLength,maxLineGap)对于线中线:对于 x1,y1,x2,y2 在线: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)

我的代码运行结果:

但是官网生成的图片是这样的:

如果你不改变代码.(使用那个链接的代码),生成的图片是这样的:

我改代码的时候,虽然有很多绿线,但是没有官网的好效果.

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

解决方案

我知道为什么效果不一样了.

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

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

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

导入 cv2将 numpy 导入为 npimg = cv2.imread('E:/image/sudoku.png')灰色 = cv2.cvtColor( img,cv2.COLOR_BGR2GRAY)边缘 = cv2.Canny(灰色,50,150,apertureSize = 3)最小线长度 = 100最大线距 = 10线 = cv2.HoughLinesP(edges,1,np.pi/180,100,minLineLength=minLineLength,maxLineGap=maxLineGap)对于线中线:对于 x1,y1,x2,y2 在线: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)

结果图片:

I learn in this link.

This is the original picture:

My test code:

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)

The result of my code running:

But the picture generated by the official website is like this:

If you don't change the code.(Use that link's code),the generated picture is like this:

When I change the code, although there are a lot of green lines,but doesn't have the official website's good effect.

Why do I have a different picture from the official website?

解决方案

I know why the effect is different.

The function cv2.HoughLinesP() in python3.X has 7 parameters.

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

But the code of the official website only writes 6 parameters.So you should write the name of the parameter,like this:

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)

Result picture:

这篇关于为什么函数 cv2.HoughLinesP() 有不同的效果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆