I have this image and I would like to find where the crossties are (either bounding boxes or masks works). Crossties are the horizontal blocks between the two rails. Just the rails in the middle are sufficient.
I have been struggling for quite a while now. I found the function cv2.HoughLinesP
but I could not make it work.
Has anyone ever done something similar or know how to do it?
It would be very helpful.
# 213, 205, 210
# 207, 200, 204
# 201, 195, 199
# 215, 208, 206
# 200, 195, 192
import cv2
import numpy as np
img = cv2.cvtColor(cv2.imread('out2168.png'), cv2.COLOR_BGR2RGB)
img = img[:, 300:400]
canny = cv2.Canny(img, 30, 120)
lines = cv2.HoughLines(canny, 1, np.pi / 360, 20)
for rho, theta in lines[0]:
a = np.cos(theta)
b = np.sin(theta)
x0 = a * rho
y0 = b * rho
x1 = int(x0 + 1000 * (-b))
y1 = int(y0 + 1000 * a)
x2 = int(x0 - 1000 * (-b))
y2 = int(y0 - 1000 * a)
cv2.line(img, (x1, y1), (x2, y2), (0, 0, 255), 2)
cv2.imshow('rail', img)
cv2.waitKey(0)
References: