-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenCV18_contours.py
79 lines (65 loc) · 3.15 KB
/
openCV18_contours.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
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
#Setting up the camera
dispW=640
dispH=480
flip=0
camSet='nvarguscamerasrc ! video/x-raw(memory:NVMM), width=3264, height=2464, format=NV12, framerate=21/1 ! nvvidconv flip-method='+str(flip)+' ! video/x-raw, width='+str(dispW)+', height='+str(dispH)+', format=BGRx ! videoconvert ! video/x-raw, format=BGR ! appsink'
cam = cv2.VideoCapture(camSet)
#Setup trackbars that allow us to quickly change HSV values
cv2.namedWindow('Trackbars')
cv2.moveWindow('Trackbars', 1320, 0)
cv2.createTrackbar('hueLow', 'Trackbars', 165, 179, nothing)
cv2.createTrackbar('hueHigh', 'Trackbars', 179, 179, nothing)
cv2.createTrackbar('hueLow2', 'Trackbars', 0, 179, nothing)
cv2.createTrackbar('hueHigh2', 'Trackbars', 15, 179, nothing)
cv2.createTrackbar('satLow', 'Trackbars', 110, 255, nothing)
cv2.createTrackbar('satHigh', 'Trackbars', 255, 255, nothing)
cv2.createTrackbar('valueLow', 'Trackbars', 110, 255, nothing)
cv2.createTrackbar('valueHigh', 'Trackbars', 255, 255, nothing)
while True:
ret, frame = cam.read()
#convert original image (frame) from BGR 2 HSV
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
#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 FGMaskcom
cv2.imshow('FGmaskcomp', FGmaskcomp)
cv2.moveWindow('FGmaskcomp', 0, 410)
contours,_ = cv2.findContours(FGmaskcomp, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = sorted(contours, key=lambda x:cv2.contourArea(x), reverse=True)
#cv2.drawContours(frame, contours, 0, (255,0,0), 3)
#sometimes we may need to track multiple objects. This for loop does this
for cnt in contours:
area = cv2.contourArea(cnt)
(x,y,w,h) = cv2.boundingRect(cnt)
if area >= 50:
#cv2.drawContours(frame, [cnt], 0, (255,0,0), 3)
cv2.rectangle(frame, (x,y), (x+w, y+h), (255,0,0), 3)
cv2.imshow('piCam',frame)
cv2.moveWindow('piCam', 0, 0)
if cv2.waitKey(1) == ord('q'):
break
#After q is pressed, release the camera back to the system
cam.release()
cv2.destroyAllWindows()