-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolorDetection.py
54 lines (43 loc) · 1.54 KB
/
colorDetection.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
import cv2
import numpy as np
frameWidth = 640
frameHeight = 480
cap = cv2.VideoCapture(0)
cap.set(3, frameWidth)
cap.set(4, frameHeight)
# cap.set(10, 150)
def empty(a):
pass
cv2.namedWindow("HSV")
cv2.resizeWindow("HSV", 640, 240)
cv2.createTrackbar("Hue Min", "HSV", 0, 179, empty)
cv2.createTrackbar("Hue Max", "HSV", 179, 179, empty)
cv2.createTrackbar("Sat Min", "HSV", 0, 255, empty)
cv2.createTrackbar("Sat Max", "HSV", 255, 255, empty)
cv2.createTrackbar("Val Min", "HSV", 0, 255, empty)
cv2.createTrackbar("Val Max", "HSV", 255, 255, empty)
while True:
success, img = cap.read()
imgHSV = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
h_min = cv2.getTrackbarPos("Hue Min", "HSV")
h_max = cv2.getTrackbarPos("Hue Max", "HSV")
s_min = cv2.getTrackbarPos("Sat Min", "HSV")
s_max = cv2.getTrackbarPos("Sat Max", "HSV")
v_min = cv2.getTrackbarPos("Val Min", "HSV")
v_max = cv2.getTrackbarPos("Val Max", "HSV")
lower = np.array([h_min, s_min, v_min])
upper = np.array([h_max, s_max, v_max])
mask = cv2.inRange(imgHSV, lower, upper)
result = cv2.bitwise_and(img, img, mask=mask)
# hacemos una transf al espacio de color bgr para poderla sumar al stack
mask = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR)
hStack = np.hstack([img, result, mask])
# cv2.imshow("Original", img)
# cv2.imshow("HSV Version", imgHSV)
# cv2.imshow('Mask', mask)
# cv2.imshow("Result", result)
cv2.imshow("Horizontal Stak", hStack)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()