-
Notifications
You must be signed in to change notification settings - Fork 0
/
camera_colorchannels.py
66 lines (51 loc) · 1.75 KB
/
camera_colorchannels.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
import numpy as np
import cv2
from tools.filters import *
from tools.processing import resize, grab_frame
import matplotlib.pyplot as plt
import sys
channel = 0
cam = cv2.VideoCapture(0) # Define camera
colors = {0: "Blue", 1: "Green", 2: "Red"}
use_cmap = False
img = resize(grab_frame(cam, channel), 0.9)
image = np.stack([img, img, img], axis=2)
threshold = 0
cv2.namedWindow("ColorChannels", cv2.WND_PROP_FULLSCREEN);
cv2.setWindowProperty("ColorChannels", cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN);
# %% Camera loop
while (True):
# with timer_manager():
# with timer_manager("FrameGrab"):
img = resize(grab_frame(cam, channel), 0.9)
key = cv2.waitKey(1) & 0xFF
if key == ord(']'):
break
elif key == ord('9'):
use_cmap = not use_cmap
print("UseCmap " + str(use_cmap))
elif key == ord('0'):
channel = np.mod(channel+1, 3)
print("ColorChannel " + str(channel))
elif key == ord('1'):
threshold = np.clip(threshold+8, 0, 255)
print("Threshold " + str(threshold))
elif key == ord('2'):
threshold = np.clip(threshold-8, 0, 255)
print("Threshold " + str(threshold))
if threshold > 0:
mask = img > threshold
image[:, :, channel] = img * mask
else:
image[:, :, channel] = img
zero_channels = [*range(3)]
zero_channels.pop(channel)
image[:, :, zero_channels] = 0
cv2.rectangle(image, (0, 0), (120, 40), (0, 0, 0), -1)
cv2.putText(image, colors[channel], (10, 30),
fontFace=cv2.QT_FONT_NORMAL, fontScale=0.75, color=(255,255,255))
if use_cmap:
image = cv2.applyColorMap(image[:,:,channel], cv2.COLORMAP_JET)
cv2.imshow('ColorChannels', image)
cam.release()
cv2.destroyAllWindows()