Jonathan M.'s Raspberry Pi Stitches Together Impressive Images of Passing Trains
Recognizing passing trains and capturing them slice by slice, this computer vision system is a trainspotter's dream.
Semi-pseudonymous software engineer and train enthusiast Jonathan M., also known as "jo-m," has put a Raspberry Pi and a Camera Module to work capturing shots of the rolling stock which passes by under his apartment — stitching dozens of small photos together into a full shot of the entire train.
"I have a rail line right under my apartment, so I built a small computer vision app running on a Rasperry Pi which records each train passing, and tries to stitch an image of it," jo-m explains. "There is a parameter which tells the program how many pixels there are per meter. From this you can compute the length after stitching. Using framerate, you can compute the speed in the same way."
The result is undeniably impressive. As a train passes by jo-m's apartment, the Raspberry Pi camera begins capturing a series of still images. Each image only contains a small slice of the complete train, but with enough overlap for them to be seamlessly stitched together automatically — creating a full-length image of the entire train, albeit with a constantly-repeating background.
Using knowledge of pixels-to-length at the distance the track runs, the software is then able to determine the length of the train as well as its speed and direction — even going so far as to offer a guess at the train's acceleration as it passes the camera.
"[It] should work with any video4linux USB cam," jo-m says of his software, "or Raspberry Pi Camera v3 modules. The computer vision used in trainbot is fairly naive and simple. There is no camera calibration, image stabilization, undistortion, perspective mapping, or 'real' object tracking. This allows us to stay away from complex dependencies like OpenCV, and keeps the computational requirements low. All processing happens on CPU."
The source code for the project is available on GitHub under the permissive MIT license; jo-m has also built a public front-end site, dubbed "OnlyTrains," for those curious about what passes by his apartment on a given day.