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.
DiceI 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.
- 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.
- remove background: Thresholding gray frame removes the background and gives us binary image with objects
- cropping the objects: Using contours to detect object and then separate them by cropping.
- detecting dots: Inverting the image we get objects that can be again simply detected using contours.
- 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.
- 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 counterEveryone 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.
- 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.
- compare last two frames: Comparing two continuously frames we got an actual movement. We get another binary frame.
- combine binary frames: Combining these frames with OR condition.
- morphological operation: Use morph opening to remove the noise.
- fill in the holes: Detecting areas using contours and detect if they are all filled.
- 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.
- 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.
- 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.