selva1909 commited on
Commit
51f6fb9
·
verified ·
1 Parent(s): c6632de

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +161 -47
app.py CHANGED
@@ -3,10 +3,9 @@ import cv2
3
  import numpy as np
4
  import matplotlib.pyplot as plt
5
 
6
-
7
- # -----------------------------
8
  # KERNELS
9
- # -----------------------------
10
  kernels = {
11
  "Blur": np.ones((3, 3), np.float32) / 9,
12
 
@@ -42,92 +41,207 @@ kernels = {
42
  }
43
 
44
 
45
- # -----------------------------
46
- # MAIN FUNCTION
47
- # -----------------------------
48
  def process_image(image, kernel_name):
49
 
50
- # Convert RGB → Grayscale
51
- gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
52
 
53
- # Get selected kernel
54
- kernel = kernels[kernel_name]
55
 
56
- # Apply convolution
57
- filtered = cv2.filter2D(gray, -1, kernel)
 
 
 
58
 
59
  # Histogram Equalization
60
  equalized = cv2.equalizeHist(gray)
61
 
62
- # Thresholding
63
  _, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
64
 
65
- # Edge Detection
 
 
 
 
 
 
 
 
 
 
66
  canny = cv2.Canny(gray, 100, 200)
67
 
68
- # -----------------------------
69
- # VISUALIZATION FIGURE
70
- # -----------------------------
71
- fig, axs = plt.subplots(2, 3, figsize=(12, 8))
 
 
 
 
 
 
 
 
 
 
72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
  axs[0, 0].imshow(image)
74
  axs[0, 0].set_title("Original Image")
75
  axs[0, 0].axis("off")
76
 
 
77
  axs[0, 1].imshow(gray, cmap='gray')
78
  axs[0, 1].set_title("Grayscale")
79
  axs[0, 1].axis("off")
80
 
81
- axs[0, 2].imshow(filtered, cmap='gray')
82
- axs[0, 2].set_title(f"{kernel_name} Output")
 
83
  axs[0, 2].axis("off")
84
 
 
 
 
 
 
 
85
  axs[1, 0].imshow(equalized, cmap='gray')
86
  axs[1, 0].set_title("Histogram Equalization")
87
  axs[1, 0].axis("off")
88
 
 
89
  axs[1, 1].imshow(binary, cmap='gray')
90
  axs[1, 1].set_title("Binary Threshold")
91
  axs[1, 1].axis("off")
92
 
93
- axs[1, 2].imshow(canny, cmap='gray')
94
- axs[1, 2].set_title("Canny Edge Detection")
 
95
  axs[1, 2].axis("off")
96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97
  plt.tight_layout()
98
 
99
  return fig
100
 
101
 
102
- # -----------------------------
103
- # GRADIO UI
104
- # -----------------------------
105
- demo = gr.Interface(
106
- fn=process_image,
107
-
108
- inputs=[
109
- gr.Image(type="numpy", label="Upload Image"),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
110
 
111
- gr.Dropdown(
112
  choices=list(kernels.keys()),
113
  value="Edge Detection",
114
  label="Select Kernel"
115
  )
116
- ],
117
-
118
- outputs=gr.Plot(label="Visualization"),
119
-
120
- title="Kernel Matrix Visualization for Image Processing",
121
-
122
- description="""
123
- Upload an image and visualize:
124
- - Grayscale conversion
125
- - Convolution kernels
126
- - Edge detection
127
- - Thresholding
128
- - Histogram Equalization
129
- - CNN-style preprocessing
130
- """
131
- )
 
 
 
 
132
 
133
  demo.launch()
 
3
  import numpy as np
4
  import matplotlib.pyplot as plt
5
 
6
+ # =========================================================
 
7
  # KERNELS
8
+ # =========================================================
9
  kernels = {
10
  "Blur": np.ones((3, 3), np.float32) / 9,
11
 
 
41
  }
42
 
43
 
44
+ # =========================================================
45
+ # IMAGE PROCESSING
46
+ # =========================================================
47
  def process_image(image, kernel_name):
48
 
49
+ if image is None:
50
+ return None
51
 
52
+ # Convert RGB → BGR for OpenCV
53
+ image_bgr = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
54
 
55
+ # Grayscale
56
+ gray = cv2.cvtColor(image_bgr, cv2.COLOR_BGR2GRAY)
57
+
58
+ # Gaussian Blur
59
+ gaussian = cv2.GaussianBlur(gray, (5, 5), 0)
60
 
61
  # Histogram Equalization
62
  equalized = cv2.equalizeHist(gray)
63
 
64
+ # Binary Threshold
65
  _, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
66
 
67
+ # Adaptive Threshold
68
+ adaptive = cv2.adaptiveThreshold(
69
+ gray,
70
+ 255,
71
+ cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
72
+ cv2.THRESH_BINARY,
73
+ 11,
74
+ 2
75
+ )
76
+
77
+ # Canny Edge Detection
78
  canny = cv2.Canny(gray, 100, 200)
79
 
80
+ # Kernel Filtering
81
+ kernel = kernels[kernel_name]
82
+ filtered = cv2.filter2D(gray, -1, kernel)
83
+
84
+ # Laplacian
85
+ laplacian = cv2.Laplacian(gray, cv2.CV_64F)
86
+ laplacian = np.uint8(np.absolute(laplacian))
87
+
88
+ # Sobel Combined
89
+ sobelx = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=3)
90
+ sobely = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=3)
91
+
92
+ sobel_combined = cv2.magnitude(sobelx, sobely)
93
+ sobel_combined = np.uint8(sobel_combined)
94
 
95
+ # =========================================================
96
+ # HISTOGRAM
97
+ # =========================================================
98
+ hist = cv2.calcHist([gray], [0], None, [256], [0, 256])
99
+
100
+ # =========================================================
101
+ # VISUALIZATION
102
+ # =========================================================
103
+ fig, axs = plt.subplots(3, 4, figsize=(18, 12))
104
+
105
+ fig.suptitle(
106
+ "Kernel Matrix & CNN Preprocessing Visualization",
107
+ fontsize=20,
108
+ fontweight='bold'
109
+ )
110
+
111
+ # ---------------- ORIGINAL ----------------
112
  axs[0, 0].imshow(image)
113
  axs[0, 0].set_title("Original Image")
114
  axs[0, 0].axis("off")
115
 
116
+ # ---------------- GRAYSCALE ----------------
117
  axs[0, 1].imshow(gray, cmap='gray')
118
  axs[0, 1].set_title("Grayscale")
119
  axs[0, 1].axis("off")
120
 
121
+ # ---------------- GAUSSIAN ----------------
122
+ axs[0, 2].imshow(gaussian, cmap='gray')
123
+ axs[0, 2].set_title("Gaussian Blur")
124
  axs[0, 2].axis("off")
125
 
126
+ # ---------------- FILTERED ----------------
127
+ axs[0, 3].imshow(filtered, cmap='gray')
128
+ axs[0, 3].set_title(f"{kernel_name} Kernel")
129
+ axs[0, 3].axis("off")
130
+
131
+ # ---------------- EQUALIZED ----------------
132
  axs[1, 0].imshow(equalized, cmap='gray')
133
  axs[1, 0].set_title("Histogram Equalization")
134
  axs[1, 0].axis("off")
135
 
136
+ # ---------------- BINARY ----------------
137
  axs[1, 1].imshow(binary, cmap='gray')
138
  axs[1, 1].set_title("Binary Threshold")
139
  axs[1, 1].axis("off")
140
 
141
+ # ---------------- ADAPTIVE ----------------
142
+ axs[1, 2].imshow(adaptive, cmap='gray')
143
+ axs[1, 2].set_title("Adaptive Threshold")
144
  axs[1, 2].axis("off")
145
 
146
+ # ---------------- CANNY ----------------
147
+ axs[1, 3].imshow(canny, cmap='gray')
148
+ axs[1, 3].set_title("Canny Edge Detection")
149
+ axs[1, 3].axis("off")
150
+
151
+ # ---------------- SOBEL ----------------
152
+ axs[2, 0].imshow(sobel_combined, cmap='gray')
153
+ axs[2, 0].set_title("Sobel Magnitude")
154
+ axs[2, 0].axis("off")
155
+
156
+ # ---------------- LAPLACIAN ----------------
157
+ axs[2, 1].imshow(laplacian, cmap='gray')
158
+ axs[2, 1].set_title("Laplacian")
159
+ axs[2, 1].axis("off")
160
+
161
+ # ---------------- HISTOGRAM ----------------
162
+ axs[2, 2].plot(hist)
163
+ axs[2, 2].set_title("Pixel Intensity Histogram")
164
+ axs[2, 2].set_xlim([0, 256])
165
+
166
+ # ---------------- KERNEL MATRIX ----------------
167
+ axs[2, 3].imshow(kernel, cmap='coolwarm')
168
+
169
+ axs[2, 3].set_title(f"{kernel_name} Matrix")
170
+
171
+ for i in range(kernel.shape[0]):
172
+ for j in range(kernel.shape[1]):
173
+ axs[2, 3].text(
174
+ j,
175
+ i,
176
+ str(kernel[i, j]),
177
+ ha='center',
178
+ va='center',
179
+ color='black',
180
+ fontsize=12,
181
+ fontweight='bold'
182
+ )
183
+
184
+ axs[2, 3].set_xticks([])
185
+ axs[2, 3].set_yticks([])
186
+
187
  plt.tight_layout()
188
 
189
  return fig
190
 
191
 
192
+ # =========================================================
193
+ # UI
194
+ # =========================================================
195
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
196
+
197
+ gr.Markdown(
198
+ """
199
+ # 🧠 Kernel Matrix & CNN Visualization Lab
200
+
201
+ Upload an image to visualize:
202
+ - Convolution Kernels
203
+ - CNN Preprocessing
204
+ - Edge Detection
205
+ - Histogram Equalization
206
+ - Sobel & Laplacian Features
207
+ - Thresholding
208
+ - Feature Extraction
209
+
210
+ Built using OpenCV + Gradio
211
+ """
212
+ )
213
+
214
+ with gr.Row():
215
+
216
+ input_image = gr.Image(
217
+ type="numpy",
218
+ label="Upload Image"
219
+ )
220
 
221
+ kernel_dropdown = gr.Dropdown(
222
  choices=list(kernels.keys()),
223
  value="Edge Detection",
224
  label="Select Kernel"
225
  )
226
+
227
+ output_plot = gr.Plot(label="Visualization Output")
228
+
229
+ # =========================================================
230
+ # AUTO SUBMIT WHEN IMAGE UPLOADED
231
+ # =========================================================
232
+ input_image.change(
233
+ fn=process_image,
234
+ inputs=[input_image, kernel_dropdown],
235
+ outputs=output_plot
236
+ )
237
+
238
+ # =========================================================
239
+ # AUTO UPDATE WHEN DROPDOWN CHANGES
240
+ # =========================================================
241
+ kernel_dropdown.change(
242
+ fn=process_image,
243
+ inputs=[input_image, kernel_dropdown],
244
+ outputs=output_plot
245
+ )
246
 
247
  demo.launch()