Rangachari Anand
December 20 2004
I am currently in the process of trying to automate my Märklin train layout. I would like nothing better than to sit back and let the computer drive the trains. Undoubtedly this puts me in the minority of modellers who prefer a more hands-on approach to controlling their layouts but this is my preference.
In order to facilitate this, the computer needs to know where
trains are. More precisely, as Dale Schultz
has pointed out on the Marklin B&G mailing list, the computer needs
to know when a train has reached a particular point on a layout. In the
case of 3-rail Märklin layouts, there are a number of ways this
can be implemented. In the case of C and K track, its very easy to
create an ad-hoc contact section anywhere desired. For example, Sean
Fanelli has provided clear instructions
for creating contact sections on C track.
Unfortunately, I already laid and ballasted the track on my
layout before
deciding to implement automated operation. After some search for train
detection alternatives, It appeared that the only
alternative would be to tear up my old M-track and replace it
with C track (at least in the visible section).
Before proceeding down this path, I decided to experiment with an
entirely
different approach to train detection - image processing. Since I
plan to use a computer to implement train control in any case, it
appeared to be straightforward to use a cheap webcam like the Logitec
messenger webcam to monitor track to determine whether it was occupied.
This article summarizes my results so far. As of now, the results look
promising.
I used a Logitec Messenger webcam for these experiments. This is a cheap webcam that can be used for casual video conferencing using such services as Yahoo messenger.

The basic idea is to use the camera to take pictures of a track segment
from overhead direcly and then to detect if the picture changes in any
way. To see if there was any difference with and without a train, I
took a few manual photos with this camera.
Here is a photo of my station yeard area without a train on the
center track.

Here is another photo of the same station with my CSine Crocodile
and a few tank cars parked in the station yard:

Clearly, to the human eye, there is a difference between the
two photos. But how well could a program detect the difference? By
applying image processing techniques, one can see the difference
between the two images:

This appeared promising so I wrote a small program in Python to continuously take snapshots and compute the difference. This program makes use of the excellent VideoCapture library developed by Markus Gritsch and the Python Imaging Library from Pythonware . I include the entire source of my little program below.
from VideoCapture import Device
import Image
import ImageChops
import ImageStat
import time
interval = 1
cam = Device(devnum=0, showVideoWindow=0)
cam.setResolution(640,480)
oldimg = cam.getImage()
while 1:
newimg = cam.getImage()
diffimg = ImageChops.difference( newimg, oldimg)
stats = ImageStat.Stat( diffimg)
x = stats.var
print x[0] + x[1] + x[2]
oldimg = newimg
time.sleep(0.5)
This little program incidentally illustrates the power of the Python language - highly recommended. As you can see, this program continuously computes the RMS difference between the current snapshot and the previous one and prints out the difference to the screen. Included below are the values computed as my train passed under the camera. Clearly the presence of the train has been detected!
41.1500794682
49.5467786398
225.245885455
354.330447283
487.994999028
737.92781744
1148.83392925
1074.24943498
752.109140229
375.913277242
41.9236298616
45.6260293864
Unlike track-based detection, this is definitely less intrusive. The
only issue is that one would have to strictly stay away from the layout
while the computer is controlling it otherwise bad things would happen
:-)
One more nice thing is that a single camer could easily monitor
several parallel tracks. The PIL library allows me to extract out
specific portions of the image for processing so the occupancy of each
track could be detected.
Text Copyright © 2004 Rangachari Anand, all rights reserved