How to extract the line segment information in a picture accurately in Python

73 Views Asked by At

I'm trying to get detailed information on each line segment, such as the coordinates of two endpoints and whether it is straight or curved, in a picture in Python.

For example, an input image below is expected to lead to an output likes: {1: {"start": (92, 162), "end": (92, 5), "type": "straight"}, 2:...}, which contains 14 lines.

input picture lines in picture

I've tried the HoughLinesP, but it didn't recognize correctly and cannot recognize a curve. Here is my code and result.

img = cv2.imread(path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 50, 100)
lines = cv2.HoughLinesP(edges, 1, np.pi/180, 100)
counts = 0
for line in lines:
    counts += 1
    for x1, y1, x2, y2 in line:
        print(counts, (x1, y1, x2, y2))
        cv2.line(img, (x1, y1), (x2, y2), (0, 255, 0), 2)
cv2.imencode('lines.jpg', img)[1].tofile('lines.jpg')

0

There are 0 best solutions below