-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment.py
More file actions
136 lines (102 loc) Β· 3.88 KB
/
Assignment.py
File metadata and controls
136 lines (102 loc) Β· 3.88 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
"""
πΌοΈ Image Blurring and Sharpening App
====================================
This app allows users to experiment with different image filtering techniques
such as Gaussian Blur, Median Blur, and Sharpening using OpenCV.
-------------------------------------------------------------
π Instructions:
1. Start the program.
2. Choose one of the following options:
------------------------------------
[1] Apply Gaussian Blur
β’ Upload or select an image
β’ Provide kernel size (odd numbers only, e.g., 3, 5, 9)
β’ Set sigma value (strength of blur)
β’ Result: Smoothened image by averaging nearby pixels
[2] Apply Median Blur
β’ Upload or select an image
β’ Provide kernel size (odd number, e.g., 3, 5, 7)
β’ Result: Removes salt-and-pepper noise using median filter
[3] Apply Sharpening Filter
β’ Upload or select an image
β’ Set ddepth value (e.g., -1 to match input depth)
β’ Result: Enhances edges and image contrast
[4] View Original Image
β’ Simply displays the uploaded image without processing
-------------------------------------------------------------
π‘ Notes:
- All filters work with color images (BGR) using OpenCV.
- Filtered images are shown in a popup window (CLI) or on the page (Streamlit).
- Users can optionally download/save the processed image.
-------------------------------------------------------------
π Learning Objectives:
β
Understand how image blurring (Gaussian & Median) works
β
Learn edge enhancement using sharpening kernels
β
Get hands-on with OpenCV functions and image processing concepts
-------------------------------------------------------------
π§ Dependencies:
β’ OpenCV (cv2)
β’ NumPy
β’ Streamlit (for web app version)
β’ PIL (Pillow, for image saving)
"""
import cv2
import numpy as np
def load_image():
path = input("Enter the full path to the image: ")
img = cv2.imread(path)
if img is None:
print("β Failed to load image. Please check the path.")
return img
def apply_gaussian_blur(img):
kx = int(input("Enter kernel size X (odd number): "))
ky = int(input("Enter kernel size Y (odd number): "))
sigma = float(input("Enter sigma value: "))
blurred = cv2.GaussianBlur(img, (kx, ky), sigma)
return blurred
def apply_median_blur(img):
k = int(input("Enter kernel size (odd number): "))
median = cv2.medianBlur(img, k)
return median
def apply_sharpening(img):
ddepth = int(input("Enter ddepth (-1 for default): "))
kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]])
sharpened = cv2.filter2D(img, ddepth, kernel)
return sharpened
def show_image(title, img):
cv2.imshow(title, img)
cv2.waitKey(0)
cv2.destroyAllWindows()
def save_image(img):
path = input("Enter filename to save (e.g., output.jpg): ")
cv2.imwrite(path, img)
print(f"β
Saved as {path}")
def main():
print("\n=== πΌοΈ Image Filtering CLI ===")
print("Choose an option:")
print("1. Apply Gaussian Blur")
print("2. Apply Median Blur")
print("3. Apply Sharpening Filter")
print("4. View Original Image")
choice = input("Enter choice (1-4): ")
img = load_image()
if img is None:
return
if choice == "1":
result = apply_gaussian_blur(img)
elif choice == "2":
result = apply_median_blur(img)
elif choice == "3":
result = apply_sharpening(img)
elif choice == "4":
show_image("Original Image", img)
return
else:
print("β Invalid choice")
return
show_image("Processed Image", result)
save = input("Do you want to save the image? (y/n): ").lower()
if save == "y":
save_image(result)
if __name__ == "__main__":
main()