-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhsvSelection.py
More file actions
71 lines (54 loc) · 1.92 KB
/
Copy pathhsvSelection.py
File metadata and controls
71 lines (54 loc) · 1.92 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
# Computer Vision I (CSE 40535/60535)
# University of Notre Dame, Fall 2021
# ________________________________________________________________
# Adam Czajka, Andrey Kuehlkamp, September 2017
import cv2
import matplotlib.pyplot as plt
# import matplotlib
# matplotlib.use("TKAgg")
# print(matplotlib.get_backend())
cam = cv2.VideoCapture(0)
def print_menu():
print("Options menu:")
print("esc - Quit program")
print("s - Snapshot")
def calc_histograms(img):
color = ['b','g','r']
colornames = ['Blue channel','Green channel','Red channel']
plt.figure()
for i,col in enumerate(color):
hist = cv2.calcHist([img], [i], None, [256], [0,256])
plt.plot(hist, color=col)
plt.xlim([0,256])
plt.legend(colornames)
plt.title('RGB Histogram')
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
channels = ['Hue','Saturation','Value']
plt.figure()
for i,ch in enumerate(channels):
hist = cv2.calcHist([hsv], [i], None, [256], [0,256])
plt.plot(hist, color=color[i])
plt.title('HSV Histogram')
plt.legend(channels)
plt.show()
if __name__ == '__main__':
print_menu()
while (True):
retval, img = cam.read()
imcrop = None
res_scale = 0.6 # rescale the input image if it's too large
img = cv2.resize(img, (0,0), fx=res_scale, fy=res_scale)
cv2.imshow("Preview", img)
action = cv2.waitKey(1)
if action == 27: # escape
break
elif action == ord('h'): # help
print_menu()
elif action == ord('s'): # snapshot
still = img
r = cv2.selectROI(still)
imcrop = still[int(r[1]):int(r[1]+r[3]), int(r[0]):int(r[0]+r[2])]
cv2.imshow("ROI",imcrop)
if imcrop is not None:
calc_histograms(imcrop)
cv2.destroyAllWindows()