1

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.

enter image description here

# 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)

enter image description here

References:

Community
  • 1
  • 1
Philippe Remy
  • 2,967
  • 4
  • 25
  • 39
  • Please, post whatever solution you’ve got so far. Why is `cv2.HoughLinesP` giving you problems? With that said, there’s a lot going on in that picture, the cross-ties at the horizon aren’t even humanly recognizable, so be prepared to adjust your expectations accordingly. – stateMachine May 04 '21 at 05:05
  • Yes if I could just get the cross-ties in the foreground it would be amazing already. I pasted my progress above (after truncating to have a simpler image to work with). – Philippe Remy May 04 '21 at 05:17
  • I am quite a beginner at this. I have to apologize for that. – Philippe Remy May 04 '21 at 05:20
  • Try running HoughLines on adaptiveThreshold'ed image instead of canny and see if it finds rails. – Knight Forked May 04 '21 at 18:34

0 Answers0