Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fallback locator point recenter #166

Merged
merged 5 commits into from
Apr 29, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 24 additions & 23 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,35 @@ export interface QRCode {
}

function scan(matrix: BitMatrix): QRCode | null {
const location = locate(matrix);
if (!location) {
const locations = locate(matrix);
if (!locations) {
return null;
}
const extracted = extract(matrix, location);
const decoded = decode(extracted.matrix);

if (!decoded) {
return null;
}

return {
binaryData: decoded.bytes,
data: decoded.text,
chunks: decoded.chunks,
location: {
topRightCorner: extracted.mappingFunction(location.dimension, 0),
topLeftCorner: extracted.mappingFunction(0, 0),
bottomRightCorner: extracted.mappingFunction(location.dimension, location.dimension),
bottomLeftCorner: extracted.mappingFunction(0, location.dimension),
for (const location of locations) {
const extracted = extract(matrix, location);
const decoded = decode(extracted.matrix);
if (decoded) {
return {
binaryData: decoded.bytes,
data: decoded.text,
chunks: decoded.chunks,
location: {
topRightCorner: extracted.mappingFunction(location.dimension, 0),
topLeftCorner: extracted.mappingFunction(0, 0),
bottomRightCorner: extracted.mappingFunction(location.dimension, location.dimension),
bottomLeftCorner: extracted.mappingFunction(0, location.dimension),

topRightFinderPattern: location.topRight,
topLeftFinderPattern: location.topLeft,
bottomLeftFinderPattern: location.bottomLeft,
topRightFinderPattern: location.topRight,
topLeftFinderPattern: location.topLeft,
bottomLeftFinderPattern: location.bottomLeft,

bottomRightAlignmentPattern: location.alignmentPattern,
},
};
bottomRightAlignmentPattern: location.alignmentPattern,
},
};
}
}
return null;
}

export interface Options {
Expand Down
72 changes: 64 additions & 8 deletions src/locator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,30 @@ function scorePattern(point: Point, ratios: number[], matrix: BitMatrix) {
}
}

function recenterLocation(matrix: BitMatrix, p: Point): Point {
Copy link
Owner

@cozmo cozmo Apr 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason we wouldn't recenter vertically too?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't think it would matter, but after testing, it appears that also leads to a slight improvement.

a52cda5

let leftX = Math.round(p.x);
while (matrix.get(leftX, Math.round(p.y))) {
leftX--;
}
let rightX = Math.round(p.x);
while (matrix.get(rightX, Math.round(p.y))) {
rightX++;
}
const x = (leftX + rightX) / 2;

let topY = Math.round(p.y);
while (matrix.get(Math.round(x), topY)) {
topY--;
}
let bottomY = Math.round(p.y);
while (matrix.get(Math.round(x), bottomY)) {
bottomY++;
}
const y = (topY + bottomY) / 2;

return { x, y };
}

interface Quad {
top: {
startX: number;
Expand All @@ -225,7 +249,7 @@ interface Quad {
};
}

export function locate(matrix: BitMatrix): QRLocation {
export function locate(matrix: BitMatrix): QRLocation[] {
const finderPatternQuads: Quad[] = [];
let activeFinderPatternQuads: Quad[] = [];
const alignmentPatternQuads: Quad[] = [];
Expand Down Expand Up @@ -361,7 +385,45 @@ export function locate(matrix: BitMatrix): QRLocation {
const { topRight, topLeft, bottomLeft } = reorderFinderPatterns(
finderPatternGroups[0].points[0], finderPatternGroups[0].points[1], finderPatternGroups[0].points[2],
);
const alignment = findAlignmentPattern(matrix, alignmentPatternQuads, topRight, topLeft, bottomLeft);
const result: QRLocation[] = [];
if (alignment) {
result.push({
alignmentPattern: { x: alignment.alignmentPattern.x, y: alignment.alignmentPattern.y },
bottomLeft: {x: bottomLeft.x, y: bottomLeft.y },
dimension: alignment.dimension,
topLeft: {x: topLeft.x, y: topLeft.y },
topRight: {x: topRight.x, y: topRight.y },
});
}

// We normally use the center of the quads as the location of the tracking points, which is optimal for most cases and will account
// for a skew in the image. However, In some cases, a slight skew might not be real and instead be caused by image compression
// errors and/or low resolution. For those cases, we'd be better off centering the point exactly in the middle of the black area. We
// compute and return the location data for the naively centered points as it is little additional work and allows for multiple
// attempts at decoding harder images.
const midTopRight = recenterLocation(matrix, topRight);
const midTopLeft = recenterLocation(matrix, topLeft);
const midBottomLeft = recenterLocation(matrix, bottomLeft);
const centeredAlignment = findAlignmentPattern(matrix, alignmentPatternQuads, midTopRight, midTopLeft, midBottomLeft);
if (centeredAlignment) {
result.push({
alignmentPattern: { x: centeredAlignment.alignmentPattern.x, y: centeredAlignment.alignmentPattern.y },
bottomLeft: { x: midBottomLeft.x, y: midBottomLeft. y },
topLeft: { x: midTopLeft.x, y: midTopLeft. y },
topRight: { x: midTopRight.x, y: midTopRight. y },
dimension: centeredAlignment.dimension,
});
}

if (result.length === 0) {
return null;
}

return result;
}

function findAlignmentPattern(matrix: BitMatrix, alignmentPatternQuads: Quad[], topRight: Point, topLeft: Point, bottomLeft: Point) {
// Now that we've found the three finder patterns we can determine the blockSize and the size of the QR code.
// We'll use these to help find the alignment pattern but also later when we do the extraction.
let dimension: number;
Expand Down Expand Up @@ -405,11 +467,5 @@ export function locate(matrix: BitMatrix): QRLocation {
// so we can only use our best guess.
const alignmentPattern = modulesBetweenFinderPatterns >= 15 && alignmentPatterns.length ? alignmentPatterns[0] : expectedAlignmentPattern;

return {
alignmentPattern: { x: alignmentPattern.x, y: alignmentPattern.y },
bottomLeft: {x: bottomLeft.x, y: bottomLeft.y },
dimension,
topLeft: {x: topLeft.x, y: topLeft.y },
topRight: {x: topRight.x, y: topRight.y },
};
return { alignmentPattern, dimension };
}
Binary file added src/locator/test-data/odd-skew.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 17 additions & 6 deletions src/locator/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe("locate", () => {

it('locates a "perfect" image', async () => {
const binarized = await loadBinarized("./src/locator/test-data/perfect.png");
expect(locate(binarized)).toEqual({
expect(locate(binarized)[0]).toEqual({
alignmentPattern: {x: 170.5, y: 170.5},
bottomLeft: {x: 3.5, y: 173.5},
dimension: 177,
Expand All @@ -21,7 +21,7 @@ describe("locate", () => {

it("locates a QR in a real world image", async () => {
const binarized = await loadBinarized("./src/locator/test-data/real-world.png");
expect(locate(binarized)).toEqual({
expect(locate(binarized)[0]).toEqual({
alignmentPattern: { x: 264.25, y: 177 },
bottomLeft: { x: 195.5, y: 191.5 },
dimension: 33,
Expand All @@ -32,7 +32,7 @@ describe("locate", () => {

it("locates a small QR code in real world photo", async () => {
const binarized = await loadBinarized("./src/locator/test-data/small-photo.png");
expect(locate(binarized)).toEqual({
expect(locate(binarized)[0]).toEqual({
alignmentPattern: { x: 103, y: 147.5 },
bottomLeft: { x: 73.5, y: 152 },
dimension: 29,
Expand All @@ -43,7 +43,7 @@ describe("locate", () => {

it("locates a extremely distored QR code", async () => {
const binarized = await loadBinarized("./src/locator/test-data/distorted-extreme.png");
expect(locate(binarized)).toEqual({
expect(locate(binarized)[0]).toEqual({
alignmentPattern: { x: 164.5, y: 39 },
bottomLeft: { x: 221.5, y: 18.5 },
dimension: 25,
Expand All @@ -54,7 +54,7 @@ describe("locate", () => {

it("locates a damaged QR code and guesses the finder pattern location", async () => {
const binarized = await loadBinarized("./src/locator/test-data/damaged.png");
expect(locate(binarized)).toEqual({
expect(locate(binarized)[0]).toEqual({
alignmentPattern: { x: 219.75, y: 221 },
bottomLeft: { x: 81.5, y: 215.5 },
dimension: 29,
Expand All @@ -65,7 +65,7 @@ describe("locate", () => {

it("locates a damaged QR code and guesses the finder pattern location", async () => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't feel too strongly but wouldn't hurt to add a test case for this - Would be as simple as taking one of the originally failing ones and committing it into the locator tests.

Copy link
Collaborator Author

@jefff jefff Apr 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const binarized = await loadBinarized("./src/locator/test-data/damaged.png");
expect(locate(binarized)).toEqual({
expect(locate(binarized)[0]).toEqual({
alignmentPattern: { x: 219.75, y: 221 },
bottomLeft: { x: 81.5, y: 215.5 },
dimension: 29,
Expand All @@ -79,4 +79,15 @@ describe("locate", () => {
const binarized = await loadBinarized("./src/locator/test-data/malformed-infinity.png");
expect(locate(binarized)).toEqual(null);
});

it("returns a centered alignment as a fallback", async () => {
const binarized = await loadBinarized("./src/locator/test-data/odd-skew.png");
expect(locate(binarized)[1]).toEqual({
alignmentPattern: { x: 163.5, y: 170 },
bottomLeft: { x: 56.5, y: 185.5 },
dimension: 29,
topLeft: { x: 57, y: 60 },
topRight: { x: 185.5, y: 57.5 },
});
});
});
56 changes: 55 additions & 1 deletion tests/end-to-end/148/output.json
Original file line number Diff line number Diff line change
@@ -1 +1,55 @@
null
{
"binaryData": [
49,
50,
51,
52,
53,
54,
55,
56,
57,
48
],
"data": "1234567890",
"chunks": [
{
"type": "numeric",
"text": "1234567890"
}
],
"location": {
"topRightCorner": {
"x": 263.49999999999994,
"y": 291.375
},
"topLeftCorner": {
"x": 290.79762807324306,
"y": 234.26604686604492
},
"bottomRightCorner": {
"x": 207.9274477212262,
"y": 264.60245217347966
},
"bottomLeftCorner": {
"x": 233.49999999999997,
"y": 208.125
},
"topRightFinderPattern": {
"x": 258.5,
"y": 277.5
},
"topLeftFinderPattern": {
"x": 276.5,
"y": 239.5
},
"bottomLeftFinderPattern": {
"x": 238.5,
"y": 222
},
"bottomRightAlignmentPattern": {
"x": 232.84618068755663,
"y": 255.48041599830515
}
}
}
Loading