-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobject_colour.py
78 lines (53 loc) · 2.2 KB
/
object_colour.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
import cv2
ref_point = []
clipping = False
def click_and_crop(event, x, y, flags, param):
global ref_point, clipping
if event == cv2.EVENT_LBUTTONDOWN:
ref_point = [(x, y)]
clipping = True
elif event == cv2.EVENT_LBUTTONUP:
ref_point.append((x, y))
clipping = False
def find_object_colour(hue_sensitivity = 10, sat_sensitivity = 10, value_sensitivity = 10):
stream = cv2.VideoCapture(0)
while True:
_, frame = stream.read()
frame = cv2.flip(frame, 1)
original_frame = frame.copy()
cv2.putText(frame, 'Press <Space> to capture image', (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 3)
cv2.imshow('Image', frame)
k = cv2.waitKey(1) & 0xFF
if k == 32:
break
stream.release()
cv2.destroyWindow('Image')
frame = original_frame.copy()
cv2.namedWindow('Image')
cv2.setMouseCallback('Image', click_and_crop)
while True:
if len(ref_point) == 2:
cv2.rectangle(frame, ref_point[0], ref_point[1], (0, 255, 0), 2)
cv2.putText(frame, 'Select a ROI', (10, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
cv2.imshow('Image', frame)
key = cv2.waitKey(1) & 0xFF
if key == ord('r'):
frame = original_frame.copy()
elif key == 32:
break
cv2.destroyAllWindows()
blurred = cv2.GaussianBlur(original_frame, (11, 11), 0)
hsv = cv2.cvtColor(blurred, cv2.COLOR_BGR2HSV)
roi = hsv[min(ref_point[0][1], ref_point[1][1]) : max(ref_point[0][1], ref_point[1][1]),
min(ref_point[0][0], ref_point[1][0]) : max(ref_point[0][0], ref_point[1][0]), :]
mean = cv2.mean(roi)[: 3]
if mean[1] < 40 < mean[2]: # White object
lower_range = (0, 0, 0)
upper_range = (360, 40, 255)
elif mean[2] < 40: # Black object
lower_range = (0, 0, 0)
upper_range = (360, 255, 40)
else:
lower_range = (mean[0] - hue_sensitivity, mean[1] - sat_sensitivity, mean[2] - value_sensitivity)
upper_range = (mean[0] + hue_sensitivity, 255, 255)
return lower_range, upper_range