Ring code: far/washed projector bench, multi-scale locator with ring check
This commit is contained in:
+193
-108
@@ -261,58 +261,51 @@ function solve(A, B) {
|
||||
return B.map((b, i) => b / A[i][i]);
|
||||
}
|
||||
|
||||
function otsu(vals) {
|
||||
const hist = new Float64Array(256);
|
||||
for (const v of vals) hist[Math.max(0, Math.min(255, v | 0))]++;
|
||||
let total = 0, sum = 0;
|
||||
for (let i = 0; i < 256; i++) { total += hist[i]; sum += i * hist[i]; }
|
||||
let wb = 0, sb = 0, best = 0, thr = 128;
|
||||
for (let i = 0; i < 256; i++) {
|
||||
wb += hist[i];
|
||||
if (!wb || wb === total) continue;
|
||||
sb += i * hist[i];
|
||||
const mb = sb / wb, mf = (sum - sb) / (total - wb), v = wb * (total - wb) * (mb - mf) ** 2;
|
||||
if (v > best) { best = v; thr = i; }
|
||||
}
|
||||
return thr;
|
||||
}
|
||||
|
||||
// candidate center discs: solid dark elliptic blobs. returns [{ cx, cy, A }] in image pixels,
|
||||
// A maps code space (center disc radius CENTER) onto the ellipse, rotation left free
|
||||
export function locate(img, opts = {}) {
|
||||
const f = Math.max(1, Math.ceil(Math.max(img.w, img.h) / (opts.maxDim || 480)));
|
||||
const w = Math.floor(img.w / f), h = Math.floor(img.h / f), small = new Float32Array(w * h);
|
||||
// dark blobs of one pyramid level that look like a filled ellipse. s = level px size in image px
|
||||
function blobs(g, w, h, s, out) {
|
||||
// dark = clearly under the brightest thing nearby. a washed projector keeps black/white around 0.6,
|
||||
// and the disc always has its light surround within reach, whatever the room does around the screen
|
||||
const T = 8, tw = Math.ceil(w / T), th = Math.ceil(h / T), tmax = new Float32Array(tw * th);
|
||||
for (let y = 0; y < h; y++) for (let x = 0; x < w; x++) {
|
||||
let s = 0;
|
||||
for (let dy = 0; dy < f; dy++) for (let dx = 0; dx < f; dx++) s += img.g[(y * f + dy) * img.w + x * f + dx];
|
||||
small[y * w + x] = s / (f * f);
|
||||
const i = ((y / T) | 0) * tw + ((x / T) | 0), v = g[y * w + x];
|
||||
if (v > tmax[i]) tmax[i] = v;
|
||||
}
|
||||
const thr = otsu(small);
|
||||
const label = new Int32Array(w * h).fill(-1), cands = [];
|
||||
const stack = [];
|
||||
for (let start = 0; start < w * h; start++) {
|
||||
if (label[start] !== -1 || small[start] > thr) continue;
|
||||
// flood fill one dark component, keep per-row extents
|
||||
const id = cands.length, rows = new Map();
|
||||
let count = 0;
|
||||
label[start] = id; stack.push(start);
|
||||
while (stack.length) {
|
||||
const i = stack.pop(), x = i % w, y = (i / w) | 0;
|
||||
count++;
|
||||
const r = rows.get(y);
|
||||
if (r) { if (x < r[0]) r[0] = x; if (x > r[1]) r[1] = x; } else rows.set(y, [x, x]);
|
||||
for (const j of [i - 1, i + 1, i - w, i + w]) {
|
||||
if (j < 0 || j >= w * h || label[j] !== -1 || small[j] > thr) continue;
|
||||
if ((j === i - 1 && x === 0) || (j === i + 1 && x === w - 1)) continue;
|
||||
label[j] = id; stack.push(j);
|
||||
}
|
||||
const dark = new Uint8Array(w * h);
|
||||
for (let ty = 0; ty < th; ty++) for (let tx = 0; tx < tw; tx++) {
|
||||
let m = 0;
|
||||
for (let dy = -1; dy <= 1; dy++) for (let dx = -1; dx <= 1; dx++) {
|
||||
const yy = ty + dy, xx = tx + dx;
|
||||
if (yy >= 0 && xx >= 0 && yy < th && xx < tw && tmax[yy * tw + xx] > m) m = tmax[yy * tw + xx];
|
||||
}
|
||||
cands.push(null);
|
||||
if (count < 30 || rows.size < 6) continue;
|
||||
// nothing bright around: not a screen
|
||||
if (m < 48) continue;
|
||||
const thr = m * 0.8, y1 = Math.min(h, (ty + 1) * T), x1 = Math.min(w, (tx + 1) * T);
|
||||
for (let y = ty * T; y < y1; y++) for (let x = tx * T; x < x1; x++) if (g[y * w + x] < thr) dark[y * w + x] = 1;
|
||||
}
|
||||
const stack = new Int32Array(w * h), lo = new Int32Array(h), hi = new Int32Array(h), stamp = new Int32Array(h).fill(-1);
|
||||
let id = 0;
|
||||
for (let start = 0; start < w * h; start++) {
|
||||
if (!dark[start]) continue;
|
||||
// flood fill one component, keep per-row extents
|
||||
let sp = 0, count = 0, y0 = h, y1 = -1;
|
||||
stack[sp++] = start; dark[start] = 0; id++;
|
||||
while (sp) {
|
||||
const i = stack[--sp], x = i % w, y = (i / w) | 0;
|
||||
count++;
|
||||
if (stamp[y] !== id) { stamp[y] = id; lo[y] = hi[y] = x; if (y < y0) y0 = y; if (y > y1) y1 = y; }
|
||||
else if (x < lo[y]) lo[y] = x; else if (x > hi[y]) hi[y] = x;
|
||||
if (x > 0 && dark[i - 1]) { dark[i - 1] = 0; stack[sp++] = i - 1; }
|
||||
if (x < w - 1 && dark[i + 1]) { dark[i + 1] = 0; stack[sp++] = i + 1; }
|
||||
if (y > 0 && dark[i - w]) { dark[i - w] = 0; stack[sp++] = i - w; }
|
||||
if (y < h - 1 && dark[i + w]) { dark[i + w] = 0; stack[sp++] = i + w; }
|
||||
}
|
||||
if (count < 30 || y1 - y0 < 5) continue;
|
||||
// moments of the row-filled shape, so the light logo inside doesn't count as holes
|
||||
let m0 = 0, mx = 0, my = 0, mxx = 0, myy = 0, mxy = 0;
|
||||
for (const [y, [x0, x1]] of rows) {
|
||||
const n = x1 - x0 + 1, sx = ((x0 + x1) * n) / 2, sxx = (x1 * (x1 + 1) * (2 * x1 + 1) - (x0 - 1) * x0 * (2 * x0 - 1)) / 6;
|
||||
for (let y = y0; y <= y1; y++) {
|
||||
if (stamp[y] !== id) continue;
|
||||
const a0 = lo[y], a1 = hi[y], n = a1 - a0 + 1, sx = ((a0 + a1) * n) / 2;
|
||||
const sxx = (a1 * (a1 + 1) * (2 * a1 + 1) - (a0 - 1) * a0 * (2 * a0 - 1)) / 6;
|
||||
m0 += n; mx += sx; my += y * n; mxx += sxx; myy += y * y * n; mxy += y * sx;
|
||||
}
|
||||
const ux = mx / m0, uy = my / m0;
|
||||
@@ -322,19 +315,81 @@ export function locate(img, opts = {}) {
|
||||
if (l2 <= 0) continue;
|
||||
const a = 2 * Math.sqrt(l1), b = 2 * Math.sqrt(l2);
|
||||
const fill = m0 / (Math.PI * a * b), solidity = count / m0;
|
||||
if (fill < 0.86 || fill > 1.14 || solidity < 0.5 || b / a < 0.38 || b < 5) continue;
|
||||
if (fill < 0.84 || fill > 1.16 || solidity < 0.5 || b / a < 0.38 || b < 3.5) continue;
|
||||
const phi = 0.5 * Math.atan2(2 * cxy, cxx - cyy);
|
||||
const c = Math.cos(phi), s = Math.sin(phi);
|
||||
const k1 = (a * f) / CENTER, k2 = (b * f) / CENTER;
|
||||
cands[id] = { cx: (ux + 0.5) * f - 0.5, cy: (uy + 0.5) * f - 0.5, A: [c * k1, -s * k2, s * k1, c * k2], area: m0 };
|
||||
const c = Math.cos(phi), sn = Math.sin(phi);
|
||||
const k1 = (a * s) / CENTER, k2 = (b * s) / CENTER;
|
||||
out.push({ cx: (ux + 0.5) * s - 0.5, cy: (uy + 0.5) * s - 0.5, A: [c * k1, -sn * k2, sn * k1, c * k2], area: m0 * s * s, r: Math.sqrt(a * b) * s });
|
||||
}
|
||||
return cands.filter(Boolean).sort((p, q) => q.area - p.area).slice(0, opts.max || 4);
|
||||
}
|
||||
|
||||
const median = (a) => { const s = [...a].sort((x, y) => x - y); return s[s.length >> 1]; };
|
||||
|
||||
// does the neighbourhood look like a ring code? dark disc, light gap all around, half-dark textured band.
|
||||
// cheap (a few dozen samples) and what keeps plain dark blobs away from the decoder
|
||||
function ringness(img, cand) {
|
||||
const [a11, a12, a21, a22] = cand.A;
|
||||
const at = (r, phi) => { const x = r * Math.sin(phi), y = -r * Math.cos(phi); return bilinear(img, cand.cx + a11 * x + a12 * y, cand.cy + a21 * x + a22 * y); };
|
||||
const n = 24, dk = [], gp = [], rb = [];
|
||||
for (let i = 0; i < n; i++) {
|
||||
const phi = (i / n) * 2 * Math.PI;
|
||||
dk.push(at(0.48, phi)); gp.push(at(0.735, phi));
|
||||
for (const R of RADII) rb.push(at(R, phi + 0.07), at(R, phi + 0.2));
|
||||
}
|
||||
// off-frame samples are NaN: a code cut by the border can't be read anyway
|
||||
if (dk.some(Number.isNaN) || gp.some(Number.isNaN) || rb.some(Number.isNaN)) return null;
|
||||
const D = median(dk), G = median(gp), span = G - D;
|
||||
if (span < 14) return null;
|
||||
const sg = [...gp].sort((x, y) => x - y), sd = [...dk].sort((x, y) => x - y);
|
||||
if (sg[4] < D + 0.5 * span || sd[n - 5] > D + 0.4 * span) return null;
|
||||
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;
|
||||
return { span, black: D, white: G };
|
||||
}
|
||||
|
||||
// candidate center discs: solid dark elliptic blobs with rings around. returns [{ cx, cy, A, span }] in image pixels,
|
||||
// A maps code space (center disc radius CENTER) onto the ellipse, rotation left free
|
||||
export function locate(img, opts = {}) {
|
||||
const f = Math.max(1, Math.ceil(Math.max(img.w, img.h) / (opts.maxDim || 640)));
|
||||
let w = Math.floor(img.w / f), h = Math.floor(img.h / f), g = new Float32Array(w * h);
|
||||
for (let y = 0; y < h; y++) for (let x = 0; x < w; x++) {
|
||||
let s = 0;
|
||||
for (let dy = 0; dy < f; dy++) for (let dx = 0; dx < f; dx++) s += img.g[(y * f + dy) * img.w + x * f + dx];
|
||||
g[y * w + x] = s / (f * f);
|
||||
}
|
||||
// each level sees discs of ~5..14 px radius, halving covers every size up to the whole frame
|
||||
const raw = [];
|
||||
for (let s = f; w >= 24 && h >= 24; s *= 2) {
|
||||
blobs(g, w, h, s, raw);
|
||||
const w2 = w >> 1, h2 = h >> 1, g2 = new Float32Array(w2 * h2);
|
||||
for (let y = 0; y < h2; y++) for (let x = 0; x < w2; x++) {
|
||||
const i = 2 * y * w + 2 * x;
|
||||
g2[y * w2 + x] = (g[i] + g[i + 1] + g[i + w] + g[i + w + 1]) / 4;
|
||||
}
|
||||
g = g2; w = w2; h = h2;
|
||||
}
|
||||
// same disc seen at two levels: keep the finer fit
|
||||
raw.sort((p, q) => q.area - p.area);
|
||||
const cands = [];
|
||||
for (const c of raw) {
|
||||
const twin = cands.findIndex((o) => Math.hypot(o.cx - c.cx, o.cy - c.cy) < 0.5 * Math.max(o.r, c.r) && o.r / c.r < 1.6 && c.r / o.r < 1.6);
|
||||
if (twin >= 0) { cands[twin] = c; continue; }
|
||||
cands.push(c);
|
||||
}
|
||||
const out = [];
|
||||
for (const c of cands) {
|
||||
const fit = (!globalThis.NOFIT && discFit(img, c)) || c, rn = ringness(img, fit);
|
||||
if (rn) out.push({ ...fit, ...rn });
|
||||
if (out.length >= (opts.max || 3)) break;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
// the rough ellipse from the center disc is a bit off, and more so as the radius grows.
|
||||
// the rings themselves are the better reference: find their dark bands along rays,
|
||||
// fit one concentric ellipse family, undo center/scale/shear
|
||||
function refine(S) {
|
||||
function refine(S, span) {
|
||||
const rows = [];
|
||||
const rays = 160, r0 = 0.74, r1 = 1.48, dr = 0.005;
|
||||
for (let i = 0; i < rays; i++) {
|
||||
@@ -344,7 +399,7 @@ function refine(S) {
|
||||
if (prof.some(Number.isNaN)) continue;
|
||||
const sorted = [...prof].sort((a, b) => a - b);
|
||||
const bg = sorted[Math.floor(sorted.length * 0.9)], lo = sorted[0];
|
||||
if (bg - lo < 15) continue;
|
||||
if (bg - lo < 0.07 * span) continue;
|
||||
RADII.forEach((R) => {
|
||||
const c = Math.round((R - r0) / dr), wdw = Math.round(0.065 / dr);
|
||||
let m = -1;
|
||||
@@ -447,44 +502,6 @@ function cubicRoots(a, b, c, d) {
|
||||
return [0, 1, 2].map((k) => r * Math.cos(phi / 3 - (2 * Math.PI * k) / 3) - b / 3);
|
||||
}
|
||||
|
||||
// image points on the center disc edge and on each ring, following rays of the rough affine map
|
||||
function edgePoints(img, cand) {
|
||||
const [a11, a12, a21, a22] = cand.A;
|
||||
const map = (x, y) => [cand.cx + a11 * x + a12 * y, cand.cy + a21 * x + a22 * y];
|
||||
const at = (x, y) => { const [u, v] = map(x, y); return bilinear(img, u, v); };
|
||||
const disc = [], rings = RADII.map(() => []), rays = 160;
|
||||
for (let i = 0; i < rays; i++) {
|
||||
const phi = (i / rays) * 2 * Math.PI, dx = Math.sin(phi), dy = -Math.cos(phi);
|
||||
// disc edge: strongest dark-to-light step between 0.42 and 0.8
|
||||
let best = 0, br = 0;
|
||||
const prof = [];
|
||||
for (let r = 0.4; r <= 0.82; r += 0.005) prof.push(at(r * dx, r * dy));
|
||||
if (!prof.some(Number.isNaN)) {
|
||||
for (let j = 2; j < prof.length - 2; j++) {
|
||||
const gstep = prof[j + 2] - prof[j - 2];
|
||||
if (gstep > best) { best = gstep; br = 0.4 + j * 0.005; }
|
||||
}
|
||||
if (best > 20) disc.push(map(br * dx, br * dy));
|
||||
}
|
||||
// rings: darkest point around each radius, only when it is a clear dip (a dark dash)
|
||||
RADII.forEach((R, k) => {
|
||||
const op = [], span = 0.07;
|
||||
for (let r = R - span; r <= R + span + 1e-9; r += 0.004) op.push(at(r * dx, r * dy));
|
||||
if (op.some(Number.isNaN)) return;
|
||||
let m = 0;
|
||||
for (let j = 1; j < op.length; j++) if (op[j] < op[m]) m = j;
|
||||
const edge = Math.max(op[0], op[op.length - 1]);
|
||||
if (m > 1 && m < op.length - 2 && edge - op[m] > 35) {
|
||||
const a = op[m - 1], b = op[m], c = op[m + 1], den = a - 2 * b + c;
|
||||
const off = den > 0 ? (0.5 * (a - c)) / den : 0;
|
||||
const r = R - span + (m + off) * 0.004;
|
||||
rings[k].push(map(r * dx, r * dy));
|
||||
}
|
||||
});
|
||||
}
|
||||
return { disc, rings };
|
||||
}
|
||||
|
||||
// center of a conic (3x3 symmetric), in its own coordinates
|
||||
function conicCenter(Q) {
|
||||
const d = Q[0][0] * Q[1][1] - Q[0][1] * Q[1][0];
|
||||
@@ -492,25 +509,89 @@ function conicCenter(Q) {
|
||||
return [-(Q[1][1] * Q[0][2] - Q[0][1] * Q[1][2]) / d, -(-Q[1][0] * Q[0][2] + Q[0][0] * Q[1][2]) / d];
|
||||
}
|
||||
|
||||
function nelderMead(f, x0, step, iters) {
|
||||
let pts = [x0, [x0[0] + step, x0[1]], [x0[0], x0[1] + step]].map((x) => ({ x, v: f(x) }));
|
||||
for (let it = 0; it < iters; it++) {
|
||||
pts.sort((a, b) => a.v - b.v);
|
||||
const [b, g, w] = pts, c = [(b.x[0] + g.x[0]) / 2, (b.x[1] + g.x[1]) / 2];
|
||||
const at = (t) => [c[0] + t * (w.x[0] - c[0]), c[1] + t * (w.x[1] - c[1])];
|
||||
const r = { x: at(-1) }; r.v = f(r.x);
|
||||
if (r.v < b.v) { const e = { x: at(-2) }; e.v = f(e.x); pts[2] = e.v < r.v ? e : r; }
|
||||
else if (r.v < g.v) pts[2] = r;
|
||||
else { const k = { x: at(0.5) }; k.v = f(k.x); if (k.v < w.v) pts[2] = k; else pts = [b, ...[g, w].map((p) => { const x = [(p.x[0] + b.x[0]) / 2, (p.x[1] + b.x[1]) / 2]; return { x, v: f(x) }; })]; }
|
||||
// image points on the center disc edge, following rays of the rough affine map
|
||||
function discEdge(img, cand, rays) {
|
||||
const [a11, a12, a21, a22] = cand.A, unit = Math.sqrt(Math.abs(a11 * a22 - a12 * a21));
|
||||
const map = (x, y) => [cand.cx + a11 * x + a12 * y, cand.cy + a21 * x + a22 * y];
|
||||
// about half a pixel per step, small discs don't need 85 samples per ray
|
||||
const dr = Math.max(0.005, 0.5 / unit), pts = [];
|
||||
for (let i = 0; i < rays; i++) {
|
||||
const phi = (i / rays) * 2 * Math.PI, dx = Math.sin(phi), dy = -Math.cos(phi);
|
||||
const prof = [];
|
||||
let lo = Infinity, hi = -Infinity, bad = false;
|
||||
for (let r = 0.4; r <= 0.84; r += dr) {
|
||||
const [u, v] = map(r * dx, r * dy), p = bilinear(img, u, v);
|
||||
if (Number.isNaN(p)) { bad = true; break; }
|
||||
prof.push(p); if (p < lo) lo = p; if (p > hi) hi = p;
|
||||
}
|
||||
if (bad || hi - lo < 10) continue;
|
||||
// strongest dark-to-light step
|
||||
let best = 0, bj = 0;
|
||||
for (let j = 2; j < prof.length - 2; j++) { const s = prof[j + 2] - prof[j - 2]; if (s > best) { best = s; bj = j; } }
|
||||
if (best < 0.2 * (hi - lo) || bj < 4 || bj > prof.length - 5) continue;
|
||||
const g0 = prof[bj + 1] - prof[bj - 3], g2 = prof[bj + 3] - prof[bj - 1], den = g0 - 2 * best + g2;
|
||||
const off = den < 0 ? Math.max(-1, Math.min(1, (0.5 * (g0 - g2)) / den)) : 0;
|
||||
const br = 0.4 + (bj + off) * dr;
|
||||
pts.push(map(br * dx, br * dy));
|
||||
}
|
||||
pts.sort((a, b) => a.v - b.v);
|
||||
return pts[0].x;
|
||||
return pts;
|
||||
}
|
||||
|
||||
// the blob moments are rough on small discs: refit the ellipse on the disc edge at full resolution
|
||||
function discFit(img, cand) {
|
||||
let cur = cand;
|
||||
for (let pass = 0; pass < 2; pass++) {
|
||||
const pts = discEdge(img, cur, 48);
|
||||
if (pts.length < 30) return pass ? cur : null;
|
||||
const mx = pts.reduce((s, p) => s + p[0], 0) / pts.length, my = pts.reduce((s, p) => s + p[1], 0) / pts.length;
|
||||
const sc = Math.sqrt(pts.reduce((s, p) => s + (p[0] - mx) ** 2 + (p[1] - my) ** 2, 0) / pts.length) || 1;
|
||||
const Q = fitConic(pts.map((p) => [(p[0] - mx) / sc, (p[1] - my) / sc]));
|
||||
const c0 = Q && conicCenter(Q);
|
||||
if (!c0) return pass ? cur : null;
|
||||
const kk = -(Q[2][2] + Q[0][2] * c0[0] + Q[1][2] * c0[1]);
|
||||
let p = Q[0][0] / kk, q = Q[0][1] / kk, r = Q[1][1] / kk;
|
||||
const det = p * r - q * q;
|
||||
if (!(p > 0 && det > 0)) return pass ? cur : null;
|
||||
// x^T M x = 1: the map from the unit circle is M^-1/2, closed form for 2x2
|
||||
const n11 = r / det, n12 = -q / det, n22 = p / det, sd = Math.sqrt(n11 * n22 - n12 * n12), t = Math.sqrt(n11 + n22 + 2 * sd);
|
||||
const k = sc / CENTER / t, A = [(n11 + sd) * k, n12 * k, n12 * k, (n22 + sd) * k];
|
||||
const size = Math.sqrt(Math.abs(A[0] * A[3] - A[1] * A[2])) * CENTER, cx = mx + c0[0] * sc, cy = my + c0[1] * sc;
|
||||
if (Math.hypot(cx - cand.cx, cy - cand.cy) > 0.4 * cand.r || size < 0.6 * cand.r || size > 1.6 * cand.r) return pass ? cur : null;
|
||||
cur = { ...cand, cx, cy, A, r: size };
|
||||
}
|
||||
return cur;
|
||||
}
|
||||
|
||||
// image points on each ring: darkest point around each radius, only when it is a clear dip (a dark dash)
|
||||
function ringDips(img, cand, span) {
|
||||
const [a11, a12, a21, a22] = cand.A;
|
||||
const map = (x, y) => [cand.cx + a11 * x + a12 * y, cand.cy + a21 * x + a22 * y];
|
||||
const at = (x, y) => { const [u, v] = map(x, y); return bilinear(img, u, v); };
|
||||
const rings = RADII.map(() => []), rays = 160;
|
||||
for (let i = 0; i < rays; i++) {
|
||||
const phi = (i / rays) * 2 * Math.PI, dx = Math.sin(phi), dy = -Math.cos(phi);
|
||||
RADII.forEach((R, k) => {
|
||||
const op = [], reach = 0.07;
|
||||
for (let r = R - reach; r <= R + reach + 1e-9; r += 0.004) op.push(at(r * dx, r * dy));
|
||||
if (op.some(Number.isNaN)) return;
|
||||
let m = 0;
|
||||
for (let j = 1; j < op.length; j++) if (op[j] < op[m]) m = j;
|
||||
const edge = Math.max(op[0], op[op.length - 1]);
|
||||
if (m > 1 && m < op.length - 2 && edge - op[m] > 0.16 * span) {
|
||||
const a = op[m - 1], b = op[m], c = op[m + 1], den = a - 2 * b + c;
|
||||
const off = den > 0 ? (0.5 * (a - c)) / den : 0;
|
||||
const r = R - reach + (m + off) * 0.004;
|
||||
rings[k].push(map(r * dx, r * dy));
|
||||
}
|
||||
});
|
||||
}
|
||||
return rings;
|
||||
}
|
||||
|
||||
// projective map code space -> image from the two conics, or null
|
||||
export let lastProjective = null;
|
||||
function projective(img, cand) {
|
||||
const { disc, rings } = edgePoints(img, cand);
|
||||
const span = cand.span || 200, disc = discEdge(img, cand, 160), rings = ringDips(img, cand, span);
|
||||
const outer = rings[rings.length - 1];
|
||||
lastProjective = { disc: disc.length, rings: rings.map((r) => r.length), stage: 'points' };
|
||||
if (disc.length < 40 || outer.length < 30) return null;
|
||||
@@ -581,11 +662,12 @@ function projective(img, cand) {
|
||||
export function decodeAt(img, cand, opts = {}) {
|
||||
const [a11, a12, a21, a22] = cand.A;
|
||||
const affine = (x, y) => bilinear(img, cand.cx + a11 * x + a12 * y, cand.cy + a21 * x + a22 * y);
|
||||
const span = cand.span || 200;
|
||||
const maps = [opts.projective !== false && projective(img, cand), affine].filter(Boolean);
|
||||
let best = null;
|
||||
for (const S0 of maps) {
|
||||
const S = (opts.refine !== false && refine(S0)) || S0;
|
||||
const r = readRings(S, opts);
|
||||
const S = (opts.refine !== false && refine(S0, span)) || S0;
|
||||
const r = readRings(S, { ...opts, span });
|
||||
if (r && r.token) return r;
|
||||
if (r && (!best || r.score > best.score)) best = r;
|
||||
}
|
||||
@@ -623,7 +705,7 @@ function readRings(S, opts) {
|
||||
const dark = [], light = [];
|
||||
best.vals.forEach((v, j) => (SYNC_BITS[j] ? dark : light).push(v));
|
||||
const md = dark.reduce((x, y) => x + y) / dark.length, ml = light.reduce((x, y) => x + y) / light.length;
|
||||
if (ml - md < 12) return { token: null, ...best };
|
||||
if (ml - md < Math.max(5, 0.055 * (opts.span || 200))) return { token: null, ...best };
|
||||
const bits = [];
|
||||
for (let k = 1; k < RING_SECTORS.length; k++) {
|
||||
// nudge scale and rotation per ring (perspective is only approximated) and keep the cleanest split
|
||||
@@ -654,3 +736,6 @@ export function decodeFrame(img, opts = {}) {
|
||||
}
|
||||
return { token: null, found: cands.length, score: last?.score ?? null };
|
||||
}
|
||||
|
||||
// internals for the lab bench
|
||||
export const _lab = { readRings, sector, bilinear, split, corr, sectorAngle };
|
||||
|
||||
Reference in New Issue
Block a user