2 * Copyright 2009 ZXing authors
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
17 using Binarizer = com.google.zxing.Binarizer;
\r
18 using LuminanceSource = com.google.zxing.LuminanceSource;
\r
19 using ReaderException = com.google.zxing.ReaderException;
\r
20 namespace com.google.zxing.common
\r
23 /// <summary> This class implements a local thresholding algorithm, which while slower than the
\r
24 /// GlobalHistogramBinarizer, is fairly efficient for what it does. It is designed for
\r
25 /// high frequency images of barcodes with black data on white backgrounds. For this application,
\r
26 /// it does a much better job than a global blackpoint with severe shadows and gradients.
\r
27 /// However it tends to produce artifacts on lower frequency images and is therefore not
\r
28 /// a good general purpose binarizer for uses outside ZXing.
\r
30 /// This class extends GlobalHistogramBinarizer, using the older histogram approach for 1D readers,
\r
31 /// and the newer local approach for 2D readers. 1D decoding using a per-row histogram is already
\r
32 /// inherently local, and only fails for horizontal gradients. We can revisit that problem later,
\r
33 /// but for now it was not a win to use local blocks for 1D.
\r
35 /// This Binarizer is the default for the unit tests and the recommended class for library users.
\r
38 /// <author> dswitkin@google.com (Daniel Switkin)
\r
40 /// <author>www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source
\r
42 public sealed class HybridBinarizer:GlobalHistogramBinarizer
\r
44 override public BitMatrix BlackMatrix
\r
48 binarizeEntireImage();
\r
54 // This class uses 5x5 blocks to compute local luminance, where each block is 8x8 pixels.
\r
55 // So this is the smallest dimension in each axis we can accept.
\r
56 private const int MINIMUM_DIMENSION = 40;
\r
58 private BitMatrix matrix = null;
\r
60 public HybridBinarizer(LuminanceSource source):base(source)
\r
64 public override Binarizer createBinarizer(LuminanceSource source)
\r
66 return new HybridBinarizer(source);
\r
69 // Calculates the final BitMatrix once for all requests. This could be called once from the
\r
70 // constructor instead, but there are some advantages to doing it lazily, such as making
\r
71 // profiling easier, and not doing heavy lifting when callers don't expect it.
\r
72 private void binarizeEntireImage()
\r
76 LuminanceSource source = LuminanceSource;
\r
77 if (source.Width >= MINIMUM_DIMENSION && source.Height >= MINIMUM_DIMENSION)
\r
79 sbyte[] luminances = source.Matrix;
\r
80 int width = source.Width;
\r
81 int height = source.Height;
\r
82 int subWidth = width >> 3;
\r
83 int subHeight = height >> 3;
\r
84 int[][] blackPoints = calculateBlackPoints(luminances, subWidth, subHeight, width);
\r
86 matrix = new BitMatrix(width, height);
\r
87 calculateThresholdForBlock(luminances, subWidth, subHeight, width, blackPoints, matrix);
\r
91 // If the image is too small, fall back to the global histogram approach.
\r
92 matrix = base.BlackMatrix;
\r
97 // For each 8x8 block in the image, calculate the average black point using a 5x5 grid
\r
98 // of the blocks around it. Also handles the corner cases, but will ignore up to 7 pixels
\r
99 // on the right edge and 7 pixels at the bottom of the image if the overall dimensions are not
\r
100 // multiples of eight. In practice, leaving those pixels white does not seem to be a problem.
\r
101 private static void calculateThresholdForBlock(sbyte[] luminances, int subWidth, int subHeight, int stride, int[][] blackPoints, BitMatrix matrix)
\r
103 for (int y = 0; y < subHeight; y++)
\r
105 for (int x = 0; x < subWidth; x++)
\r
107 int left = (x > 1)?x:2;
\r
108 left = (left < subWidth - 2)?left:subWidth - 3;
\r
109 int top = (y > 1)?y:2;
\r
110 top = (top < subHeight - 2)?top:subHeight - 3;
\r
112 for (int z = - 2; z <= 2; z++)
\r
114 int[] blackRow = blackPoints[top + z];
\r
115 sum += blackRow[left - 2];
\r
116 sum += blackRow[left - 1];
\r
117 sum += blackRow[left];
\r
118 sum += blackRow[left + 1];
\r
119 sum += blackRow[left + 2];
\r
121 int average = sum / 25;
\r
122 threshold8x8Block(luminances, x << 3, y << 3, average, stride, matrix);
\r
127 // Applies a single threshold to an 8x8 block of pixels.
\r
128 private static void threshold8x8Block(sbyte[] luminances, int xoffset, int yoffset, int threshold, int stride, BitMatrix matrix)
\r
130 for (int y = 0; y < 8; y++)
\r
132 int offset = (yoffset + y) * stride + xoffset;
\r
133 for (int x = 0; x < 8; x++)
\r
135 int pixel = luminances[offset + x] & 0xff;
\r
136 if (pixel < threshold)
\r
138 matrix.set_Renamed(xoffset + x, yoffset + y);
\r
144 // Calculates a single black point for each 8x8 block of pixels and saves it away.
\r
145 private static int[][] calculateBlackPoints(sbyte[] luminances, int subWidth, int subHeight, int stride)
\r
147 int[][] blackPoints = new int[subHeight][];
\r
148 for (int i = 0; i < subHeight; i++)
\r
150 blackPoints[i] = new int[subWidth];
\r
152 for (int y = 0; y < subHeight; y++)
\r
154 for (int x = 0; x < subWidth; x++)
\r
159 for (int yy = 0; yy < 8; yy++)
\r
161 int offset = ((y << 3) + yy) * stride + (x << 3);
\r
162 for (int xx = 0; xx < 8; xx++)
\r
164 int pixel = luminances[offset + xx] & 0xff;
\r
177 // If the contrast is inadequate, use half the minimum, so that this block will be
\r
178 // treated as part of the white background, but won't drag down neighboring blocks
\r
180 int average = (max - min > 24)?(sum >> 6):(min >> 1);
\r
181 blackPoints[y][x] = average;
\r
184 return blackPoints;
\r