Filtering holes after the margin let -2 mm grow 1 mm specks past the 3 mm cutoff, and could drop real holes shrunk by a positive margin. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
46 lines
1.8 KiB
TypeScript
46 lines
1.8 KiB
TypeScript
import { readFileSync } from 'node:fs';
|
|
import { fileURLToPath } from 'node:url';
|
|
import jpeg from 'jpeg-js';
|
|
import { beforeAll, describe, expect, it } from 'vitest';
|
|
import { detect, otsuThreshold } from '../src/lib/detect';
|
|
import { prepareForExport, shapeSize } from '../src/lib/geometry';
|
|
import { loadOpenCV, type CV } from '../src/lib/opencv-loader';
|
|
import type { Point, RasterImage } from '../src/lib/types';
|
|
|
|
type FixtureCase = {
|
|
file: string;
|
|
threshold: number | null;
|
|
invert: boolean;
|
|
seed: Point | null;
|
|
pxPerMm: number;
|
|
expected: { widthMm: number; heightMm: number; holes: number };
|
|
toleranceMm: number;
|
|
};
|
|
|
|
const dir = fileURLToPath(new URL('./fixtures/', import.meta.url));
|
|
const cases: FixtureCase[] = JSON.parse(readFileSync(dir + 'manifest.json', 'utf8'));
|
|
|
|
function readJpeg(path: string): RasterImage {
|
|
const decoded = jpeg.decode(readFileSync(path), { useTArray: true, formatAsRGBA: true });
|
|
return { width: decoded.width, height: decoded.height, data: new Uint8ClampedArray(decoded.data) };
|
|
}
|
|
|
|
describe.skipIf(cases.length === 0)('detect on real photos', () => {
|
|
let cv: CV;
|
|
beforeAll(async () => {
|
|
({ cv } = await loadOpenCV());
|
|
});
|
|
|
|
it.each(cases)('$file', (c) => {
|
|
const image = readJpeg(dir + c.file);
|
|
const threshold = c.threshold ?? otsuThreshold(cv, image);
|
|
const shape = detect(cv, image, { threshold, invert: c.invert, seed: c.seed, marginPx: 0, minHolePx: 3 * c.pxPerMm });
|
|
expect(shape).not.toBeNull();
|
|
const mm = prepareForExport(shape!, c.pxPerMm)!;
|
|
const size = shapeSize(mm);
|
|
expect(Math.abs(size.width - c.expected.widthMm)).toBeLessThanOrEqual(c.toleranceMm);
|
|
expect(Math.abs(size.height - c.expected.heightMm)).toBeLessThanOrEqual(c.toleranceMm);
|
|
expect(mm.holes).toHaveLength(c.expected.holes);
|
|
});
|
|
});
|