我在这个学习link .
我的测试代码:
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)
当我修改代码时,虽然出现了很多绿线,但是没有官网的好效果。
为什么我的图片和官网的不一样?
最佳答案
我知道为什么效果不同了。
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)
结果图片:
关于python - 为什么函数cv2.HoughLinesP()有不同的效果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48496032/