-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhsv_threshold.py
More file actions
140 lines (125 loc) · 4.66 KB
/
hsv_threshold.py
File metadata and controls
140 lines (125 loc) · 4.66 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import cv2
import numpy as np
import matplotlib.pyplot as plt
import argparse
"""
Performs HSV thresholding, then tries to removes top of frame above the road
Author: Ashwin Vangipuram
"""
# 0, 180, 22, 255, 0, 207
max_value = 255
low_H = 0
low_S = 22
low_V = 0
high_H = 180
high_S = 255
high_V = 207
window_capture_name = 'Video Capture'
window_detection_name = 'Object Detection'
low_H_name = 'Low H'
low_S_name = 'Low S'
low_V_name = 'Low V'
high_H_name = 'High H'
high_S_name = 'High S'
high_V_name = 'High V'
pauseWhenFound = 0
old_gray = None
p0 = None
heur_thresh = 200
# params for ShiTomasi corner detection
feature_params = dict( maxCorners = 100,
qualityLevel = 0.3,
minDistance = 7,
blockSize = 7 )
# Parameters for lucas kanade optical flow
lk_params = dict( winSize = (15,15),
maxLevel = 2,
criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03))
# Create some random colors
color = np.random.randint(0,255,(100,3))
def trueFalsePause(val):
global pauseWhenFound
pauseWhenFound = val
cv2.setTrackbarPos('pausing', window_capture_name, pauseWhenFound)
def on_low_H_thresh_trackbar(val):
global low_H
global high_H
low_H = val
low_H = min(high_H-1, low_H)
cv2.setTrackbarPos(low_H_name, window_detection_name, low_H)
def on_high_H_thresh_trackbar(val):
global low_H
global high_H
high_H = val
high_H = max(high_H, low_H+1)
cv2.setTrackbarPos(high_H_name, window_detection_name, high_H)
def on_low_S_thresh_trackbar(val):
global low_S
global high_S
low_S = val
low_S = min(high_S-1, low_S)
cv2.setTrackbarPos(low_S_name, window_detection_name, low_S)
def on_high_S_thresh_trackbar(val):
global low_S
global high_S
high_S = val
high_S = max(high_S, low_S+1)
cv2.setTrackbarPos(high_S_name, window_detection_name, high_S)
def on_low_V_thresh_trackbar(val):
global low_V
global high_V
low_V = val
low_V = min(high_V-1, low_V)
cv2.setTrackbarPos(low_V_name, window_detection_name, low_V)
def on_high_V_thresh_trackbar(val):
global low_V
global high_V
high_V = val
high_V = max(high_V, low_V+1)
cv2.setTrackbarPos(high_V_name, window_detection_name, high_V)
parser = argparse.ArgumentParser(description='Code for Thresholding Operations using inRange tutorial.')
parser.add_argument('camera', help='Camera devide number.', default=0, type=str)
args = parser.parse_args()
cap = cv2.VideoCapture(args.camera)
cv2.namedWindow(window_capture_name)
cv2.namedWindow(window_detection_name)
cv2.createTrackbar(low_H_name, window_detection_name , low_H, max_value, on_low_H_thresh_trackbar)
cv2.createTrackbar(high_H_name, window_detection_name , high_H, max_value, on_high_H_thresh_trackbar)
cv2.createTrackbar(low_S_name, window_detection_name , low_S, max_value, on_low_S_thresh_trackbar)
cv2.createTrackbar(high_S_name, window_detection_name , high_S, max_value, on_high_S_thresh_trackbar)
cv2.createTrackbar(low_V_name, window_detection_name , low_V, max_value, on_low_V_thresh_trackbar)
cv2.createTrackbar(high_V_name, window_detection_name , high_V, max_value, on_high_V_thresh_trackbar)
cv2.createTrackbar('pausing', window_capture_name, pauseWhenFound, 1, trueFalsePause)
#cv2.createTrackbar('low_canny', 'canny', low_canny, 500, lcanny)
paused = False
points = []
while True:
if not paused:
ret, frame = cap.read() #reads the frame
else:
frame = untampered
if ret:
if not paused:
frame = cv2.resize(frame, (0,0), fx=0.4, fy=0.4)#resizes frame so that it fits on screen
blur = cv2.GaussianBlur(frame, (5, 5), 1)
frame_HSV = cv2.cvtColor(blur, cv2.COLOR_BGR2HSV)
#frame_gray = cv2.cvtColor(blur, cv2.COLOR_BGR2GRAY)
#canny = cv2.Canny(frame_gray, 200, 3, True)
frame_threshold = cv2.inRange(frame_HSV, (low_H, low_S, low_V), (high_H, high_S, high_V)) #low_S ideal = 98 Sets threshold in hsv
frame_threshold = cv2.bitwise_not(frame_threshold)
res = cv2.bitwise_and(frame, frame, mask=frame_threshold)
# print(frame.shape, res.shape, frame_threshold.shape)
blacked = np.copy(res)
for row in range(res.shape[0]):
if np.count_nonzero(res[row]) < 10:
blacked[:row] = np.zeros((res.shape[1], 3))
break
cv2.imshow(window_capture_name, frame)
cv2.imshow(window_detection_name, frame_threshold)
cv2.imshow('Result', blacked)
untampered = np.copy(frame)
key = cv2.waitKey(30)
if key == ord('q') or key == 27:
break
if key == ord('p'):
paused = not paused