mirror of
https://github.com/zxing/zxing.git
synced 2025-03-05 20:48:51 -08:00
Fixed up some comments and formatting.
git-svn-id: https://zxing.googlecode.com/svn/trunk@1979 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
parent
4dfe5d7828
commit
f74e806694
|
@ -75,7 +75,8 @@ public final class HybridBinarizer extends GlobalHistogramBinarizer {
|
||||||
int[][] blackPoints = calculateBlackPoints(luminances, subWidth, subHeight, width, height);
|
int[][] blackPoints = calculateBlackPoints(luminances, subWidth, subHeight, width, height);
|
||||||
|
|
||||||
BitMatrix newMatrix = new BitMatrix(width, height);
|
BitMatrix newMatrix = new BitMatrix(width, height);
|
||||||
calculateThresholdForBlock(luminances, subWidth, subHeight, width, height, blackPoints, newMatrix);
|
calculateThresholdForBlock(luminances, subWidth, subHeight, width, height, blackPoints,
|
||||||
|
newMatrix);
|
||||||
matrix = newMatrix;
|
matrix = newMatrix;
|
||||||
} else {
|
} else {
|
||||||
// If the image is too small, fall back to the global histogram approach.
|
// If the image is too small, fall back to the global histogram approach.
|
||||||
|
@ -91,13 +92,8 @@ public final class HybridBinarizer extends GlobalHistogramBinarizer {
|
||||||
// For each 8x8 block in the image, calculate the average black point using a 5x5 grid
|
// For each 8x8 block in the image, calculate the average black point using a 5x5 grid
|
||||||
// of the blocks around it. Also handles the corner cases (fractional blocks are computed based
|
// of the blocks around it. Also handles the corner cases (fractional blocks are computed based
|
||||||
// on the last 8 pixels in the row/column which are also used in the previous block).
|
// on the last 8 pixels in the row/column which are also used in the previous block).
|
||||||
private static void calculateThresholdForBlock(byte[] luminances,
|
private static void calculateThresholdForBlock(byte[] luminances, int subWidth, int subHeight,
|
||||||
int subWidth,
|
int width, int height, int[][] blackPoints, BitMatrix matrix) {
|
||||||
int subHeight,
|
|
||||||
int width,
|
|
||||||
int height,
|
|
||||||
int[][] blackPoints,
|
|
||||||
BitMatrix matrix) {
|
|
||||||
for (int y = 0; y < subHeight; y++) {
|
for (int y = 0; y < subHeight; y++) {
|
||||||
int yoffset = y << BLOCK_SIZE_POWER;
|
int yoffset = y << BLOCK_SIZE_POWER;
|
||||||
if ((yoffset + BLOCK_SIZE) >= height) {
|
if ((yoffset + BLOCK_SIZE) >= height) {
|
||||||
|
@ -115,7 +111,8 @@ public final class HybridBinarizer extends GlobalHistogramBinarizer {
|
||||||
int sum = 0;
|
int sum = 0;
|
||||||
for (int z = -2; z <= 2; z++) {
|
for (int z = -2; z <= 2; z++) {
|
||||||
int[] blackRow = blackPoints[top + z];
|
int[] blackRow = blackPoints[top + z];
|
||||||
sum += blackRow[left - 2] + blackRow[left - 1] + blackRow[left] + blackRow[left + 1] + blackRow[left + 2];
|
sum += blackRow[left - 2] + blackRow[left - 1] + blackRow[left] + blackRow[left + 1] +
|
||||||
|
blackRow[left + 2];
|
||||||
}
|
}
|
||||||
int average = sum / 25;
|
int average = sum / 25;
|
||||||
threshold8x8Block(luminances, xoffset, yoffset, average, width, matrix);
|
threshold8x8Block(luminances, xoffset, yoffset, average, width, matrix);
|
||||||
|
@ -124,15 +121,11 @@ public final class HybridBinarizer extends GlobalHistogramBinarizer {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Applies a single threshold to an 8x8 block of pixels.
|
// Applies a single threshold to an 8x8 block of pixels.
|
||||||
private static void threshold8x8Block(byte[] luminances,
|
private static void threshold8x8Block(byte[] luminances, int xoffset, int yoffset, int threshold,
|
||||||
int xoffset,
|
int stride, BitMatrix matrix) {
|
||||||
int yoffset,
|
|
||||||
int threshold,
|
|
||||||
int stride,
|
|
||||||
BitMatrix matrix) {
|
|
||||||
for (int y = 0, offset = yoffset * stride + xoffset; y < BLOCK_SIZE; y++, offset += stride) {
|
for (int y = 0, offset = yoffset * stride + xoffset; y < BLOCK_SIZE; y++, offset += stride) {
|
||||||
for (int x = 0; x < BLOCK_SIZE; x++) {
|
for (int x = 0; x < BLOCK_SIZE; x++) {
|
||||||
// Comparison needs to be <= so that black == 0 pixels are black even if the threshold is 0
|
// Comparison needs to be <= so that black == 0 pixels are black even if the threshold is 0.
|
||||||
if ((luminances[offset + x] & 0xFF) <= threshold) {
|
if ((luminances[offset + x] & 0xFF) <= threshold) {
|
||||||
matrix.set(xoffset + x, yoffset + y);
|
matrix.set(xoffset + x, yoffset + y);
|
||||||
}
|
}
|
||||||
|
@ -140,19 +133,11 @@ public final class HybridBinarizer extends GlobalHistogramBinarizer {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Esimates blackPoint from previously calculated neighbor esitmates
|
|
||||||
private static int getBlackPointFromNeighbors(int[][] blackPoints, int x, int y) {
|
|
||||||
return (blackPoints[y-1][x] +
|
|
||||||
2*blackPoints[y][x-1] +
|
|
||||||
blackPoints[y-1][x-1]) >> 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Calculates a single black point for each 8x8 block of pixels and saves it away.
|
// Calculates a single black point for each 8x8 block of pixels and saves it away.
|
||||||
private static int[][] calculateBlackPoints(byte[] luminances,
|
// See the following thread for a discussion of this algorithm:
|
||||||
int subWidth,
|
// http://groups.google.com/group/zxing/browse_thread/thread/d06efa2c35a7ddc0
|
||||||
int subHeight,
|
private static int[][] calculateBlackPoints(byte[] luminances, int subWidth, int subHeight,
|
||||||
int width,
|
int width, int height) {
|
||||||
int height) {
|
|
||||||
int[][] blackPoints = new int[subHeight][subWidth];
|
int[][] blackPoints = new int[subHeight][subWidth];
|
||||||
for (int y = 0; y < subHeight; y++) {
|
for (int y = 0; y < subHeight; y++) {
|
||||||
int yoffset = y << BLOCK_SIZE_POWER;
|
int yoffset = y << BLOCK_SIZE_POWER;
|
||||||
|
@ -180,36 +165,29 @@ public final class HybridBinarizer extends GlobalHistogramBinarizer {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// See
|
// The default estimate is the average of the values in the block.
|
||||||
// http://groups.google.com/group/zxing/browse_thread/thread/d06efa2c35a7ddc0
|
|
||||||
|
|
||||||
// The default estimate is the average of the values in the block
|
|
||||||
int average = sum >> 6;
|
int average = sum >> 6;
|
||||||
|
|
||||||
if (max - min <= 24) {
|
if (max - min <= 24) {
|
||||||
// If variation wihthin the block is low, assume this is a
|
// If variation within the block is low, assume this is a block with only light or only
|
||||||
// block with only light or only dark pixels.
|
// dark pixels. In that case we do not want to use the average, as it would divide this
|
||||||
|
// low contrast area into black and white pixels, essentially creating data out of noise.
|
||||||
// The default assumption is that the block is light/background.
|
//
|
||||||
// Since no estimate for the level of dark pixels
|
// The default assumption is that the block is light/background. Since no estimate for
|
||||||
// exists locally, use half the min for the block.
|
// the level of dark pixels exists locally, use half the min for the block.
|
||||||
average = min >> 1;
|
average = min >> 1;
|
||||||
|
|
||||||
if (y > 0 && x > 0) {
|
|
||||||
// Correct the "white/background" assumption for blocks
|
|
||||||
// that have neighbors by comparing the pixels in this
|
|
||||||
// block to the previously calculated blackpoints. This is
|
|
||||||
// based on the fact that dark barcode symbology is always
|
|
||||||
// surrounded by some amount of light background for which
|
|
||||||
// reasonable blackpoint esimates were made. The bp estimated
|
|
||||||
// at the bondaries is used for the interior.
|
|
||||||
|
|
||||||
// The (min < bp) seems pretty arbitrary but works better than
|
|
||||||
// other heurstics that were tried.
|
|
||||||
|
|
||||||
int bp = getBlackPointFromNeighbors(blackPoints, x, y);
|
if (y > 0 && x > 0) {
|
||||||
if (min < bp) {
|
// Correct the "white background" assumption for blocks that have neighbors by comparing
|
||||||
average = bp;
|
// the pixels in this block to the previously calculated black points. This is based on
|
||||||
|
// the fact that dark barcode symbology is always surrounded by some amount of light
|
||||||
|
// background for which reasonable black point estimates were made. The bp estimated at
|
||||||
|
// the boundaries is used for the interior.
|
||||||
|
|
||||||
|
// The (min < bp) is arbitrary but works better than other heuristics that were tried.
|
||||||
|
int averageNeighborBlackPoint = (blackPoints[y - 1][x] + (2 * blackPoints[y][x - 1]) +
|
||||||
|
blackPoints[y - 1][x - 1]) >> 2;
|
||||||
|
if (min < averageNeighborBlackPoint) {
|
||||||
|
average = averageNeighborBlackPoint;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue