Repository navigation
Expand file tree
/
Copy pathext3_kernel_compare.html
More file actions
executable file
·524 lines (478 loc) · 20.4 KB
/
Copy pathext3_kernel_compare.html
File metadata and controls
executable file
·524 lines (478 loc) · 20.4 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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
<!--
* @Author: NaoMenDDD 2017954808@qq.com
* @Date: 2026-05-19 20:15:25
* @LastEditors: QingKongWuYu 2017954808@qq.com
* @LastEditTime: 2026-05-26 04:51:15
* @Description: Sobel 边缘检测:卷积核大小对比(3、7、11) - 理论与建模 & 预期实验结果
-->
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sobel 边缘检测:卷积核大小对比</title>
<!-- MathJax 专业公式渲染 -->
<script src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js" id="MathJax-script" async></script>
<style>
:root {
--bg: #ffffff;
--surface: #f5f5f7;
--text: #1d1d1f;
--text-secondary: #515154;
--text-tertiary: #86868b;
--accent: #c76b3d;
--accent-light: #fdf3ee;
--border: #d2d2d7;
--border-light: #e5e5ea;
--shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.04);
--shadow-md: 0 4px 16px rgba(0, 0, 0, 0.08);
--shadow-lg: 0 12px 40px rgba(0, 0, 0, 0.1);
--radius: 16px;
--radius-xl: 24px;
--transition: 0.5s cubic-bezier(0.22, 1, 0.36, 1);
--font: -apple-system, BlinkMacSystemFont, 'SF Pro Display', 'PingFang SC', 'Microsoft YaHei', sans-serif;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html {
scroll-behavior: smooth;
}
body {
font-family: var(--font);
background: var(--bg);
color: var(--text);
line-height: 1.5;
-webkit-font-smoothing: antialiased;
overflow-x: hidden;
letter-spacing: -0.01em;
}
.progress-bar {
position: fixed;
top: 0;
left: 0;
height: 3px;
background: var(--accent);
z-index: 1000;
transition: width 0.1s linear;
width: 0;
}
.container {
max-width: 1100px;
margin: 0 auto;
padding: 60px 30px 100px;
}
.section {
margin-bottom: 100px;
opacity: 0;
transform: translateY(30px);
transition: opacity 0.8s cubic-bezier(0.22, 1, 0.36, 1), transform 0.8s cubic-bezier(0.22, 1, 0.36, 1);
}
.section.visible {
opacity: 1;
transform: translateY(0);
}
.hero {
text-align: center;
padding: 40px 0 20px;
opacity: 0;
transform: translateY(20px);
transition: opacity 0.8s cubic-bezier(0.22, 1, 0.36, 1) 0.1s, transform 0.8s cubic-bezier(0.22, 1, 0.36, 1) 0.1s;
}
.hero.visible {
opacity: 1;
transform: translateY(0);
}
.hero .tag {
display: inline-block;
font-size: 0.8rem;
font-weight: 600;
letter-spacing: 0.04em;
text-transform: uppercase;
color: var(--accent);
background: var(--accent-light);
padding: 5px 14px;
border-radius: 20px;
margin-bottom: 16px;
}
.hero h1 {
font-size: 2.4rem;
font-weight: 700;
letter-spacing: -0.03em;
margin-bottom: 8px;
background: linear-gradient(135deg, #1d1d1f 0%, #6b3a2a 100%);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
background-clip: text;
}
.hero .subtitle {
font-size: 1.1rem;
color: var(--text-secondary);
font-weight: 400;
max-width: 720px;
margin: 0 auto;
}
.card {
background: var(--surface);
border-radius: var(--radius-xl);
padding: 40px 44px;
box-shadow: var(--shadow-sm);
border: 1px solid var(--border-light);
transition: all var(--transition);
}
.card:hover {
box-shadow: var(--shadow-lg);
transform: translateY(-2px);
border-color: #c5c5cc;
}
.card h2 {
font-size: 1.6rem;
font-weight: 700;
margin-bottom: 24px;
letter-spacing: -0.02em;
display: flex;
align-items: center;
gap: 12px;
}
.card h2 .num {
display: inline-flex;
align-items: center;
justify-content: center;
width: 36px;
height: 36px;
background: var(--accent);
color: #fff;
border-radius: 10px;
font-size: 1rem;
font-weight: 700;
flex-shrink: 0;
}
.equation {
background: #ffffff;
padding: 14px 18px;
border-radius: var(--radius);
margin: 12px 0;
border-left: 3px solid var(--accent);
overflow-x: auto;
box-shadow: var(--shadow-sm);
font-size: 1rem;
line-height: 1.6;
}
.eq-caption {
font-size: 0.78rem;
color: var(--text-tertiary);
margin-top: -6px;
margin-bottom: 16px;
padding-left: 24px;
}
.knowledge-badge {
display: inline-flex;
align-items: center;
gap: 5px;
font-size: 0.72rem;
font-weight: 600;
letter-spacing: 0.03em;
color: var(--accent);
background: var(--accent-light);
padding: 4px 12px;
border-radius: 14px;
margin-right: 8px;
margin-bottom: 8px;
white-space: nowrap;
}
.knowledge-badge::before {
content: '📘';
font-size: 0.85rem;
}
.compare-table {
display: grid;
grid-template-columns: 1fr 1fr 1fr 1fr;
gap: 0;
margin: 20px 0;
border-radius: var(--radius);
overflow: hidden;
box-shadow: var(--shadow-sm);
font-size: 0.85rem;
}
.compare-table .th {
background: #1d1d1f;
color: #fff;
padding: 12px 16px;
font-weight: 700;
text-align: center;
font-size: 0.8rem;
}
.compare-table .td {
background: #ffffff;
padding: 12px 16px;
text-align: center;
border-bottom: 1px solid var(--border-light);
color: var(--text-secondary);
}
.compare-table .td:first-child {
font-weight: 600;
color: var(--text);
}
.canvas-wrapper {
background: #ffffff;
border-radius: var(--radius);
padding: 12px;
box-shadow: var(--shadow-sm);
text-align: center;
}
.canvas-caption {
font-size: 0.78rem;
color: var(--text-secondary);
margin-top: 8px;
font-weight: 500;
}
.demo-grid {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 20px;
margin: 20px 0;
}
.footer-note {
text-align: center;
font-size: 0.85rem;
color: var(--text-tertiary);
margin-top: 80px;
padding: 20px;
border-top: 1px solid var(--border-light);
}
@media (max-width: 768px) {
.container {
padding: 30px 16px 60px;
}
.hero h1 {
font-size: 1.6rem;
}
.card {
padding: 24px 20px;
}
.compare-table {
font-size: 0.7rem;
}
.demo-grid {
grid-template-columns: 1fr;
}
}
</style>
</head>
<body>
<div class="progress-bar" id="progressBar"></div>
<div class="container">
<div class="hero" id="hero">
<span class="tag">信号与系统 · 课设 · 延伸</span>
<h1>Sobel边缘检测:卷积核大小的影响</h1>
<p class="subtitle">对比 Sobel 算子不同核大小(3、7、11)对边缘检测结果的影响,分析平滑性、边缘粗细与噪声抑制的权衡</p>
</div>
<!-- 第一部分:理论与建模 -->
<div class="section" id="section1">
<div class="card">
<h2><span class="num">1</span> 理论与建模</h2>
<h3 style="font-weight:600; font-size:1rem; margin-bottom:10px;">Sobel 算子与核尺寸</h3>
<p style="color: var(--text-secondary); margin-bottom: 16px;">
Sobel 算子是一种离散微分算子,通过计算图像亮度函数的梯度来检测边缘。其核大小(ksize)决定了参与梯度计算的邻域范围,直接影响边缘的平滑程度、定位精度和噪声鲁棒性。
</p>
<div class="equation">
\[
\text{Sobel 核的通用构造(分离形式)}:
\]
\[
G_x = \mathbf{s}_y^T \cdot \mathbf{d}_x, \qquad
G_y = \mathbf{d}_y^T \cdot \mathbf{s}_x
\]
</div>
<div class="eq-caption">
其中 \(\mathbf{s}\) 为平滑核(归一化的三角窗或高斯核),\(\mathbf{d}\) 为反对称差分核。不同核大小对应的平滑核与差分核长度不同:
\[
\begin{aligned}
\text{ksize}=3 &: \quad \mathbf{s} = \frac{1}{4}[1,2,1],\ \mathbf{d} = [-1,0,1] \\[4pt]
\text{ksize}=7 &: \quad \mathbf{s} = \frac{1}{63}[1,2,3,3,3,2,1],\ \mathbf{d} = [-3,-2,-1,0,1,2,3]
\\[4pt]
\text{ksize}=11 &: \quad \mathbf{s} = \frac{1}{..}[1,2,3,4,5,5,5,4,3,2,1],\ \mathbf{d} =
[-5,-4,-3,-2,-1,0,1,2,3,4,5]
\end{aligned}
\]
通过外积即可得到完整的二维卷积核。
</div>
<p style="color: var(--text-secondary); margin-top: 12px;">
<strong>核大小的影响机制:</strong> 更大的核覆盖更多的像素,相当于在求导前先进行更强的平滑滤波。因此,核越大,对噪声的抑制能力越强,但边缘变得越粗,细节丢失越严重。
</p>
<h3 style="font-weight:600; font-size:1rem; margin-top:28px; margin-bottom:10px;">核大小与边缘检测性能</h3>
<div class="demo-grid">
<div class="canvas-wrapper">
<canvas id="originalImage" width="280" height="200"></canvas>
<div class="canvas-caption">原始图像(模拟)</div>
</div>
<div class="canvas-wrapper">
<canvas id="sobelKernel3" width="280" height="200"></canvas>
<div class="canvas-caption">Sobel (ksize=3) — 细节丰富,边缘较细</div>
</div>
<div class="canvas-wrapper">
<canvas id="sobelKernel7" width="280" height="200"></canvas>
<div class="canvas-caption">Sobel (ksize=7) — 边缘变粗,细节减少</div>
</div>
<div class="canvas-wrapper">
<canvas id="sobelKernel11" width="280" height="200"></canvas>
<div class="canvas-caption">Sobel (ksize=11) — 强平滑,粗边缘,噪声抑制好</div>
</div>
</div>
<div class="compare-table">
<div class="th">属性</div>
<div class="th">ksize = 3</div>
<div class="th">ksize = 7</div>
<div class="th">ksize = 11</div>
<div class="td">边缘定位精度</div>
<div class="td">最高</div>
<div class="td">中等</div>
<div class="td">较低</div>
<div class="td">噪声敏感性</div>
<div class="td">高</div>
<div class="td">中等</div>
<div class="td">低</div>
<div class="td">边缘粗细</div>
<div class="td">细(1~2 像素)</div>
<div class="td">较粗(3~5 像素)</div>
<div class="td">粗(5~7 像素)</div>
<div class="td">计算复杂度</div>
<div class="td">低</div>
<div class="td">中等</div>
<div class="td">较高</div>
</div>
<p style="color: var(--text-secondary); margin-top: 20px;">
<strong>数学本质:</strong> 增大 Sobel
核等价于在更大的邻域内近似梯度,平滑作用增强,因此大核适用于强噪声环境或对边缘精确位置要求不高的任务;小核保留更多细节,适合精细边缘检测。
</p>
<span class="knowledge-badge">离散梯度近似</span>
<span class="knowledge-badge">梯度与尺度空间</span>
<span class="knowledge-badge">多尺度边缘检测</span>
</div>
</div>
<!-- 第二部分:预期实验结果/效果对比 -->
<div class="section" id="section2">
<div class="card">
<h2><span class="num">2</span> 预期实验结果:结论与工程选型建议</h2>
<p style="color: var(--text-secondary);">
Sobel 算子的核大小是影响边缘检测效果的关键参数。<br>
<strong>小尺寸核(如 3×3)</strong>:边缘定位精度高,细节丰富,但对噪声敏感,适合清晰、低噪声的图像。<br>
<strong>大尺寸核(如 7×7、11×11)</strong>:平滑作用强,能有效抑制噪声,但边缘会变粗,细节损失较多,适合高噪声或对边缘精确位置要求不高的场景。<br>
实际应用中需根据图像噪声水平和任务需求权衡:若侧重细节则用小核,若侧重鲁棒性则用大核。也可采用多尺度策略或结合 Canny 等更高级的算子。
</p>
<span class="knowledge-badge">参数调优</span>
<span class="knowledge-badge">工程实践</span>
</div>
</div>
<div class="footer-note">信号与系统 · 灰度图像频域处理 | Sobel 核大小对比</div>
</div>
<script>
(() => {
const progressBar = document.getElementById('progressBar');
window.addEventListener('scroll', () => {
const scrollTop = window.scrollY;
const docHeight = document.documentElement.scrollHeight - window.innerHeight;
progressBar.style.width = (scrollTop / docHeight) * 100 + '%';
});
const sections = document.querySelectorAll('.section');
const hero = document.getElementById('hero');
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => { if (entry.isIntersecting) entry.target.classList.add('visible'); });
}, { rootMargin: '0px 0px -60px 0px', threshold: 0.15 });
sections.forEach(s => observer.observe(s));
const heroObs = new IntersectionObserver((entries) => {
entries.forEach(entry => { if (entry.isIntersecting) entry.target.classList.add('visible'); });
}, { threshold: 0.2 });
heroObs.observe(hero);
// 绘制原始图像(模拟一个带有圆形边缘 + 噪声的原始图)
function drawOriginal(canvasId) {
const canvas = document.getElementById(canvasId);
if (!canvas) return;
const ctx = canvas.getContext('2d');
const w = canvas.width, h = canvas.height;
const imageData = ctx.createImageData(w, h);
for (let y = 0; y < h; y++) {
for (let x = 0; x < w; x++) {
const dx = x - w / 2, dy = y - h / 2;
const dist = Math.sqrt(dx * dx + dy * dy);
// 理想圆形区域,内部亮,外部暗
let val = (dist < 60) ? 200 : 50;
// 添加一些纹理噪声
const noise = Math.random() * 40;
val = Math.min(255, Math.max(0, val + noise));
const idx = (y * w + x) * 4;
imageData.data[idx] = val;
imageData.data[idx + 1] = val;
imageData.data[idx + 2] = val;
imageData.data[idx + 3] = 255;
}
}
ctx.putImageData(imageData, 0, 0);
ctx.fillStyle = '#fff';
ctx.font = 'bold 12px -apple-system, sans-serif';
ctx.shadowColor = 'black';
ctx.shadowBlur = 2;
ctx.fillText('含噪声 & 边缘', w - 90, 20);
ctx.shadowColor = 'transparent';
}
// 模拟 Sobel 边缘效果(径向渐变 + 人工纹理)
function drawSobelSim(canvasId, ksize, smoothing) {
const canvas = document.getElementById(canvasId);
if (!canvas) return;
const ctx = canvas.getContext('2d');
const w = canvas.width, h = canvas.height;
const imageData = ctx.createImageData(w, h);
// 模拟一个带有圆形边缘 + 噪声 + 不同平滑水平的图像
for (let y = 0; y < h; y++) {
for (let x = 0; x < w; x++) {
const dx = x - w / 2, dy = y - h / 2;
const dist = Math.sqrt(dx * dx + dy * dy);
// 理想边缘:半径 65 处阶跃
let edgeVal = 0;
if (Math.abs(dist - 65) < smoothing) {
// 边缘宽度随平滑程度增加
edgeVal = 255 * (1 - Math.abs(dist - 65) / smoothing);
}
// 添加模拟噪声
let noise = 0;
if (ksize === 3) noise = Math.random() * 80;
else if (ksize === 7) noise = Math.random() * 40;
else noise = Math.random() * 20;
let val = edgeVal + noise;
val = Math.min(255, Math.max(0, val));
const idx = (y * w + x) * 4;
imageData.data[idx] = val;
imageData.data[idx + 1] = val;
imageData.data[idx + 2] = val;
imageData.data[idx + 3] = 255;
}
}
ctx.putImageData(imageData, 0, 0);
// 加文字标注
ctx.fillStyle = '#fff';
ctx.font = 'bold 12px -apple-system, sans-serif';
ctx.shadowColor = 'black';
ctx.shadowBlur = 2;
if (ksize === 3) ctx.fillText('更多细节', w - 80, 20);
else if (ksize === 7) ctx.fillText('边缘变粗', w - 70, 20);
else ctx.fillText('强平滑', w - 60, 20);
ctx.shadowColor = 'transparent';
}
setTimeout(() => {
drawOriginal('originalImage');
drawSobelSim('sobelKernel3', 3, 3);
drawSobelSim('sobelKernel7', 7, 6);
drawSobelSim('sobelKernel11', 11, 9);
}, 400);
window.addEventListener('resize', () => {
drawOriginal('originalImage');
drawSobelSim('sobelKernel3', 3, 3);
drawSobelSim('sobelKernel7', 7, 6);
drawSobelSim('sobelKernel11', 11, 9);
});
})();
</script>
</body>
</html>