GlobalHistogramBinarizer.php 6.4 KB

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