OpenCV Python Color Detection Example

In this OpenCV Python article i want to show a basics Color Detection Example in OpenCV. firs of all let’s talk about Color Detection in OpenCV.

 

 

What is OpenCV Python Color Detection?

Color detection in OpenCV Python involves identifying and isolating specific colors or ranges of colors inside an image or video stream. This process typically involves the following steps:

  1. Image Loading: Load an image or capture frames from a video source using OpenCV’s cv2.imread() function or video capture methods.
  2. Color Space Conversion: Convert the image from the default BGR (Blue-Green-Red) color space to another color space such as HSV (Hue-Saturation-Value) or RGB (Red-Green-Blue). HSV color space is commonly used for color detection because it separates color information from intensity information.
  3. Thresholding: Apply a thresholding technique for creating a binary mask, where pixels that fall inside the specific color range are set to white (255) and pixels outside the range are set to black (0). This step helps isolate the desired color from the rest of the image.
  4. Morphological Operations (Optional): Perform morphological operations such as erosion and dilation to further refine the binary mask and remove noise or small unwanted objects.
  5. Contour Detection (Optional): Find contours in the binary mask to identify the shapes or regions corresponding to the detected color blobs.
  6. Object Detection and Analysis (Optional): Analyze the detected color regions, such as calculating their centroids, areas, bounding boxes, or applying further processing based on specific requirements.

 

 

 

How does color work on a computer?

So we represent colors on a computers by color-space or color models which basically describes range of colors as tuples of numbers. Instead of going for each color, we’ll discuss most common color-space we use .i.e. RGB(Red, Green, Blue) and HSV (Hue, Saturation, Value).

RGB basically describes color as a tuple of three components. Each component can take a value between 0 and 255, where the tuple (0, 0, 0) represents black and (255, 255, 255) represents white. For example, if we were to show a pure blue pixel on-screen, then the R value would be 0, the G value would be 0, and the B value would be 255.

 

 

Below are a few more examples of colors in RGB:

Color RGB value
Red 255, 0, 0
Orange 255, 128, 0
Pink 255, 153, 255

 

 

 

OpenCV Python Color Detection Example

Let’s create an Example, So now this is the complete code for OpenCV Python Color Detection.

Now let’s describe the code one by one:

  1. Import Libraries: The code starts by importing the necessary libraries:
  2. Load the Image: Our code loads an image named  using the cv2.imread() function. This image will be processed to detect the red color.
  3. Convert to HSV: The loaded image is converted from default BGR color space to the HSV (Hue-Saturation-Value) color space using cv2.cvtColor() function. HSV is preferred for color detection tasks as it separates the color information from intensity information.
  4. Define Color Range: The lower and upper bounds of the red color range in HSV color space are defined using NumPy arrays lower_range and upper_range.
  5. Create Mask: binary mask is created by thresholding the HSV image using cv2.inRange() function. this mask contains white pixels where the color falls inside the specific range and black pixels otherwise.
  6. Apply Mask: The mask is applied to the original image using bitwise AND operation (cv2.bitwise_and()), resulting in an image where only the red color regions are visible, while the rest of the image is blacked out.
  7. Display Images: Matplotlib’s plt.subplot() function is used to create a grid of subplots for displaying the original image.

 

 

 

 

OK first of all you need to have an image in your working directory iam using this image, you can get this image from below.

OpenCV Color Detection
OpenCV Color Detection

 

 

 

 

 

Run the complete code and this will be the result

OpenCV Python Color Detection Example
OpenCV Python Color Detection Example

Subscribe and Get Free Video Courses & Articles in your Email

 

1 thought on “OpenCV Python Color Detection Example”

  1. hello!!
    I have a question, once that the color is detected, how could i save a variable acknowledging that the color has been detected? I want the code to enter a loop if the color is detected, but i dont know how to convert it to a variable, thank you!!

    Reply

Leave a Comment

Share via
Copy link
Powered by Social Snap
×