Ring decoder: vanishing line search when the fit misses (screens at 30-45 deg), sync score ignores a one-sided glare; glare bench

This commit is contained in:
2026-11-16 23:18:47 +01:00
parent ef60da4349
commit b455e1644a
5 changed files with 142 additions and 12 deletions
+85 -7
View File
@@ -273,6 +273,23 @@ function corr(vals, bits) {
return dv && db ? num / Math.sqrt(dv * db) : 0;
}
// same as corr after removing the first angular harmonic from both sides: a reflection or a lamp lights one side
// of the ring brighter, and that slow swell alone pulls a clean sync ring under the threshold
const flatten = (v) => {
const n = v.length;
let a = 0, b = 0, c = 0;
for (let j = 0; j < n; j++) { const t = (2 * Math.PI * j) / n; a += v[j]; b += v[j] * Math.cos(t); c += v[j] * Math.sin(t); }
a /= n; b *= 2 / n; c *= 2 / n;
return v.map((x, j) => { const t = (2 * Math.PI * j) / n; return x - a - b * Math.cos(t) - c * Math.sin(t); });
};
const SYNC_FLAT = flatten(SYNC_BITS);
function corrFlat(vals) {
const f = flatten(vals);
let num = 0, dv = 0, db = 0;
for (let i = 0; i < f.length; i++) { num += f[i] * -SYNC_FLAT[i]; dv += f[i] * f[i]; db += SYNC_FLAT[i] * SYNC_FLAT[i]; }
return dv && db ? num / Math.sqrt(dv * db) : 0;
}
// otsu split of one ring: threshold + how well it separates (between-class / total variance)
function split(vals) {
const s = [...vals].sort((a, b) => a - b), n = s.length;
@@ -682,6 +699,14 @@ function projective(img, cand) {
if (!l || Math.abs(l[2]) < 1e-9) return null;
l = l.map((v) => v / l[2]);
lastProjective.l = l; lastProjective.stage = 'line';
const map = mapFromLine(Q1, l, mx, my, sc);
lastProjective.stage = map ? 'ok' : 'ellipse';
lastProjective.map = map;
return map;
}
// code space -> image from the disc conic Q1 and a vanishing line l (normalized coordinates), or null
function mapFromLine(Q1, l, mx, my, sc) {
// rectify: P maps the vanishing line to infinity
const P = [[1, 0, 0], [0, 1, 0], l];
const Pi = inv3(P);
@@ -696,20 +721,28 @@ function projective(img, cand) {
// (x-c0)^T (M/kk) (x-c0) = 1 is the disc: scale so it becomes radius CENTER
let a = M[0][0] / kk, b = M[0][1] / kk, c = M[1][1] / kk;
if (a < 0) { a = -a; b = -b; c = -c; }
lastProjective.ellipse = [a, b, c]; lastProjective.stage = 'ellipse';
if (!(a > 0 && a * c - b * b > 0)) return null;
lastProjective.stage = 'ok';
const p = Math.sqrt(a), q = b / p, s = Math.sqrt(c - q * q);
// affine from code space (radius CENTER) into the rectified plane: x = c0 + B t, B = (A^-1) / CENTER
const B = [[1 / p / CENTER, -q / (p * s) / CENTER], [0, 1 / s / CENTER]];
const map = (x, y) => {
return (x, y) => {
const u = c0[0] + B[0][0] * x + B[0][1] * y, v = c0[1] + B[1][1] * y;
const w = Pi[2][0] * u + Pi[2][1] * v + Pi[2][2];
// points beyond the vanishing line fold back: report them off image
if (w <= 0) return [NaN, NaN];
const X = (Pi[0][0] * u + Pi[0][1] * v + Pi[0][2]) / w, Y = (Pi[1][0] * u + Pi[1][1] * v + Pi[1][2]) / w;
return [X * sc + mx, Y * sc + my];
};
lastProjective.map = map;
return map;
}
// disc conic in normalized coordinates, for the tilt search
function discConic(img, cand) {
const disc = discEdge(img, cand, 160);
if (disc.length < 40) return null;
const mx = disc.reduce((s, p) => s + p[0], 0) / disc.length, my = disc.reduce((s, p) => s + p[1], 0) / disc.length;
const sc = Math.sqrt(disc.reduce((s, p) => s + (p[0] - mx) ** 2 + (p[1] - my) ** 2, 0) / disc.length) || 1;
const Q1 = fitConic(disc.map((p) => [(p[0] - mx) / sc, (p[1] - my) / sc]));
return Q1 && { Q1, mx, my, sc };
}
// unsharp mask on a small crop: undoes part of the camera blur and flattens slow brightness changes
@@ -800,6 +833,51 @@ export function decodeAt(img, cand, opts = {}) {
if (r && r.token) return r;
if (r && (!best || r.score > best.score)) best = r;
}
// looks like a code but the geometry is off: a screen seen at 30-45 deg bends the outer rings more than the
// conic fit can follow. search the vanishing line directly, scored on the sync ring and the outer ring
if (opts.tilt !== false && best && best.score > 0.55 && best.theta != null) {
const D = discConic(img, cand);
if (D) {
const at = (map) => (x, y) => { const [u, v] = map(x, y); return bilinear(tone, u - x0, v - y0); };
const n0 = RING_SECTORS[0], s0 = (2 * Math.PI) / n0, nk = RING_SECTORS[3], sk = (2 * Math.PI) / nk;
const quick = (S) => {
let top = null;
for (let dt = -12; dt <= 12; dt += 2) {
const th = best.theta + (dt * Math.PI) / 180, vals = [];
for (let j = 0; j < n0; j++) vals.push(sector(S, RADII[0], sectorAngle(th, j, n0), s0));
if (vals.some(Number.isNaN)) return null;
const sc = Math.max(corr(vals, SYNC_BITS), corrFlat(vals));
if (!top || sc > top.sc) top = { sc, th };
}
const outer = [];
for (let j = 0; j < nk; j++) outer.push(sector(S, RADII[3], sectorAngle(top.th, j, nk), sk));
if (outer.some(Number.isNaN)) return null;
return top.sc + split(outer).quality;
};
const tries = [];
for (const m of [0.015, 0.03, 0.045, 0.06, 0.08, 0.1]) for (let i = 0; i < 16; i++) {
const a = (i / 16) * 2 * Math.PI, map = mapFromLine(D.Q1, [m * Math.cos(a), m * Math.sin(a), 1], D.mx, D.my, D.sc);
if (!map) continue;
const S = at(map), q = quick(S);
if (q != null) tries.push({ q, S, l: [m * Math.cos(a), m * Math.sin(a)] });
}
tries.sort((p, q) => q.q - p.q);
for (const t of tries.slice(0, 3)) {
// polish around the pick on a finer grid before the full read
let pick = t;
for (const dx of [-0.008, 0, 0.008]) for (const dy of [-0.008, 0, 0.008]) {
if (!dx && !dy) continue;
const map = mapFromLine(D.Q1, [t.l[0] + dx, t.l[1] + dy, 1], D.mx, D.my, D.sc);
if (!map) continue;
const S = at(map), q = quick(S);
if (q != null && q > pick.q) pick = { q, S, l: [t.l[0] + dx, t.l[1] + dy] };
}
const r = readRings(pick.S, { ...opts, span });
if (r && r.token) return { ...r, tilt: pick.l };
if (r && r.score > best.score) best = r;
}
}
}
return best;
}
@@ -809,7 +887,7 @@ function readRings(S, opts) {
const syncAt = (theta, scale) => {
const vals = [];
for (let j = 0; j < n0; j++) vals.push(sector(S, RADII[0] * scale, sectorAngle(theta, j, n0), step0));
return vals.some(Number.isNaN) ? null : { score: corr(vals, SYNC_BITS), theta, scale, vals };
return vals.some(Number.isNaN) ? null : { score: Math.max(corr(vals, SYNC_BITS), corrFlat(vals)), theta, scale, vals };
};
// coarse pass over the full turn (no rotational symmetry), then a fine pass around the best hits.
// ~5x fewer samples than a dense search, which matters on low-end phones
@@ -914,4 +992,4 @@ export function makeReader(opts = {}) {
}
// internals for the lab bench
export const _lab = { readRings, sector, bilinear, split, corr, sectorAngle, classify, tokenOf };
export const _lab = { readRings, sector, bilinear, split, corr, sectorAngle, classify, tokenOf, discConic, mapFromLine, refine };