-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.mjs
More file actions
165 lines (143 loc) · 4.81 KB
/
script.mjs
File metadata and controls
165 lines (143 loc) · 4.81 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
"use strict";
import { adaptiveThreshold } from "./methods/adaptiveThresholding.mjs";
import { simpleThresholdAndBlur } from "./methods/simpleThresholdAndBlur.mjs";
import { infoTexts } from "./texts/texts.mjs";
import { optionsTexts } from "./texts/optionsTexts.mjs";
const loadingMsg = document.getElementById("loadingMsg");
const upload = document.getElementById("upload");
const tabButtons = document.querySelectorAll(".tabs button");
const canvasOriginal = document.getElementById("canvasOriginal");
const canvasProcessed = document.getElementById("canvasProcessed");
const ctxOriginal = canvasOriginal.getContext("2d", {
willReadFrequently: true,
});
const controlsContainer = document.querySelector(".controls");
const methodInfoBox = document.querySelector(".method_info_box");
const optionInfoBox = document.querySelector(".additional_options_info_box");
const Get = (id) => controlsContainer.querySelector(`#${id}`);
let img = new Image();
function drawOriginal() {
canvasOriginal.width = img.width;
canvasOriginal.height = img.height;
ctxOriginal.drawImage(img, 0, 0);
}
function processImage(option) {
if (!cv || !img.complete) return;
canvasProcessed.width = img.width;
canvasProcessed.height = img.height;
const src = cv.imread(canvasOriginal);
cv.cvtColor(src, src, cv.COLOR_RGBA2GRAY);
const activeTab = document.querySelector(".tabs button.active")?.dataset
.template;
let dst = new cv.Mat();
if (activeTab === "simpleThresholdControlsTemplate") {
if (option == "otsu") {
const result = simpleThresholdAndBlur(src, dst, Get, "otsu");
dst = result.dst;
// update slider value so user can see which value method applied
if (result.otsuThreshold > 0) {
const thresholdSlider = Get("threshold");
thresholdSlider.value = Math.round(result.otsuThreshold);
updateSliderValue(thresholdSlider);
}
} else {
simpleThresholdAndBlur(src, dst, Get);
}
} else if (activeTab === "adaptiveThresholdControlsTemplate") {
dst = adaptiveThreshold(src, dst, Get);
}
loadingMsg.classList.add("hidden");
cv.imshow(canvasProcessed, dst);
src.delete();
dst.delete();
}
upload.addEventListener("change", (e) => {
const file = e.target.files[0];
if (!file) return;
img.src = URL.createObjectURL(file);
img.onload = () => {
drawOriginal();
processImage();
};
});
function updateSliderValue(slider) {
const valueEl = controlsContainer.querySelector(`#${slider.id}Value`);
if (valueEl) valueEl.textContent = slider.value;
}
function loadTemplate(templateId) {
const tpl = document.getElementById(templateId).content.cloneNode(true);
controlsContainer.replaceChildren(tpl);
addDownloadButton();
// Add listeners dynamically
controlsContainer
.querySelectorAll("input[type='range']")
.forEach((slider) => {
// set initial label text
updateSliderValue(slider);
slider.oninput = () => {
updateSliderValue(slider);
processImage();
};
});
if (templateId == "simpleThresholdControlsTemplate") {
addOtsuOption();
}
}
function addOtsuOption() {
const otsuButton = controlsContainer.querySelector("#OtsuBinarization");
otsuButton.addEventListener("click", () => {
processImage("otsu");
updateSliderValue("threshold");
});
}
function addDownloadButton() {
const downloadBtn = document
.getElementById("downloadButtonTemplate")
.content.cloneNode(true);
downloadBtn.querySelector("#download").addEventListener("click", () => {
const link = document.createElement("a");
link.download = "processed_image.png";
link.href = canvasProcessed.toDataURL();
link.click();
});
controlsContainer.appendChild(downloadBtn);
}
tabButtons.forEach((btn) => {
btn.addEventListener("click", () => {
tabButtons.forEach((b) => b.classList.remove("active"));
btn.classList.add("active");
const template = btn.dataset.template;
updateInfoBox(template);
loadTemplate(template);
processImage();
});
});
function updateInfoBox(templateId) {
methodInfoBox.innerHTML = infoTexts[templateId] || "<p>No info available</p>";
if (typeof optionsTexts[templateId] === "undefined") {
optionInfoBox.style.display = "none";
} else {
optionInfoBox.style.display = "block";
optionInfoBox.innerHTML =
optionsTexts[templateId] || "<p>No info available</p>";
}
}
function setUp() {
const templateId = "simpleThresholdControlsTemplate";
img.src = "demoPhoto/manja-vitolic-gKXKBY-C-Dk-unsplash.jpg";
loadTemplate(templateId);
updateInfoBox(templateId);
//initial preprocess
img.onload = () => {
drawOriginal();
processImage();
};
img.onerror = (err) => {
console.error("Failed to load image:", err);
};
}
window.onload = () => {
loadingMsg.textContent = "Loading OpenCV...";
loadingMsg.classList.remove("hidden");
setUp();
};