-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment.py
More file actions
178 lines (134 loc) Β· 5.17 KB
/
Assignment.py
File metadata and controls
178 lines (134 loc) Β· 5.17 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
"""
### π§ Edge Detection & Thresholding App
\======================================
This app allows users to explore essential edge detection and thresholding techniques using OpenCV,
including **Canny Edge Detection**, **Binary & Adaptive Thresholding**, and **Bitwise Operations**.
---
π **Instructions**:
1. Start the program.
2. ## Choose one of the following options:
**\[1] Apply Canny Edge Detection**
* Upload or select an image
* Set lower and upper threshold values (e.g., 50, 150)
* Result: Detects edges based on gradient intensity
**\[2] Apply Binary Thresholding**
* Upload or select an image
* Provide threshold value (e.g., 127) and max value (e.g., 255)
* Result: Converts image to binary (black & white)
* Type: `cv2.THRESH_BINARY`, `cv2.THRESH_BINARY_INV`
**\[3] Apply Adaptive Thresholding**
* Upload or select an image
* Choose method: `cv2.ADAPTIVE_THRESH_MEAN_C` or `cv2.ADAPTIVE_THRESH_GAUSSIAN_C`
* Provide block size (odd number, e.g., 11) and C value (e.g., 2)
* Result: Locally adaptive thresholding that works well in varying lighting
**\[4] Apply Bitwise Operations**
* Choose two thresholded or edge-detected images (e.g., from previous steps)
* Choose operation: `AND`, `OR`, `XOR`, `NOT`
* Result: Combines images based on binary logic
**\[5] View Original Image**
* Displays the original uploaded image without modifications
---
π‘ **Notes**:
* All operations work with **grayscale images**, and the app converts if needed.
* Edge-detected and thresholded images are shown in popup windows (CLI) or on-screen (Streamlit).
* Users can **save/download** the result for further analysis.
---
π **Learning Objectives**:
β
Understand edge detection using gradients (Canny)
β
Learn the difference between binary and adaptive thresholding
β
Practice combining image masks using bitwise logic
β
Strengthen knowledge of OpenCV image processing techniques
---
π§ **Dependencies**:
* `OpenCV (cv2)`
* `NumPy`
* `Streamlit` *(optional for web app version)*
* `PIL (Pillow)` *(optional for image saving)*
"""
import cv2
import numpy as np
def load_image(path="sample.jpg"):
img = cv2.imread(path)
if img is None:
print("Error: Could not load image.")
exit()
return img
def show_image(winname, img):
cv2.imshow(winname, img)
cv2.waitKey(0)
cv2.destroyAllWindows()
def canny_edge(img):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
low = int(input("Enter lower threshold: "))
high = int(input("Enter upper threshold: "))
edges = cv2.Canny(gray, low, high)
show_image("Canny Edge", edges)
return edges
def binary_threshold(img):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
thresh = int(input("Enter threshold value: "))
maxval = int(input("Enter max value: "))
_, binary = cv2.threshold(gray, thresh, maxval, cv2.THRESH_BINARY)
show_image("Binary Threshold", binary)
return binary
def adaptive_threshold(img):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
block_size = int(input("Enter block size (odd number): "))
C = int(input("Enter C value: "))
method = input("Method? (mean/gaussian): ").strip().lower()
if method == "mean":
adaptive = cv2.adaptiveThreshold(
gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, block_size, C
)
else:
adaptive = cv2.adaptiveThreshold(
gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, block_size, C
)
show_image("Adaptive Threshold", adaptive)
return adaptive
def bitwise_ops(img1, img2):
op = input("Choose operation: and/or/xor/not: ").strip().lower()
if op == "and":
result = cv2.bitwise_and(img1, img2)
elif op == "or":
result = cv2.bitwise_or(img1, img2)
elif op == "xor":
result = cv2.bitwise_xor(img1, img2)
elif op == "not":
result = cv2.bitwise_not(img1)
else:
print("Invalid operation.")
return
show_image("Bitwise Result", result)
def main():
path = input("Enter image path: ")
img = load_image(path)
while True:
print("""
[1] Apply Canny Edge Detection
[2] Apply Binary Thresholding
[3] Apply Adaptive Thresholding
[4] Apply Bitwise Operations
[5] View Original Image
[0] Exit
""")
choice = input("Choose an option: ")
if choice == "1":
edges = canny_edge(img)
elif choice == "2":
binary = binary_threshold(img)
elif choice == "3":
adaptive = adaptive_threshold(img)
elif choice == "4":
print("Make sure you already ran at least one threshold/edge function!")
img1 = binary_threshold(img)
img2 = adaptive_threshold(img)
bitwise_ops(img1, img2)
elif choice == "5":
show_image("Original Image", img)
elif choice == "0":
break
else:
print("Invalid choice!")
if __name__ == "__main__":
main()