Simple Color-Detection Using OpenCV

Introduction: Simple Color-Detection Using OpenCV

About: Freelance Artist | Keen-Tech Developer

Hi! Today I am going to show a simple method of detecting a color from a live video using OpenCV and python.

Basically I will just test the required color is present in the background frame or not and using OpenCV modules I will mask that region and simultaneously displaying the frame.

Step 1: The Header Files

Now here I have used two header files namely cv2 and NumPy. Basically cv2 is the OpenCV library which loads all the c++ files that are important while using the commands in the codes(it contains all the definitions).

And Numpy is a python library that is essential for storing a multidimensional array. We will be using to store our color range co-ordinates.

And numpy as np basically helps our code to shorten a bit by using np every time instead of numpy.

Step 2: Capturing the Video

This is pretty simple while using python. Here we just need to turn on video recorder so that it can start recording the frames.

Now the value inside the VideoCapture indicates the camera, in my case the camera is connected to my laptop, so 0.

You can go similarly to 1 for the secondary camera and so on. VideoCapture creates the object for it.

Step 3: Capturing Frame and Defining Color

Now here we have to do something so that we can capture the instant frame of the video which will help us to extract the image and we can work on that as per requirement.

"while" loop will help us to run the loop to our requirement time. Now " _,frame =" is used to check the validity of the Frame captured and stores it. " is a boolean variable and returns true if the frame is read correctly and if you get no frames it will not show any error, you will simply get None.

Now line 11 and line 12 basically define the range of color we need to detect. For this, I have used to blue color.

You can proceed with any color for that you need to just type BGR values for that particular color. It's better to define two arrays using numpy arrays as detecting a particular color in the real world will not serve our purpose rather we will define a range of blue color so that it detects within the range.

For this, I have defined two variables storing the lower BGR values and upper BGR values.

Step 4: Masking and Extracting

Now here comes the main task of masking the frame and extracting the color of the frame. I used the predefined commands present in the library in OpenCV to do the masking. Basically masking is the process of removing some portion of the frame, i.e we will remove the pixels that whose color BGR values that don't lie in the defined color range and this is done by cv2.inRange. Afterward, we apply the color range to the masked image depending upon the pixel values and for this, we will use cv2.bitwise_and , It will simply assign the colors to the masked region depending upon the mask and color range values.

Link for cv2. bitwise_and :

Step 5: Finally Displaying!

Here I have used the basic cv2.imshow() for displaying for each frame as an image. Since I have the frame data stored in variables I can retrieve them in imshow(). Here I have displayed all the three frames, original, masked, and colored.

Now we have to exit from the while loop. For this, we can simply implement the cv2.wait.Key(). Basically it tells the wait time before responding. So if you pass 0 it will wait infinitely and 0xFF tells the architecture is 64bit. "ord()" specifies the character that when pressed will execute the break command in if block and it will come out of the loop.

Then cap.release() closes the video recorder and cv2.destroyAllWindows() closes all the opened windows.

If you have any issue, please let me know.

Link to source code :

Step 6: Demo!

Be the First to Share


    • First Time Author Contest

      First Time Author Contest
    • Backyard Contest

      Backyard Contest
    • DIY Summer Camp Contest

      DIY Summer Camp Contest



    Reply 1 year ago

    You're welcome