The functions for this are available in OpenCV, but they are not available with CUDA implementation. Determining object color with OpenCV by Adrian Rosebrock on February 15, 2016 This is the final post in our three part series on shape detection and analysis. Since colors in the RGB colorspace are coded using the three channels, it is more difficult to segment an object in the image based on its color. If you’ve followed up to this point, you should end up with a mask that only has filled in white pixels for where the red circle was. A mask is simply a specific part of the image. The areas that match will be set to the mask variable. We can see that red equals 237, green equals 28, and blue equals 36.

The picture below is a close up of the red color pattern described above. Note color contours around saturated pixels that are difficult to observe in the multicolor input image.

Complete Code

Browse other questions tagged opencv colors detection or ask your own question. You can use this code to find the HSV value of any pixel from your source image. You can see a good explanation about HSV color space here, download the HSV colour wheel from there and manually find out the HSV range. I want to detect a specific color say, blue, from a live video stream. I have written the following code which displays the live video stream and change it into HSV and grayscale. Since I am completely new to opencv I have no idea what to do next.

Then we will calculate the distance from each color and find the shortest one. In the previous step, we generated a mask to determine the region in the frame corresponding to the opencv color detection detected color. We refine this mask and then use it for segmenting out the cloth from the frame. Using the above line, we combine masks generated for both the red color range.


In this color detection Python project, we are going to build an application through which you can automatically get the name of the color by clicking on them. So for this, we will have a data file that contains the color name and its values.

  • Smooth images makes it easier to detect colors.
  • First, since I opted for color detection over object detection, I thought it would be a good idea to blur or smooth each frame.
  • I used the in-built Gaussian Blurring method of OpenCV.

As an example, we’ll build a simple ESP32 Camera Web Server that includes color detection and tracking of a moving object. We shall begin experimenting with OpenCV by performing some basic operations on live image data. In this recipe, we shall perform some basic image processing to allow detection of different colored objects and track their location on screen. Inside the while loop we define the HSV ranges , we create the mask and we show only the object with the red color. Understanding the concepts of balancing these three elements, we can implement a basic object recognition based on colors. In this tutorial, I will explain in a few steps how to create a mask to balance the recognition of our object in real-time. Essentially, you have a rough segmentation of Nemo in HSV color space.

Calculate Distance To Get Color Name

You’ll notice there are a few stray pixels along the segmentation border, and if you like, you can use a Gaussian blur to tidy up the small false detections. RGB is considered an “additive” color space, and colors can be imagined as being produced from shining quantities of red, blue, and green light onto a black background.

We will be using these numbers with the converter to automatically generate the respective lower range and upper range HSV values for OpenCV. In the screenshot, a red cap is the target in an ordinary room environment with an ordinary 60W fluorescent lamp.

Multiple Color Detection Opencv Python

This method allows the RGB trackbars to be set with minimal effort. The image is 4channel; RGBA where A is the level of transparency. In this tutorial, A will set set at 100% opacity, namely 255. The code is based on the fact that, besides the A plane, the image opencv color detection has 3 color planes, RGB, each pixel in each plane having a value between 0 and 255. The high/low limits are applied to the corresponding color planes for each pixel. This guide introduces OpenCV.js and OpenCV tools for the ESP32 Camera Web Server environment.

The red cap reflects red, green, and blue but principally red. The method of detecting the amount of each reflected color will be described now.

Getting Started With Opencv Sdk

Set the thresholds for the HSV images for the range of your selected colors. Once you can define and distinguish the desired pixels representing the object you want to track, you can create your program to perform Cloud Application Security your desired functions. Next step is simplying setting a threshold for the color you require and masking the frame as per the threshold. This ensures any pixel not within your set threshold is simply ignored.

Pixels falling within your threshold are converted to white while those outside your set threshold are set to black. Python, To summarize, we have used the Hough line and circle transforms to detect objects with regular shapes. Contours and convexity can also be used for shape detection.