GlobalHistogramBinarizer.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 Binarizer implementation uses the old ZXing global histogram approach. It is suitable
  23. * for low-end mobile devices which don't have enough CPU or memory to use a local thresholding
  24. * algorithm. However, because it picks a global black point, it cannot handle difficult shadows
  25. * and gradients.
  26. *
  27. * Faster mobile devices and all desktop applications should probably use HybridBinarizer instead.
  28. *
  29. * @author dswitkin@google.com (Daniel Switkin)
  30. * @author Sean Owen
  31. */
  32. class GlobalHistogramBinarizer extends Binarizer
  33. {
  34. private static $LUMINANCE_BITS = 5;
  35. private static $LUMINANCE_SHIFT = 3;
  36. private static $LUMINANCE_BUCKETS = 32;
  37. private static $EMPTY = [];
  38. private $luminances = [];
  39. private $buckets = [];
  40. private $source = [];
  41. public function __construct($source)
  42. {
  43. self::$LUMINANCE_SHIFT = 8 - self::$LUMINANCE_BITS;
  44. self::$LUMINANCE_BUCKETS = 1 << self::$LUMINANCE_BITS;
  45. parent::__construct($source);
  46. $this->luminances = self::$EMPTY;
  47. $this->buckets = fill_array(0, self::$LUMINANCE_BUCKETS, 0);
  48. $this->source = $source;
  49. }
  50. // Applies simple sharpening to the row data to improve performance of the 1D Readers.
  51. public function getBlackRow($y, $row = null)
  52. {
  53. $this->source = $this->getLuminanceSource();
  54. $width = $this->source->getWidth();
  55. if ($row == null || $row->getSize() < $width) {
  56. $row = new BitArray($width);
  57. } else {
  58. $row->clear();
  59. }
  60. $this->initArrays($width);
  61. $localLuminances = $this->source->getRow($y, $this->luminances);
  62. $localBuckets = $this->buckets;
  63. for ($x = 0; $x < $width; $x++) {
  64. $pixel = $localLuminances[$x] & 0xff;
  65. $localBuckets[$pixel >> self::$LUMINANCE_SHIFT]++;
  66. }
  67. $blackPoint = self::estimateBlackPoint($localBuckets);
  68. $left = $localLuminances[0] & 0xff;
  69. $center = $localLuminances[1] & 0xff;
  70. for ($x = 1; $x < $width - 1; $x++) {
  71. $right = $localLuminances[$x + 1] & 0xff;
  72. // A simple -1 4 -1 box filter with a weight of 2.
  73. $luminance = (($center * 4) - $left - $right) / 2;
  74. if ($luminance < $blackPoint) {
  75. $row->set($x);
  76. }
  77. $left = $center;
  78. $center = $right;
  79. }
  80. return $row;
  81. }
  82. // Does not sharpen the data, as this call is intended to only be used by 2D Readers.
  83. private function initArrays($luminanceSize)
  84. {
  85. if (count($this->luminances) < $luminanceSize) {
  86. $this->luminances = [];
  87. }
  88. for ($x = 0; $x < self::$LUMINANCE_BUCKETS; $x++) {
  89. $this->buckets[$x] = 0;
  90. }
  91. }
  92. private static function estimateBlackPoint($buckets)
  93. {
  94. // Find the tallest peak in the histogram.
  95. $numBuckets = count($buckets);
  96. $maxBucketCount = 0;
  97. $firstPeak = 0;
  98. $firstPeakSize = 0;
  99. for ($x = 0; $x < $numBuckets; $x++) {
  100. if ($buckets[$x] > $firstPeakSize) {
  101. $firstPeak = $x;
  102. $firstPeakSize = $buckets[$x];
  103. }
  104. if ($buckets[$x] > $maxBucketCount) {
  105. $maxBucketCount = $buckets[$x];
  106. }
  107. }
  108. // Find the second-tallest peak which is somewhat far from the tallest peak.
  109. $secondPeak = 0;
  110. $secondPeakScore = 0;
  111. for ($x = 0; $x < $numBuckets; $x++) {
  112. $distanceToBiggest = $x - $firstPeak;
  113. // Encourage more distant second peaks by multiplying by square of distance.
  114. $score = $buckets[$x] * $distanceToBiggest * $distanceToBiggest;
  115. if ($score > $secondPeakScore) {
  116. $secondPeak = $x;
  117. $secondPeakScore = $score;
  118. }
  119. }
  120. // Make sure firstPeak corresponds to the black peak.
  121. if ($firstPeak > $secondPeak) {
  122. $temp = $firstPeak;
  123. $firstPeak = $secondPeak;
  124. $secondPeak = $temp;
  125. }
  126. // If there is too little contrast in the image to pick a meaningful black point, throw rather
  127. // than waste time trying to decode the image, and risk false positives.
  128. if ($secondPeak - $firstPeak <= $numBuckets / 16) {
  129. throw NotFoundException::getNotFoundInstance();
  130. }
  131. // Find a valley between them that is low and closer to the white peak.
  132. $bestValley = $secondPeak - 1;
  133. $bestValleyScore = -1;
  134. for ($x = $secondPeak - 1; $x > $firstPeak; $x--) {
  135. $fromFirst = $x - $firstPeak;
  136. $score = $fromFirst * $fromFirst * ($secondPeak - $x) * ($maxBucketCount - $buckets[$x]);
  137. if ($score > $bestValleyScore) {
  138. $bestValley = $x;
  139. $bestValleyScore = $score;
  140. }
  141. }
  142. return ($bestValley << self::$LUMINANCE_SHIFT);
  143. }
  144. public function getBlackMatrix()
  145. {
  146. $source = $this->getLuminanceSource();
  147. $width = $source->getWidth();
  148. $height = $source->getHeight();
  149. $matrix = new BitMatrix($width, $height);
  150. // Quickly calculates the histogram by sampling four rows from the image. This proved to be
  151. // more robust on the blackbox tests than sampling a diagonal as we used to do.
  152. $this->initArrays($width);
  153. $localBuckets = $this->buckets;
  154. for ($y = 1; $y < 5; $y++) {
  155. $row = (int)($height * $y / 5);
  156. $localLuminances = $source->getRow($row, $this->luminances);
  157. $right = (int)(($width * 4) / 5);
  158. for ($x = (int)($width / 5); $x < $right; $x++) {
  159. $pixel = ($localLuminances[(int)($x)] & 0xff);
  160. $localBuckets[($pixel >> self::$LUMINANCE_SHIFT)]++;
  161. }
  162. }
  163. $blackPoint = self::estimateBlackPoint($localBuckets);
  164. // We delay reading the entire image luminance until the black point estimation succeeds.
  165. // Although we end up reading four rows twice, it is consistent with our motto of
  166. // "fail quickly" which is necessary for continuous scanning.
  167. $localLuminances = $source->getMatrix();
  168. for ($y = 0; $y < $height; $y++) {
  169. $offset = $y * $width;
  170. for ($x = 0; $x < $width; $x++) {
  171. $pixel = (int)($localLuminances[$offset + $x] & 0xff);
  172. if ($pixel < $blackPoint) {
  173. $matrix->set($x, $y);
  174. }
  175. }
  176. }
  177. return $matrix;
  178. }
  179. public function createBinarizer($source)
  180. {
  181. return new GlobalHistogramBinarizer($source);
  182. }
  183. }