-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp8.html
More file actions
356 lines (318 loc) · 19.8 KB
/
p8.html
File metadata and controls
356 lines (318 loc) · 19.8 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Practical 8: Spatial Filtering | CSU2543</title>
<meta name="description" content="Practical 8: Box, Median, Laplacian, and Sobel spatial filters. CSU2543 Digital Image Processing.">
<meta name="author" content="Divya Mohan">
<script>(function(){var t=localStorage.getItem('dip-theme');if(!t){t=window.matchMedia&&window.matchMedia('(prefers-color-scheme:dark)').matches?'dark':'light';}document.documentElement.setAttribute('data-theme',t);})();</script>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<script async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
<link rel="stylesheet" href="./css/base.css">
<link rel="stylesheet" href="./css/components.css">
</head>
<body>
<a href="#main-content" class="skip-link">Skip to main content</a>
<nav class="navbar navbar-expand-lg sticky-top dip-navbar" id="mainNav" aria-label="Main navigation">
<div class="container">
<a class="navbar-brand fw-semibold" href="./index.html">DIP Lab</a>
<button class="navbar-toggler border-0" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation"><span class="navbar-toggler-icon"></span></button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav ms-auto gap-1">
<li class="nav-item"><a class="nav-link" href="./index.html" data-page="index">Home</a></li>
<li class="nav-item"><a class="nav-link" href="./p1.html" data-page="p1">P1</a></li>
<li class="nav-item"><a class="nav-link" href="./p2.html" data-page="p2">P2</a></li>
<li class="nav-item"><a class="nav-link" href="./p3.html" data-page="p3">P3</a></li>
<li class="nav-item"><a class="nav-link" href="./p4.html" data-page="p4">P4</a></li>
<li class="nav-item"><a class="nav-link" href="./p5.html" data-page="p5">P5</a></li>
<li class="nav-item"><a class="nav-link" href="./p6.html" data-page="p6">P6</a></li>
<li class="nav-item"><a class="nav-link" href="./p7.html" data-page="p7">P7</a></li>
<li class="nav-item"><a class="nav-link active" href="./p8.html" data-page="p8">P8</a></li>
</ul>
<button id="themeToggle" class="btn btn-sm ms-3 theme-toggle" type="button" aria-label="Toggle theme"><span class="theme-toggle-label">Dark</span></button>
</div>
</div>
</nav>
<!-- Print cover page -->
<div class="print-only print-cover" aria-hidden="true">
<img class="cover-logo" src="https://shooliniuniversity.com/assets/images/logo.png" alt="Shoolini University Logo">
<div class="cover-university">Shoolini University</div>
<div class="cover-dept">Faculty of Engineering & Technology<br>Department of Computer Science & Engineering</div>
<div class="cover-rule"></div>
<div class="cover-subject">Digital Image Processing</div>
<div class="cover-code">Course Code: CSU2543</div>
<div class="cover-practical">Practical 8: Spatial Filtering — Smoothing & Sharpening</div>
<table class="cover-table">
<tr><td>Submitted by:</td><td>Divya Mohan</td></tr>
<tr><td>Programme:</td><td>B.Tech CSE (Cybersecurity)</td></tr>
<tr><td>Semester:</td><td>8th</td></tr>
<tr><td>Faculty Guide:</td><td>Ishani Sharma</td></tr>
<tr><td>Reference Text:</td><td>Gonzalez & Woods, <em>Digital Image Processing</em>, 3rd Ed.</td></tr>
</table>
<div class="cover-rule-sm"></div>
<div class="cover-session">Academic Session 2025–2026</div>
</div>
<div class="print-only print-header" aria-hidden="true">
<div class="ph-line">Shoolini University · CSU2543 · Digital Image Processing · Divya Mohan</div>
</div>
<main id="main-content">
<div class="container" style="max-width: 960px;">
<h1 style="margin: 2rem 0 0.5rem; font-size: clamp(1.6rem,1rem+2.5vw,2.5rem); font-weight: 800; letter-spacing: -0.04em;">Practical 8: Spatial Filtering — Smoothing & Sharpening</h1>
<p style="color: var(--text-muted); margin-bottom: 0.5rem;">
<a href="https://colab.research.google.com/github/divyamohan1993/dip-practical/blob/main/Practical_8.ipynb" target="_blank" rel="noopener">
<img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open Practical 8 in Colab" style="vertical-align: middle;">
</a>
<span style="font-size: 0.82rem; margin-left: 0.4rem;">Open the Python notebook version</span>
</p>
<section id="aim" class="section-card">
<h3>Aim</h3>
<p>To implement and analyze the four canonical spatial-domain filters of digital image processing: box (averaging) and median filters for smoothing, and Laplacian and Sobel operators for sharpening and edge detection.</p>
<div class="theory-box">
<strong>Theory:</strong>
<ul style="margin-top:0.5rem;margin-bottom:0">
<li><strong>Box (mean):</strong> \( \hat f = \frac{1}{mn}\sum f \) — linear smoothing.</li>
<li><strong>Median:</strong> \( \hat f = \mathrm{median}\{f\} \) — non-linear, edge-preserving, ideal for impulse noise.</li>
<li><strong>Laplacian:</strong> \( \nabla^2 f \approx f_N + f_S + f_E + f_W - 4f \); sharpened \(g = f - \nabla^2 f\).</li>
<li><strong>Sobel:</strong> \(G_x, G_y\) gradients, magnitude \(|G|\approx |G_x|+|G_y|\), direction \(\theta=\arctan(G_y/G_x)\).</li>
</ul>
</div>
</section>
<div class="picker-bar" id="mainPicker"></div>
<section id="part1" class="section-card">
<h3>Part 1: Box Filter (Smoothing)</h3>
<p>Apply the box filter at multiple kernel sizes (3, 5, 9, 15) to the selected image. Larger kernels produce stronger blur because they low-pass at progressively lower cut-off frequencies (proportional to \(1/k\)).</p>
<button class="btn-run" id="btnBox" disabled>Run Box Series</button>
<div id="part1Result"></div>
</section>
<section id="part2" class="section-card">
<h3>Part 2: Median Filter on Salt-and-Pepper Noise</h3>
<p>Inject 5% salt-and-pepper noise into the selected image (the static deployment generates the noise client-side, equivalent to G&W Fig. 3.35) and apply the median filter at multiple kernel sizes.</p>
<button class="btn-run" id="btnMedian" disabled>Run Median Series</button>
<div id="part2Result"></div>
</section>
<section id="part3" class="section-card">
<h3>Part 3: Box vs Median — Edge Preservation</h3>
<p>Apply box and median at the same 3×3 kernel size to the noisy input. The box filter blurs noise and edges together; the median filter removes the noise but leaves step edges intact.</p>
<button class="btn-run" id="btnCompare" disabled>Compare Box vs Median</button>
<div id="part3Result"></div>
</section>
<section id="part4" class="section-card">
<h3>Part 4: Laplacian Sharpening</h3>
<p>Compute \(\nabla^2 f\) with the 4-neighbour and 8-neighbour kernels and form the sharpened image \(g = f - \nabla^2 f\).</p>
<button class="btn-run" id="btnLaplacian" disabled>Run Laplacian</button>
<div id="part4Result"></div>
</section>
<section id="part5" class="section-card">
<h3>Part 5: Sobel Edge Detection</h3>
<p>Compute \(G_x\), \(G_y\), gradient magnitude \(|G|\) and direction \(\theta\), then threshold the magnitude to obtain a clean edge map.</p>
<button class="btn-run" id="btnSobel" disabled>Run Sobel</button>
<div id="part5Result"></div>
</section>
<div class="analysis-box">
<h4>Analysis Questions</h4>
<ol>
<li>As the box-filter kernel grows from 3×3 to 35×35, what happens to the image, and why does this happen at \(O(1/k)\) in the spatial-frequency sense?</li>
<li>Why does the median filter remove salt-and-pepper noise without blurring edges, while the box filter blurs both?</li>
<li>The Laplacian is a second-derivative operator. Why does subtracting its response sharpen the image rather than darken it? How does the 8-neighbour kernel differ from the 4-neighbour version?</li>
<li>Sobel uses two separate kernels and combines their magnitudes. Why is it preferred over the Laplacian for edge detection, even though both involve derivatives?</li>
</ol>
</div>
</div>
</main>
<footer class="site-footer" aria-label="Site footer">
<div class="container">
<p>CSU2543 Digital Image Processing · Shoolini University · Divya Mohan · Ishani Sharma</p>
</div>
</footer>
<div id="toast" class="toast" role="status" aria-live="polite" aria-atomic="true"></div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<script src="./js/core.js"></script>
<script src="./js/theme.js"></script>
<script src="./js/nav.js"></script>
<script src="./js/interactive.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chart.js@4/dist/chart.umd.min.js"></script>
<script src="./js/dip.js"></script>
<script src="./js/plot.js"></script>
<script src="./js/picker.js"></script>
<script>
(function() {
'use strict';
var picker;
var noisyCache = null; // cached salt-pepper noisy version of current image
function showError(container, msg) {
container.textContent = '';
var p = document.createElement('p');
p.style.color = 'var(--error)';
p.textContent = 'Error: ' + msg;
container.appendChild(p);
}
function loadCurrent() {
var sel = picker.getSelection();
return DIP.loadImage(sel.url);
}
DIP.createPicker('mainPicker', {
label: 'Image',
defaultImage: 'cameraman',
onReady: function() {
document.querySelectorAll('.btn-run').forEach(function(b) { b.disabled = false; });
}
}).then(function(p) {
picker = p;
p.onChange(function() { noisyCache = null; });
});
// ---- Part 1: Box Filter ----
document.getElementById('btnBox').addEventListener('click', function() {
var container = document.getElementById('part1Result');
DIP.setLoading(container, true);
loadCurrent().then(function(img) {
var sizes = [3, 5, 9, 15];
var panelDefs = [{ type: 'image', title: 'Original' }];
sizes.forEach(function(k) { panelDefs.push({ type: 'image', title: 'Box ' + k + 'x' + k }); });
var panels = DIP.figure(container, 'Box Filter: Effect of Kernel Size', 5, panelDefs);
DIP.draw.gray(panels[0].canvas, img.gray, img.width, img.height);
sizes.forEach(function(k, idx) {
var out = DIP.boxFilter(img.gray, img.width, img.height, k);
DIP.draw.gray(panels[idx + 1].canvas, out, img.width, img.height);
});
DIP.setLoading(container, false);
}).catch(function(err) {
DIP.setLoading(container, false);
showError(container, err.message);
});
});
// ---- Part 2: Median Filter on noisy image ----
document.getElementById('btnMedian').addEventListener('click', function() {
var container = document.getElementById('part2Result');
DIP.setLoading(container, true);
loadCurrent().then(function(img) {
if (!noisyCache) {
noisyCache = {
gray: DIP.saltPepperNoise(img.gray, 0.05),
width: img.width, height: img.height,
};
}
var sizes = [3, 5, 7, 9];
var panelDefs = [{ type: 'image', title: 'Noisy (5% salt & pepper)' }];
sizes.forEach(function(k) { panelDefs.push({ type: 'image', title: 'Median ' + k + 'x' + k }); });
var panels = DIP.figure(container, 'Median Filter on Salt-and-Pepper Noise', 5, panelDefs);
DIP.draw.gray(panels[0].canvas, noisyCache.gray, noisyCache.width, noisyCache.height);
sizes.forEach(function(k, idx) {
var out = DIP.medianFilter(noisyCache.gray, noisyCache.width, noisyCache.height, k);
DIP.draw.gray(panels[idx + 1].canvas, out, noisyCache.width, noisyCache.height);
});
DIP.setLoading(container, false);
}).catch(function(err) {
DIP.setLoading(container, false);
showError(container, err.message);
});
});
// ---- Part 3: Box vs Median ----
document.getElementById('btnCompare').addEventListener('click', function() {
var container = document.getElementById('part3Result');
DIP.setLoading(container, true);
loadCurrent().then(function(img) {
if (!noisyCache) {
noisyCache = {
gray: DIP.saltPepperNoise(img.gray, 0.05),
width: img.width, height: img.height,
};
}
var noisy = noisyCache.gray;
var w = noisyCache.width, h = noisyCache.height;
var k = 3;
var box = DIP.boxFilter(noisy, w, h, k);
var med = DIP.medianFilter(noisy, w, h, k);
var diffBox = DIP.absDiff(noisy, box);
var diffMed = DIP.absDiff(noisy, med);
var panels = DIP.figure(container, 'Box vs Median: Edge Preservation', 3, [
{ type: 'image', title: 'Noisy input' },
{ type: 'image', title: 'Box ' + k + 'x' + k },
{ type: 'image', title: 'Median ' + k + 'x' + k },
{ type: 'image', title: '' },
{ type: 'image', title: 'Removed by Box (noise + edges)' },
{ type: 'image', title: 'Removed by Median (mostly noise)' }
]);
DIP.draw.gray(panels[0].canvas, noisy, w, h);
DIP.draw.gray(panels[1].canvas, box, w, h);
DIP.draw.gray(panels[2].canvas, med, w, h);
DIP.draw.gray(panels[4].canvas, diffBox, w, h);
DIP.draw.gray(panels[5].canvas, diffMed, w, h);
var meanBox = 0, meanMed = 0;
for (var i = 0; i < diffBox.length; i++) { meanBox += diffBox[i]; meanMed += diffMed[i]; }
meanBox /= diffBox.length; meanMed /= diffMed.length;
var note = document.createElement('p');
note.style.fontSize = '0.9rem';
note.style.color = 'var(--text-muted)';
note.textContent = 'Mean energy removed by box: ' + meanBox.toFixed(2) +
' | by median: ' + meanMed.toFixed(2);
container.appendChild(note);
DIP.setLoading(container, false);
}).catch(function(err) {
DIP.setLoading(container, false);
showError(container, err.message);
});
});
// ---- Part 4: Laplacian ----
document.getElementById('btnLaplacian').addEventListener('click', function() {
var container = document.getElementById('part4Result');
DIP.setLoading(container, true);
loadCurrent().then(function(img) {
var lap4 = DIP.laplacian(img.gray, img.width, img.height, 4);
var lap8 = DIP.laplacian(img.gray, img.width, img.height, 8);
var sharp4 = DIP.laplacianSharpen(img.gray, img.width, img.height, 4);
var sharp8 = DIP.laplacianSharpen(img.gray, img.width, img.height, 8);
var panels = DIP.figure(container, 'Laplacian Sharpening', 3, [
{ type: 'image', title: 'Original' },
{ type: 'image', title: 'Laplacian (4-neighbour)' },
{ type: 'image', title: 'Sharpened: f - lap4' },
{ type: 'image', title: 'Original' },
{ type: 'image', title: 'Laplacian (8-neighbour)' },
{ type: 'image', title: 'Sharpened: f - lap8' }
]);
DIP.draw.gray(panels[0].canvas, img.gray, img.width, img.height);
DIP.draw.gray(panels[1].canvas, DIP.normalizeUint8(lap4), img.width, img.height);
DIP.draw.gray(panels[2].canvas, sharp4, img.width, img.height);
DIP.draw.gray(panels[3].canvas, img.gray, img.width, img.height);
DIP.draw.gray(panels[4].canvas, DIP.normalizeUint8(lap8), img.width, img.height);
DIP.draw.gray(panels[5].canvas, sharp8, img.width, img.height);
DIP.setLoading(container, false);
}).catch(function(err) {
DIP.setLoading(container, false);
showError(container, err.message);
});
});
// ---- Part 5: Sobel ----
document.getElementById('btnSobel').addEventListener('click', function() {
var container = document.getElementById('part5Result');
DIP.setLoading(container, true);
loadCurrent().then(function(img) {
var s = DIP.sobel(img.gray, img.width, img.height);
var thresh = DIP.threshold(s.Gmag, 64);
var panels = DIP.figure(container, 'Sobel Edge Detection', 3, [
{ type: 'image', title: 'Original' },
{ type: 'image', title: '|Gx| (vertical edges)' },
{ type: 'image', title: '|Gy| (horizontal edges)' },
{ type: 'image', title: '|G| = |Gx|+|Gy|' },
{ type: 'image', title: 'Gradient direction' },
{ type: 'image', title: 'Thresholded edges' }
]);
DIP.draw.gray(panels[0].canvas, img.gray, img.width, img.height);
DIP.draw.gray(panels[1].canvas, s.GxAbs, img.width, img.height);
DIP.draw.gray(panels[2].canvas, s.GyAbs, img.width, img.height);
DIP.draw.gray(panels[3].canvas, s.Gmag, img.width, img.height);
DIP.draw.gray(panels[4].canvas, s.GthetaUint8, img.width, img.height);
DIP.draw.gray(panels[5].canvas, thresh, img.width, img.height);
DIP.setLoading(container, false);
}).catch(function(err) {
DIP.setLoading(container, false);
showError(container, err.message);
});
});
})();
</script>
</body>
</html>