Python OpenCV Project – Color Detection
FREE Online Courses: Elevate Your Skills, Zero Cost Attached - Enroll Now!
Color Detection using Python OpenCV is a computer vision technique that involves the identification and analysis of colors within digital images or video streams. OpenCV, a powerful open-source library, facilitates color recognition by converting images into a format where colors can be analyzed, allowing applications in fields like image processing, object tracking, and more. This process aids in automating tasks, enhancing visual understanding, and enabling a wide range of real-world applications.
Webcolors library
The webcolors library is a Python module that provides a comprehensive database of color names and their corresponding representations in various color systems, including RGB and HEX. It allows easy conversion between color names and their corresponding values, making it valuable for tasks like color recognition, web development, and data visualization. This library simplifies color management in Python applications and supports CSS3 color name.
Prerequisites For Python OpenCV Color Detection Project
To work with color detection using Python OpenCV, a solid understanding of Python and the OpenCV library is essential, along with the necessary system requirements.
- Python 3.7 (64-bit) and above
- Any Python editor (VS code, Pycharm, etc.)
Download Python OpenCV Color Detection Project
Please download the source code of the Python OpenCV Color Detection Project: Python OpenCV Color Detection Project Code.
Installation
Open windows cmd as administrator
1. Install OpenCV library.
pip install opencv-python
2. Install webcolors library.
pip install webcolors
Let’s Implement
1. Import all the packages.
import cv2 import numpy as np from webcolors import CSS3_HEX_TO_NAMES, hex_to_rgb
2. It calculates the sum of absolute difference between corresponding elements of two RGB color values.
def Col_diff(rgb_1, rgb_2):
return sum((abs(a-b) for a, b in zip(rgb_1, rgb_2)))
3. Define the function and pass the parameters.
def event_click(event, x, y, flags, param):
4. It checks if the mouse event is a left button-down click event.
if event == cv2.EVENT_LBUTTONDOWN:
5. It extracts BGR color values from an image at the mouse click position, converts them to HSV format and initializes variables for color name detection.
b, g, r = image[y, x]
rgb_color = (r, g, b)
hsv_color = cv2.cvtColor(np.uint8([[rgb_color]]), cv2.COLOR_RGB2HSV)[0][0]
close_col_name = None
close_col_diff = float('inf')
6. It iterates through predefined colors, calculates differences with the clicked RGB color and finds the closest color name based on the smallest difference.
for hex_color, color_name in CSS3_HEX_TO_NAMES.items():
color_rgb = hex_to_rgb(hex_color)
difference = Col_diff(rgb_color, color_rgb)
if difference < close_col_diff:
close_col_diff = difference
close_col_name = color_name
7. It creates a copy of the image and positions the color name text to fit within the window, updating the image with the text for display.
image_copy = image.copy()
text_position = (x, y)
text_size, _ = cv2.getTextSize(f'Color: {close_col_name}', cv2.FONT_HERSHEY_SIMPLEX, 1, 2)
if text_position[0] + text_size[0] > image_copy.shape[1]:
text_position = (image_copy.shape[1] - text_size[0], text_position[1])
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(image_copy, f'Color: {close_col_name}', text_position, font, 1, (0, 0, 255), 2)
cv2.imshow('ProjectGurukul', image_copy)
image = image_copy
Note:- write steps 4-7 under step 3 function.
8. It reads an image, displays it in an OpenCV window and allows mouse interaction to detect and display color names. After pressing any, it closes the window.
image = cv2.imread('im3.jpg')
cv2.imshow('ProjectGurukul', image)
cv2.setMouseCallback('ProjectGurukul', event_click)
cv2.waitKey(0)
cv2.destroyAllWindows()
Python OpenCV Color Detection Output
Python OpenCV Color Detection Video Output
Conclusion
In conclusion, color detection using Python OpenCV is a valuable technique enabling precise color identification in digital images and video streams. Its versatility spans diverse fields, from image processing to object tracking, thereby enhancing automation and visual comprehension. This robust tool empowers applications with the ability to interpret and respond to color information, opening doors to innovative solutions in numerous domains. Its impact is undeniable, contributing to the evolution of computer vision and the seamless integration of color-based insights into our digital world.


