Implementation of python opencv Detection Line cv2.HoughLinesP

  • 2021-11-13 02:06:16
  • OfStack

The cv2.HoughLines () function looks for lines in a 2-valued image, and the cv2.HoughLinesP () function looks for line segments.

cv2.HoughLinesP () function prototype:


HoughLinesP(image, rho, theta, threshold, lines=None, minLineLength=None, maxLineGap=None) 
image: It must be a 2-value image, and it is recommended to use the result image of canny edge detection; rho: Line segment distance accuracy in pixels, double type, recommended 1.0 theta: Angular accuracy of line segments in radians, recommended numpy. pi/180 threshod: The threshold parameter of the accumulation plane, int type, exceeds the set threshold before the line segment is detected. The larger the value, basically means that the longer the line segment is detected and the less the number of line segments is detected. According to the situation, it is recommended to try 100 first lines: The meaning of this parameter is unknown. It is found that different lines has no effect on the result, but don't ignore its existence minLineLength: Minimum length of line segment in pixels, set according to application scenario maxLineGap: Two line segments in the same direction are judged as the maximum allowable interval (break) of one line segment. If it exceeds the set value, the two line segments are regarded as one line segment. The larger the value, the greater the break on the allowable line segment, and the more likely it is to detect the potential straight line segment

Example of HoughLinesP () call:


# coding=utf-8
import cv2
import numpy as np
 
img = cv2.imread('02.jpg')
 
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
 
gaus = cv2.GaussianBlur(gray,(3,3),0)
 
edges = cv2.Canny(gaus, 50, 150, apertureSize=3)
 
minLineLength = 100
maxLineGap = 10
lines = cv2.HoughLinesP(edges, 1, np.pi / 180, 100, minLineLength, maxLineGap)
 
for x1, y1, x2, y2 in lines[0]:
    cv2.line(img, (x1, y1), (x2, y2), (0, 255, 0), 2)
 
cv2.imshow("houghline",img)
cv2.waitKey()
cv2.destroyAllWindows()

Related articles: