1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class Binarizer |
4
|
|
|
* |
5
|
|
|
* @created 17.01.2021 |
6
|
|
|
* @author ZXing Authors |
7
|
|
|
* @author Smiley <[email protected]> |
8
|
|
|
* @copyright 2021 Smiley |
9
|
|
|
* @license Apache-2.0 |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace chillerlan\QRCode\Decoder; |
13
|
|
|
|
14
|
|
|
use chillerlan\QRCode\Data\QRMatrix; |
15
|
|
|
use function array_fill, count, max; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* This class implements a local thresholding algorithm, which while slower than the |
19
|
|
|
* GlobalHistogramBinarizer, is fairly efficient for what it does. It is designed for |
20
|
|
|
* high frequency images of barcodes with black data on white backgrounds. For this application, |
21
|
|
|
* it does a much better job than a global blackpoint with severe shadows and gradients. |
22
|
|
|
* However, it tends to produce artifacts on lower frequency images and is therefore not |
23
|
|
|
* a good general purpose binarizer for uses outside ZXing. |
24
|
|
|
* |
25
|
|
|
* This class extends GlobalHistogramBinarizer, using the older histogram approach for 1D readers, |
26
|
|
|
* and the newer local approach for 2D readers. 1D decoding using a per-row histogram is already |
27
|
|
|
* inherently local, and only fails for horizontal gradients. We can revisit that problem later, |
28
|
|
|
* but for now it was not a win to use local blocks for 1D. |
29
|
|
|
* |
30
|
|
|
* This Binarizer is the default for the unit tests and the recommended class for library users. |
31
|
|
|
* |
32
|
|
|
* @author [email protected] (Daniel Switkin) |
33
|
|
|
*/ |
34
|
|
|
final class Binarizer{ |
35
|
|
|
|
36
|
|
|
// This class uses 5x5 blocks to compute local luminance, where each block is 8x8 pixels. |
37
|
|
|
// So this is the smallest dimension in each axis we can accept. |
38
|
|
|
private const BLOCK_SIZE_POWER = 3; |
39
|
|
|
private const BLOCK_SIZE = 8; // ...0100...00 |
40
|
|
|
private const BLOCK_SIZE_MASK = 7; // ...0011...11 |
41
|
|
|
private const MINIMUM_DIMENSION = 40; |
42
|
|
|
private const MIN_DYNAMIC_RANGE = 24; |
43
|
|
|
|
44
|
|
|
# private const LUMINANCE_BITS = 5; |
45
|
|
|
private const LUMINANCE_SHIFT = 3; |
46
|
|
|
private const LUMINANCE_BUCKETS = 32; |
47
|
|
|
|
48
|
|
|
private LuminanceSourceInterface $source; |
49
|
|
|
private array $luminances; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* |
53
|
|
|
*/ |
54
|
|
|
public function __construct(LuminanceSourceInterface $source){ |
55
|
|
|
$this->source = $source; |
56
|
|
|
$this->luminances = $this->source->getLuminances(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @throws \chillerlan\QRCode\Decoder\QRCodeDecoderException |
61
|
|
|
*/ |
62
|
|
|
private function estimateBlackPoint(array $buckets):int{ |
63
|
|
|
// Find the tallest peak in the histogram. |
64
|
|
|
$numBuckets = count($buckets); |
65
|
|
|
$maxBucketCount = 0; |
66
|
|
|
$firstPeak = 0; |
67
|
|
|
$firstPeakSize = 0; |
68
|
|
|
|
69
|
|
|
for($x = 0; $x < $numBuckets; $x++){ |
70
|
|
|
|
71
|
|
|
if($buckets[$x] > $firstPeakSize){ |
72
|
|
|
$firstPeak = $x; |
73
|
|
|
$firstPeakSize = $buckets[$x]; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if($buckets[$x] > $maxBucketCount){ |
77
|
|
|
$maxBucketCount = $buckets[$x]; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
// Find the second-tallest peak which is somewhat far from the tallest peak. |
82
|
|
|
$secondPeak = 0; |
83
|
|
|
$secondPeakScore = 0; |
84
|
|
|
|
85
|
|
|
for($x = 0; $x < $numBuckets; $x++){ |
86
|
|
|
$distanceToBiggest = ($x - $firstPeak); |
87
|
|
|
// Encourage more distant second peaks by multiplying by square of distance. |
88
|
|
|
$score = ($buckets[$x] * $distanceToBiggest * $distanceToBiggest); |
89
|
|
|
|
90
|
|
|
if($score > $secondPeakScore){ |
91
|
|
|
$secondPeak = $x; |
92
|
|
|
$secondPeakScore = $score; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
// Make sure firstPeak corresponds to the black peak. |
97
|
|
|
if($firstPeak > $secondPeak){ |
98
|
|
|
$temp = $firstPeak; |
99
|
|
|
$firstPeak = $secondPeak; |
100
|
|
|
$secondPeak = $temp; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
// If there is too little contrast in the image to pick a meaningful black point, throw rather |
104
|
|
|
// than waste time trying to decode the image, and risk false positives. |
105
|
|
|
if(($secondPeak - $firstPeak) <= ($numBuckets / 16)){ |
106
|
|
|
throw new QRCodeDecoderException('no meaningful dark point found'); // @codeCoverageIgnore |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
// Find a valley between them that is low and closer to the white peak. |
110
|
|
|
$bestValley = ($secondPeak - 1); |
111
|
|
|
$bestValleyScore = -1; |
112
|
|
|
|
113
|
|
|
for($x = ($secondPeak - 1); $x > $firstPeak; $x--){ |
114
|
|
|
$fromFirst = ($x - $firstPeak); |
115
|
|
|
$score = ($fromFirst * $fromFirst * ($secondPeak - $x) * ($maxBucketCount - $buckets[$x])); |
116
|
|
|
|
117
|
|
|
if($score > $bestValleyScore){ |
118
|
|
|
$bestValley = $x; |
119
|
|
|
$bestValleyScore = $score; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return ($bestValley << self::LUMINANCE_SHIFT); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Calculates the final BitMatrix once for all requests. This could be called once from the |
128
|
|
|
* constructor instead, but there are some advantages to doing it lazily, such as making |
129
|
|
|
* profiling easier, and not doing heavy lifting when callers don't expect it. |
130
|
|
|
* |
131
|
|
|
* Converts a 2D array of luminance data to 1 bit data. As above, assume this method is expensive |
132
|
|
|
* and do not call it repeatedly. This method is intended for decoding 2D barcodes and may or |
133
|
|
|
* may not apply sharpening. Therefore, a row from this matrix may not be identical to one |
134
|
|
|
* fetched using getBlackRow(), so don't mix and match between them. |
135
|
|
|
* |
136
|
|
|
* @return \chillerlan\QRCode\Decoder\BitMatrix The 2D array of bits for the image (true means black). |
137
|
|
|
*/ |
138
|
|
|
public function getBlackMatrix():BitMatrix{ |
139
|
|
|
$width = $this->source->getWidth(); |
140
|
|
|
$height = $this->source->getHeight(); |
141
|
|
|
|
142
|
|
|
if($width >= self::MINIMUM_DIMENSION && $height >= self::MINIMUM_DIMENSION){ |
143
|
|
|
$subWidth = ($width >> self::BLOCK_SIZE_POWER); |
144
|
|
|
|
145
|
|
|
if(($width & self::BLOCK_SIZE_MASK) !== 0){ |
146
|
|
|
$subWidth++; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
$subHeight = ($height >> self::BLOCK_SIZE_POWER); |
150
|
|
|
|
151
|
|
|
if(($height & self::BLOCK_SIZE_MASK) !== 0){ |
152
|
|
|
$subHeight++; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return $this->calculateThresholdForBlock($subWidth, $subHeight, $width, $height); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
// If the image is too small, fall back to the global histogram approach. |
159
|
|
|
return $this->getHistogramBlackMatrix($width, $height); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* |
164
|
|
|
*/ |
165
|
|
|
private function getHistogramBlackMatrix(int $width, int $height):BitMatrix{ |
166
|
|
|
|
167
|
|
|
// Quickly calculates the histogram by sampling four rows from the image. This proved to be |
168
|
|
|
// more robust on the blackbox tests than sampling a diagonal as we used to do. |
169
|
|
|
$buckets = array_fill(0, self::LUMINANCE_BUCKETS, 0); |
170
|
|
|
|
171
|
|
|
for($y = 1; $y < 5; $y++){ |
172
|
|
|
$row = (int)($height * $y / 5); |
173
|
|
|
$localLuminances = $this->source->getRow($row); |
174
|
|
|
$right = (int)(($width * 4) / 5); |
175
|
|
|
|
176
|
|
|
for($x = (int)($width / 5); $x < $right; $x++){ |
177
|
|
|
$pixel = ($localLuminances[$x] & 0xff); |
178
|
|
|
$buckets[($pixel >> self::LUMINANCE_SHIFT)]++; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
$blackPoint = $this->estimateBlackPoint($buckets); |
183
|
|
|
|
184
|
|
|
// We delay reading the entire image luminance until the black point estimation succeeds. |
185
|
|
|
// Although we end up reading four rows twice, it is consistent with our motto of |
186
|
|
|
// "fail quickly" which is necessary for continuous scanning. |
187
|
|
|
$matrix = new BitMatrix(max($width, $height)); |
188
|
|
|
|
189
|
|
|
for($y = 0; $y < $height; $y++){ |
190
|
|
|
$offset = ($y * $width); |
191
|
|
|
|
192
|
|
|
for($x = 0; $x < $width; $x++){ |
193
|
|
|
$matrix->set($x, $y, (($this->luminances[($offset + $x)] & 0xff) < $blackPoint), QRMatrix::M_DATA); |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
return $matrix; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Calculates a single black point for each block of pixels and saves it away. |
202
|
|
|
* See the following thread for a discussion of this algorithm: |
203
|
|
|
* |
204
|
|
|
* @see http://groups.google.com/group/zxing/browse_thread/thread/d06efa2c35a7ddc0 |
205
|
|
|
*/ |
206
|
|
|
private function calculateBlackPoints(int $subWidth, int $subHeight, int $width, int $height):array{ |
207
|
|
|
$blackPoints = array_fill(0, $subHeight, array_fill(0, $subWidth, 0)); |
208
|
|
|
|
209
|
|
|
for($y = 0; $y < $subHeight; $y++){ |
210
|
|
|
$yoffset = ($y << self::BLOCK_SIZE_POWER); |
211
|
|
|
$maxYOffset = ($height - self::BLOCK_SIZE); |
212
|
|
|
|
213
|
|
|
if($yoffset > $maxYOffset){ |
214
|
|
|
$yoffset = $maxYOffset; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
for($x = 0; $x < $subWidth; $x++){ |
218
|
|
|
$xoffset = ($x << self::BLOCK_SIZE_POWER); |
219
|
|
|
$maxXOffset = ($width - self::BLOCK_SIZE); |
220
|
|
|
|
221
|
|
|
if($xoffset > $maxXOffset){ |
222
|
|
|
$xoffset = $maxXOffset; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
$sum = 0; |
226
|
|
|
$min = 255; |
227
|
|
|
$max = 0; |
228
|
|
|
|
229
|
|
|
for($yy = 0, $offset = ($yoffset * $width + $xoffset); $yy < self::BLOCK_SIZE; $yy++, $offset += $width){ |
230
|
|
|
|
231
|
|
|
for($xx = 0; $xx < self::BLOCK_SIZE; $xx++){ |
232
|
|
|
$pixel = ((int)($this->luminances[(int)($offset + $xx)]) & 0xff); |
233
|
|
|
$sum += $pixel; |
234
|
|
|
// still looking for good contrast |
235
|
|
|
if($pixel < $min){ |
236
|
|
|
$min = $pixel; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
if($pixel > $max){ |
240
|
|
|
$max = $pixel; |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
// short-circuit min/max tests once dynamic range is met |
245
|
|
|
if(($max - $min) > self::MIN_DYNAMIC_RANGE){ |
246
|
|
|
// finish the rest of the rows quickly |
247
|
|
|
for($yy++, $offset += $width; $yy < self::BLOCK_SIZE; $yy++, $offset += $width){ |
248
|
|
|
for($xx = 0; $xx < self::BLOCK_SIZE; $xx++){ |
249
|
|
|
$sum += ((int)($this->luminances[(int)($offset + $xx)]) & 0xff); |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
// The default estimate is the average of the values in the block. |
256
|
|
|
$average = ($sum >> (self::BLOCK_SIZE_POWER * 2)); |
257
|
|
|
|
258
|
|
|
if(($max - $min) <= self::MIN_DYNAMIC_RANGE){ |
259
|
|
|
// If variation within the block is low, assume this is a block with only light or only |
260
|
|
|
// dark pixels. In that case we do not want to use the average, as it would divide this |
261
|
|
|
// low contrast area into black and white pixels, essentially creating data out of noise. |
262
|
|
|
// |
263
|
|
|
// The default assumption is that the block is light/background. Since no estimate for |
264
|
|
|
// the level of dark pixels exists locally, use half the min for the block. |
265
|
|
|
$average = (int)($min / 2); |
266
|
|
|
|
267
|
|
|
if($y > 0 && $x > 0){ |
268
|
|
|
// Correct the "white background" assumption for blocks that have neighbors by comparing |
269
|
|
|
// the pixels in this block to the previously calculated black points. This is based on |
270
|
|
|
// the fact that dark barcode symbology is always surrounded by some amount of light |
271
|
|
|
// background for which reasonable black point estimates were made. The bp estimated at |
272
|
|
|
// the boundaries is used for the interior. |
273
|
|
|
|
274
|
|
|
// The (min < bp) is arbitrary but works better than other heuristics that were tried. |
275
|
|
|
$averageNeighborBlackPoint = (int)( |
276
|
|
|
($blackPoints[($y - 1)][$x] + (2 * $blackPoints[$y][($x - 1)]) + $blackPoints[($y - 1)][($x - 1)]) / 4 |
277
|
|
|
); |
278
|
|
|
|
279
|
|
|
if($min < $averageNeighborBlackPoint){ |
280
|
|
|
$average = $averageNeighborBlackPoint; |
281
|
|
|
} |
282
|
|
|
} |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
$blackPoints[$y][$x] = $average; |
286
|
|
|
} |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
return $blackPoints; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* For each block in the image, calculate the average black point using a 5x5 grid |
294
|
|
|
* of the surrounding blocks. Also handles the corner cases (fractional blocks are computed based |
295
|
|
|
* on the last pixels in the row/column which are also used in the previous block). |
296
|
|
|
*/ |
297
|
|
|
private function calculateThresholdForBlock(int $subWidth, int $subHeight, int $width, int $height):BitMatrix{ |
298
|
|
|
$matrix = new BitMatrix(max($width, $height)); |
299
|
|
|
$blackPoints = $this->calculateBlackPoints($subWidth, $subHeight, $width, $height); |
300
|
|
|
|
301
|
|
|
for($y = 0; $y < $subHeight; $y++){ |
302
|
|
|
$yoffset = ($y << self::BLOCK_SIZE_POWER); |
303
|
|
|
$maxYOffset = ($height - self::BLOCK_SIZE); |
304
|
|
|
|
305
|
|
|
if($yoffset > $maxYOffset){ |
306
|
|
|
$yoffset = $maxYOffset; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
for($x = 0; $x < $subWidth; $x++){ |
310
|
|
|
$xoffset = ($x << self::BLOCK_SIZE_POWER); |
311
|
|
|
$maxXOffset = ($width - self::BLOCK_SIZE); |
312
|
|
|
|
313
|
|
|
if($xoffset > $maxXOffset){ |
314
|
|
|
$xoffset = $maxXOffset; |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
$left = $this->cap($x, 2, ($subWidth - 3)); |
318
|
|
|
$top = $this->cap($y, 2, ($subHeight - 3)); |
319
|
|
|
$sum = 0; |
320
|
|
|
|
321
|
|
|
for($z = -2; $z <= 2; $z++){ |
322
|
|
|
$br = $blackPoints[($top + $z)]; |
323
|
|
|
$sum += ($br[($left - 2)] + $br[($left - 1)] + $br[$left] + $br[($left + 1)] + $br[($left + 2)]); |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
$average = (int)($sum / 25); |
327
|
|
|
|
328
|
|
|
// Applies a single threshold to a block of pixels. |
329
|
|
|
for($j = 0, $o = ($yoffset * $width + $xoffset); $j < self::BLOCK_SIZE; $j++, $o += $width){ |
330
|
|
|
for($i = 0; $i < self::BLOCK_SIZE; $i++){ |
331
|
|
|
// Comparison needs to be <= so that black == 0 pixels are black even if the threshold is 0. |
332
|
|
|
$v = (((int)($this->luminances[($o + $i)]) & 0xff) <= $average); |
333
|
|
|
|
334
|
|
|
$matrix->set(($xoffset + $i), ($yoffset + $j), $v, QRMatrix::M_DATA); |
335
|
|
|
} |
336
|
|
|
} |
337
|
|
|
} |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
return $matrix; |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
/** |
344
|
|
|
* @noinspection PhpSameParameterValueInspection |
345
|
|
|
*/ |
346
|
|
|
private function cap(int $value, int $min, int $max):int{ |
347
|
|
|
|
348
|
|
|
if($value < $min){ |
349
|
|
|
return $min; |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
if($value > $max){ |
353
|
|
|
return $max; |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
return $value; |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
} |
360
|
|
|
|