You are here

function _smartcrop_imagick_entropy_slice in Imagick 7

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

Parameters

$image_data An image object:

$x Starting X coordinate.:

$y Starting Y coordinate.:

$width The width of the slice.:

$height The height of the slice.:

2 calls to _smartcrop_imagick_entropy_slice()
image_imagick_smartcrop_crop in imagick_smartcrop/imagick_smartcrop.inc
Crop an image, removing the lowest entropy areas.
SmartcropImagickTestCase::testEntropy in imagick_smartcrop/imagick_smartcrop.test
Verify the entropy calculation with a known image.

File

imagick_smartcrop/imagick_smartcrop.inc, line 103

Code

function _smartcrop_imagick_entropy_slice($image_data, $x, $y, $width, $height) {
  $histogram = _smartcrop_imagick_histogram($image_data->resource, $x, $y, $width, $height);
  $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;
}