How to detect colors using OpenCV Python

Gowtham

--

In this post, we are going to see how to detect colors by using the OpenCV module in python.

The first step to get into this is just to install the modules as I mentioned below

pip install opencv-pythonpip install numpy

Then, import the modules as well. Read the image and convert the BGR image to HSV(Hue, Saturation, Value) image using the cvtColor() function in the OpenCV module

Example code snippet

Now, choose the color which you want to detect and get the lower and higher HSV values using the HSV color map shown below. In OpenCV, Hue has values from 0 to 180, Saturation, and Value from 0 to 255. Thus, OpenCV use HSV values ranges between (0–180, 0–255, 0–255).

HSV Color Map
HSV Color Map

H is taken according to the x-axis, S is taken according to the y-axis, and V is always taken in the range between (20–255).

Using the HSV values, we need to find the mask using the inRange() function in the OpenCV module and assign it to the variable(mask). Using the bitwise_and() function we can get the detected color image that we chose by passing the BGR image as the first and second argument and the third argument will be the mask and assign it to a variable(detected_img).

Example code snippet

The detected_img will be the final output of the program and display using imshow() function in the OpenCV module.

In our case, we are going to detect the red and green color of the input image and the following code will be in detecting the red and green colors only.

Image used as input:

Input Image
Input Image

Entire Program:

Output Image:

Output Image
Output Image

Thank You For Reading…

--

--

Responses (2)