HybridBinarizer.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. /*
  3. * Copyright 2009 ZXing authors
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. namespace Zxing\Common;
  18. use Zxing\Binarizer;
  19. use Zxing\LuminanceSource;
  20. use Zxing\NotFoundException;
  21. /**
  22. * This class implements a local thresholding algorithm, which while slower than the
  23. * GlobalHistogramBinarizer, is fairly efficient for what it does. It is designed for
  24. * high frequency images of barcodes with black data on white backgrounds. For this application,
  25. * it does a much better job than a global blackpoint with severe shadows and gradients.
  26. * However it tends to produce artifacts on lower frequency images and is therefore not
  27. * a good general purpose binarizer for uses outside ZXing.
  28. *
  29. * This class extends GlobalHistogramBinarizer, using the older histogram approach for 1D readers,
  30. * and the newer local approach for 2D readers. 1D decoding using a per-row histogram is already
  31. * inherently local, and only fails for horizontal gradients. We can revisit that problem later,
  32. * but for now it was not a win to use local blocks for 1D.
  33. *
  34. * This Binarizer is the default for the unit tests and the recommended class for library users.
  35. *
  36. * @author dswitkin@google.com (Daniel Switkin)
  37. */
  38. final class HybridBinarizer extends GlobalHistogramBinarizer
  39. {
  40. // This class uses 5x5 blocks to compute local luminance, where each block is 8x8 pixels.
  41. // So this is the smallest dimension in each axis we can accept.
  42. private static $BLOCK_SIZE_POWER = 3;
  43. private static $BLOCK_SIZE = 8; // ...0100...00
  44. private static $BLOCK_SIZE_MASK = 7; // ...0011...11
  45. private static $MINIMUM_DIMENSION = 40;
  46. private static $MIN_DYNAMIC_RANGE = 24;
  47. private $matrix;
  48. public function __construct($source)
  49. {
  50. parent::__construct($source);
  51. self::$BLOCK_SIZE_POWER = 3;
  52. self::$BLOCK_SIZE = 1 << self::$BLOCK_SIZE_POWER; // ...0100...00
  53. self::$BLOCK_SIZE_MASK = self::$BLOCK_SIZE - 1; // ...0011...11
  54. self::$MINIMUM_DIMENSION = self::$BLOCK_SIZE * 5;
  55. self::$MIN_DYNAMIC_RANGE = 24;
  56. }
  57. /**
  58. * Calculates the final BitMatrix once for all requests. This could be called once from the
  59. * constructor instead, but there are some advantages to doing it lazily, such as making
  60. * profiling easier, and not doing heavy lifting when callers don't expect it.
  61. */
  62. public function getBlackMatrix()
  63. {
  64. if ($this->matrix !== null) {
  65. return $this->matrix;
  66. }
  67. $source = $this->getLuminanceSource();
  68. $width = $source->getWidth();
  69. $height = $source->getHeight();
  70. if ($width >= self::$MINIMUM_DIMENSION && $height >= self::$MINIMUM_DIMENSION) {
  71. $luminances = $source->getMatrix();
  72. $subWidth = $width >> self::$BLOCK_SIZE_POWER;
  73. if (($width & self::$BLOCK_SIZE_MASK) != 0) {
  74. $subWidth++;
  75. }
  76. $subHeight = $height >> self::$BLOCK_SIZE_POWER;
  77. if (($height & self::$BLOCK_SIZE_MASK) != 0) {
  78. $subHeight++;
  79. }
  80. $blackPoints = self::calculateBlackPoints($luminances, $subWidth, $subHeight, $width, $height);
  81. $newMatrix = new BitMatrix($width, $height);
  82. self::calculateThresholdForBlock($luminances, $subWidth, $subHeight, $width, $height, $blackPoints, $newMatrix);
  83. $this->matrix = $newMatrix;
  84. } else {
  85. // If the image is too small, fall back to the global histogram approach.
  86. $this->matrix = parent::getBlackMatrix();
  87. }
  88. return $this->matrix;
  89. }
  90. /**
  91. * Calculates a single black point for each block of pixels and saves it away.
  92. * See the following thread for a discussion of this algorithm:
  93. * http://groups.google.com/group/zxing/browse_thread/thread/d06efa2c35a7ddc0
  94. */
  95. private static function calculateBlackPoints(
  96. $luminances,
  97. $subWidth,
  98. $subHeight,
  99. $width,
  100. $height
  101. ) {
  102. $blackPoints = fill_array(0, $subHeight, 0);
  103. foreach ($blackPoints as $key => $point) {
  104. $blackPoints[$key] = fill_array(0, $subWidth, 0);
  105. }
  106. for ($y = 0; $y < $subHeight; $y++) {
  107. $yoffset = ($y << self::$BLOCK_SIZE_POWER);
  108. $maxYOffset = $height - self::$BLOCK_SIZE;
  109. if ($yoffset > $maxYOffset) {
  110. $yoffset = $maxYOffset;
  111. }
  112. for ($x = 0; $x < $subWidth; $x++) {
  113. $xoffset = ($x << self::$BLOCK_SIZE_POWER);
  114. $maxXOffset = $width - self::$BLOCK_SIZE;
  115. if ($xoffset > $maxXOffset) {
  116. $xoffset = $maxXOffset;
  117. }
  118. $sum = 0;
  119. $min = 0xFF;
  120. $max = 0;
  121. for ($yy = 0, $offset = $yoffset * $width + $xoffset; $yy < self::$BLOCK_SIZE; $yy++, $offset += $width) {
  122. for ($xx = 0; $xx < self::$BLOCK_SIZE; $xx++) {
  123. $pixel = ((int)($luminances[(int)($offset + $xx)]) & 0xFF);
  124. $sum += $pixel;
  125. // still looking for good contrast
  126. if ($pixel < $min) {
  127. $min = $pixel;
  128. }
  129. if ($pixel > $max) {
  130. $max = $pixel;
  131. }
  132. }
  133. // short-circuit min/max tests once dynamic range is met
  134. if ($max - $min > self::$MIN_DYNAMIC_RANGE) {
  135. // finish the rest of the rows quickly
  136. for ($yy++, $offset += $width; $yy < self::$BLOCK_SIZE; $yy++, $offset += $width) {
  137. for ($xx = 0; $xx < self::$BLOCK_SIZE; $xx++) {
  138. $sum += ($luminances[$offset + $xx] & 0xFF);
  139. }
  140. }
  141. }
  142. }
  143. // The default estimate is the average of the values in the block.
  144. $average = ($sum >> (self::$BLOCK_SIZE_POWER * 2));
  145. if ($max - $min <= self::$MIN_DYNAMIC_RANGE) {
  146. // If variation within the block is low, assume this is a block with only light or only
  147. // dark pixels. In that case we do not want to use the average, as it would divide this
  148. // low contrast area into black and white pixels, essentially creating data out of noise.
  149. //
  150. // The default assumption is that the block is light/background. Since no estimate for
  151. // the level of dark pixels exists locally, use half the min for the block.
  152. $average = (int)($min / 2);
  153. if ($y > 0 && $x > 0) {
  154. // Correct the "white background" assumption for blocks that have neighbors by comparing
  155. // the pixels in this block to the previously calculated black points. This is based on
  156. // the fact that dark barcode symbology is always surrounded by some amount of light
  157. // background for which reasonable black point estimates were made. The bp estimated at
  158. // the boundaries is used for the interior.
  159. // The (min < bp) is arbitrary but works better than other heuristics that were tried.
  160. $averageNeighborBlackPoint =
  161. (int)(($blackPoints[$y - 1][$x] + (2 * $blackPoints[$y][$x - 1]) + $blackPoints[$y - 1][$x - 1]) / 4);
  162. if ($min < $averageNeighborBlackPoint) {
  163. $average = $averageNeighborBlackPoint;
  164. }
  165. }
  166. }
  167. $blackPoints[$y][$x] = (int)($average);
  168. }
  169. }
  170. return $blackPoints;
  171. }
  172. /**
  173. * For each block in the image, calculate the average black point using a 5x5 grid
  174. * of the blocks around it. Also handles the corner cases (fractional blocks are computed based
  175. * on the last pixels in the row/column which are also used in the previous block).
  176. */
  177. private static function calculateThresholdForBlock(
  178. $luminances,
  179. $subWidth,
  180. $subHeight,
  181. $width,
  182. $height,
  183. $blackPoints,
  184. $matrix
  185. ) {
  186. for ($y = 0; $y < $subHeight; $y++) {
  187. $yoffset = ($y << self::$BLOCK_SIZE_POWER);
  188. $maxYOffset = $height - self::$BLOCK_SIZE;
  189. if ($yoffset > $maxYOffset) {
  190. $yoffset = $maxYOffset;
  191. }
  192. for ($x = 0; $x < $subWidth; $x++) {
  193. $xoffset = ($x << self::$BLOCK_SIZE_POWER);
  194. $maxXOffset = $width - self::$BLOCK_SIZE;
  195. if ($xoffset > $maxXOffset) {
  196. $xoffset = $maxXOffset;
  197. }
  198. $left = self::cap($x, 2, $subWidth - 3);
  199. $top = self::cap($y, 2, $subHeight - 3);
  200. $sum = 0;
  201. for ($z = -2; $z <= 2; $z++) {
  202. $blackRow = $blackPoints[$top + $z];
  203. $sum += $blackRow[$left - 2] + $blackRow[$left - 1] + $blackRow[$left] + $blackRow[$left + 1] + $blackRow[$left + 2];
  204. }
  205. $average = (int)($sum / 25);
  206. self::thresholdBlock($luminances, $xoffset, $yoffset, $average, $width, $matrix);
  207. }
  208. }
  209. }
  210. private static function cap($value, $min, $max)
  211. {
  212. if ($value < $min) {
  213. return $min;
  214. } elseif ($value > $max) {
  215. return $max;
  216. } else {
  217. return $value;
  218. }
  219. }
  220. /**
  221. * Applies a single threshold to a block of pixels.
  222. */
  223. private static function thresholdBlock(
  224. $luminances,
  225. $xoffset,
  226. $yoffset,
  227. $threshold,
  228. $stride,
  229. $matrix
  230. ) {
  231. for ($y = 0, $offset = $yoffset * $stride + $xoffset; $y < self::$BLOCK_SIZE; $y++, $offset += $stride) {
  232. for ($x = 0; $x < self::$BLOCK_SIZE; $x++) {
  233. // Comparison needs to be <= so that black == 0 pixels are black even if the threshold is 0.
  234. if (($luminances[$offset + $x] & 0xFF) <= $threshold) {
  235. $matrix->set($xoffset + $x, $yoffset + $y);
  236. }
  237. }
  238. }
  239. }
  240. public function createBinarizer($source)
  241. {
  242. return new HybridBinarizer($source);
  243. }
  244. }