Counting dice and train wagons using computer vision

Computer vision exercises with preprocessing

Before the next project I decided to do some computer vision exercises. Each example is based on a simple logic image preprocessing. No data structure or learning is required.


I got this idea while browsing the net. I was curious about how hard can it be to write such a script. I'll describe the algorithm in steps.

  1. movement detection: Comparing few frames with thresholds gives us the information, whether something is moving in the frame. Adding some small time frame after the movement stops gives us more precise information.
  2. remove background: Thresholding gray frame removes the background and gives us binary image with objects
  3. cropping the objects: Using contours to detect object and then separate them by cropping.
  4. detecting dots: Inverting the image we get objects that can be again simply detected using contours.
  5. filtering dots: If dice is visible also from the side therefore dots from that side can be recognized as well. But we can simply filter these dots by comparing side ratio from their bounding boxes.
  6. result: Count the recognized dots and do some visualization to output frame.

The results are quite good. More testing with different dice and different background should be the next step.

Train Wagon counter

Everyone is doing car counter on highways. But you can't find any for counting train wagons. For the next exercise i chose static video of a train from youtube. Again, I'll briefly describe the algorithm in steps.

  1. compare frame with background: Comparing every new frame to a background frame. By background I mean the frame without train. We get a first binary frame.
  2. compare last two frames: Comparing two continuously frames we got an actual movement. We get another binary frame.
  3. combine binary frames: Combining these frames with OR condition.
  4. morphological operation: Use morph opening to remove the noise.
  5. fill in the holes: Detecting areas using contours and detect if they are all filled.
  6. select the area: The idea was to choose an area in a frame where we can clearly see the background in-between wagons. For this video we've chosen the right edge of the image.
  7. signal processing: Now we're facing a new problem. We're finding local minimum in a signal function. Adding some thresholds and limitations for repetition we can get the local minimum.
  8. result: Count the wagons from filtering function and do some visualization to see when and where the local minimum is detected.

This approach is very limited and works only in a good light conditions and wagons should be the same type and color. Next steps should be using colors, shapes or more wagon details to be more accurate.


Popular posts from this blog

YI Dash Car Camera API

Toilet light wall

Working with android contacts