A guide to detecting color and objects in images

Let’s start this newsletter with a quote about computer vision!

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

HSVThresholder.py

HSVThresholder.py

3.68 KBX-PYTHON File

gyuw4.png

gyuw4.png

94.95 KBPNG File

cropped_image.jpg

cropped_image.jpg

2.56 KBJPEG File

Steps to run the code:

  1. Download and place all the files in the same folder.

  2. Run this command in your terminal after installing Python with PATH. (Guide: How To Install Python on Windows | phoenixNAP KB)

  3. Install all the required dependencies by running this code in the command prompt.

pip install opencv-python numpy matplotlib pillow pysimplegui
  1. Run the HSVthresholder.py file

python HSVThresholder.py
  1. Play 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!

If you want detailed explanation of the code, and a really in depth understanding of what’s going on, shoot me an email at [email protected], I would be down to setup a call with you and catch you up to speed on everything there is!

Keep reading