Counting dice and train wagons using computer vision

Computer vision exercises with preprocessing


Before starting my next project, I decided to work on some computer vision exercises. Each example is based on straightforward image preprocessing techniques. No complex data structures or machine learning are involved.

Dice Detection

I got this idea while browsing the net and became curious about how challenging it would be to write such a script. Here’s a step-by-step breakdown of the algorithm:


  1. Movement Detection: By comparing several frames with thresholds, we can determine if there is any movement in the frame. Adding a small time buffer after the movement stops gives us more accurate information.

  2. Removing the Background: Thresholding the grayscale frame helps to remove the background, leaving us with a binary image that highlights the objects.

  3. Cropping the Objects: Using contours, we can detect and isolate the objects by cropping them.

  4. Detecting Dots: By inverting the image, the dots on the dice become more distinguishable, allowing us to detect them using contours once again.

  5. Filtering Dots: When the dice is visible from the side, dots from that side may also be recognized. However, we can easily filter these out by comparing the aspect ratio of their bounding boxes.

  6. Result: Finally, we count the recognized dots and visualize the results in the output frame.

  7. The initial results are quite promising. The next step would be to conduct further testing with different dice and backgrounds.


You can find the source code here: 
https://github.com/mbodis/dice_recognition






Train Wagon Counter

While car counters on highways are common, finding a solution for counting train wagons is much rarer. For my next exercise, I chose a static video of a train from YouTube. Here’s a brief overview of the algorithm I used:

  1. Compare Frame with Background: Start by comparing each new frame with a background frame, which is essentially a frame without the train. This comparison produces a binary frame.
  2. Compare Last Two Frames: By comparing two consecutive frames, we can detect actual movement, resulting in another binary frame.

  3. Combine Binary Frames: Combine the binary frames from the previous steps using an OR condition.

  4. Morphological Operation: Apply morphological opening to remove noise from the combined binary frame.

  5. Fill in the Holes: Use contours to detect areas and check if they are fully enclosed.

  6. Select the Area: The idea is to choose a specific area in the frame where the background is clearly visible between the wagons. In this video, I selected the right edge of the image.

  7. Signal Processing: The next challenge is to find the local minimum in a signal function. By adding thresholds and limitations to prevent repetition, we can accurately detect these local minima.

  8. Result: Count the wagons by filtering the function and visualize the points where the local minima are detected.


This approach is quite limited, as it works best in good lighting conditions with wagons of the same type and color. The next steps should involve incorporating color, shapes, or additional wagon details to improve accuracy.

You can find the source code here: 



Comments

Popular posts from this blog

Skate Tricks Recognition Using Gyroscope

Play table