Colour Detection using Python and pandas, Cv2 librarys

Colour Detection using Python and pandas, Cv2 librarys.

Problem:-

We have 16 million colors based on the different RGB(R = Red, G = Green, B = Blue) color values.
Combination of the red, green and blue are called colors.
Each color has integer values between 0 and 255.
For example blue have (0,0 255).
If we multiply 255*255*255.Then answer are approximately 16.5 million.
That's why, we don't remember the name of colors.
It is very difficult to get the color name.

Solution:-

I am writing python code to over come this problem.

Prerequisites:-

Before starting coding,you must need basic knowledge of Python and library.
1) Data manipulation and analysis library of Python  that is pandas.  
2) Computer vision library of Python That is Cv2. 

project folder structure it has 3 files:-

1) color_detection.py – main source code file.

2) image01.jpg – image for testing.

3) wikipedia_color_names.csv – a file that contains our dataset.Can you download CSV file from wikipedia.

Start Creating Projects:-



1) First line,i am importing python library that is pandas and Cv2.


2) In second line i am declaring variables. Which is using in this  project.


 

 

3) In third line, i am reading CSV file.Which has all the name of colors with there values.After reading the file, i saved the CSV file data in dataFrame. Which nothing more than a variable.


4) In fourth and fifth line, i am doing data cleaning and data processing.First i am removing columns, which is not used in this project.
After that i renamed some of columns name. I used inplace True because it save the
dataFrame with new data. Axis 1 is showing that i am using columns,by default
it has 0.Which stand for row values.


5) Sixth line, i am reading image.For this i used cv2.imread() function and pass the image name variable.Which is already declared.


 6) Seventh line,i created user defined function.Which give me the color name of the selected area of image.abs() function is used to get the absolute value of the RGV.


 

 

 

 

 

 

7) Eighth line,is created user defined function.Which gives the x y coordinates of selected area of the images on mouse click event.

8) Ninth line,it is the main function of the project. Or we can say it is starting point of the project.
In this, i am using cv2.nameWindow() function to set the name of the output window.
In next line i call the cv2.setMouseCallback() function, check the mouse click event and get the values.
If event value is true then, i use the cv2.rectangle() function to draw the rectangle over the image. With color name.
Write the color name, i use cv2.putText() function. In this function i need to pass the image and the values of the color name.
To get the name of the color i used user defined function getColorNane().Which is created on seventh line.
For final output, i used cv2.imshow() function.It takes the output windows name and the final image.
For stop the program i am using key values to check the values. I used cv2.waitkey() function.It has a value 20, which is ESC key Value.
At last i used cv2.destroyallwindows() function to destroys all the windows we created.

Conclusion:-

In this project, we learned how to extract color RGB values and the color name of selected area.
With the help of mouse click event we extract the value of the color and read CSV files to get the name of the color.

You can download project code from my GitHub Repository .

 Video on my YouTube channel is Espresso Coding Studio.


Post a Comment

Previous Post Next Post