You are here

public static function GdImageAnalysis::entropy in Image Effects 8.3

Same name and namespace in other branches
  1. 8 src/Component/GdImageAnalysis.php \Drupal\image_effects\Component\GdImageAnalysis::entropy()
  2. 8.2 src/Component/GdImageAnalysis.php \Drupal\image_effects\Component\GdImageAnalysis::entropy()

Computes the entropy of an image, defined as -sum(p.*log2(p)).

Parameters

resource $img: A GD image resource.

Return value

float The entropy of the image.

3 calls to GdImageAnalysis::entropy()
GdImageAnalysisTest::testDifference in tests/src/Unit/GdImageAnalysisTest.php
Verify the difference calculation.
GdImageAnalysisTest::testEntropy in tests/src/Unit/GdImageAnalysisTest.php
Verify the entropy calculation with a known image.
GDOperationTrait::getAreaEntropy in src/Plugin/ImageToolkit/Operation/gd/GDOperationTrait.php
Computes the entropy of the area of an image.

File

src/Component/GdImageAnalysis.php, line 112

Class

GdImageAnalysis
Image analysis helper methods for GD.

Namespace

Drupal\image_effects\Component

Code

public static function entropy($img) {
  $histogram = static::histogram($img);
  $histogram_size = array_sum($histogram);
  $entropy = 0;
  foreach ($histogram as $p) {
    if ($p == 0) {
      continue;
    }
    $p = $p / $histogram_size;
    $entropy += $p * log($p, 2);
  }
  return $entropy * -1;
}