@xprite/pixelate 0.2.0 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/deskew.js +28 -15
- package/dist/fft-autocorr.d.ts +12 -0
- package/dist/fft-autocorr.js +116 -0
- package/dist/grid-arbitrate.d.ts +8 -0
- package/dist/grid-arbitrate.js +227 -66
- package/dist/grid-cut-snap.js +2 -38
- package/dist/grid-description-length.js +416 -111
- package/dist/grid-edge-coherence.js +4 -46
- package/dist/grid-edge-path.js +2 -33
- package/dist/grid-frame-lattice.js +109 -14
- package/dist/grid-period-comb.js +126 -22
- package/dist/grid-period-distance.js +59 -10
- package/dist/grid-phase-field.js +128 -48
- package/dist/grid-pitch-lattice.js +21 -9
- package/dist/grid-pitch-search.js +38 -18
- package/dist/grid-sampling.d.ts +6 -0
- package/dist/grid-sampling.js +111 -10
- package/dist/grid-step-profiles.d.ts +13 -0
- package/dist/grid-step-profiles.js +88 -0
- package/dist/grid-strip-shift.js +153 -35
- package/package.json +1 -1
package/dist/deskew.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { neighbourSteps, stepOf } from "./color-step.js";
|
|
1
|
+
import { neighbourSteps, OPAQUE_STEPS, stepOf, TRANSLUCENT } from "./color-step.js";
|
|
2
2
|
/**
|
|
3
3
|
* Tilt estimation and straightening for pixel grids that were photographed, scanned or rotated a
|
|
4
4
|
* little before they reached the importer. The tilt is the angle that makes the projection
|
|
@@ -49,13 +49,21 @@ function collectEdges(image) {
|
|
|
49
49
|
let vertical = 0, horizontal = 0;
|
|
50
50
|
for (let y = 0, p = 0; y < height; y++)
|
|
51
51
|
for (let x = 0; x < width; x++, p++) {
|
|
52
|
-
if (x + 1 < width
|
|
53
|
-
|
|
54
|
-
|
|
52
|
+
if (x + 1 < width) {
|
|
53
|
+
const rc = right[p];
|
|
54
|
+
const rStep = rc < TRANSLUCENT ? OPAQUE_STEPS[rc] : stepOf(right, data, p, 1);
|
|
55
|
+
if (rStep > EDGE_STEP) {
|
|
56
|
+
flags[p] |= VERTICAL_EDGE;
|
|
57
|
+
vertical++;
|
|
58
|
+
}
|
|
55
59
|
}
|
|
56
|
-
if (y + 1 < height
|
|
57
|
-
|
|
58
|
-
|
|
60
|
+
if (y + 1 < height) {
|
|
61
|
+
const dc = down[p];
|
|
62
|
+
const dStep = dc < TRANSLUCENT ? OPAQUE_STEPS[dc] : stepOf(down, data, p, width);
|
|
63
|
+
if (dStep > EDGE_STEP) {
|
|
64
|
+
flags[p] |= HORIZONTAL_EDGE;
|
|
65
|
+
horizontal++;
|
|
66
|
+
}
|
|
59
67
|
}
|
|
60
68
|
}
|
|
61
69
|
return {
|
|
@@ -71,13 +79,18 @@ function sharpness(points, degrees, scale, bins, offset) {
|
|
|
71
79
|
const cos = Math.cos(degrees * DEGREES) * scale, sin = Math.sin(degrees * DEGREES) * scale;
|
|
72
80
|
const { vertical, horizontal } = points;
|
|
73
81
|
// Vertical edges project on u = x cos + y sin, horizontal edges on v = -x sin + y cos.
|
|
82
|
+
// With offset = longest * scale and |degrees| <= MAX_TILT_DEGREES, projections are strictly >= 0.
|
|
74
83
|
bins.fill(0);
|
|
75
|
-
for (let i = 0
|
|
76
|
-
|
|
84
|
+
for (let i = 0, len = vertical.length; i < len; i += 2) {
|
|
85
|
+
const val = vertical[i] * cos + vertical[i + 1] * sin + offset;
|
|
86
|
+
bins[val >= 0 ? val | 0 : Math.floor(val)]++;
|
|
87
|
+
}
|
|
77
88
|
let total = squaredCounts(bins);
|
|
78
89
|
bins.fill(0);
|
|
79
|
-
for (let i = 0
|
|
80
|
-
|
|
90
|
+
for (let i = 0, len = horizontal.length; i < len; i += 2) {
|
|
91
|
+
const val = -horizontal[i] * sin + horizontal[i + 1] * cos + offset;
|
|
92
|
+
bins[val >= 0 ? val | 0 : Math.floor(val)]++;
|
|
93
|
+
}
|
|
81
94
|
total += squaredCounts(bins);
|
|
82
95
|
return total / Math.max(1, (vertical.length + horizontal.length) / 2);
|
|
83
96
|
}
|
|
@@ -117,15 +130,15 @@ function measureTilt(image) {
|
|
|
117
130
|
return UPRIGHT;
|
|
118
131
|
const bins = new Int32Array(Math.ceil(3 * longest) + 8);
|
|
119
132
|
const offset = longest;
|
|
120
|
-
const evaluate = (points, degrees, scale) => sharpness(points, degrees, scale, bins, offset * scale);
|
|
121
133
|
// Coarse pass on a shrunken copy of the coordinates so the narrow peak is not stepped over.
|
|
122
134
|
const coarseScale = Math.min(1, COARSE_SIZE / longest);
|
|
135
|
+
const coarseBins = coarseScale < 1 ? new Int32Array(Math.ceil(3 * longest * coarseScale) + 8) : bins;
|
|
123
136
|
const coarsePoints = scaled(edges, coarseScale);
|
|
124
137
|
let best = 0, bestScore = -Infinity;
|
|
125
138
|
const scores = new Map();
|
|
126
139
|
for (let step = -MAX_TILT_DEGREES / COARSE_STEP; step <= MAX_TILT_DEGREES / COARSE_STEP; step++) {
|
|
127
140
|
const degrees = Math.round(step * COARSE_STEP * 1000) / 1000;
|
|
128
|
-
const score =
|
|
141
|
+
const score = sharpness(coarsePoints, degrees, coarseScale, coarseBins, offset * coarseScale);
|
|
129
142
|
scores.set(degrees, score);
|
|
130
143
|
if (score > bestScore) {
|
|
131
144
|
bestScore = score;
|
|
@@ -136,13 +149,13 @@ function measureTilt(image) {
|
|
|
136
149
|
const fineStep = Math.max(0.005, Math.min(0.05, 1 / longest / DEGREES / 2));
|
|
137
150
|
let fine = best, fineScore = -Infinity;
|
|
138
151
|
for (let degrees = best - FINE_SPAN; degrees <= best + FINE_SPAN + 1e-9; degrees += fineStep) {
|
|
139
|
-
const score =
|
|
152
|
+
const score = sharpness(edges, degrees, 1, bins, offset);
|
|
140
153
|
if (score > fineScore) {
|
|
141
154
|
fineScore = score;
|
|
142
155
|
fine = degrees;
|
|
143
156
|
}
|
|
144
157
|
}
|
|
145
|
-
const upright =
|
|
158
|
+
const upright = sharpness(edges, 0, 1, bins, offset);
|
|
146
159
|
const gain = upright > 0 ? fineScore / upright : 1;
|
|
147
160
|
const angle = Math.round(fine * 1000) / 1000;
|
|
148
161
|
if (Math.abs(angle) < MIN_TILT_DEGREES || gain < MIN_SHARPNESS_GAIN)
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 1D Radix-2 Cooley-Tukey FFT autocorrelation for real signals.
|
|
3
|
+
*
|
|
4
|
+
* Computes the unnormalized linear autocorrelation R[lag] = sum_{i=0}^{n-1-lag} x[i] * x[i+lag]
|
|
5
|
+
* for lag = 0..maxLag in O(N log N) time where N = nextPow2(n + maxLag), matching the naive
|
|
6
|
+
* O(n * maxLag) inner product to floating-point precision (~1e-12).
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Computes linear autocorrelation `out[lag] = sum_{i=0}^{n-1-lag} signal[i] * signal[i+lag]`
|
|
10
|
+
* for `0 <= lag <= maxLag`. Uses direct summation for small inputs where FFT overhead exceeds it.
|
|
11
|
+
*/
|
|
12
|
+
export declare function linearAutocorrelation(signal: Float64Array, maxLag: number, out: Float64Array): void;
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 1D Radix-2 Cooley-Tukey FFT autocorrelation for real signals.
|
|
3
|
+
*
|
|
4
|
+
* Computes the unnormalized linear autocorrelation R[lag] = sum_{i=0}^{n-1-lag} x[i] * x[i+lag]
|
|
5
|
+
* for lag = 0..maxLag in O(N log N) time where N = nextPow2(n + maxLag), matching the naive
|
|
6
|
+
* O(n * maxLag) inner product to floating-point precision (~1e-12).
|
|
7
|
+
*/
|
|
8
|
+
const plans = new Map();
|
|
9
|
+
function getPlan(size) {
|
|
10
|
+
let plan = plans.get(size);
|
|
11
|
+
if (plan)
|
|
12
|
+
return plan;
|
|
13
|
+
const bits = Math.round(Math.log2(size));
|
|
14
|
+
const bitrev = new Uint32Array(size);
|
|
15
|
+
for (let i = 0; i < size; i++) {
|
|
16
|
+
let x = i;
|
|
17
|
+
let r = 0;
|
|
18
|
+
for (let b = 0; b < bits; b++) {
|
|
19
|
+
r = (r << 1) | (x & 1);
|
|
20
|
+
x >>= 1;
|
|
21
|
+
}
|
|
22
|
+
bitrev[i] = r;
|
|
23
|
+
}
|
|
24
|
+
const half = size >> 1;
|
|
25
|
+
const cos = new Float64Array(half);
|
|
26
|
+
const sin = new Float64Array(half);
|
|
27
|
+
const step = (-2 * Math.PI) / size;
|
|
28
|
+
for (let k = 0; k < half; k++) {
|
|
29
|
+
const angle = k * step;
|
|
30
|
+
cos[k] = Math.cos(angle);
|
|
31
|
+
sin[k] = Math.sin(angle);
|
|
32
|
+
}
|
|
33
|
+
plan = {
|
|
34
|
+
size,
|
|
35
|
+
bitrev,
|
|
36
|
+
cos,
|
|
37
|
+
sin,
|
|
38
|
+
re: new Float64Array(size),
|
|
39
|
+
im: new Float64Array(size),
|
|
40
|
+
};
|
|
41
|
+
plans.set(size, plan);
|
|
42
|
+
return plan;
|
|
43
|
+
}
|
|
44
|
+
function fftInPlace(plan, inverse) {
|
|
45
|
+
const { size, bitrev, cos, sin, re, im } = plan;
|
|
46
|
+
for (let i = 0; i < size; i++) {
|
|
47
|
+
const j = bitrev[i];
|
|
48
|
+
if (j > i) {
|
|
49
|
+
const tr = re[i];
|
|
50
|
+
re[i] = re[j];
|
|
51
|
+
re[j] = tr;
|
|
52
|
+
const ti = im[i];
|
|
53
|
+
im[i] = im[j];
|
|
54
|
+
im[j] = ti;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
const sign = inverse ? -1 : 1;
|
|
58
|
+
for (let len = 2; len <= size; len <<= 1) {
|
|
59
|
+
const half = len >> 1;
|
|
60
|
+
const stride = size / len;
|
|
61
|
+
for (let start = 0; start < size; start += len) {
|
|
62
|
+
for (let k = 0, tw = 0; k < half; k++, tw += stride) {
|
|
63
|
+
const wr = cos[tw];
|
|
64
|
+
const wi = sign * sin[tw];
|
|
65
|
+
const u = start + k;
|
|
66
|
+
const v = u + half;
|
|
67
|
+
const vr = re[v] * wr - im[v] * wi;
|
|
68
|
+
const vi = re[v] * wi + im[v] * wr;
|
|
69
|
+
re[v] = re[u] - vr;
|
|
70
|
+
im[v] = im[u] - vi;
|
|
71
|
+
re[u] += vr;
|
|
72
|
+
im[u] += vi;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
if (inverse) {
|
|
77
|
+
const inv = 1 / size;
|
|
78
|
+
for (let i = 0; i < size; i++) {
|
|
79
|
+
re[i] *= inv;
|
|
80
|
+
im[i] *= inv;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Computes linear autocorrelation `out[lag] = sum_{i=0}^{n-1-lag} signal[i] * signal[i+lag]`
|
|
86
|
+
* for `0 <= lag <= maxLag`. Uses direct summation for small inputs where FFT overhead exceeds it.
|
|
87
|
+
*/
|
|
88
|
+
export function linearAutocorrelation(signal, maxLag, out) {
|
|
89
|
+
const n = signal.length;
|
|
90
|
+
if (n * (maxLag + 1) <= 8192) {
|
|
91
|
+
for (let lag = 0; lag <= maxLag; lag++) {
|
|
92
|
+
let s = 0;
|
|
93
|
+
for (let i = 0, end = n - lag; i < end; i++)
|
|
94
|
+
s += signal[i] * signal[i + lag];
|
|
95
|
+
out[lag] = s;
|
|
96
|
+
}
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
let size = 1;
|
|
100
|
+
const needed = n + maxLag;
|
|
101
|
+
while (size < needed)
|
|
102
|
+
size <<= 1;
|
|
103
|
+
const plan = getPlan(size);
|
|
104
|
+
const { re, im } = plan;
|
|
105
|
+
re.set(signal);
|
|
106
|
+
re.fill(0, n);
|
|
107
|
+
im.fill(0);
|
|
108
|
+
fftInPlace(plan, false);
|
|
109
|
+
for (let i = 0; i < size; i++) {
|
|
110
|
+
re[i] = re[i] * re[i] + im[i] * im[i];
|
|
111
|
+
im[i] = 0;
|
|
112
|
+
}
|
|
113
|
+
fftInPlace(plan, true);
|
|
114
|
+
for (let lag = 0; lag <= maxLag; lag++)
|
|
115
|
+
out[lag] = re[lag];
|
|
116
|
+
}
|
package/dist/grid-arbitrate.d.ts
CHANGED
|
@@ -37,12 +37,20 @@ export declare class GridEvidenceReader {
|
|
|
37
37
|
private readonly described;
|
|
38
38
|
private readonly strips;
|
|
39
39
|
private readonly pixels;
|
|
40
|
+
private readonly placedReadings;
|
|
40
41
|
private readonly readings;
|
|
41
42
|
constructor(image: PixelBuffer);
|
|
43
|
+
private admit;
|
|
44
|
+
private toEvidence;
|
|
42
45
|
read(grids: readonly PixelGridAnalysis[]): {
|
|
43
46
|
readings: GridEvidence[];
|
|
44
47
|
pixels: number;
|
|
45
48
|
};
|
|
49
|
+
/**
|
|
50
|
+
* Two-pass arbitration: evaluate cheap placed readings first to tighten passthrough and global
|
|
51
|
+
* score bounds before running expensive strip-shift / phase-field / chance-evidence searches.
|
|
52
|
+
*/
|
|
53
|
+
arbitrate(grids: readonly PixelGridAnalysis[]): ArbitratedGrid | null;
|
|
46
54
|
}
|
|
47
55
|
/**
|
|
48
56
|
* The best-scoring grid among the proposals, or null when the passthrough scores lower. The
|
package/dist/grid-arbitrate.js
CHANGED
|
@@ -190,9 +190,9 @@ function shiftedReadings(strips, cutsX, cutsY) {
|
|
|
190
190
|
];
|
|
191
191
|
});
|
|
192
192
|
}
|
|
193
|
-
function
|
|
193
|
+
function unshiftedReadingOf(strips, described, grid, cutsX, cutsY) {
|
|
194
194
|
const placed = placedReading(strips.image, described, grid, cutsX, cutsY);
|
|
195
|
-
|
|
195
|
+
return {
|
|
196
196
|
cuts: placed.cuts,
|
|
197
197
|
length: placed.length,
|
|
198
198
|
twin: descriptionLength(described, placed.twin()),
|
|
@@ -200,6 +200,10 @@ function readingOf(strips, described, grid, cutsX, cutsY, shifted) {
|
|
|
200
200
|
frame: () => described,
|
|
201
201
|
sampling: strips.image.sampling,
|
|
202
202
|
};
|
|
203
|
+
}
|
|
204
|
+
function applyShiftedReadings(base, grid, cutsX, cutsY, shifted, bounds) {
|
|
205
|
+
let best = base;
|
|
206
|
+
let exact = true;
|
|
203
207
|
const cuts = {
|
|
204
208
|
cutsX,
|
|
205
209
|
cutsY,
|
|
@@ -208,9 +212,16 @@ function readingOf(strips, described, grid, cutsX, cutsY, shifted) {
|
|
|
208
212
|
for (const { shear, frame, twin, chance } of shifted()) {
|
|
209
213
|
const straight = frame();
|
|
210
214
|
const cost = shear.cost / straight.samples;
|
|
215
|
+
const length = descriptionLength(straight, cuts) + cost;
|
|
216
|
+
// If straightening + paying shift cost fails to improve description length over the
|
|
217
|
+
// incumbent reading AND cannot lower passthrough, skip expensive anti-phase twin evaluation.
|
|
218
|
+
if (bounds && length >= best.length && length >= bounds.passthrough) {
|
|
219
|
+
exact = false;
|
|
220
|
+
continue;
|
|
221
|
+
}
|
|
211
222
|
const reading = {
|
|
212
223
|
cuts,
|
|
213
|
-
length
|
|
224
|
+
length,
|
|
214
225
|
twin: descriptionLength(twin(), cuts) + cost,
|
|
215
226
|
chance: 0,
|
|
216
227
|
frame,
|
|
@@ -220,25 +231,49 @@ function readingOf(strips, described, grid, cutsX, cutsY, shifted) {
|
|
|
220
231
|
const optimistic = phaseScore(reading), incumbent = phaseScore(best);
|
|
221
232
|
if (optimistic >= incumbent)
|
|
222
233
|
continue;
|
|
234
|
+
// When called from arbitrateGrids: if this reading can neither lower passthrough nor beat the
|
|
235
|
+
// best known global score even with zero chance evidence, skip measuring chance evidence.
|
|
236
|
+
if (bounds && reading.length >= bounds.passthrough && optimistic >= bounds.score) {
|
|
237
|
+
exact = false;
|
|
238
|
+
continue;
|
|
239
|
+
}
|
|
223
240
|
const canBound = Number.isFinite(reading.length) &&
|
|
224
241
|
Number.isFinite(reading.twin) &&
|
|
225
242
|
Number.isFinite(optimistic) &&
|
|
226
243
|
Number.isFinite(incumbent);
|
|
244
|
+
const globalCanBound = canBound &&
|
|
245
|
+
bounds !== undefined &&
|
|
246
|
+
reading.length >= bounds.passthrough &&
|
|
247
|
+
Number.isFinite(bounds.score);
|
|
248
|
+
const effectiveCeiling = globalCanBound ? Math.min(incumbent, bounds.score) : incumbent;
|
|
227
249
|
reading.chance = chance(canBound
|
|
228
250
|
? (lowerBound) => phaseScore({ length: reading.length, twin: reading.twin, chance: lowerBound }) >=
|
|
229
|
-
|
|
251
|
+
effectiveCeiling
|
|
230
252
|
: undefined);
|
|
253
|
+
if (globalCanBound && reading.chance === Infinity && effectiveCeiling < incumbent)
|
|
254
|
+
exact = false;
|
|
231
255
|
if (phaseScore(reading) < incumbent)
|
|
232
256
|
best = reading;
|
|
233
257
|
}
|
|
234
|
-
return best;
|
|
258
|
+
return { reading: best, exact };
|
|
235
259
|
}
|
|
260
|
+
/**
|
|
261
|
+
* When Pass 1 (placed readings) already finds a strongly phase-locked grid, non-coarser proposals
|
|
262
|
+
* that trail far behind do not need strip-shift / phase-field searches. Coarser proposals are
|
|
263
|
+
* always allowed to run shifted models because a drifted or block-reset grid can look locked at a
|
|
264
|
+
* finer octave before straightening.
|
|
265
|
+
*/
|
|
266
|
+
const STRONG_PLACED_EVIDENCE = 0.2;
|
|
267
|
+
const STRONG_PLACED_MARGIN = 0.15;
|
|
268
|
+
const STRONG_PLACED_SHIFT_WINDOW = 0.3;
|
|
269
|
+
const COARSER_CELL_RATIO = 0.85;
|
|
236
270
|
/** Reuse one evaluation view's descriptions and distinct readings during Auto's decision. */
|
|
237
271
|
export class GridEvidenceReader {
|
|
238
272
|
image;
|
|
239
273
|
described;
|
|
240
274
|
strips;
|
|
241
275
|
pixels;
|
|
276
|
+
placedReadings = new Map();
|
|
242
277
|
readings = new Map();
|
|
243
278
|
constructor(image) {
|
|
244
279
|
this.image = image;
|
|
@@ -246,11 +281,9 @@ export class GridEvidenceReader {
|
|
|
246
281
|
this.strips = new StripImage(image);
|
|
247
282
|
this.pixels = pixelLength(this.described);
|
|
248
283
|
}
|
|
249
|
-
|
|
250
|
-
const { image
|
|
284
|
+
admit(grids) {
|
|
285
|
+
const { image } = this;
|
|
251
286
|
const seen = new Set();
|
|
252
|
-
const readings = [];
|
|
253
|
-
// Admission is scoped to geometry; every model of admitted cuts is evaluated.
|
|
254
287
|
const ordered = grids
|
|
255
288
|
.map((grid) => {
|
|
256
289
|
const { cutsX, cutsY } = cutsOf(image, grid);
|
|
@@ -265,17 +298,7 @@ export class GridEvidenceReader {
|
|
|
265
298
|
a.grid.cellHeight - b.grid.cellHeight ||
|
|
266
299
|
a.grid.offsetX - b.grid.offsetX ||
|
|
267
300
|
a.grid.offsetY - b.grid.offsetY);
|
|
268
|
-
|
|
269
|
-
// those once both models are read; persistent readings retain only their winning frame.
|
|
270
|
-
const pending = new Map();
|
|
271
|
-
const corrections = new Map();
|
|
272
|
-
for (const { geometry, key } of ordered) {
|
|
273
|
-
if (this.readings.has(key))
|
|
274
|
-
continue;
|
|
275
|
-
const models = pending.get(geometry) ?? new Set();
|
|
276
|
-
models.add(key);
|
|
277
|
-
pending.set(geometry, models);
|
|
278
|
-
}
|
|
301
|
+
const admitted = [];
|
|
279
302
|
const admittedGeometries = new Set();
|
|
280
303
|
let bestConfidence = -Infinity;
|
|
281
304
|
for (const { grid, cutsX, cutsY, geometry, key } of ordered) {
|
|
@@ -286,14 +309,59 @@ export class GridEvidenceReader {
|
|
|
286
309
|
continue;
|
|
287
310
|
seen.add(key);
|
|
288
311
|
if (!admittedGeometries.has(geometry) &&
|
|
289
|
-
|
|
312
|
+
admitted.length > 0 &&
|
|
290
313
|
bestConfidence >= STRONG_GRID_CONFIDENCE &&
|
|
291
314
|
bestConfidence - grid.confidence >= DISTINCT_GEOMETRY_CONFIDENCE_GAP)
|
|
292
315
|
continue;
|
|
293
316
|
admittedGeometries.add(geometry);
|
|
317
|
+
admitted.push({ grid, cutsX, cutsY, columns, rows, geometry, key });
|
|
318
|
+
bestConfidence = Math.max(bestConfidence, grid.confidence);
|
|
319
|
+
}
|
|
320
|
+
return admitted;
|
|
321
|
+
}
|
|
322
|
+
toEvidence(proposal, reading) {
|
|
323
|
+
const { grid, columns, rows } = proposal;
|
|
324
|
+
const { cuts, length, twin, chance, frame, sampling } = reading;
|
|
325
|
+
return {
|
|
326
|
+
grid: {
|
|
327
|
+
...grid,
|
|
328
|
+
width: columns,
|
|
329
|
+
height: rows,
|
|
330
|
+
cutsX: [...cuts.cutsX],
|
|
331
|
+
cutsY: [...cuts.cutsY],
|
|
332
|
+
sampling,
|
|
333
|
+
},
|
|
334
|
+
length,
|
|
335
|
+
twin,
|
|
336
|
+
chance,
|
|
337
|
+
frame,
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
read(grids) {
|
|
341
|
+
const { described, strips } = this;
|
|
342
|
+
const admitted = this.admit(grids);
|
|
343
|
+
// Only pending models of the same geometry need shared auxiliary descriptions. Release
|
|
344
|
+
// those once both models are read; persistent readings retain only their winning frame.
|
|
345
|
+
const pending = new Map();
|
|
346
|
+
const corrections = new Map();
|
|
347
|
+
for (const { geometry, key } of admitted) {
|
|
348
|
+
if (this.readings.has(key))
|
|
349
|
+
continue;
|
|
350
|
+
const models = pending.get(geometry) ?? new Set();
|
|
351
|
+
models.add(key);
|
|
352
|
+
pending.set(geometry, models);
|
|
353
|
+
}
|
|
354
|
+
const readings = [];
|
|
355
|
+
for (const proposal of admitted) {
|
|
356
|
+
const { grid, cutsX, cutsY, geometry, key } = proposal;
|
|
294
357
|
let reading = this.readings.get(key);
|
|
295
358
|
if (!reading) {
|
|
296
|
-
|
|
359
|
+
let base = this.placedReadings.get(key);
|
|
360
|
+
if (!base) {
|
|
361
|
+
base = unshiftedReadingOf(strips, described, grid, cutsX, cutsY);
|
|
362
|
+
this.placedReadings.set(key, base);
|
|
363
|
+
}
|
|
364
|
+
reading = applyShiftedReadings(base, grid, cutsX, cutsY, () => {
|
|
297
365
|
if (grid.placed)
|
|
298
366
|
return [];
|
|
299
367
|
let shifted = corrections.get(geometry);
|
|
@@ -303,32 +371,151 @@ export class GridEvidenceReader {
|
|
|
303
371
|
corrections.set(geometry, shifted);
|
|
304
372
|
}
|
|
305
373
|
return shifted;
|
|
306
|
-
});
|
|
374
|
+
}).reading;
|
|
307
375
|
this.readings.set(key, reading);
|
|
308
376
|
const models = pending.get(geometry);
|
|
309
377
|
models.delete(key);
|
|
310
378
|
if (!models.size)
|
|
311
379
|
corrections.delete(geometry);
|
|
312
380
|
}
|
|
313
|
-
|
|
314
|
-
readings.push({
|
|
315
|
-
grid: {
|
|
316
|
-
...grid,
|
|
317
|
-
width: columns,
|
|
318
|
-
height: rows,
|
|
319
|
-
cutsX: [...cuts.cutsX],
|
|
320
|
-
cutsY: [...cuts.cutsY],
|
|
321
|
-
sampling,
|
|
322
|
-
},
|
|
323
|
-
length,
|
|
324
|
-
twin,
|
|
325
|
-
chance,
|
|
326
|
-
frame,
|
|
327
|
-
});
|
|
328
|
-
bestConfidence = Math.max(bestConfidence, grid.confidence);
|
|
381
|
+
readings.push(this.toEvidence(proposal, reading));
|
|
329
382
|
}
|
|
330
383
|
return { readings, pixels: this.pixels };
|
|
331
384
|
}
|
|
385
|
+
/**
|
|
386
|
+
* Two-pass arbitration: evaluate cheap placed readings first to tighten passthrough and global
|
|
387
|
+
* score bounds before running expensive strip-shift / phase-field / chance-evidence searches.
|
|
388
|
+
*/
|
|
389
|
+
arbitrate(grids) {
|
|
390
|
+
const { described, strips } = this;
|
|
391
|
+
const admitted = this.admit(grids);
|
|
392
|
+
if (!admitted.length)
|
|
393
|
+
return null;
|
|
394
|
+
// Pass 1: placed readings (or cached full readings) for every admitted candidate.
|
|
395
|
+
const pass1 = [];
|
|
396
|
+
let passthrough = this.pixels;
|
|
397
|
+
for (const proposal of admitted) {
|
|
398
|
+
const { grid, cutsX, cutsY, key } = proposal;
|
|
399
|
+
const cached = this.readings.get(key);
|
|
400
|
+
if (cached) {
|
|
401
|
+
pass1.push({ proposal, reading: cached, complete: true });
|
|
402
|
+
passthrough = Math.min(passthrough, Math.max(cached.length, cached.twin - cached.chance));
|
|
403
|
+
continue;
|
|
404
|
+
}
|
|
405
|
+
let base = this.placedReadings.get(key);
|
|
406
|
+
if (!base) {
|
|
407
|
+
base = unshiftedReadingOf(strips, described, grid, cutsX, cutsY);
|
|
408
|
+
this.placedReadings.set(key, base);
|
|
409
|
+
}
|
|
410
|
+
const complete = !!grid.placed;
|
|
411
|
+
if (complete)
|
|
412
|
+
this.readings.set(key, base);
|
|
413
|
+
pass1.push({ proposal, reading: base, complete });
|
|
414
|
+
passthrough = Math.min(passthrough, Math.max(base.length, base.twin));
|
|
415
|
+
}
|
|
416
|
+
let bestPlaced = null;
|
|
417
|
+
let bestScore = passthrough - NULL_MARGIN;
|
|
418
|
+
for (const entry of [...pass1].sort((a, b) => phaseScore(a.reading) - phaseScore(b.reading))) {
|
|
419
|
+
const phase = phaseScore(entry.reading);
|
|
420
|
+
if (phase >= bestScore)
|
|
421
|
+
break;
|
|
422
|
+
const { cutsX, cutsY } = entry.reading.cuts;
|
|
423
|
+
const total = phase + octaveShortfall(entry.reading.frame(), cutsX, cutsY);
|
|
424
|
+
if (total < bestScore) {
|
|
425
|
+
bestPlaced = { proposal: entry.proposal, reading: entry.reading };
|
|
426
|
+
bestScore = total;
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
const strongPlaced = bestPlaced !== null &&
|
|
430
|
+
bestPlaced.reading.twin - bestPlaced.reading.length >= STRONG_PLACED_EVIDENCE &&
|
|
431
|
+
bestScore < passthrough - NULL_MARGIN - STRONG_PLACED_MARGIN;
|
|
432
|
+
// Pass 2: evaluate shifted models for candidates that can still compete.
|
|
433
|
+
// Under a strongly locked placed winner, keep at most the best placed candidate per
|
|
434
|
+
// (columns, rows) size within the shift window so duplicate offsets do not repeat shifts.
|
|
435
|
+
const bestPlacedKeyBySize = new Map();
|
|
436
|
+
if (strongPlaced) {
|
|
437
|
+
for (const entry of pass1) {
|
|
438
|
+
if (entry.complete)
|
|
439
|
+
continue;
|
|
440
|
+
const sizeKey = `${entry.proposal.columns}x${entry.proposal.rows}`;
|
|
441
|
+
const ps = phaseScore(entry.reading);
|
|
442
|
+
const prev = bestPlacedKeyBySize.get(sizeKey);
|
|
443
|
+
if (!prev || ps < prev.score)
|
|
444
|
+
bestPlacedKeyBySize.set(sizeKey, { key: entry.proposal.key, score: ps });
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
const pending = new Map();
|
|
448
|
+
const corrections = new Map();
|
|
449
|
+
const needShift = new Set();
|
|
450
|
+
for (const entry of pass1) {
|
|
451
|
+
if (entry.complete)
|
|
452
|
+
continue;
|
|
453
|
+
if (strongPlaced &&
|
|
454
|
+
entry.proposal.columns >= bestPlaced.proposal.columns * COARSER_CELL_RATIO &&
|
|
455
|
+
entry.proposal.rows >= bestPlaced.proposal.rows * COARSER_CELL_RATIO &&
|
|
456
|
+
(phaseScore(entry.reading) > bestScore + STRONG_PLACED_SHIFT_WINDOW ||
|
|
457
|
+
bestPlacedKeyBySize.get(`${entry.proposal.columns}x${entry.proposal.rows}`)?.key !==
|
|
458
|
+
entry.proposal.key))
|
|
459
|
+
continue;
|
|
460
|
+
const { geometry, key } = entry.proposal;
|
|
461
|
+
needShift.add(key);
|
|
462
|
+
const models = pending.get(geometry) ?? new Set();
|
|
463
|
+
models.add(key);
|
|
464
|
+
pending.set(geometry, models);
|
|
465
|
+
}
|
|
466
|
+
const bounds = { passthrough, score: bestScore };
|
|
467
|
+
const readings = [];
|
|
468
|
+
for (const entry of pass1) {
|
|
469
|
+
const { proposal } = entry;
|
|
470
|
+
let reading = entry.reading;
|
|
471
|
+
if (needShift.has(proposal.key)) {
|
|
472
|
+
const { grid, cutsX, cutsY, geometry, key } = proposal;
|
|
473
|
+
const result = applyShiftedReadings(reading, grid, cutsX, cutsY, () => {
|
|
474
|
+
let shifted = corrections.get(geometry);
|
|
475
|
+
if (!shifted) {
|
|
476
|
+
shifted = shiftedReadings(strips, cutsX, cutsY);
|
|
477
|
+
if (pending.get(geometry).size > 1)
|
|
478
|
+
corrections.set(geometry, shifted);
|
|
479
|
+
}
|
|
480
|
+
return shifted;
|
|
481
|
+
}, bounds);
|
|
482
|
+
reading = result.reading;
|
|
483
|
+
if (result.exact)
|
|
484
|
+
this.readings.set(key, reading);
|
|
485
|
+
const models = pending.get(geometry);
|
|
486
|
+
models.delete(key);
|
|
487
|
+
if (!models.size)
|
|
488
|
+
corrections.delete(geometry);
|
|
489
|
+
bounds.passthrough = Math.min(bounds.passthrough, Math.max(reading.length, reading.twin - reading.chance));
|
|
490
|
+
bounds.score = Math.min(bounds.score, bounds.passthrough - NULL_MARGIN);
|
|
491
|
+
}
|
|
492
|
+
readings.push(this.toEvidence(proposal, reading));
|
|
493
|
+
}
|
|
494
|
+
let finalPassthrough = this.pixels;
|
|
495
|
+
for (const reading of readings)
|
|
496
|
+
finalPassthrough = Math.min(finalPassthrough, Math.max(reading.length, reading.twin - reading.chance));
|
|
497
|
+
let best = null;
|
|
498
|
+
let score = finalPassthrough - NULL_MARGIN;
|
|
499
|
+
for (const reading of [...readings].sort((a, b) => phaseScore(a) - phaseScore(b))) {
|
|
500
|
+
const phase = phaseScore(reading);
|
|
501
|
+
if (phase >= score)
|
|
502
|
+
break;
|
|
503
|
+
const { cutsX, cutsY } = reading.grid;
|
|
504
|
+
const total = phase + octaveShortfall(reading.frame(), cutsX, cutsY);
|
|
505
|
+
if (total < score) {
|
|
506
|
+
best = reading;
|
|
507
|
+
score = total;
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
if (!best)
|
|
511
|
+
return null;
|
|
512
|
+
const share = Math.max(0, Math.min(1, (best.twin - best.length) / best.twin));
|
|
513
|
+
return {
|
|
514
|
+
grid: { ...best.grid, confidence: Math.max(best.grid.confidence, share), reliable: true },
|
|
515
|
+
length: best.length,
|
|
516
|
+
score,
|
|
517
|
+
};
|
|
518
|
+
}
|
|
332
519
|
}
|
|
333
520
|
/**
|
|
334
521
|
* The best-scoring grid among the proposals, or null when the passthrough scores lower. The
|
|
@@ -336,31 +523,5 @@ export class GridEvidenceReader {
|
|
|
336
523
|
* its phase saves.
|
|
337
524
|
*/
|
|
338
525
|
export function arbitrateGrids(image, grids, evidence = new GridEvidenceReader(image)) {
|
|
339
|
-
|
|
340
|
-
let passthrough = pixels;
|
|
341
|
-
for (const reading of readings)
|
|
342
|
-
passthrough = Math.min(passthrough, Math.max(reading.length, reading.twin - reading.chance));
|
|
343
|
-
let best = null;
|
|
344
|
-
let score = passthrough - NULL_MARGIN;
|
|
345
|
-
// The octave shortfall only adds to a score, so readings are checked best phase score first
|
|
346
|
-
// and the search stops at the first that cannot win even without it.
|
|
347
|
-
for (const reading of [...readings].sort((a, b) => phaseScore(a) - phaseScore(b))) {
|
|
348
|
-
const phase = phaseScore(reading);
|
|
349
|
-
if (phase >= score)
|
|
350
|
-
break;
|
|
351
|
-
const { cutsX, cutsY } = reading.grid;
|
|
352
|
-
const total = phase + octaveShortfall(reading.frame(), cutsX, cutsY);
|
|
353
|
-
if (total < score) {
|
|
354
|
-
best = reading;
|
|
355
|
-
score = total;
|
|
356
|
-
}
|
|
357
|
-
}
|
|
358
|
-
if (!best)
|
|
359
|
-
return null;
|
|
360
|
-
const share = Math.max(0, Math.min(1, (best.twin - best.length) / best.twin));
|
|
361
|
-
return {
|
|
362
|
-
grid: { ...best.grid, confidence: Math.max(best.grid.confidence, share), reliable: true },
|
|
363
|
-
length: best.length,
|
|
364
|
-
score,
|
|
365
|
-
};
|
|
526
|
+
return evidence.arbitrate(grids);
|
|
366
527
|
}
|