perf: prepare the lighting-corrected image once per photo

Threshold, click, scale and margin changes now reuse it, so each update
skips the ~200 ms grayscale and lighting fit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
immortaly007andClaude Opus 5.5 committed 2026-10-15 15:22:36 +02:00
1 parent 8ea525a1e8
commit 077c3cc31d
3 files changed
+60 -15

No files matched your search

+16 -1
View File
@@ -1,5 +1,5 @@
import { beforeAll, describe, expect, it } from 'vitest';
import { detect, otsuThreshold } from '../src/lib/detect';
import { detect, detectPrepared, otsuThreshold, prepareImage, preparedThreshold } from '../src/lib/detect';
import { bounds } from '../src/lib/geometry';
import { loadOpenCV, type CV } from '../src/lib/opencv-loader';
import type { DetectParams } from '../src/lib/types';
@@ -114,4 +114,19 @@ describe('detect', () => {
const framed = grayImage(400, 300, (x, y) => (inRect(x, y, 0, 60, 240, 260) || inRect(x, y, 330, 100, 340, 110) ? 220 : 30));
expect(detect(cv, framed, params())).toBeNull();
});
it('reuses a prepared image for repeated detections', () => {
const prepared = prepareImage(cv, plateScene);
try {
expect(preparedThreshold(cv, prepared)).toBe(otsuThreshold(cv, plateScene));
for (const marginPx of [0, -5]) {
const direct = detect(cv, plateScene, params({ marginPx }))!;
const reused = detectPrepared(cv, prepared, params({ marginPx }))!;
expect(bounds(reused.outer)).toEqual(bounds(direct.outer));
expect(reused.holes).toHaveLength(direct.holes.length);
}
} finally {
prepared.gray.delete();
}
});
});