GlobalHistogramBinarizer.php 7.2 KB

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