cv2.HoughLineP()的参数为
代码实例
import cv2
import numpy as np
img = cv2.imread('line-cycle.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 50, 120)
minLineLength = 20 # 最小直线长度,太小舍去
maxLineGap = 5 # 最大线段间隙, 太大舍去
lines = cv2.HoughLinesP( # P意味着概率,所谓“概率版本的Hough变换”
edges, 1, np.pi/180, 100, minLineLength, maxLineGap
)
for each in lines[0: 10]: # 绘制直线,十条
for x1, y1, x2, y2 in each:
cv2.line(img, (x1, y1), (x2, y2), (255, 0, 0), 2)
cv2.imwrite('line-cycle-detect.jpg', img)
cv2.imshow('edges', edges)
cv2.imshow('lines', img)
cv2.waitKey()
cv2.destroyAllWindows()
如图所示为直线检测图。用蓝色的线条绘制出了十条检测出来的直线。这是书上的例子。
检测一个单纯一点的图,包含一条直线和一个圆。如图为将直线用蓝色的线条替代。
绘制圆和圆心
代码
import cv2
import numpy as np
planets = cv2.imread('planet_glow.jpg')
gray_img = cv2.cvtColor(planets, cv2.COLOR_BGR2GRAY)
img = cv2.medianBlur(gray_img, 5)
cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
circles = cv2.HoughCircles(img,cv2.HOUGH_GRADIENT,1,120,
param1=100,param2=30,minRadius=0,maxRadius=0)
circles = np.uint16(np.around(circles))
for i in circles[0,:]:
# draw the outer circle
cv2.circle(planets,(i[0],i[1]),i[2],(0,255,0),2)
# draw the center of the circle
cv2.circle(planets,(i[0],i[1]),2,(0,0,255),3)
cv2.imwrite("planets_circles.jpg", planets)
cv2.imshow("HoughCirlces", planets)
cv2.waitKey()
cv2.destroyAllWindows()
参考
《opencv3 计算机视觉: python语言实现》