speedy-vision
Version:
GPU-accelerated Computer Vision for JavaScript
129 lines (108 loc) • 4.1 kB
HTML
<!--
speedy-vision.js
GPU-accelerated Computer Vision for JavaScript
Copyright 2020-2022 Alexandre Martins <alemartf(at)gmail.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
image-blurring.html
Gaussian Blur
-->
<html>
<head>
<meta charset="utf-8">
<meta name="description" content="speedy-vision.js: GPU-accelerated Computer Vision for JavaScript">
<meta name="author" content="Alexandre Martins">
<title>Gaussian Blur</title>
<script src="../dist/speedy-vision.js"></script>
<link href="style.css" rel="stylesheet">
</head>
<body>
<h1>Gaussian Blur</h1>
<form autocomplete="off">
<div>
<label for="ksize">Kernel size</label>
<select id="ksize">
<option value="3">3x3</option>
<option value="5">5x5</option>
<option value="7">7x7</option>
<option value="9">9x9</option>
<option value="11" selected>11x11</option>
<option value="13">13x13</option>
<option value="15">15x15</option>
</select>
<span> </span>
</div>
<div>
<label for="sigma">Sigma</label>
<input type="range" min="0" max="5" value="0" step="0.1" id="sigma">
<span id="sigma-view">auto</span>
</div>
</form>
<img src="../assets/speedy-wall.jpg" title="Image by Bride of Frankenstein (CC-BY)">
<script>
window.onload = async function()
{
// form controls
const ksize = document.getElementById('ksize');
const sigma = document.getElementById('sigma');
const sigmaView = document.getElementById('sigma-view');
ksize.oninput = render;
sigma.oninput = () => {
sigmaView.innerText = sigma.value > 0 ? sigma.value : 'auto';
render();
};
/*
This is the pipeline:
Image ---> Gaussian ---> Image
Source Blur Sink
*/
// load an image
const img = document.querySelector('img');
const media = await Speedy.load(img);
// create the pipeline
const pipeline = Speedy.Pipeline();
const source = Speedy.Image.Source();
const sink = Speedy.Image.Sink();
const gaussian = Speedy.Filter.GaussianBlur();
source.media = media;
source.output().connectTo(gaussian.input());
gaussian.output().connectTo(sink.input());
pipeline.init(source, sink, gaussian);
// display the result
const canvas = createCanvas(media.width, media.height, img.title);
async function render()
{
gaussian.kernelSize = Speedy.Size(ksize.value, ksize.value);
gaussian.sigma = Speedy.Vector2(sigma.value, sigma.value);
const { image } = await pipeline.run();
draw(image, canvas);
}
render();
}
function createCanvas(width, height, title)
{
const canvas = document.getElementById('canvas-demo') || document.createElement('canvas');
canvas.width = width;
canvas.height = height;
canvas.title = title;
if(!document.body.contains(canvas))
document.body.appendChild(canvas);
return canvas;
}
function draw(media, canvas, x = 0, y = 0, width = media.width, height = media.height)
{
const ctx = canvas.getContext('2d');
ctx.drawImage(media.source, x, y, width, height);
}
</script>
<mark>Powered by <a href="https://github.com/alemart/speedy-vision">speedy-vision.js</a></mark>
</body>
</html>