Detection of a specific color(blue here) using OpenCV with Python?

Image processing and color detection might seem complex at first, but OpenCV makes it straightforward. In this tutorial, we'll learn how to detect specific colors (blue in this case) using Python and OpenCV.

Understanding Color Models

Computers represent colors using color models that describe colors as tuples of numbers. The two most common models are RGB (Red, Green, Blue) and HSV (Hue, Saturation, Value).

RGB Color Model

RGB represents colors as three components, each ranging from 0 to 255. The tuple (0, 0, 0) represents black, while (255, 255, 255) represents white. For pure blue, the values would be (0, 0, 255).

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

HSV Color Model

HSV uses three parameters: Hue (color/shade), Saturation (color intensity), and Value (brightness). HSV is often preferred for color detection because it separates color information from lighting conditions.

Setting Up the Environment

First, let's import the required libraries and load our test image ?

import cv2
import numpy as np

# Load the image (replace with your image path)
img = cv2.imread('tiger.jpg')

Converting to HSV Color Space

We convert the image to HSV because it makes color detection more robust to lighting variations ?

# Convert BGR to HSV
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

Defining Color Range

Next, we define the lower and upper bounds for the blue color we want to detect. These HSV values represent the range of blue colors ?

# Define range for blue color in HSV
lower_blue = np.array([110, 50, 50])
upper_blue = np.array([130, 255, 255])

Creating the Color Mask

The mask isolates pixels within our specified color range. White pixels indicate detected blue areas, while black pixels represent other colors ?

# Create mask for blue color
mask = cv2.inRange(hsv, lower_blue, upper_blue)

Complete Blue Detection Program

import cv2
import numpy as np

# Load the image
img = cv2.imread('tiger.jpg')

# Convert BGR to HSV
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

# Define range for blue color in HSV
lower_blue = np.array([110, 50, 50])
upper_blue = np.array([130, 255, 255])

# Create mask for blue color
mask = cv2.inRange(hsv, lower_blue, upper_blue)

# Display original image and mask
cv2.imshow('Original Image', img)
cv2.imshow('Blue Mask', mask)

# Wait for key press and cleanup
while True:
    k = cv2.waitKey(5) & 0xFF
    if k == 27:  # ESC key
        break

cv2.destroyAllWindows()

Understanding the Results

The program displays two windows: the original image and the blue color mask. In the mask, white areas represent detected blue pixels, while black areas show non-blue regions. You might notice small black spots within blue areas ? this is noise that can be reduced using morphological operations like erosion and dilation.

Tips for Better Color Detection

Adjust HSV ranges: Different lighting conditions may require tweaking the lower and upper bounds for optimal detection.

Noise reduction: Use morphological operations to clean up the mask and remove small noise artifacts.

Multiple color ranges: Some colors may require multiple HSV ranges for complete detection due to hue wrap-around.

Conclusion

Color detection with OpenCV involves converting images to HSV color space and creating masks using cv2.inRange(). This technique is fundamental for many computer vision applications including object tracking and image segmentation.

Updated on: 2026-03-25T05:42:41+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements