-
Notifications
You must be signed in to change notification settings - Fork 0
/
callibrate.py
84 lines (79 loc) · 2.94 KB
/
callibrate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import cv2
import numpy as np
import pickle
def mouseRGB(event,x,y,flags,param):
hsv_1 = []
hsv_2 = []
hsv_3 = []
global dict_hsv
if(event == cv2.EVENT_LBUTTONDOWN):
k = cv2.waitKey(0) & 0xFF
if (k == ord('r')):
print("Red Color Captured!!")
hsv_1.append(image[y,x,0])
hsv_1.append(image[y,x,1])
hsv_1.append(image[y,x,2])
colors = image[y,x]
dict_hsv['hsv_1'] = {"lower_bound":[abs(i-10) for i in hsv_1],"upper_bound":[i+10 for i in hsv_1]}
hsv_value= np.uint8([[[i for i in hsv_1]]])
hsv = cv2.cvtColor(hsv_value,cv2.COLOR_BGR2HSV)
print ("HSV : " ,hsv)
print("Red: ",hsv_1[2])
print("Green: ",hsv_1[1])
print("Blue: ",hsv_1[0])
print("BRG Format: ",colors)
print("Coordinates of pixel: X: ",x,"Y: ",y)
elif (k == ord('g')):
print("green Color Captured!!")
hsv_2.append(image[y,x,0])
hsv_2.append(image[y,x,1])
hsv_2.append(image[y,x,2])
colors = image[y,x]
dict_hsv['hsv_2'] = {"lower_bound":[abs(i-10) for i in hsv_2],"upper_bound":[i+10 for i in hsv_2]}
hsv_value= np.uint8([[[i for i in hsv_2]]])
hsv = cv2.cvtColor(hsv_value,cv2.COLOR_BGR2HSV)
print ("HSV : " ,hsv)
print("Red: ",hsv_2[2])
print("Green: ",hsv_2[1])
print("Blue: ",hsv_2[0])
print("BRG Format: ",colors)
print("Coordinates of pixel: X: ",x,"Y: ",y)
elif (k == ord('y')):
print("yellow Color Captured!!")
hsv_3.append(image[y,x,0])
hsv_3.append(image[y,x,1])
hsv_3.append(image[y,x,2])
colors = image[y,x]
dict_hsv['hsv_3'] = {"lower_bound":[abs(i-10) for i in hsv_3],"upper_bound":[i+10 for i in hsv_3]}
hsv_value= np.uint8([[[i for i in hsv_3]]])
hsv = cv2.cvtColor(hsv_value,cv2.COLOR_BGR2HSV)
print ("HSV : " ,hsv)
print("Red: ",hsv_3[2])
print("Green: ",hsv_3[1])
print("Blue: ",hsv_3[0])
print("BRG Format: ",colors)
print("Coordinates of pixel: X: ",x,"Y: ",y)
else:
print("Invalid option")
hsv_1 = list()
hsv_2 = list()
hsv_3 = list()
dict_hsv = dict()
flag_r,flag_y,flag_g = (0,0,0)
cap = cv2.VideoCapture(1)
if cap.read()[0]==False:
cap = cv2.VideoCapture(0)
while(1):
_, image = cap.read()
cv2.namedWindow('mouseRGB')
cv2.setMouseCallback('mouseRGB',mouseRGB)
cv2.imshow('mouseRGB',image)
if cv2.waitKey(1) & 0xFF == 27:
with open("range.pickle","wb") as file:
pickle.dump(dict_hsv,file)
break
else:
pass
print(dict_hsv)
cv2.destroyAllWindows()
cap.release()