Color Detection is the process of detecting name of any color. Simple isn't? Well for humans it's an extremely easy task but for computers it's not that straightforward. Human eyes and brain work together to translate light into color. Light receptors that are present in our eyes transmit the signal to the brain. Our brain than recognizes the color. We'll be using somewhat same strategy to detect color in computers.
In this project we built an application through which we can automatically get the name of the color by clicking on them. For that we need a dataset that contains the color name and its values.
We represent colors on a computers by color-space or color models which basically describes range of colors as tuples of numbers.
Instead of going for each color, we’ll discuss most common color-space we use .i.e. RGB(Red, Green, Blue) and HSV (Hue, Saturation, Value).
RGB basically describes color as a tuple of three components. Each component can take a value between 0 and 255, where the tuple (0, 0, 0) represents black and (255, 255, 255) represents white. For example, if we were to show a pure blue pixel on-screen, then the R value would be 0, the G value would be 0, and the B value would be 255.
Steps for Building the Project– Color Detection
Here are the steps to build an application in Python that can detect colors:
1. Download and unzip the zip file for dataset.
Color Detection Zip File
2. Importing Libraries and taking an image from the user
In below line of code, first two lines handle all the imports. In third line, I’m importing imutils module, which helps in resizing images and finding the range of colors. In line 4 we’ve open the image.
import numpy as np
img = cv2.imread('color2.jpg')
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. We have converted the image to an hsv image because hsv helps to differentiate intensity from color.
#Reading csv file with pandas and giving names to each column
csv = pd.read_csv('colors.csv', names=index, header=None)
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
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.
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)
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"]))
minimum = d
cname = csv.loc[i,"color_name"]
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.
8. Run Python File
The beginner Python project is now complete, you can run the Python file from the command prompt. Make sure to give an image path using ‘-i’ argument. If the image is in another directory, then you need to give full path of the image:
|python color_detection.py -i <add your image path here>|
In this Python project with source code, we learned about colors and how we can extract color RGB values and the color name of a pixel
INTRODUCTION of PROJECT DEVELOPER
The color detection project is developed by Jagrati from Maharani Girls Engineering College, Jaipur. This project is developed during Goeduhub online summer training in Artificial Intelligence, Machine Learning and Deep Learning.