-
- You can use
createFastLineDetector
for detecting each line.
-
- Calculate the slope of the current and neighboring lines.
-
- If the slope of current and neighboring lines are the same draw line.
Initializing Line Detector
We will be using ximgproc
library for detecting lines.
import cv2
img = cv2.imread("lines.png")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
d = cv2.ximgproc.createFastLineDetector()
lines = d.detect(gray)
- The
lines
variable returns similar values like [[14.82, 78.90, 90.89, 120.78]]
where x1=14.82
, y1=78.90
, x2=90.89
, y2=120.78
respectively.
Calculating Slope
The slope of a line is calculated with the formula: m = (y2
- y1
) / (x2
- x1
)
For a given line object, get the coordinates and return the slope.
-
def calculate_slope(line_object):
x_point1 = line_object[0]
y_point1 = line_object[1]
x_point2 = line_object[2]
y_point2 = line_object[3]
m = abs((y_point2 - y_point1) / (x_point2 - x_point1))
m = float("{:.2f}".format(m))
return m
Comparing Slopes
-
- Check the equality of the lines. if the points are equal, that means they are the same line.
-
- If the lines are not equal, calculate the neighbor's line slope.
if not equal_arrays:
neighbor_slope = calculate_slope(neighbor_line[0])
-
- If slopes are equal, draw the line. From
neighbor
to current
and current
to neighbor
.
if abs(current_slope - neighbor_slope) < 1e-3:
neighbor_x1 = int(neighbor_line[0][0])
neighbor_y1 = int(neighbor_line[0][1])
neighbor_x2 = int(neighbor_line[0][2])
neighbor_y2 = int(neighbor_line[0][3])
cv2.line(img,
pt1=(neighbor_x1, neighbor_y1),
pt2=(current_x2, current_y2),
color=(255, 255, 255),
thickness=3)
cv2.line(img,
pt1=(current_x1, current_y1),
pt2=(neighbor_x2, neighbor_y2),
color=(255, 255, 255),
thickness=3)
Result

Possible Question But why couldn't you connect the following parts?

Answer
Well, the red dotted line slopes are not equal. Therefore I couldn't connect them.
Possible Question Why didn't you use dilate
and erode
methods? as shown in here
Answer
I tried, but the result is not satisfactory.