You are here

function _smartcrop_imagick_histogram in Imagick 7

Compute a histogram of an image.

Parameters

resource $img Imagemagick image resource.:

$x Starting X coordinate.:

$y Starting Y coordinate.:

$width The width of the slice.:

$height The height of the slice.:

Return value

array histogram as an array.

1 call to _smartcrop_imagick_histogram()
_smartcrop_imagick_entropy_slice in imagick_smartcrop/imagick_smartcrop.inc
Compute the entropy of an image slice, defined as -sum(p.*log2(p)).

File

imagick_smartcrop/imagick_smartcrop.inc, line 126

Code

function _smartcrop_imagick_histogram($img, $x, $y, $width, $height) {
  $histogram = array_fill(0, 768, 0);
  $it = $img
    ->getPixelRegionIterator((int) $x, (int) $y, (int) $width, (int) $height);
  foreach ($it as $row => $pixels) {
    foreach ($pixels as $column => $pixel) {
      $rgb = $pixel
        ->getColor();
      $r = $rgb['r'];
      $g = $rgb['g'];
      $b = $rgb['b'];
      $histogram[$r]++;
      $histogram[$g + 256]++;
      $histogram[$b + 512]++;
    }
  }
  return $histogram;
}