Color Detection using OpenCV Python
Color detection is necessary to recognize objects, it is also used as a tool in various image editing and drawing apps. I write a simple Python code to detect the color in the image using OpenCV and pandas (python libraries).
What is Color Detection?
Color detection is the process of detecting the name of any color. Simple isn’t it? Well, for humans this is an extremely easy task but for computers, it is not straightforward. Human eyes and brains work together to translate light into color. Light receptors that are present in our eyes transmit the signal to the brain. Our brain then recognizes the color.
About the Python Project
If you want to jump straight into the code without explanation then you can scroll down to the bottom of this page, where the complete code is provided or click here for GitHub link.
In this color detection Python project, we are going to build an application through which you can automatically get the name of the color by clicking on them. So for this, we will have a data file that contains the color name and its values. Then we will calculate the distance from each color and find the shortest one.
The Data set
Colors are made up of 3 primary colors; red, green, and blue. In computers, we define each color value within a range of 0 to 255. So in how many ways we can define a color? The answer is 256*256*256 = 16,581,375. There are approximately 16.5 million different ways to represent a color. In our dataset, we need to map each color’s values with their corresponding names. But don’t worry, we don’t need to map all the values. We will be using a dataset that contains RGB values with their corresponding names.click here to download csv file . csv file includes 865 color names along with their RGB and hex values.
Steps for detecting color in an image
Here are the steps to build an application in Python that can detect colors:
1. Download and unzip the zip file
The project folder contains 3 files:
- Color detection.ipynb — main source code of our project.
- color palette.jpg — sample image for experimenting.
- Colors.csv — a file that contains our data set.
2. importing required package and load the image
import cv2
import numpy as np
import pandas as pdimg_path = “D://OpenCV//shape-detection//New folder//color palette.jpg”
img = cv2.imread(img_path)
3. Next, we read the CSV file with pandas
The pandas library is very useful when we need to perform various operations on data files like CSV. pd.read_csv() reads the CSV file and loads it into the pandas DataFrame. We have assigned each column with a name for easy accessing.
index=[“color”,”color_name”,”hex”,”R”,”G”,”B”]
csv = pd.read_csv(‘colors.csv’, names=index, header=None)
4. Set a mouse callback event on a window
First, we created a window in which the input image will display. Then, we set a callback function which will be called when a mouse event happens.
cv2.namedWindow(‘color detection’)
cv2.setMouseCallback(‘color detection’,draw_function)
5. Create the draw_function
It will calculate the rgb values of the pixel which we double click. The function parameters have the event name, (x,y) coordinates of the mouse position, etc. In the function, we check if the event is double-clicked then we calculate and set the r,g,b values along with x,y positions of the mouse.
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)
6. Calculate distance to get color name
We have the r,g and b values. Now, we need another function which will return us the color name from RGB values. To get the color name, we calculate a distance(d) which tells us how close we are to color and choose the one having minimum distance.
Our distance is calculated by this formula:
d = abs(Red — ithRedColor) + (Green — ithGreenColor) + (Blue — ithBlueColor)
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
7. Display image on the window
Whenever a double click event occurs, it will update the color name and RGB values on the window.
Using the cv2.imshow() function, we draw the image on the window. When the user double clicks the window, we draw a rectangle and get the color name to draw text on the window using cv2.rectangle and cv2.putText() functions.
while(1):cv2.imshow("color detection",img)
if (clicked):
cv2.rectangle(img,(20,20), (750,60), (b,g,r), -1) text = getColorName(r,g,b)+'R='+str(r)+'G='+ str(g) +'B='+ str(b)
cv2.putText(img, text,(50,50),2,0.8, (255,255,255),2,cv2.LINE_AA) if(r+g+b>=600):
cv2.putText(img, text,(50,50),2,0.8,(0,0,0),2,cv2.LINE_AA)
clicked=Falseif cv2.waitKey(20) & 0xFF ==27:
break
cv2.destroyAllWindows()
Full Code
<script src=”https://gist.github.com/GeekyPRAVEE/fec689243de555827affc0c11a62fe86.js"></script>
output
Github Repo Link — https://github.com/GeekyPRAVEE/OpenCV-Projects/tree/master/color%20detection
you can find me on
Instagram @programming_fever
Facebook @GeekyPRAVE
Twitter @GeekyPRAVEE