0% found this document useful (0 votes)
19 views7 pages

SEPM 11 (Case Study)

This document describes a Python-based application for color detection using OpenCV and Pandas, allowing users to identify colors in images through interactive clicks. The system processes RGB values from an image, compares them to a predefined dataset of colors, and displays the closest matching color name. The application is useful in various fields such as graphic design, education, and accessibility for color-blind individuals.

Uploaded by

paritosh8514
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)
19 views7 pages

SEPM 11 (Case Study)

This document describes a Python-based application for color detection using OpenCV and Pandas, allowing users to identify colors in images through interactive clicks. The system processes RGB values from an image, compares them to a predefined dataset of colors, and displays the closest matching color name. The application is useful in various fields such as graphic design, education, and accessibility for color-blind individuals.

Uploaded by

paritosh8514
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/ 7

Color Detection Using Python

Abstract
Color is a critical attribute in image processing and computer vision, playing a significant role in
recognition, tracking, and segmentation tasks. This paper presents a Python-based application
that detects and identifies the name of a color in an image upon user interaction. The proposed
system employs OpenCV for image manipulation and Pandas for color data handling. Upon
clicking any point in an image, the system detects the pixel's RGB value and maps it to the closest
matching color name from a predefined dataset. The implementation demonstrates the integration
of computer vision techniques with real-time user interaction, offering practical utility for
educational, design, and accessibility applications.

Keywords
Color Detection, Computer Vision, OpenCV, Python, Pandas, RGB, Image Processing, Dataset
Matching

I. Introduction
Color recognition is a foundational task in various domains such as image editing, digital design,
robotics, and accessibility tools for the visually impaired. Humans can recognize and name colors
easily, but training machines to do the same requires the interpretation of pixel values and
comparison with predefined color names. This project addresses this challenge by building a
desktop application using Python libraries to recognize and display color names based on user
clicks within an image.
The growing accessibility of powerful libraries such as OpenCV and Pandas makes it possible to
develop such intuitive tools with minimal resources. This paper explores the implementation of a
simple yet effective color detection application that utilizes a color dataset to recognize color
names by matching RGB values.

II. Literature Review


Several tools and applications have been developed for color recognition. Traditional approaches
rely on color histograms and clustering techniques such as k-means. More advanced systems
utilize machine learning and deep learning for object and color detection. However, these
solutions may be computationally expensive and over-engineered for tasks that require basic color
identification.
The system described in this paper relies on a simpler and more interpretable approach—
comparing pixel RGB values to a dataset of labeled colors and calculating the Euclidean distance
to determine the closest match.

III. Methodology
A. Dataset
The dataset used contains 865 color entries, each with a unique name, hexadecimal code, and
RGB values. It serves as a reference point for identifying colors. This dataset is processed using
the Pandas library, which allows efficient searching and matching operations.
B. Tools and Technologies
• Python 3.x: The programming language used.
• OpenCV: Utilized for image processing tasks including image loading and mouse event
handling.
• Pandas: Used for loading and handling the CSV file containing color data.
• NumPy: Employed for numerical operations like calculating Euclidean distance.
C. Algorithm
1. Image Loading: The input image is loaded using OpenCV’s cv2.imread() function.
2. Mouse Callback Function: A function is registered to capture (x, y) coordinates on
double-click.
3. Pixel Color Retrieval: The pixel's RGB value is fetched using the image matrix.
4. Distance Calculation: Euclidean distance is calculated between the clicked RGB values
and each entry in the dataset.
5. Name Mapping: The minimum distance identifies the closest matching color name.
6. Display: The color name and its RGB values are displayed on the image near the clicked
point.
IV. Results and Discussion
The application accurately detects colors for a wide range of images under standard lighting
conditions. Testing with various images showed that the color names displayed were sufficiently
accurate for practical usage.
However, the precision of color matching is dependent on the dataset. Some perceptually similar
colors may map to names that are not intuitively expected by users. Enhancing the dataset with
more descriptive or human-perceived color names could improve the user experience.
Furthermore, lighting and image quality can affect the perceived color, though this project works
reliably with standard digital images.

V. Applications
• Graphic Design: Identifying and matching color tones.
• Educational Tools: Teaching color theory and computer vision basics.
• Accessibility: Assisting color-blind users in identifying colors.
• Image Analysis: Used in research and visual content curation.

VI. Conclusion
The proposed system offers a lightweight and effective method for real-time color detection using
Python. By combining image processing techniques with a structured dataset, the tool can assist
users in quickly identifying colors within any digital image. The approach is extendable and can
serve as a base for more complex computer vision applications.

References
[1] Bradski, G. "The OpenCV Library." Dr. Dobb’s Journal of Software Tools, 2000.
[2] McKinney, W. "Data Structures for Statistical Computing in Python." Proceedings of the 9th
Python in Science Conference, 2010.
Color Detector Using Python
Program:
import cv2
import numpy as np
import pandas as pd
import argparse

#Creating argument parser to take image path from command line


ap = argparse.ArgumentParser()
ap.add_argument('-i', '--image', required=True, help="Image Path")
args = vars(ap.parse_args())
img_path = args['image']

#Reading the image with opencv


img = cv2.imread(img_path)

#declaring global variables (are used later on)


clicked = False
r = g = b = xpos = ypos = 0

#Reading csv file with pandas and giving names to each column
index=["color","color_name","hex","R","G","B"]
csv = pd.read_csv('colors.csv', names=index, header=None)

#function to calculate minimum distance from all colors and get the most matching color
def getColorName(R,G,B):
minimum = 10000
for i in range(len(csv)):
d = abs(R- int(csv.loc[i,"R"])) + abs(G- int(csv.loc[i,"G"]))+ abs(B- int(csv.loc[i,"B"]))
if(d<=minimum):
minimum = d
cname = csv.loc[i,"color_name"]
return cname

#function to get x,y coordinates of mouse double click


def draw_function(event, x,y,flags,param):
if event == cv2.EVENT_LBUTTONDBLCLK:
global b,g,r,xpos,ypos, clicked
clicked = True
xpos = x
ypos = y
b,g,r = img[y,x]
b = int(b)
g = int(g)
r = int(r)

cv2.namedWindow('image')
cv2.setMouseCallback('image',draw_function)

while(1):

cv2.imshow("image",img)
if (clicked):
#cv2.rectangle(image, startpoint, endpoint, color, thickness)-1 fills entire rectangle
cv2.rectangle(img,(20,20), (750,60), (b,g,r), -1)

#Creating text string to display( Color name and RGB values )


text = getColorName(r,g,b) + ' R='+ str(r) + ' G='+ str(g) + ' B='+ str(b)

#cv2.putText(img,text,start,font(0-7),fontScale,color,thickness,lineType )
cv2.putText(img, text,(50,50),2,0.8,(255,255,255),2,cv2.LINE_AA)

#For very light colours we will display text in black colour


if(r+g+b>=600):
cv2.putText(img, text,(50,50),2,0.8,(0,0,0),2,cv2.LINE_AA)

clicked=False

#Break the loop when user hits 'esc' key


if cv2.waitKey(20) & 0xFF ==27:
break

cv2.destroyAllWindows()
Output:
Open command prompt from the project folder itself.

It opens the image.

Double-click on the image to detect the color on the double-clicked point

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy