-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathconvolutie.html
More file actions
158 lines (146 loc) · 5.44 KB
/
convolutie.html
File metadata and controls
158 lines (146 loc) · 5.44 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
---
permalink: /convolutie/
---
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Convolution Filter App</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
font-family: Arial, sans-serif;
}
.container {
display: flex;
gap: 20px;
}
canvas {
border: 1px solid black;
}
.filters {
display: flex;
flex-direction: column;
gap: 10px;
}
.filter {
padding: 10px;
border: 1px solid black;
cursor: pointer;
text-align: center;
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 2px;
width: 100px;
justify-content: center;
}
.filter div {
padding: 5px;
text-align: center;
border: 1px solid gray;
background-color: lightgray;
}
/* Make .cell elements square */
.cell {
aspect-ratio: 1 / 1;
}
/* display the elements inside the element_column class in a flex column */
.element_column {
display: flex;
flex-direction: column;
gap: 10px;
}
/* Center the text inside the col_title class */
.col_title {
text-align: center;
font-size: medium;
}
</style>
</head>
<body>
<div class="container">
<div class="element_column">
<h1 class="col_title">Invoer afbeelding</h1>
<canvas id="inputCanvas" width="250" height="196"></canvas>
</div>
<div class="element_column">
<h1 class="col_title">Filter</h1>
<div class="filters" id="filters"></div>
</div>
<div class="element_column">
<h1 class="col_title">Uitvoer afbeelding</h1>
<canvas id="outputCanvas" width="250" height="196"></canvas>
</div>
</div>
<script>
const filters = [
{ name: 'Edge Detection', kernel: [-1, -1, -1, -1, 8, -1, -1, -1, -1] },
{ name: 'Blur', kernel: [1/9, 1/9, 1/9, 1/9, 1/9, 1/9, 1/9, 1/9, 1/9] },
{ name: 'Sharpen', kernel: [0, -1, 0, -1, 5, -1, 0, -1, 0] },
{ name: 'Emboss', kernel: [-2, -1, 0, -1, 1, 1, 0, 1, 2] },
{ name: 'Outline', kernel: [0, 1, 0, 1, -4, 1, 0, 1, 0] }
];
const inputCanvas = document.getElementById('inputCanvas');
const outputCanvas = document.getElementById('outputCanvas');
const ctxInput = inputCanvas.getContext('2d');
const ctxOutput = outputCanvas.getContext('2d');
const filtersDiv = document.getElementById('filters');
function fractionFormat(value) {
if (value === 1/9) return '1/9';
if (value === -1/9) return '-1/9';
if (Number.isInteger(value)) return value.toString();
return value.toFixed(2);
}
filters.forEach(filter => {
const filterDiv = document.createElement('div');
filterDiv.classList.add('filter');
filterDiv.onclick = () => applyFilter(filter.kernel);
filter.kernel.forEach(value => {
const cell = document.createElement('div');
cell.classList.add('cell');
cell.textContent = fractionFormat(value);
filterDiv.appendChild(cell);
});
filtersDiv.appendChild(filterDiv);
});
const img = new Image();
img.src = '/assets/images/convolutie/igent.png'; // Example B/W image
img.crossOrigin = "anonymous";
img.onload = () => {
ctxInput.drawImage(img, 0, 0, inputCanvas.width, inputCanvas.height);
};
function applyFilter(kernel) {
const imageData = ctxInput.getImageData(0, 0, inputCanvas.width, inputCanvas.height);
const data = imageData.data;
const width = imageData.width;
const height = imageData.height;
const outputData = ctxOutput.createImageData(width, height);
const outputPixels = outputData.data;
for (let y = 1; y < height - 1; y++) {
for (let x = 1; x < width - 1; x++) {
let r = 0, g = 0, b = 0;
let index = (y * width + x) * 4;
for (let ky = -1; ky <= 1; ky++) {
for (let kx = -1; kx <= 1; kx++) {
let pixelIndex = ((y + ky) * width + (x + kx)) * 4;
let weight = kernel[(ky + 1) * 3 + (kx + 1)];
r += data[pixelIndex] * weight;
g += data[pixelIndex + 1] * weight;
b += data[pixelIndex + 2] * weight;
}
}
outputPixels[index] = Math.min(Math.max(r, 0), 255);
outputPixels[index + 1] = Math.min(Math.max(g, 0), 255);
outputPixels[index + 2] = Math.min(Math.max(b, 0), 255);
outputPixels[index + 3] = 255;
}
}
ctxOutput.putImageData(outputData, 0, 0);
}
</script>
</body>
</html>