Detecting Shapes and Colors with Emgu CV
When I was younger, I liked to imagine what the future would be like at a technological level. I imagined cars levitating and moon bases, but instead, we have a global pandemic. Sigh… However, there are already some things that look like they came out of a science fiction movie. Countless computer programs recognize our faces; we can search for stuff on the internet using pictures of that same stuff, and there are even cars that can drive themselves. But still without turbo boost.
Although the technologies I mentioned usually use their own ultra-advanced software, there is an open-source tool that can help us do these kinds of things. Do not expect to program an autonomous vehicle with it, but an application that can detect traffic signals and people through a camera is perfectly possible. I am obviously talking about OpenCV. An awesome computer vision library.
Today, I am going to show you parts of a small application that I made using this technology, or better yet, using Emgu CV, which is an Open CV wrapper for .NET languages. This of course because C# is the best programming language in the world 😉.
This application is very simple. It detects circles and squares through the webcam, as well as colors, more specifically, red, green, and blue.
Using a VideoCapture object and a timer, frames are sent to Emgu CV analyze, but first, these must be converted to grayscale:
To detect circles, the Hough transform is used (I will not explain what this does in detail since I don’t have enough knowledge about it, but it’s basically an algorithm that detects geometric shapes in digital images):
To detect rectangles, first, edges are found using the Canny edge detector (another tricky algorithm that discovers edges in an image):
Then, it is necessary to obtain the contours using the following method:
And for each contour found, try to generate polygons:
If the polygon has 4 vertices (approxContour.Size == 4), we are in the presence of a quadrilateral. But that does not mean that it is a rectangle. To be one, its internal angles must have 90°.
So, let us use the following algorithm to check if the quadrilateral at least looks like a rectangle:
Finally, to detect colors, and since only additive primary colors (red, green, and blue) are being spotted, we just need to check if the color we’re looking for is between 150 and 255 of the RGB color model, and if the remaining primary colors are between 0 and 50, using the following method:
For non-primary colors, it would make sense to use the hue of the HSV model.
Emgu CV finds the pixels with colors within the range that we defined, and then, we just need to have a threshold to decide the quantity of those colors that must appear in our camera to trigger the actions we want:
In the application that was developed, when a square is found, the geometric figure in the center of the screen grows, and when a circle is found, it rotates. When large quantities of one of the following colors are detected: red, green, or blue; the figure changes to that same color.
For more details on how to make an application of this kind, you can visit this link.
Comments are closed