0% found this document useful (0 votes)
31 views3 pages

Exp3 2

This document describes a Python program that uses OpenCV to detect colors in an image. It explains how the program works by converting the image to HSV color space and creating a binary mask to identify pixels within a specified color range. The program outputs an image with only the detected color pixels, allowing analysis based on color characteristics. In conclusion, the experiment presents a practical approach to color detection and provides a tool for applications like tracking, segmentation and scene analysis.

Uploaded by

Piyush Kushwah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views3 pages

Exp3 2

This document describes a Python program that uses OpenCV to detect colors in an image. It explains how the program works by converting the image to HSV color space and creating a binary mask to identify pixels within a specified color range. The program outputs an image with only the detected color pixels, allowing analysis based on color characteristics. In conclusion, the experiment presents a practical approach to color detection and provides a tool for applications like tracking, segmentation and scene analysis.

Uploaded by

Piyush Kushwah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Piyush Kushwah (0901AM211036)

Experiment - 3
Aim: Write a program for color detection.

Description: The provided Python program detect_color utilizes the OpenCV library to read
an image from a given file path. It then converts the image from the RGB color space to the
HSV (Hue, Saturation, Value) color space, which is more suitable for color detection tasks.
The lower and upper bounds of the color to be detected are defined in the HSV color space.

Using the specified lower and upper bounds, the program creates a binary mask that
identifies pixels within the specified color range. This mask is then applied to the original
image using bitwise AND operation, resulting in an image where only the pixels within the
specified color range are retained, while all other pixels are set to zero.

The resulting image, containing only the detected color, is returned as output. This allows
users to easily identify and analyze regions of interest within the image based on their color
characteristics.

import cv2
from google.colab.patches import cv2_imshow
import numpy as np

def detect_color(image_path, color_lower, color_upper):


# Read the image
image = cv2.imread(image_path)
# Convert image to HSV color space
hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
# Define the lower and upper bounds for the color to detect
lower_color = np.array(color_lower)
upper_color = np.array(color_upper)
# Create a mask using the specified lower and upper bounds
mask = cv2.inRange(hsv_image, lower_color, upper_color)
# Apply the mask to the original image
result = cv2.bitwise_and(image, image, mask=mask)
return result

!wget -O image.jpg
https://imgeng.jagran.com/images/2023/oct/Jarvo1696758979907.jpg

# Input image path


image_path = "/content/image.jpg"
# Define the lower and upper bounds for the color to detect
# Example: detecting blue color
blue_lower = [100, 50, 50] # Lower bound for blue color in HSV
blue_upper = [130, 255, 255] # Upper bound for blue color in HSV
# Detect the specified color in the image
color_detected_image = detect_color(image_path, blue_lower,
blue_upper)
# Display the original and color-detected images
cv2_imshow(cv2.putText(cv2.imread(image_path), "Original Image",
(10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2))
cv2_imshow(cv2.putText(color_detected_image, "Color Detected Image",
(10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2))
cv2.waitKey(0)
cv2.destroyAllWindows()

Conclusion: In conclusion, this experiment presents a practical approach to color detection


using Python and OpenCV. The program effectively identifies specific colors within an image
by isolating pixels within a specified color range in the HSV color space. By leveraging the
capabilities of OpenCV, the program provides a versatile tool for various applications such as
object tracking, image segmentation, and scene analysis. This experiment lays the
foundation for further exploration and experimentation in the field of computer vision and
image processing, offering insights into the implementation of color-based image analysis
techniques.

You might also like