Skip to content

Commit 8232be2

Browse files
authored
Composite transparent color samples over white (#38)
1 parent e86c328 commit 8232be2

4 files changed

Lines changed: 43 additions & 17 deletions

File tree

‎docs/COLOR_PIPELINE_NOTES.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ retrofit of it:
4949
geometry (a `PerspectiveTransform`, the same primitive every other
5050
format's detector already produces), samples an averaging window per
5151
module in the **raw, not binarized** RGBA image and nearest-colour
52-
matches it against the palette in plain Euclidean RGB space.
52+
matches it against the palette in plain Euclidean RGB space. Each pixel
53+
is composited over white before averaging, matching the monochrome
54+
image pipeline's transparent-background convention.
5355

5456
## What was measured (synthetic testing only — see the gap below)
5557

‎src/js/color/classify.js‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
* yet. This module only classifies a grid whose corners are already known
4747
* (e.g. supplied by a caller, or a future detector).
4848
*
49-
* The classifier itself is a first pass: average RGB over a sampling
49+
* The classifier itself is a first pass: composite RGBA over white, average RGB over a sampling
5050
* window per module, nearest-colour match in plain Euclidean RGB space.
5151
* That is a real, working starting point, not a finished algorithm —
5252
* lighting colour temperature, print dye variation and camera white
@@ -84,7 +84,7 @@ function nearestPaletteIndex(sample, palette) {
8484
/**
8585
* Sample and classify a `width` x `height` grid.
8686
*
87-
* @param {{data: Uint8ClampedArray, width: number, height: number}} image Raw RGBA, NOT binarized.
87+
* @param {{data: Uint8ClampedArray, width: number, height: number}} image Raw RGBA, NOT binarized. Transparent pixels are composited over white.
8888
* @param {number} width Modules across.
8989
* @param {number} height Modules down.
9090
* @param {import('../image/perspective.js').PerspectiveTransform} transform Grid space -> image space.
@@ -119,15 +119,15 @@ export function classifyGrid(image, width, height, transform, palette) {
119119
if (sx < 0 || sy < 0 || sx >= image.width || sy >= image.height)
120120
continue;
121121
const p = (sy * image.width + sx) * 4;
122-
sumR += image.data[p];
123-
sumG += image.data[p + 1];
124-
sumB += image.data[p + 2];
122+
const alpha = image.data[p + 3] / 255;
123+
const background = 255 * (1 - alpha);
124+
sumR += image.data[p] * alpha + background;
125+
sumG += image.data[p + 1] * alpha + background;
126+
sumB += image.data[p + 2] * alpha + background;
125127
count++;
126128
}
127129
}
128-
const sample = count > 0
129-
? [sumR / count, sumG / count, sumB / count]
130-
: [image.data[(cy * image.width + cx) * 4], image.data[(cy * image.width + cx) * 4 + 1], image.data[(cy * image.width + cx) * 4 + 2]];
130+
const sample = [sumR / count, sumG / count, sumB / count];
131131
out.set(x, y, nearestPaletteIndex(sample, palette));
132132
}
133133
}

‎src/ts/color/classify.ts‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
* yet. This module only classifies a grid whose corners are already known
4848
* (e.g. supplied by a caller, or a future detector).
4949
*
50-
* The classifier itself is a first pass: average RGB over a sampling
50+
* The classifier itself is a first pass: composite RGBA over white, average RGB over a sampling
5151
* window per module, nearest-colour match in plain Euclidean RGB space.
5252
* That is a real, working starting point, not a finished algorithm —
5353
* lighting colour temperature, print dye variation and camera white
@@ -89,7 +89,7 @@ function nearestPaletteIndex(sample, palette) {
8989
/**
9090
* Sample and classify a `width` x `height` grid.
9191
*
92-
* @param {{data: Uint8ClampedArray, width: number, height: number}} image Raw RGBA, NOT binarized.
92+
* @param {{data: Uint8ClampedArray, width: number, height: number}} image Raw RGBA, NOT binarized. Transparent pixels are composited over white.
9393
* @param {number} width Modules across.
9494
* @param {number} height Modules down.
9595
* @param {import('../image/perspective.js').PerspectiveTransform} transform Grid space -> image space.
@@ -126,15 +126,15 @@ export function classifyGrid(image, width, height, transform, palette) {
126126
const sy = cy + dy;
127127
if (sx < 0 || sy < 0 || sx >= image.width || sy >= image.height) continue;
128128
const p = (sy * image.width + sx) * 4;
129-
sumR += image.data[p];
130-
sumG += image.data[p + 1];
131-
sumB += image.data[p + 2];
129+
const alpha = image.data[p + 3] / 255;
130+
const background = 255 * (1 - alpha);
131+
sumR += image.data[p] * alpha + background;
132+
sumG += image.data[p + 1] * alpha + background;
133+
sumB += image.data[p + 2] * alpha + background;
132134
count++;
133135
}
134136
}
135-
const sample = count > 0
136-
? [sumR / count, sumG / count, sumB / count]
137-
: [image.data[(cy * image.width + cx) * 4], image.data[(cy * image.width + cx) * 4 + 1], image.data[(cy * image.width + cx) * 4 + 2]];
137+
const sample = [sumR / count, sumG / count, sumB / count];
138138

139139
out.set(x, y, nearestPaletteIndex(sample, palette));
140140
}

‎test/kartrak.test.js‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,30 @@ import {
1111
kartrakCheckDigit,
1212
toColorImageData,
1313
} from '../src/js/kartrak/index.js';
14+
import { classifyGrid } from '../src/js/color/classify.js';
15+
import { PerspectiveTransform } from '../src/js/image/perspective.js';
16+
17+
const CLASSIFY_PALETTE = [[0, 0, 0], [255, 255, 255], [128, 128, 128]];
18+
const IDENTITY = new PerspectiveTransform(1, 0, 0, 0, 1, 0, 0, 0, 1);
19+
20+
function classifySolid(pixel) {
21+
const data = new Uint8ClampedArray(3 * 3 * 4);
22+
for (let i = 0; i < data.length; i += 4) data.set(pixel, i);
23+
return classifyGrid({ data, width: 3, height: 3 }, 1, 1, IDENTITY, CLASSIFY_PALETTE).get(0, 0);
24+
}
25+
26+
test('color grid classification composites transparent and partial alpha over white', () => {
27+
assert.equal(classifySolid([0, 0, 0, 0]), classifySolid([255, 255, 255, 255]));
28+
assert.equal(classifySolid([0, 0, 0, 0]), 1);
29+
assert.equal(classifySolid([0, 0, 0, 128]), classifySolid([127, 127, 127, 255]));
30+
assert.equal(classifySolid([0, 0, 0, 128]), 2);
31+
});
32+
33+
test('color grid classification preserves opaque palette colors', () => {
34+
assert.equal(classifySolid([0, 0, 0, 255]), 0);
35+
assert.equal(classifySolid([255, 255, 255, 255]), 1);
36+
assert.equal(classifySolid([128, 128, 128, 255]), 2);
37+
});
1438

1539
// The worked example from Wikipedia's "KarTrak" article, decoded from the
1640
// photograph shown at the top of that page: "Start 8350199918 Stop 5".

0 commit comments

Comments
 (0)