-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedgedetection.py
More file actions
103 lines (82 loc) · 3.44 KB
/
edgedetection.py
File metadata and controls
103 lines (82 loc) · 3.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
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
import cv2
import numpy as np
import matplotlib.pyplot as plt
def display_image(title, image):
"""Utility function to display an image."""
plt.figure(figsize=(8, 8))
if len(image.shape) == 2: # Grayscale image
plt.imshow(image, cmap='gray')
else: # Color image
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
plt.title(title)
plt.axis('off')
plt.show()
def interactive_edge_detection(image_path):
"""Interactive activity for edge detection and filtering."""
image = cv2.imread(image_path)
if image is None:
print("Error: Image not found!")
return
# Convert to grayscale
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
display_image("Original Grayscale Image", gray_image)
print("Select an option:")
print("1. Sobel Edge Detection")
print("2. Canny Edge Detection")
print("3. Laplacian Edge Detection")
print("4. Gaussian Smoothing")
print("5. Median Filtering")
print("6. Exit")
while True:
choice = input("Enter your choice (1-6): ")
if choice == "1":
# Sobel Edge Detection
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.bitwise_or(sobelx.astype(np.uint8), sobely.astype(np.uint8))
display_image("Sobel Edge Detection", combined_sobel)
elif choice == "2":
# Canny Edge Detection
print("Adjust thresholds for Canny (default: 100 and 200)")
lower_thresh = int(input("Enter lower threshold: "))
upper_thresh = int(input("Enter upper threshold: "))
edges = cv2.Canny(gray_image, lower_thresh, upper_thresh)
display_image("Canny Edge Detection", edges)
elif choice == "3":
# Laplacian Edge Detection
laplacian = cv2.Laplacian(gray_image, cv2.CV_64F)
display_image("Laplacian Edge Detection", np.abs(laplacian).astype(np.uint8))
elif choice == "4":
# Gaussian Smoothing
print("Adjust kernel size for Gaussian blur (must be odd, default: 5)")
kernel_size = int(input("Enter kernel size (odd number): "))
blurred = cv2.GaussianBlur(image, (kernel_size, kernel_size), 0)
display_image("Gaussian Smoothing", blurred)
elif choice == "5":
# Median Filtering
print("Adjust kernel size for Median filtering (must be odd, default: 5)")
kernel_size = int(input("Enter kernel size (odd number): "))
median_filtered = cv2.medianBlur(image, kernel_size)
display_image("Median Filtering", median_filtered)
elif choice == "6":
print("Exiting...")
break
else:
print("Invalid choice. Please select a number between 1 and 6.")
# Provide the path to an image for the activity
interactive_edge_detection('Anothermustang.avif')
def upload_image():
"""Allows the user to upload an image file."""
root = tk.Tk()
root.withdraw() # Hide the main window
file_path = filedialog.askopenfilename(
title="Select Image File",
filetypes=[("Image files", "*.png;*.jpg;*.jpeg;*.gif;*.bmp")]
)
if file_path:
interactive_edge_detection(file_path)
else:
print("No image selected.")
# Start the image upload process
if __name__ == "__main__":
upload_image()