Ask Your Question

Line endpoint detection

asked 2020-11-11 00:20:51 -0500

sachet12345 gravatar image

Hi, I would like to find enpoints of lines in my image and then connect them to fix line breaks. The blue part in the image are the line breaks I would like to fix. The green part is the houghline detected.

I have tried probabilistic hough transform but it is detecting only one line instead of all the lines. What other methods can be used for this purpose? Please guide or provide me with relevant literature. Thank you.

image description image description

import numpy as np
import cv2
import matplotlib.pyplot as plt

img = cv2.imread(r"C:\Users\Sachet\Desktop\7c563eea-8b71-4cb9-8136-ce445ce10fde.jpg")
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray,50,150,apertureSize = 3)
minLineLength = 40
maxLineGap = 5
lines = cv2.HoughLinesP(edges,1,np.pi/180,100,minLineLength,maxLineGap)
for x1,y1,x2,y2 in lines[0]:

edit retag flag offensive close merge delete


I would hesitate to simply "fix" electrical diagrams. it's a visual language. if you make non-connecting lines cross, you have to also signify connecting lines, but differently. that would typically be a bold dot on the intersection. your diagram doesn't show this style used.

crackwitz gravatar imagecrackwitz ( 2020-11-12 07:24:04 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2020-11-11 05:20:22 -0500

kbarni gravatar image

Probably you can tune the parameters of the probabilistic Hough transform to detect all the lines; however it will sometimes bridge the gaps.

You can try to compute the skeleton transform of the image (cv.ximgproc.thinning) then follow every line. The endpoints are the pixels having only one neighbor. To bridge the gaps, you can search in the neighborhood of an endpoint for another endpoint (also check if the directions match).

edit flag offensive delete link more


Hi, Could you provide any literature about how to proceed after getting the skeleton? I am new to opencv.

sachet12345 gravatar imagesachet12345 ( 2020-11-11 22:00:27 -0500 )edit
  • create a new black image for storing the endpoints
  • for the endpoints (points having only one neighbor in the skeleton) mark the corresponding point in the endpoint image with 1
  • for every pixel=1 in the endpoint image, check the neighborhoods (two for loops) for other endpoints. If you find a corresponding endpoint, connect the two points in the original image and set their value to 0 in the endpoint image (as they are no longer endpoints)
kbarni gravatar imagekbarni ( 2020-11-12 11:50:32 -0500 )edit

Question Tools

1 follower


Asked: 2020-11-11 00:20:51 -0500

Seen: 962 times

Last updated: Nov 11 '20