Repository navigation
Expand file tree
/
Copy pathext3_sobel_canny_compare.html
More file actions
executable file
·537 lines (486 loc) · 20.4 KB
/
Copy pathext3_sobel_canny_compare.html
File metadata and controls
executable file
·537 lines (486 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
525
526
527
528
529
530
531
532
533
534
535
536
537
<!--
* @Author: NaoMenDDD 2017954808@qq.com
* @Date: 2026-05-19 10:00:00
* @LastEditors: QingKongWuYu 2017954808@qq.com
* @Description: Sobel vs Canny 边缘检测对比 - 理论与建模 & 预期实验结果
-->
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sobel vs Canny 边缘检测对比</title>
<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: #3b6db5;
--accent-light: #eaf1f9;
--color-sobel: #c76b3d;
--color-canny: #3b8c6e;
--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.2rem;
font-weight: 700;
letter-spacing: -0.03em;
margin-bottom: 8px;
background: linear-gradient(135deg, #1d1d1f 0%, #2a4a70 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;
}
.block-diagram {
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: center;
gap: 16px;
margin: 24px 0;
padding: 20px 0;
}
.block {
background: #ffffff;
border: 1.5px solid #dcdce0;
border-radius: var(--radius);
padding: 12px 16px;
text-align: center;
font-weight: 600;
font-size: 0.85rem;
white-space: nowrap;
box-shadow: var(--shadow-sm);
transition: all 0.25s;
min-width: 80px;
}
.block:hover {
border-color: var(--accent);
box-shadow: var(--shadow-md);
transform: translateY(-2px);
}
.block.sobel {
border-color: var(--color-sobel);
background: #fef9f6;
font-weight: 700;
}
.block.canny {
border-color: var(--color-canny);
background: #e8f5ef;
font-weight: 700;
}
.arrow {
font-size: 1.6rem;
color: #86868b;
font-weight: 300;
align-self: center;
}
.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;
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-row {
display: flex;
gap: 20px;
flex-wrap: wrap;
justify-content: center;
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.5rem;
}
.card {
padding: 24px 20px;
}
.block-diagram {
flex-direction: column;
gap: 8px;
}
.arrow {
transform: rotate(90deg);
}
.compare-table {
font-size: 0.7rem;
}
}
</style>
</head>
<body>
<div class="progress-bar" id="progressBar"></div>
<div class="container">
<div class="hero" id="hero">
<span class="tag">信号与系统 · 课设 · 延伸</span>
<h1>Sobel vs Canny 边缘检测对比</h1>
<p class="subtitle">对比空域Sobel梯度与Canny多级边缘检测,分析两种方法的原理差异与效果异同</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;">两种边缘提取路径</h3>
<p style="color: var(--text-secondary); margin-bottom: 16px;">
同一输入图像分别经过<strong>空域Sobel算子</strong>和<strong>Canny多级算法</strong>,得到边缘强度图进行对比。
</p>
<div class="block-diagram" style="flex-wrap:wrap; gap:20px;">
<div style="display:flex; align-items:center; gap:8px; flex-wrap:wrap; justify-content:center;">
<span style="font-weight:700; color:var(--color-sobel);">路径A (Sobel):</span>
<div class="block sobel">f(x,y)</div><span class="arrow">→</span>
<div class="block sobel">Sobel<br><small>Gx, Gy</small></div><span class="arrow">→</span>
<div class="block sobel">|G|<br><small>幅值</small></div><span class="arrow">→</span>
<div class="block sobel">g<sub>A</sub>(x,y)</div>
</div>
<div style="display:flex; align-items:center; gap:8px; flex-wrap:wrap; justify-content:center;">
<span style="font-weight:700; color:var(--color-canny);">路径B (Canny):</span>
<div class="block canny">f(x,y)</div><span class="arrow">→</span>
<div class="block canny">Gaussian<br><small>平滑</small></div><span class="arrow">→</span>
<div class="block canny">梯度<br><small>幅值+方向</small></div><span class="arrow">→</span>
<div class="block canny">非极大值<br><small>抑制</small></div><span class="arrow">→</span>
<div class="block canny">双阈值<br><small>连接</small></div><span class="arrow">→</span>
<div class="block canny">g<sub>B</sub>(x,y)</div>
</div>
</div>
<h3 style="font-weight:600; font-size:1rem; margin-top:28px; margin-bottom:10px;">核心方程对比(第4章、第6章)</h3>
<div class="equation">\[ G_x = f * S_x,\quad G_y = f * S_y \quad\rightarrow\quad |G| = \sqrt{G_x^2 +
G_y^2} \]</div>
<div class="eq-caption">
Sobel算子:3×3局部卷积,S<sub>x</sub>=[[-1,0,1],[-2,0,2],[-1,0,1]],S<sub>y</sub>=S<sub>x</sub><sup>T</sup>。自带平滑效果。
</div>
<div class="equation">
\[
\begin{aligned}
&\text{1. Gaussian平滑: } f_s = f * G_\sigma \\
&\text{2. 梯度幅值/方向: } M = \sqrt{G_x^2+G_y^2}\ \quad \theta = \arctan(G_y/G_x) \\
&\text{3. 非极大值抑制 } \rightarrow \text{4. 双阈值连接}
\end{aligned}
\]
</div>
<div class="eq-caption">Canny边缘检测:多级优化,包含平滑降噪、梯度计算、非极大值抑制和双阈值滞后连接,产生单像素、连续且噪声抑制强的边缘。</div>
<h3 style="font-weight:600; font-size:1rem; margin-top:28px; margin-bottom:10px;">两种边缘检测范式的理论对比</h3>
<div class="demo-row">
<div class="canvas-wrapper"><canvas id="sobelEdgeCanvas" width="260" height="200"></canvas>
<div class="canvas-caption">Sobel边缘 (梯度幅值)</div>
</div>
<div class="canvas-wrapper"><canvas id="cannyEdgeCanvas" width="260" height="200"></canvas>
<div class="canvas-caption">Canny边缘 (单像素、干净)</div>
</div>
</div>
<div class="compare-table">
<div class="th">对比维度</div>
<div class="th">Sobel空域卷积</div>
<div class="th">Canny边缘检测</div>
<div class="td">处理域</div>
<div class="td">空域 (局部3×3)</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">无</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">O(N²) (卷积)</div>
<div class="td">O(N²) (多次卷积)</div>
</div>
<div style="margin-top:14px;">
<span class="knowledge-badge">卷积定理 · 第4章</span>
<span class="knowledge-badge">空域梯度算子</span>
<span class="knowledge-badge">边缘检测理论 · Canny准则</span>
</div>
</div>
</div>
<!-- 第二部分:预期实验结果/效果对比 -->
<div class="section" id="section2">
<div class="card">
<h2><span class="num">2</span> 预期实验结果:适用场景与互补性</h2>
<p style="color:var(--text-secondary);">
<strong>Sobel算子</strong>计算简单、响应速度快,适合实时性要求高且对边缘精度要求不高的任务,但梯度幅值较粗,受噪声干扰。<br>
<strong>Canny边缘检测</strong>在信噪比、定位精度和单边缘响应之间取得了最佳平衡,是目前应用最广泛的边缘检测方法之一,尤其适合高质量图像分析和计算机视觉预处理。<br>
两者通过梯度计算相联系:Sobel直接输出梯度幅值,而Canny在此基础上增加了非极大值抑制和双阈值连接,得到更细、更准确的边缘。
</p>
<span class="knowledge-badge">空域梯度</span>
<span class="knowledge-badge">Canny三准则</span>
</div>
</div>
<div class="footer-note">信号与系统 · 灰度图像频域处理 | Sobel vs Canny边缘检测对比</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 drawSobelEdge() {
const canvas = document.getElementById('sobelEdgeCanvas'); 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);
const edgeDist = Math.abs(dist - 65);
const edge = Math.exp(-edgeDist * edgeDist / 60);
let val = edge * 200 + 10;
val = Math.min(255, Math.max(5, val));
const idx = (y * w + x) * 4;
imageData.data[idx] = imageData.data[idx + 1] = imageData.data[idx + 2] = val;
imageData.data[idx + 3] = 255;
}
}
ctx.putImageData(imageData, 0, 0);
ctx.fillStyle = '#fff';
ctx.font = 'bold 11px -apple-system, sans-serif';
ctx.fillText('边缘较粗', w - 65, 18);
}
function drawCannyEdge() {
const canvas = document.getElementById('cannyEdgeCanvas'); 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);
const edgeDist = Math.abs(dist - 65);
const edge = Math.exp(-edgeDist * edgeDist / 80);
let val = (edge > 0.9) ? 255 : 0;
if (Math.abs(edgeDist) < 3) val = 255;
const idx = (y * w + x) * 4;
imageData.data[idx] = imageData.data[idx + 1] = imageData.data[idx + 2] = val;
imageData.data[idx + 3] = 255;
}
}
ctx.putImageData(imageData, 0, 0);
ctx.fillStyle = '#fff';
ctx.font = 'bold 11px -apple-system, sans-serif';
ctx.fillText('单像素、干净', w - 80, 18);
}
setTimeout(() => { drawSobelEdge(); drawCannyEdge(); }, 400);
window.addEventListener('resize', () => { drawSobelEdge(); drawCannyEdge(); });
})();
</script>
</body>
</html>