Modified WhiteRectangleDetector

Continue expanding the rectangle until at least one black point has been
found on each side, instead of stopping immediately when all sides are
white - which might still be inside the barcode.
This commit is contained in:
tgibson11 2014-02-12 10:18:22 -07:00
parent 78e2ac4522
commit 670e238cfc

View file

@ -32,8 +32,7 @@ import com.google.zxing.common.BitMatrix;
*/ */
public final class WhiteRectangleDetector { public final class WhiteRectangleDetector {
private static final float MIN_PCNT = 0.1; private static final int INIT_SIZE = 10;
private static final int MIN_SIZE = 10;
private static final int CORR = 1; private static final int CORR = 1;
private final BitMatrix image; private final BitMatrix image;
@ -45,10 +44,7 @@ public final class WhiteRectangleDetector {
private final int upInit; private final int upInit;
public WhiteRectangleDetector(BitMatrix image) throws NotFoundException { public WhiteRectangleDetector(BitMatrix image) throws NotFoundException {
int maxImageDimension = Math.max(image.getHeight, image.getWidth()); this(image, INIT_SIZE, image.getWidth() / 2, image.getHeight() / 2);
int pcntBasedInitSize = maxImageDimension * MIN_PCNT;
int initSize = Math.max(pcntBasedInitSize, MIN_SIZE));
this(image, initSize, image.getWidth() / 2, image.getHeight() / 2);
} }
/** /**
@ -91,6 +87,11 @@ public final class WhiteRectangleDetector {
boolean sizeExceeded = false; boolean sizeExceeded = false;
boolean aBlackPointFoundOnBorder = true; boolean aBlackPointFoundOnBorder = true;
boolean atLeastOneBlackPointFoundOnBorder = false; boolean atLeastOneBlackPointFoundOnBorder = false;
boolean atLeastOneBlackPointFoundOnRight = false;
boolean atLeastOneBlackPointFoundOnBottom = false;
boolean atLeastOneBlackPointFoundOnLeft = false;
boolean atLeastOneBlackPointFoundOnTop = false;
while (aBlackPointFoundOnBorder) { while (aBlackPointFoundOnBorder) {
@ -100,11 +101,14 @@ public final class WhiteRectangleDetector {
// . | // . |
// ..... // .....
boolean rightBorderNotWhite = true; boolean rightBorderNotWhite = true;
while (rightBorderNotWhite && right < width) { while ((rightBorderNotWhite || !atLeastOneBlackPointFoundOnRight) && right < width) {
rightBorderNotWhite = containsBlackPoint(up, down, right, false); rightBorderNotWhite = containsBlackPoint(up, down, right, false);
if (rightBorderNotWhite) { if (rightBorderNotWhite) {
right++; right++;
aBlackPointFoundOnBorder = true; aBlackPointFoundOnBorder = true;
atLeastOneBlackPointFoundOnRight = true;
} else if (!atLeastOneBlackPointFoundOnRight) {
right++;
} }
} }
@ -117,11 +121,14 @@ public final class WhiteRectangleDetector {
// . . // . .
// .___. // .___.
boolean bottomBorderNotWhite = true; boolean bottomBorderNotWhite = true;
while (bottomBorderNotWhite && down < height) { while ((bottomBorderNotWhite || !atLeastOneBlackPointFoundOnBottom) && down < height) {
bottomBorderNotWhite = containsBlackPoint(left, right, down, true); bottomBorderNotWhite = containsBlackPoint(left, right, down, true);
if (bottomBorderNotWhite) { if (bottomBorderNotWhite) {
down++; down++;
aBlackPointFoundOnBorder = true; aBlackPointFoundOnBorder = true;
atLeastOneBlackPointFoundOnBottom = true;
} else if (!atLeastOneBlackPointFoundOnBottom) {
down++;
} }
} }
@ -134,11 +141,14 @@ public final class WhiteRectangleDetector {
// | . // | .
// ..... // .....
boolean leftBorderNotWhite = true; boolean leftBorderNotWhite = true;
while (leftBorderNotWhite && left >= 0) { while ((leftBorderNotWhite || !atLeastOneBlackPointFoundOnLeft) && left >= 0) {
leftBorderNotWhite = containsBlackPoint(up, down, left, false); leftBorderNotWhite = containsBlackPoint(up, down, left, false);
if (leftBorderNotWhite) { if (leftBorderNotWhite) {
left--; left--;
aBlackPointFoundOnBorder = true; aBlackPointFoundOnBorder = true;
atLeastOneBlackPointFoundOnLeft = true;
} else if (!atLeastOneBlackPointFoundOnLeft) {
left--;
} }
} }
@ -151,11 +161,14 @@ public final class WhiteRectangleDetector {
// . . // . .
// ..... // .....
boolean topBorderNotWhite = true; boolean topBorderNotWhite = true;
while (topBorderNotWhite && up >= 0) { while ((topBorderNotWhite || !atLeastOneBlackPointFoundOnTop) && up >= 0) {
topBorderNotWhite = containsBlackPoint(left, right, up, true); topBorderNotWhite = containsBlackPoint(left, right, up, true);
if (topBorderNotWhite) { if (topBorderNotWhite) {
up--; up--;
aBlackPointFoundOnBorder = true; aBlackPointFoundOnBorder = true;
atLeastOneBlackPointFoundOnTop = true;
} else if (!atLeastOneBlackPointFoundOnTop) {
up--;
} }
} }
@ -167,8 +180,8 @@ public final class WhiteRectangleDetector {
if (aBlackPointFoundOnBorder) { if (aBlackPointFoundOnBorder) {
atLeastOneBlackPointFoundOnBorder = true; atLeastOneBlackPointFoundOnBorder = true;
} }
} }
if (!sizeExceeded && atLeastOneBlackPointFoundOnBorder) { if (!sizeExceeded && atLeastOneBlackPointFoundOnBorder) {