python detect lines

Solutions on MaxInterview for python detect lines by the best coders in the world

showing results for - "python detect lines"
Emily
10 Jan 2017
1img = cv2.imread('src.png')
2gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
3
4kernel_size = 5
5blur_gray = cv2.GaussianBlur(gray,(kernel_size, kernel_size),0)
6
7low_threshold = 50
8high_threshold = 150
9edges = cv2.Canny(blur_gray, low_threshold, high_threshold)
10
11rho = 1  # distance resolution in pixels of the Hough grid
12theta = np.pi / 180  # angular resolution in radians of the Hough grid
13threshold = 15  # minimum number of votes (intersections in Hough grid cell)
14min_line_length = 50  # minimum number of pixels making up a line
15max_line_gap = 20  # maximum gap in pixels between connectable line segments
16line_image = np.copy(img) * 0  # creating a blank to draw lines on
17
18# Run Hough on edge detected image
19# Output "lines" is an array containing endpoints of detected line segments
20lines = cv2.HoughLinesP(edges, rho, theta, threshold, np.array([]),
21                    min_line_length, max_line_gap)
22
23for line in lines:
24    for x1,y1,x2,y2 in line:
25    cv2.line(line_image,(x1,y1),(x2,y2),(255,0,0),5)
26    
27lines_edges = cv2.addWeighted(img, 0.8, line_image, 1, 0)