Files
Ubuntu 32b43f85bd a
2026-07-03 16:58:28 +08:00

67 lines
2.6 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];
}
const hsv = Img.bgrImageToHsv(bgr, w, h);
const blueMask = Img.hsvInRange(hsv, w, h, 180, 260, 30, 255, 30, 220);
const closed = Img.morphClose(blueMask, w, h, 5);
const ctrs = Img.findContours(closed, w, h);
console.log(`Contours: ${ctrs.length}`);
const GP = {
MIN_CONTOUR_AREA: 100, MAX_AREA_RATIO: 0.3, MIN_RECTANGULARITY: 0.5,
BIG_LOCATOR_RATIO_SHORT: 2.0, BIG_LOCATOR_RATIO_LONG: 3.0,
SMALL_LOCATOR_RATIO: 1.667,
MIN_BIG_LOCATOR_AREA: 200, MAX_BIG_LOCATOR_AREA: 8000,
MIN_SMALL_LOCATOR_AREA: 50, MAX_SMALL_LOCATOR_AREA: 2000
};
for (const ct of ctrs) {
const area = Img.contourArea(ct);
if (area < GP.MIN_CONTOUR_AREA) continue;
const brect = Img.boundingRect(ct);
const bbArea = brect.w * brect.h;
const rectRatio = area / bbArea;
if (rectRatio < GP.MIN_RECTANGULARITY) continue;
const mr = Img.minAreaRect(ct);
const mrArea = mr.size.w * mr.size.h;
const rectRatio2 = area / mrArea;
if (rectRatio2 < GP.MIN_RECTANGULARITY) continue;
const aspectRatio = Math.max(mr.size.w, mr.size.h) / Math.max(Math.min(mr.size.w, mr.size.h), 1);
if (aspectRatio >= 0.9 && aspectRatio <= 1.1) continue;
if (aspectRatio > 4.0) continue;
const tol = 0.35;
const isBig = Math.abs(aspectRatio - GP.BIG_LOCATOR_RATIO_SHORT) < tol ||
Math.abs(aspectRatio - GP.BIG_LOCATOR_RATIO_LONG) < tol;
const isSmall = Math.abs(aspectRatio - GP.SMALL_LOCATOR_RATIO) < tol;
console.log(` area=${area.toFixed(0)} brect=${JSON.stringify(brect)} rectRatio=${rectRatio.toFixed(3)} mr=(${mr.size.w.toFixed(0)}x${mr.size.h.toFixed(0)} ${mr.angle.toFixed(1)}) aspect=${aspectRatio.toFixed(3)} isBig=${isBig} isSmall=${isSmall}`);
const cx = Math.round(mr.center.x);
const cy = Math.round(mr.center.y);
if (cx >= 0 && cx < w && cy >= 0 && cy < h) {
const j = (cy * w + cx) * 3;
const [hh, ss, vv] = Img.bgrToHsv(bgr[j], bgr[j+1], bgr[j+2]);
console.log(` color at (${cx},${cy}): H=${hh.toFixed(1)} S=${ss.toFixed(0)} V=${vv.toFixed(0)}`);
}
}
process.exit(0);
});