Introduction: Why Recognizing a Coca-Cola Can is Harder Than It Seems
Imagine: you are writing a robot that must automatically sort trash. Or a system that counts the number of items on a shelf. One of the most popular tasks in interviews and educational projects is recognizing a Coca-Cola can. It seems simple, right? A bright red cylinder with a white label. But in practice, everything turns out differently.
On Stack Overflow, the question "Image Processing: Algorithm Improvement for 'Coca-Cola Can' Recognition" has gathered thousands of views and dozens of answers. Programmers struggled with the problem: how to reliably distinguish a can from other red objects (a fire extinguisher, a red mug, a piece of plastic) under different lighting conditions?
In this article, we will not just provide ready-made C++ code using OpenCV. We will step-by-step analyze image processing algorithms that turn a raw video stream into confident recognition. You will learn how to improve detection accuracy, avoid false positives, and write code that works in real time.
The Problem: Why Simple Methods Don't Work
A typical beginner's mistake is trying to find an object only by color. For example, isolating all red pixels and finding the largest contour among them. But:
- Lighting changes — red color under fluorescent light and under the sun looks different.
- Highlights on metal — the can reflects light, and its color may appear white or orange.
- Background noise — red t-shirts, chairs, billboards confuse the detector.
- Shape distortion — the can may be rotated, partially occluded, or deformed.
Therefore, improving the recognition algorithm is not just about tuning thresholds, but a combination of methods: color filtering, contour detection, shape and texture analysis.
Solution Architecture: From Pixels to Decision
We will build a pipeline of three stages:
- Preprocessing — blurring, conversion to HSV, morphology.
- Segmentation — extracting candidates by color and contours.
- Verification — filtering by aspect ratio, area, texture features.
This approach is used in computer vision C++ for industrial tasks — from part sorting to quality control.
Step 1: Color Filtering in HSV
The first and most important step is to convert the image from RGB to HSV (Hue, Saturation, Value). Why? Because HSV separates color (hue) from brightness. This allows you to define a range for red color that works under different lighting.
#include <opencv2/opencv.hpp>using namespace cv;
Mat hsv, mask;cvtColor(src, hsv, COLOR_BGR2HSV);
// Range for red color (two intervals due to HSV cyclicity)Scalar lower_red1 = Scalar(0, 70, 50);Scalar upper_red1 = Scalar(10, 255, 255);Scalar lower_red2 = Scalar(170, 70, 50);Scalar upper_red2 = Scalar(180, 255, 255);
Mat mask1, mask2;inRange(hsv, lower_red1, upper_red1, mask1);inRange(hsv, lower_red2, upper_red2, mask2);mask = mask1 | mask2;Note: red color is at the boundary of HSV (0 and 180 degrees), so we need two ranges. The Saturation (70) and Value (50) parameters filter out too pale or too dark pixels.
Step 2: Morphology and Contour Detection
After masking, we get a binary image, but it contains noise. We apply morphological closing to fill holes inside the can, and opening to remove small dots.
Mat kernel = getStructuringElement(MORPH_ELLIPSE, Size(5, 5));morphologyEx(mask, mask, MORPH_CLOSE, kernel);morphologyEx(mask, mask, MORPH_OPEN, kernel);