Ring locator: contrast ranking, all-round ink check; bench baseline switch

This commit is contained in:
2026-11-16 20:12:33 +01:00
parent 20fe44257a
commit e97a5f6f89
6 changed files with 30 additions and 10 deletions
+14 -2
View File
@@ -351,6 +351,15 @@ function ringness(img, cand) {
const mean = rb.reduce((x, y) => x + y, 0) / rb.length, q = (G - mean) / span;
const dev = Math.sqrt(rb.reduce((x, y) => x + (y - mean) ** 2, 0) / rb.length) / span;
if (q < 0.1 || q > 0.8 || dev < 0.1) return null;
// dashes go all the way round: text or a line next to a dark disc only inks one side
for (let k = 0; k < 4; k++) {
let s = 0, g = 0;
for (let i = k * 48; i < (k + 1) * 48; i++) s += rb[i];
// against the gap of the same side, bands shift whole sides
for (let i = k * 6; i < (k + 1) * 6; i++) g += gp[i];
const qk = (g / 6 - s / 48) / span;
if (qk < 0.08 || qk > 0.85) return null;
}
return { span, black: D, white: G };
}
@@ -389,9 +398,10 @@ export function locate(img, opts = {}) {
const fit = discFit(img, c) || c, rn = ringness(img, fit);
if (opts.dbg && Math.hypot(c.cx - opts.dbg.cx, c.cy - opts.dbg.cy) < 0.4 * opts.dbg.r) opts.dbg.stage = rn ? "ok" : fit === c ? "nofit" : "ringness";
if (rn) out.push({ ...fit, ...rn });
if (out.length >= (opts.max || 3)) break;
if (out.length >= 8) break;
}
return out;
// a projected code is the most contrasted thing around: dim look-alikes in the room go last
return out.sort((p, q) => q.span - p.span).slice(0, opts.max || 5);
}
// the rough ellipse from the center disc is a bit off, and more so as the radius grows.
@@ -775,6 +785,8 @@ function readRings(S, opts) {
}
}
coarse.sort((p, q) => q.score - p.score);
// nothing like the sync pattern at any angle: not a code, skip the fine search
if (coarse[0].score < 0.5) return { token: null, ...coarse[0], vals: undefined };
let best = null;
for (const c of coarse.slice(0, 3)) {
for (const ds of [-0.02, 0, 0.02]) for (let dt = -10; dt <= 10; dt++) {