You are here

function _smartcrop_gd_entropy in Smart Crop 7

Same name and namespace in other branches
  1. 6 imageapi_gd.inc \_smartcrop_gd_entropy()

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

Parameters

resource $img GD image resource.:

Return value

float The entropy of the image.

2 calls to _smartcrop_gd_entropy()
SmartcropTestCase::testEntropy in tests/smartcrop.test
Verify the entropy calculation with a known image.
_smartcrop_gd_entropy_slice in ./image.gd.inc
Compute the entropy of an image slice.

File

./image.gd.inc, line 129
libgd implementation of smartcrop action.

Code

function _smartcrop_gd_entropy($img) {
  $histogram = _smartcrop_gd_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;
}