48 lines
1.7 KiB
JavaScript
48 lines
1.7 KiB
JavaScript
const { Img } = require("./paper-recognizer.js");
|
|
const chunks = [];
|
|
process.stdin.on("data", c => chunks.push(c));
|
|
process.stdin.on("end", () => {
|
|
const buf = Buffer.concat(chunks);
|
|
const w = buf.readUInt32LE(0);
|
|
const h = buf.readUInt32LE(4);
|
|
const pixels = new Uint8Array(buf.slice(8));
|
|
|
|
const bgr = new Uint8Array(w * h * 3);
|
|
for (let i = 0; i < w * h; i++) {
|
|
const src4 = i * 4, dst3 = i * 3;
|
|
bgr[dst3] = pixels[src4 + 2];
|
|
bgr[dst3+1] = pixels[src4 + 1];
|
|
bgr[dst3+2] = pixels[src4];
|
|
}
|
|
|
|
function testHsv(x, y) {
|
|
const j = (y * w + x) * 3;
|
|
const [h,s,v] = Img.bgrToHsv(bgr[j], bgr[j+1], bgr[j+2]);
|
|
console.log(`HSV at (${x},${y}): H=${h.toFixed(1)} S=${s.toFixed(0)} V=${v.toFixed(0)}`);
|
|
}
|
|
testHsv(89, 200);
|
|
testHsv(669, 200);
|
|
|
|
const hsv = Img.bgrImageToHsv(bgr, w, h);
|
|
const blueMask = Img.hsvInRange(hsv, w, h, 180, 260, 30, 255, 30, 220);
|
|
let count = 0;
|
|
for (let i = 0; i < w * h; i++) if (blueMask[i] > 0) count++;
|
|
console.log(`Blue mask foreground: ${count}`);
|
|
|
|
const morphed = Img.morphClose(blueMask, w, h, 5);
|
|
let mCount = 0;
|
|
for (let i = 0; i < w * h; i++) if (morphed[i] > 0) mCount++;
|
|
console.log(`After close: ${mCount}`);
|
|
|
|
const ctrs = Img.findContours(morphed, w, h);
|
|
console.log(`Contours: ${ctrs.length}`);
|
|
for (let i = 0; i < ctrs.length; i++) {
|
|
const area = Img.contourArea(ctrs[i]);
|
|
console.log(` ${i}: ${ctrs[i].length} pts, area=${area.toFixed(0)}, rect=${JSON.stringify(Img.boundingRect(ctrs[i]))}`);
|
|
const mr = Img.minAreaRect(ctrs[i]);
|
|
console.log(` mr: w=${mr.size.w.toFixed(0)} h=${mr.size.h.toFixed(0)} angle=${mr.angle.toFixed(1)}`);
|
|
}
|
|
|
|
process.exit(0);
|
|
});
|