opencv straight line detection

Straight line detection must be detected against the binary image of the result of edge detection, and cannot be detected against the gray image

The binary image and the result image of edge detection are still different.

The detection result of the binary graph:

api:

cv.HoughLines(image, rho, theta, threshold, lines=None, srn=None, stn=None, min_theta=None, max_theta=None)

parameter: the
first parameter is a binary image, so the Hough Before transformation, binarization or Canny edge detection is required.

 

The HoughLinesP function of opencv is a statistical probability Hough line transformation function, which can output the end points of the detected line. Its function prototype is: HoughLinesP(image, rho, theta, threshold[, lines[, minLineLength[, maxLineGap]]] ) -> lines

The image parameter represents the output image of edge detection, which is a single-channel 8-bit binary image.

The rho parameter represents the resolution of the parameter polar diameter in pixel values, and 1 pixel is generally used here.

The theta parameter represents the resolution of the parameter polar angle in radians, and 1 degree is used here. Generally set to CV_PI/180;

The threshold parameter represents the minimum curve intersection point required to detect a straight line.

The lines parameter represents the container that stores the parameter pairs of the detected line, that is, the coordinates of the two end points of the line segment.

The minLineLength parameter indicates the minimum number of points that can form a straight line. Lines with insufficient points will be discarded.

The maxLineGap parameter represents the maximum distance of a bright spot that can be considered to be in a straight line.
 

demo:

 

import cv2
import numpy as np
image=cv2.imread("erzhi.jpg")

start=time.time()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 50, 120)
line = 100
minLineLength = 15
lines = cv2.HoughLinesP(edges, 1, np.pi / 90, 10, lines=line, minLineLength=minLineLength)
lines1 = lines[:, 0, :]
print(time.time()-start,lines.shape)
for x1, y1, x2, y2 in lines1:


    cv2.line(image, (x1, y1), (x2, y2), (0, 255, 0), 2)
cv2.imshow("edges",edges)
cv2.imshow("lines",image)
cv2.waitKey()

Guess you like

Origin blog.csdn.net/jacke121/article/details/115213997