-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenCV16a_HSV_C920.py
117 lines (95 loc) · 4.14 KB
/
openCV16a_HSV_C920.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#Program will read a pic (smarties.png) from file, then by converting the image into the
#HSV color space, identify the various colors in the image. Trackbars are used to make
#colour selection easier.
#As in the HSV colour space, red goes from roughly 160 - 20, we ned to use 2 trackbars and
#seperate arrays, as we need to get the values 160-179 and 0 to 20, then combine them into one array.
#same as openCV16a, except now the user can click on individual pixels in the "hsv" image to return
#the HSV and BGR values of that pixel.
#same as previous, except uses C920
import cv2
import numpy as np #numpy is the mathematical library that works with arrays.
#Dummy function as createTrackbar function requires it.
def nothing(x):
pass
#When the mouse click event is dtected, this function is called.
def click(event, x, y, flags, params):
if event == cv2.EVENT_LBUTTONDOWN:
blue = frame[y,x,0]
green = frame[y,x,1]
red = frame[y,x,2]
hue = hsv[y,x,0]
sat = hsv[y,x,1]
value = hsv[y,x,2]
print("BGR Values: ", blue,green,red)
print("HSV Values: ", hue,sat,value)
#Setting up the camera
cam = cv2.VideoCapture(1)
cam.set(3,640)
cam.set(4,480)
cv2.namedWindow('hsv')
cv2.setMouseCallback('hsv', click)
#Setup trackbars that allow us to quickly change HSV values
cv2.namedWindow('Trackbars')
cv2.moveWindow('Trackbars', 1320, 0)
cv2.createTrackbar('hueLow', 'Trackbars', 50, 179, nothing)
cv2.createTrackbar('hueHigh', 'Trackbars', 100, 179, nothing)
cv2.createTrackbar('hueLow2', 'Trackbars', 50, 179, nothing)
cv2.createTrackbar('hueHigh2', 'Trackbars', 100, 179, nothing)
cv2.createTrackbar('satLow', 'Trackbars', 100, 255, nothing)
cv2.createTrackbar('satHigh', 'Trackbars', 255, 255, nothing)
cv2.createTrackbar('valueLow', 'Trackbars', 100, 255, nothing)
cv2.createTrackbar('valueHigh', 'Trackbars', 255, 255, nothing)
while True:
ret, frame = cam.read()
#frame = cv2.imread('smarties.png')
cv2.imshow('smarties',frame)
cv2.moveWindow('smarties', 0, 0)
#convert original image (frame) from BGR 2 HSV
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
#Show HSV Image
cv2.imshow('hsv', hsv)
cv2.moveWindow('hsv', 705, 0)
#Get values from Trackbars
hueLow = cv2.getTrackbarPos('hueLow', 'Trackbars')
hueHigh = cv2.getTrackbarPos('hueHigh', 'Trackbars')
hueLow2 = cv2.getTrackbarPos('hueLow2', 'Trackbars')
hueHigh2 = cv2.getTrackbarPos('hueHigh2', 'Trackbars')
satLow = cv2.getTrackbarPos('satLow', 'Trackbars')
satHigh = cv2.getTrackbarPos('satHigh', 'Trackbars')
valueLow = cv2.getTrackbarPos('valueLow', 'Trackbars')
valueHigh = cv2.getTrackbarPos('valueHigh', 'Trackbars')
#Create arrays to store HSV Low High values
lower_bound = np.array([hueLow, satLow, valueLow])
upper_bound = np.array([hueHigh, satHigh, valueHigh])
lower_bound2 = np.array([hueLow2, satLow, valueLow])
upper_bound2 = np.array([hueHigh2, satHigh, valueHigh])
#Create foreground mask. Fuction only returns 1's and 0's. 1 for any pixel that
#is in range, 0 for any vale out of range
FGmask = cv2.inRange(hsv, lower_bound, upper_bound)
FGmask2 = cv2.inRange(hsv, lower_bound2, upper_bound2)
FGmaskcomp = cv2.add(FGmask,FGmask2)
#Show the FGmask result
#cv2.imshow('FGmaskcomp', FGmaskcomp)
#cv2.moveWindow('FGmaskcomp', 0, 410)
#Now AND the FGmask and the original image
FG = cv2.bitwise_and(frame, frame, mask = FGmaskcomp)
cv2.imshow('FG', FG)
cv2.moveWindow('FG', 0, 530)
#Create Background Mask. Just invert the FGmask
BGmask = cv2.bitwise_not(FGmaskcomp)
#Show the BGmask result
#cv2.imshow('BGmask', BGmask)
#cv2.moveWindow('BGmask', 900, 0)
#Convert from grayscale to color matrix type
BG = cv2.cvtColor(BGmask, cv2.COLOR_GRAY2BGR)
#cv2.imshow('BG', BG)
#cv2.moveWindow('BG', 900, 410)
#Final Image. Add the Background and the Foreground
#final = cv2.add(FG,BG)
#cv2.imshow('final', final)
#cv2.moveWindow('final', 710, 530)
if cv2.waitKey(1) == ord('q'):
break
#After q is pressed, release the camera back to the system
cam.release()
cv2.destroyAllWindows()