Why the function cv2.HoughLinesP() has different effects?

徘徊边缘 提交于 2021-02-10 11:13:05

问题


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?


回答1:


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:



来源:https://stackoverflow.com/questions/48496032/why-the-function-cv2-houghlinesp-has-different-effects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!