-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrealtimecolor.py
More file actions
73 lines (62 loc) · 2.44 KB
/
realtimecolor.py
File metadata and controls
73 lines (62 loc) · 2.44 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
import cv2
def apply_filter(image, filter_type):
"""Apply the selected color filter or edge detection."""
# Create a copy of the image to avoid modifying the original
filtered_image = image.copy()
if filter_type == "red_tint":
filtered_image[:, :, 1] = 0 # Green channel to 0
filtered_image[:, :, 0] = 0 # Blue channel to 0
elif filter_type == "green_tint":
filtered_image[:, :, 0] = 0 # Blue channel to 0
filtered_image[:, :, 2] = 0 # Red channel to 0
elif filter_type == "blue_tint":
filtered_image[:, :, 1] = 0 # Green channel to 0
filtered_image[:, :, 2] = 0 # Red channel to 0
elif filter_type == "sobel":
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
sobelx = cv2.Sobel(gray_image, cv2.CV_64F, 1, 0, ksize=3)
sobely = cv2.Sobel(gray_image, cv2.CV_64F, 0, 1, ksize=3)
combined_sobel = cv2.cvtColor(combined_sobel, cv2.COLOR_GRAY2BGR)
elif filter_type == "canny":
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray_image, 100, 200)
filtered_Image = cv2.cvtColor(edges, cv2.COLOR_GRAY2BGR)
return filtered_image
# Load the image
image_path = 'Anothermustang.avif' # Provide your image path
image = cv2.imread(image_path)
if image is None:
print("Error: Image not found.")
else:
filter_type = "original" # Default filter type
print("Press the following keys to apply filters:")
print("r: Red Tint")
print("g: Green Tint")
print("b: Blue Tint")
print("s: Sobel Edge Detection")
print("c: Canny Edge Detection")
print("q: Quit")
while True:
# Apply the selected filter
filtered_image = apply_filter(image, filter_type)
# Display the filtered image
cv2.imshow("Filtered Image", filtered_image)
# Wait for a key press
key = cv2.waitKey(1) & 0xFF
# Map key presses to filters
if key == ord('r'):
filter_type = "red_tint"
elif key == ord('g'):
filter_type = "green_tint"
elif key == ord('b'):
filter_type = "blue_tint"
elif key == ord('s'):
filter_type = "sobel"
elif key == ord('c'):
filter_type = "canny"
elif key == ord('q'):
print("Exiting...")
break
else:
print("Invalid key. Please use 'r', 'g', 'b', 's', 'c', or 'q'.")
cv2.destroyAllWindows()