HybridBinarizer.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. // This class uses 5x5 blocks to compute local luminance, where each block is 8x8 pixels.
  40. // So this is the smallest dimension in each axis we can accept.
  41. private static $BLOCK_SIZE_POWER = 3;
  42. private static $BLOCK_SIZE = 8; // ...0100...00
  43. private static $BLOCK_SIZE_MASK = 7; // ...0011...11
  44. private static $MINIMUM_DIMENSION = 40;
  45. private static $MIN_DYNAMIC_RANGE=24;
  46. private $matrix;
  47. public function __construct($source) {
  48. parent::__construct($source);
  49. self::$BLOCK_SIZE_POWER = 3;
  50. self::$BLOCK_SIZE = 1 << self::$BLOCK_SIZE_POWER; // ...0100...00
  51. self::$BLOCK_SIZE_MASK = self::$BLOCK_SIZE - 1; // ...0011...11
  52. self::$MINIMUM_DIMENSION = self::$BLOCK_SIZE * 5;
  53. self::$MIN_DYNAMIC_RANGE = 24;
  54. }
  55. /**
  56. * Calculates the final BitMatrix once for all requests. This could be called once from the
  57. * constructor instead, but there are some advantages to doing it lazily, such as making
  58. * profiling easier, and not doing heavy lifting when callers don't expect it.
  59. */
  60. //@Override
  61. public function getBlackMatrix(){
  62. if ($this->matrix != null) {
  63. return $this->matrix;
  64. }
  65. $source = $this->getLuminanceSource();
  66. $width = $source->getWidth();
  67. $height = $source->getHeight();
  68. if ($width >= self::$MINIMUM_DIMENSION && $height >= self::$MINIMUM_DIMENSION) {
  69. $luminances = $source->getMatrix();
  70. $subWidth = $width >> self::$BLOCK_SIZE_POWER;
  71. if (($width & self::$BLOCK_SIZE_MASK) != 0) {
  72. $subWidth++;
  73. }
  74. $subHeight = $height >> self::$BLOCK_SIZE_POWER;
  75. if (($height & self::$BLOCK_SIZE_MASK) != 0) {
  76. $subHeight++;
  77. }
  78. $blackPoints = $this->calculateBlackPoints($luminances, $subWidth, $subHeight, $width, $height);
  79. $newMatrix = new BitMatrix($width, $height);
  80. $this->calculateThresholdForBlock($luminances, $subWidth, $subHeight, $width, $height, $blackPoints, $newMatrix);
  81. $this->matrix = $newMatrix;
  82. } else {
  83. // If the image is too small, fall back to the global histogram approach.
  84. $this->matrix = parent::getBlackMatrix();
  85. }
  86. return $this->matrix;
  87. }
  88. //@Override
  89. public function createBinarizer($source) {
  90. return new HybridBinarizer($source);
  91. }
  92. /**
  93. * For each block in the image, calculate the average black point using a 5x5 grid
  94. * of the blocks around it. Also handles the corner cases (fractional blocks are computed based
  95. * on the last pixels in the row/column which are also used in the previous block).
  96. */
  97. private static function calculateThresholdForBlock($luminances,
  98. $subWidth,
  99. $subHeight,
  100. $width,
  101. $height,
  102. $blackPoints,
  103. $matrix) {
  104. for ($y = 0; $y < $subHeight; $y++) {
  105. $yoffset = intval32bits($y << self::$BLOCK_SIZE_POWER);
  106. $maxYOffset = $height - self::$BLOCK_SIZE;
  107. if ($yoffset > $maxYOffset) {
  108. $yoffset = $maxYOffset;
  109. }
  110. for ($x = 0; $x < $subWidth; $x++) {
  111. $xoffset = intval32bits($x << self::$BLOCK_SIZE_POWER);
  112. $maxXOffset = $width - self::$BLOCK_SIZE;
  113. if ($xoffset > $maxXOffset) {
  114. $xoffset = $maxXOffset;
  115. }
  116. $left = self::cap($x, 2, $subWidth - 3);
  117. $top = self::cap($y, 2, $subHeight - 3);
  118. $sum = 0;
  119. for ($z = -2; $z <= 2; $z++) {
  120. $blackRow = $blackPoints[$top + $z];
  121. $sum += $blackRow[$left - 2] + $blackRow[$left - 1] + $blackRow[$left] + $blackRow[$left + 1] + $blackRow[$left + 2];
  122. }
  123. $average = intval($sum / 25);
  124. self::thresholdBlock($luminances, $xoffset, $yoffset, $average, $width, $matrix);
  125. }
  126. }
  127. }
  128. private static function cap($value, $min, $max) {
  129. if($value<$min){
  130. return $min;
  131. }elseif($value>$max){
  132. return $max;
  133. }else{
  134. return $value;
  135. }
  136. }
  137. /**
  138. * Applies a single threshold to a block of pixels.
  139. */
  140. private static function thresholdBlock($luminances,
  141. $xoffset,
  142. $yoffset,
  143. $threshold,
  144. $stride,
  145. $matrix) {
  146. for ($y = 0, $offset = $yoffset * $stride + $xoffset; $y < self::$BLOCK_SIZE; $y++, $offset += $stride) {
  147. for ($x = 0; $x < self::$BLOCK_SIZE; $x++) {
  148. // Comparison needs to be <= so that black == 0 pixels are black even if the threshold is 0.
  149. if (($luminances[$offset + $x] & 0xFF) <= $threshold) {
  150. $matrix->set($xoffset + $x, $yoffset + $y);
  151. }
  152. }
  153. }
  154. }
  155. /**
  156. * Calculates a single black point for each block of pixels and saves it away.
  157. * See the following thread for a discussion of this algorithm:
  158. * http://groups.google.com/group/zxing/browse_thread/thread/d06efa2c35a7ddc0
  159. */
  160. private static function calculateBlackPoints($luminances,
  161. $subWidth,
  162. $subHeight,
  163. $width,
  164. $height) {
  165. $blackPoints = fill_array(0,$subHeight,0);
  166. foreach($blackPoints as $key=>$point){
  167. $blackPoints[$key] = fill_array(0,$subWidth,0);
  168. }
  169. for ($y = 0; $y < $subHeight; $y++) {
  170. $yoffset = intval32bits($y << self::$BLOCK_SIZE_POWER);
  171. $maxYOffset = $height - self::$BLOCK_SIZE;
  172. if ($yoffset > $maxYOffset) {
  173. $yoffset = $maxYOffset;
  174. }
  175. for ($x = 0; $x < $subWidth; $x++) {
  176. $xoffset = intval32bits($x << self::$BLOCK_SIZE_POWER);
  177. $maxXOffset = $width - self::$BLOCK_SIZE;
  178. if ($xoffset > $maxXOffset) {
  179. $xoffset = $maxXOffset;
  180. }
  181. $sum = 0;
  182. $min = 0xFF;
  183. $max = 0;
  184. for ($yy = 0, $offset = $yoffset * $width + $xoffset; $yy < self::$BLOCK_SIZE; $yy++, $offset += $width) {
  185. for ($xx = 0; $xx < self::$BLOCK_SIZE; $xx++) {
  186. $pixel = intval32bits(intval($luminances[intval($offset +$xx)]) & 0xFF);
  187. $sum += $pixel;
  188. // still looking for good contrast
  189. if ($pixel < $min) {
  190. $min = $pixel;
  191. }
  192. if ($pixel > $max) {
  193. $max = $pixel;
  194. }
  195. }
  196. // short-circuit min/max tests once dynamic range is met
  197. if ($max - $min > self::$MIN_DYNAMIC_RANGE) {
  198. // finish the rest of the rows quickly
  199. for ($yy++, $offset += $width; $yy < self::$BLOCK_SIZE; $yy++, $offset += $width) {
  200. for ($xx = 0; $xx < self::$BLOCK_SIZE; $xx++) {
  201. $sum += intval32bits($luminances[$offset +$xx] & 0xFF);
  202. }
  203. }
  204. }
  205. }
  206. // The default estimate is the average of the values in the block.
  207. $average = intval32bits($sum >> (self::$BLOCK_SIZE_POWER * 2));
  208. if ($max - $min <= self::$MIN_DYNAMIC_RANGE) {
  209. // If variation within the block is low, assume this is a block with only light or only
  210. // dark pixels. In that case we do not want to use the average, as it would divide this
  211. // low contrast area into black and white pixels, essentially creating data out of noise.
  212. //
  213. // The default assumption is that the block is light/background. Since no estimate for
  214. // the level of dark pixels exists locally, use half the min for the block.
  215. $average = intval($min / 2);
  216. if ($y > 0 && $x > 0) {
  217. // Correct the "white background" assumption for blocks that have neighbors by comparing
  218. // the pixels in this block to the previously calculated black points. This is based on
  219. // the fact that dark barcode symbology is always surrounded by some amount of light
  220. // background for which reasonable black point estimates were made. The bp estimated at
  221. // the boundaries is used for the interior.
  222. // The (min < bp) is arbitrary but works better than other heuristics that were tried.
  223. $averageNeighborBlackPoint =
  224. intval(($blackPoints[$y - 1][$x] + (2 * $blackPoints[$y][$x - 1]) + $blackPoints[$y - 1][$x - 1]) / 4);
  225. if ($min < $averageNeighborBlackPoint) {
  226. $average = $averageNeighborBlackPoint;
  227. }
  228. }
  229. }
  230. $blackPoints[$y][$x] = intval($average);
  231. }
  232. }
  233. return $blackPoints;
  234. }
  235. }