Repository navigation
Expand file tree
/
Copy patht5.py
More file actions
132 lines (107 loc) · 4.03 KB
/
Copy patht5.py
File metadata and controls
132 lines (107 loc) · 4.03 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
'''
- color detection
- imagery bitwise operations
- mode switching state machine
- font display
- color scheme conversion
'''
import numpy as np
import cv2
# videocam stores video source
videocam = cv2.VideoCapture(0)
img = cv2.imread('assets/fruitbasket.jpg')
# Store the current operation state (initialize with original frame)
current_operation = 'w' # 'w' means original frame
ret, frame = videocam.read()
width = int(videocam.get(3))
height = int(videocam.get(4))
# makes sure the image dimensions are suitable for bitwise operations
resized = cv2.resize(img, (width, height))
print('key operations: ','\n',
'n - not','\n',
'o - or','\n',
'a - and','\n',
'r - nor','\n',
'x - xor','\n',
'w - original','\n',
'm - mask','\n',
'd - detection and let through','\n',
'q - quit','\n',
'')
while True:
# gets frame and status from the source
ret, frame = videocam.read()
if not ret:
break
# H-Hue, color type, 0-179
# S-Saturation, colour diversity, 0-255
# V-value, brightness, 0-255
# converting frame colour contents to HSV color space
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
lower_blue = np.array([110, 50, 50])
upper_blue = np.array([130, 255, 255])
# displays white if the color detected is within color range
mask = cv2.inRange(hsv, lower_blue, upper_blue)
# Check for key press to change operation mode
key = cv2.waitKey(1) & 0xFF
# Update current operation based on key press
if key == ord('n'):
current_operation = 'n' # bitwise NOT
elif key == ord('o'):
current_operation = 'o' # bitwise OR
elif key == ord('a'):
current_operation = 'a' # bitwise AND
elif key == ord('r'):
current_operation = 'r' # bitwise NOR (NOT of OR)
elif key == ord('x'):
current_operation = 'x' # bitwise XOR
elif key == ord('w'):
current_operation = 'w' # original frame
elif key == ord('d'):
current_operation = 'd' # detects colors within range
elif key == ord('m'):
current_operation = 'm' # mask display
elif key == ord('q'):
break # quit application
# Apply the current operation (persists until changed)
'''
cv2.bitwise_and, cv2.bitwise_or, cv2.bitwise_nor, cv2.bitwise_xor
be mindful of the dimensions - both sources must have same dimensions
bitwise_operation(source1, source2, destination,)
dimensions of src1 and 2 have to be the same
'''
if current_operation == 'n': # inversion
final = cv2.bitwise_not(frame)
elif current_operation == 'o':
final = cv2.bitwise_or(frame, resized)
elif current_operation == 'a': # final = frame & resized
final = cv2.bitwise_and(frame, resized)
elif current_operation == 'r': # NOR = NOT(OR)
or_result = cv2.bitwise_or(frame, resized)
final = cv2.bitwise_not(or_result)
elif current_operation == 'x': # detects difference
final = cv2.bitwise_xor(frame, resized)
elif current_operation == 'w': # original frame
final = frame
elif current_operation == 'm':
# normal display doesnt work because of greyscale
# hence must convert 1-channel mask back to 3-channel BGR for text display
# still black and white(greyscale) for the rest of the display
final = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR)
elif current_operation == 'd':
# lets the frame content through if within mask colour range
# final = (frame & frame) & mask
final = cv2.bitwise_and(frame,frame,mask=mask)
else:
final = frame # default to original
# Display the current operation mode on the frame
cv2.putText(final, f"Mode: {current_operation}", (10, 30),
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
cv2.putText(final, "Press keys: n,o,a,r,x,w,m,d,q", (10, 60),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
# Display the result
cv2.imshow('frame', final)
# Release resources
videocam.release()
frame.release()
cv2.destroyAllWindows()