-
Notifications
You must be signed in to change notification settings - Fork 0
/
图片彩色转黑白.html
38 lines (37 loc) · 1.15 KB
/
图片彩色转黑白.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>图片彩色转黑白</title>
</head>
<body>
<h1>图片彩色转黑白</h1>
<p id="status">OpenCV.js is loading...</p>
<div>
<input type="file" id="fileInput" name="file"/></div>
<h2>原图</h2>
<img id="imageSrc" style="max-width:40vw;max-height:40vh;" alt="No Image"/>
<h2>处理后</h2>
<canvas id="canvasOutput"></canvas>
</div>
<script type="text/javascript">
let imgElement = document.getElementById('imageSrc');
let inputElement = document.getElementById('fileInput');
inputElement.addEventListener('change', (e) => {
imgElement.src = URL.createObjectURL(e.target.files[0]);
}, false);
imgElement.onload = function () {
let src = cv.imread(imgElement);
let dst = new cv.Mat();
cv.cvtColor(src, dst, cv.COLOR_RGBA2GRAY);
cv.imshow('canvasOutput', dst);
src.delete();
dst.delete();
};
function onOpenCvReady() {
document.getElementById('status').innerHTML = 'OpenCV.js is ready.';
}
</script>
<script async src="opencv-4.5.0.js" onload="onOpenCvReady();" type="text/javascript"></script>
</body>
</html>