2 * Copyright 2009 ZXing authors
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.google.zxing.common;
19 import com.google.zxing.Binarizer;
20 import com.google.zxing.LuminanceSource;
21 import com.google.zxing.NotFoundException;
24 * This Binarizer implementation uses the old ZXing global histogram approach. It is suitable
25 * for low-end mobile devices which don't have enough CPU or memory to use a local thresholding
26 * algorithm. However, because it picks a global black point, it cannot handle difficult shadows
29 * Faster mobile devices and all desktop applications should probably use HybridBinarizer instead.
31 * @author dswitkin@google.com (Daniel Switkin)
34 public class GlobalHistogramBinarizer extends Binarizer {
36 private static final int LUMINANCE_BITS = 5;
37 private static final int LUMINANCE_SHIFT = 8 - LUMINANCE_BITS;
38 private static final int LUMINANCE_BUCKETS = 1 << LUMINANCE_BITS;
39 private static final byte[] EMPTY = new byte[0];
41 private byte[] luminances;
42 private final int[] buckets;
44 public GlobalHistogramBinarizer(LuminanceSource source) {
47 buckets = new int[LUMINANCE_BUCKETS];
50 // Applies simple sharpening to the row data to improve performance of the 1D Readers.
52 public BitArray getBlackRow(int y, BitArray row) throws NotFoundException {
53 LuminanceSource source = getLuminanceSource();
54 int width = source.getWidth();
55 if (row == null || row.getSize() < width) {
56 row = new BitArray(width);
62 byte[] localLuminances = source.getRow(y, luminances);
63 int[] localBuckets = buckets;
64 for (int x = 0; x < width; x++) {
65 int pixel = localLuminances[x] & 0xff;
66 localBuckets[pixel >> LUMINANCE_SHIFT]++;
68 int blackPoint = estimateBlackPoint(localBuckets);
70 int left = localLuminances[0] & 0xff;
71 int center = localLuminances[1] & 0xff;
72 for (int x = 1; x < width - 1; x++) {
73 int right = localLuminances[x + 1] & 0xff;
74 // A simple -1 4 -1 box filter with a weight of 2.
75 int luminance = ((center << 2) - left - right) >> 1;
76 if (luminance < blackPoint) {
85 // Does not sharpen the data, as this call is intended to only be used by 2D Readers.
87 public BitMatrix getBlackMatrix() throws NotFoundException {
88 LuminanceSource source = getLuminanceSource();
89 int width = source.getWidth();
90 int height = source.getHeight();
91 BitMatrix matrix = new BitMatrix(width, height);
93 // Quickly calculates the histogram by sampling four rows from the image. This proved to be
94 // more robust on the blackbox tests than sampling a diagonal as we used to do.
96 int[] localBuckets = buckets;
97 for (int y = 1; y < 5; y++) {
98 int row = height * y / 5;
99 byte[] localLuminances = source.getRow(row, luminances);
100 int right = (width << 2) / 5;
101 for (int x = width / 5; x < right; x++) {
102 int pixel = localLuminances[x] & 0xff;
103 localBuckets[pixel >> LUMINANCE_SHIFT]++;
106 int blackPoint = estimateBlackPoint(localBuckets);
108 // We delay reading the entire image luminance until the black point estimation succeeds.
109 // Although we end up reading four rows twice, it is consistent with our motto of
110 // "fail quickly" which is necessary for continuous scanning.
111 byte[] localLuminances = source.getMatrix();
112 for (int y = 0; y < height; y++) {
113 int offset = y * width;
114 for (int x = 0; x< width; x++) {
115 int pixel = localLuminances[offset + x] & 0xff;
116 if (pixel < blackPoint) {
126 public Binarizer createBinarizer(LuminanceSource source) {
127 return new GlobalHistogramBinarizer(source);
130 private void initArrays(int luminanceSize) {
131 if (luminances.length < luminanceSize) {
132 luminances = new byte[luminanceSize];
134 for (int x = 0; x < LUMINANCE_BUCKETS; x++) {
139 private static int estimateBlackPoint(int[] buckets) throws NotFoundException {
140 // Find the tallest peak in the histogram.
141 int numBuckets = buckets.length;
142 int maxBucketCount = 0;
144 int firstPeakSize = 0;
145 for (int x = 0; x < numBuckets; x++) {
146 if (buckets[x] > firstPeakSize) {
148 firstPeakSize = buckets[x];
150 if (buckets[x] > maxBucketCount) {
151 maxBucketCount = buckets[x];
155 // Find the second-tallest peak which is somewhat far from the tallest peak.
157 int secondPeakScore = 0;
158 for (int x = 0; x < numBuckets; x++) {
159 int distanceToBiggest = x - firstPeak;
160 // Encourage more distant second peaks by multiplying by square of distance.
161 int score = buckets[x] * distanceToBiggest * distanceToBiggest;
162 if (score > secondPeakScore) {
164 secondPeakScore = score;
168 // Make sure firstPeak corresponds to the black peak.
169 if (firstPeak > secondPeak) {
170 int temp = firstPeak;
171 firstPeak = secondPeak;
175 // If there is too little contrast in the image to pick a meaningful black point, throw rather
176 // than waste time trying to decode the image, and risk false positives.
177 if (secondPeak - firstPeak <= numBuckets >> 4) {
178 throw NotFoundException.getNotFoundInstance();
181 // Find a valley between them that is low and closer to the white peak.
182 int bestValley = secondPeak - 1;
183 int bestValleyScore = -1;
184 for (int x = secondPeak - 1; x > firstPeak; x--) {
185 int fromFirst = x - firstPeak;
186 int score = fromFirst * fromFirst * (secondPeak - x) * (maxBucketCount - buckets[x]);
187 if (score > bestValleyScore) {
189 bestValleyScore = score;
193 return bestValley << LUMINANCE_SHIFT;