Fix teeth band filling the whole mouth

Three causes, all of them mine:

Otsu always returns a split, including on a homogeneous region - given a dark
cavity with no teeth it invents a threshold and calls half the pixels bright.
The gate is now the separation between the two class means, which is the only
thing that says whether the split means anything. Coverage was the wrong
signal: it is high both when the mouth is full of teeth and when the region is
uniformly dark and Otsu has split noise.

The row scan tracked the last qualifying row anywhere rather than where the run
from the top stops, so one bright row near the bottom - a lit lower lip inside
the ring - pushed the line to full height. It now breaks at the first failing
row once the run has started.

MediaPipe's inner lip landmarks sit slightly outside the real opening, so the
sampled region included lip pixels, which are bright and sit exactly at the
boundary where they do most damage. The ring is now eroded toward its centroid
before sampling, with the amount exposed as a knob.

Adds a diagnostic panel showing the sampled crop, pixels above threshold, and
the resolved line, because tuning this from numbers alone does not tell you
whether the region being measured is even the right region.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Your Name 2026-09-24 15:14:49 -04:00
parent 941022b69f
commit c6daf827d1
4 changed files with 164 additions and 33 deletions

View file

@ -12,7 +12,7 @@ import { fitSimilarity, applySim, procrustesMean, smoothTransforms } from './mat
import { stabilize, toRasterRing, selectKeys, activeKey } from './pipeline.js';
import { IndexedRaster, hexToRgb } from './raster.js';
import { writeTake } from './take.js';
import { clipPolyAbove } from './interior.js';
import { clipPolyAbove, otsuForTest } from './interior.js';
import { synthDense } from './synth.js';
const results = [];
@ -203,6 +203,30 @@ export function run() {
Math.min(...ct.map((p) => p.x)) >= 0 && Math.max(...ct.map((p) => p.x)) <= 8);
}
// Otsu on a uniform region must report near-zero class separation. It will
// still return a threshold - that is what Otsu does - so the separation is the
// only thing that distinguishes "found teeth" from "split noise in a dark
// mouth", which is what made the band fill the whole cavity.
{
const flat = new Uint32Array(256); flat[40] = 500;
const f = otsuForTest(flat, 500);
ok('uniform region yields ~no class separation',
Math.abs(f.mBright - f.mDark) / 255 < 0.02, `sep ${((f.mBright - f.mDark) / 255).toFixed(4)}`);
const noisy = new Uint32Array(256);
for (let i = 30; i <= 60; i++) noisy[i] = 20; // dark cavity, some spread
const nz = otsuForTest(noisy, 31 * 20);
ok('dark-but-noisy region stays below a sane gate',
(nz.mBright - nz.mDark) / 255 < 0.14, `sep ${((nz.mBright - nz.mDark) / 255).toFixed(4)}`);
const teeth = new Uint32Array(256);
for (let i = 20; i <= 45; i++) teeth[i] = 40; // cavity
for (let i = 180; i <= 220; i++) teeth[i] = 30; // teeth
const tt = otsuForTest(teeth, 26 * 40 + 41 * 30);
ok('real bright/dark split clears the gate',
(tt.mBright - tt.mDark) / 255 > 0.4, `sep ${((tt.mBright - tt.mDark) / 255).toFixed(4)}`);
}
// take writer round-trip
const take = {
name: 'test', frames: 72, width: 320, height: 200, exposure: 2,