diff --git a/src/lib/autoMask.test.ts b/src/lib/autoMask.test.ts index 4c457ba..d52ac19 100644 --- a/src/lib/autoMask.test.ts +++ b/src/lib/autoMask.test.ts @@ -68,6 +68,60 @@ describe('segmentation', () => { expect(background[32 * width + 32]).toBe(0); }); + it('leaves a saturated object that runs off the frame out of the subject', () => { + // Grass around a red mat that spans the whole bottom edge, with the person + // sitting in the middle: the mat touches three borders, the person none. + const width = 96; + const height = 64; + const data = new Uint8Array(width * height * 4); + for (let y = 0; y < height; y++) { + for (let x = 0; x < width; x++) { + const o = (y * width + x) * 4; + const mat = y > height * 0.6; + const person = x > width * 0.42 && x < width * 0.58 && y > height * 0.35 && y < height * 0.68; + if (person) { + data[o] = 40; + data[o + 1] = 70; + data[o + 2] = 180; + } else if (mat) { + data[o] = 200; + data[o + 1] = 30; + data[o + 2] = 40; + } else { + data[o] = 90; + data[o + 1] = 130; + data[o + 2] = 70; + } + data[o + 3] = 255; + } + } + const mask = segmentMask(data, width, height, 'subject'); + const at = (x: number, y: number) => mask[Math.round(y) * width + Math.round(x)]; + expect(at(width * 0.5, height * 0.5)).toBe(255); + expect(at(4, height * 0.9)).toBe(0); + expect(at(width * 0.5, height * 0.95)).toBe(0); + }); + + it('does not swallow a dark scene with no clear subject', () => { + const width = 64; + const height = 64; + const data = new Uint8Array(width * height * 4); + for (let y = 0; y < height; y++) { + for (let x = 0; x < width; x++) { + const o = (y * width + x) * 4; + const streak = y > 30 && y < 33 && x > 8 && x < 52; + data[o] = streak ? 220 : 18; + data[o + 1] = streak ? 90 : 22; + data[o + 2] = streak ? 40 : 34; + data[o + 3] = 255; + } + } + const mask = segmentMask(data, width, height, 'subject'); + let inside = 0; + for (const value of mask) if (value) inside++; + expect(inside / mask.length).toBeLessThan(0.6); + }); + it('splits a bimodal score map', () => { const values = new Float32Array(1000); for (let i = 0; i < 500; i++) values[i] = 0.1;