-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdilation_filter.py
More file actions
64 lines (49 loc) · 1.54 KB
/
dilation_filter.py
File metadata and controls
64 lines (49 loc) · 1.54 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
import cv2
import numpy as np
def main():
"""Apply grayscale dilation to webcam stream."""
cap = cv2.VideoCapture(0)
if not cap.isOpened():
print("Error: Could not open webcam")
return
print("Webcam opened successfully!")
print("Press 'q' to quit")
# 5x5 square kernel for dilation
kernel = np.ones((5, 5), np.uint8)
while True:
ret, frame = cap.read()
if not ret:
print("Error: Could not read frame")
break
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
dilated = cv2.dilate(gray, kernel, iterations=1)
# Convert to BGR for stacking
gray_bgr = cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR)
dilated_bgr = cv2.cvtColor(dilated, cv2.COLOR_GRAY2BGR)
combined = np.hstack((frame, gray_bgr, dilated_bgr))
cv2.putText(combined, "Original", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
cv2.putText(
combined,
"Grayscale",
(frame.shape[1] + 10, 30),
cv2.FONT_HERSHEY_SIMPLEX,
0.7,
(0, 255, 0),
2,
)
cv2.putText(
combined,
"Dilated Gray",
(frame.shape[1] * 2 + 10, 30),
cv2.FONT_HERSHEY_SIMPLEX,
0.7,
(0, 255, 0),
2,
)
cv2.imshow("Grayscale Dilation Filter", combined)
if cv2.waitKey(1) & 0xFF == ord("q"):
break
cap.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
main()