
A guide to detecting color and objects in images
Color and Images (The computer perspective)
What is color? In the computer perspective, color is the mixture of the three primary colors R🟥 G🟩 B🟦. Which stand for Red, Green and Blue respectively. By mixing these colors in different compositions, you can get any color between White ⬜ and Black ⬛.
Now, with the possibility of manipulating the color of a single square, imagine being able to manipulate the colors in many different squares all at once, that gives you an IMAGE!
Here is an example of what an image consists in terms of the RGB spectrum:

Color images
So basically, you have 3 sheets of RGB, and you mix them with their differing intensities 0 being the lowest and 255 being the highest to get a desired color in the output. (Output is gray here, but trust me, each square represents a color).
Y’all are prolly like, no way it’s this simple, but images are literally a bunch of squares T-T (see below for an example between high-res and low-res images)

High vs Low-res image
The more the squares, the curvier it can get 😏(don’t quote me on that).
So essentially, the important takeaway here is that RGB or BGR is the standard way images are represented in almost any application today. These are more popularly known as color spaces. The RGB color space is used in common image encoding formats like JPEG, PNG, and BMP.
Well, if RGB is the standard color space then why do other color spaces exist? From a google search, it is apparently because different color spaces offer various advantages over other color spaces.
If you really wanna get deep into it, refer to the link below to learn more about color spaces!
The Hue, Saturation, Value (HSV) color space
From my experience of image processing, I assumed that RGB is the GOAT and that I would be able to do anything with it, like color detection for example, in my case, I wanted to detect different colors in the image of a resistor.

Colors in an image of a resistor
But I was so WRONG, really wrong, after many failed attempts of trying to manipulate the RGB values in the image, I was at the brink of giving up. However, doing further research, I found out about the HSV color space. This was a color space designed to be able to filter out colors inside an image, no AI necessary.
So, to simplify it for you guys, the HSV color space is essentially the color wheel that we use every day.

HSV Color space
With my sudden moment of enlightenment, I wanted to make my own script that lets me import any image of my choice and tweak the HSV VALUES TO CROP OUT COLORS MWAHAHA
Link to anyone who’s interested to learn more about HSV, there’s a cool simulation that you can check out!
The code and how to run it

Steps to run the code:
Download and place all the files in the same folder.
Run this command in your terminal after installing Python with PATH. (Guide: How To Install Python on Windows | phoenixNAP KB)
Install all the required dependencies by running this code in the command prompt.
pip install opencv-python numpy matplotlib pillow pysimpleguiRun the HSVthresholder.py file
python HSVThresholder.pyPlay with the sliders and enjoy detecting your colors!
PS: A bounding box will be drawn when a color is detected in a region of your webcam!



