Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a57b79d

Browse files
Merge pull request avinashkranjan#537 from deepshikha007/issue-500
Added the python-script for Colour detection project
2 parents 61874e3 + 77801a8 commit a57b79d

File tree

4 files changed

+949
-0
lines changed

4 files changed

+949
-0
lines changed

‎Color_detection/Color_detection.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import pandas as pd
2+
import cv2
3+
4+
img_path = './Color_detection/colorpic.jpg'
5+
csv_path = './Color_detection/colors.csv'
6+
7+
#Reading csv file with pandas and giving names to each column
8+
index = ['color', 'color_name', 'hex', 'R', 'G', 'B']
9+
df = pd.read_csv(csv_path, names=index, header=None)
10+
11+
#Reading the image with opencv
12+
img = cv2.imread(img_path)
13+
img = cv2.resize(img, (800, 600))
14+
15+
#declaring global variables
16+
clicked = False
17+
r = g = b = xpos = ypos = 0
18+
19+
#function to calculate minimum distance from all colors and get the most matching color
20+
def get_color_name(R,G,B):
21+
minimum = 10000
22+
for i in range(len(df)):
23+
d = abs(R - int(df.loc[i, 'R'])) + abs(G - int(df.loc[i, 'G'])) + abs(B - int(df.loc[i, 'B']))
24+
if d <= minimum:
25+
minimum = d
26+
cname = df.loc[i, 'color_name']
27+
28+
return cname
29+
30+
#function to get x,y coordinates of mouse double click
31+
def draw_function(event, x, y, flags, param):
32+
if event == cv2.EVENT_LBUTTONDBLCLK:
33+
global clicked, r, g, b, xpos, ypos
34+
clicked = True
35+
xpos = x
36+
ypos = y
37+
b, g, r = img[y, x]
38+
b = int(b)
39+
g = int(g)
40+
r = int(r)
41+
42+
cv2.namedWindow('image')
43+
cv2.setMouseCallback('image', draw_function)
44+
45+
while True:
46+
cv2.imshow('image', img)
47+
if clicked:
48+
# cv2.rectangle(image, startpoint, endpoint, color, thickness)-1 fills entire rectangle
49+
cv2.rectangle(img, (20, 20), (600, 60), (b, g, r), -1)
50+
51+
# Creating text string to display( Color name and RGB values )
52+
text = get_color_name(r, g, b) + ' R=' + str(r) + 'G=' + str(g) + ' B=' + str(b)
53+
54+
# cv2.putText(img,text,start,font(0-7),fontScale,color,thickness,lineType )
55+
cv2.putText(img, text, (50,50), 2, 0.8, (255, 255, 255), 2, cv2.LINE_AA)
56+
57+
# For very light colours we will display text in black colour
58+
if (r+ g+ b>= 600):
59+
cv2.putText(img, text, (50, 50), 2, 0.8, (0, 0, 0), 2, cv2.LINE_AA)
60+
clicked = False
61+
62+
# Break the loop when user hits 'esc' key
63+
if cv2.waitKey(20) & 0xFF == 27:
64+
break
65+
66+
67+
cv2.destroyAllWindows()

‎Color_detection/Readme.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
# Colour Detection using Pandas and OpenCV
3+
4+
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.
5+
6+
# Steps Used
7+
- ##Importing required package and load the image.</br>
8+
Package used - Pandas and cv2 for OpenCV bindings.</br>
9+
- ##Reading the CSV file with pandas.</br>
10+
- ##Set a mouse callback event on a window</br>
11+
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.</br>
12+
**draw_function** -
13+
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.</br>
14+
- ##Calculate distance to get color name</br>
15+
d = abs(Red — ithRedColor) + (Green — ithGreenColor) + (Blue — ithBlueColor)</br>
16+
- ##Display image on the window</br>
17+
Whenever a double click event occurs, it will update the color name and RGB values on the window.

‎Color_detection/colorpic.jpg

1.22 MB
Loading[フレーム]

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /