Detection of a specific color (Red) using OpenCV and Python

Sanpreet Singh
2 min readMar 31, 2020

In this short tutorial, I am going to explain to you in a simple way how to detect a specific color (red) using OpenCV and Python. I am highly thankful to Pysource whose video Detecting Colors (HSV Color Space)-OpenCV with Python helped me to make the concepts more clear. This video is recommended to readers who read from here. The concept for this blog is to capture the video and read frame by frame. Video is a collection of images. For making this blog, I have used a video named Red Race Car & Yellow Tow Truck — First Race | Motorville — 3D Cars Cartoon for Kids by Seezis Media s.r.o. I am using this video only for just explaining the concept and the copyright for the video belongs to Seezis Media s.r.o. Once the frames are read in a loop are converted into HSV (Hue Saturation Value) . The next step is to find lower and upper red values as they will form mask for red color. Mask is made using cv2 inRange function. This mask is bitwiseand with the original frame to detect only red color from the video. I will attach the video in the end. You can play the video to understand the concept in a better way. Before proceeding further, there is another valuable blog written by me on Face Detection using Viola Jones whose link I am adding below

Face and eyes detection with Viola-Jones along with python code

Python code using OpenCV

Video used in this code can be downloaded from the link Red Race Car & Yellow Tow Truck — First Race | Motorville — 3D Cars Cartoon for Kids by Seezis Media s.r.o. You may use your own video which has different colors (must have red color). This code will detect red color from the rest of color and make others black. Code is shown in the below screenshot

Detection of a specific color (Red) using OpenCV and Python
Detection of a specific color (Red) using OpenCV and Python

Sample Video for demonstration

Demonstration video for red color detection in the video | OpenCV Python

Feel free to ask questions in the comment section to share knowledge as well as clear doubts.

Originally published at http://ersanpreet.wordpress.com on March 31, 2020.

--

--