Skip to content

Commit aa4953b

Browse files
Add files via upload
1 parent 7d29109 commit aa4953b

File tree

6 files changed

+935
-0
lines changed

6 files changed

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

0 commit comments

Comments
 (0)