You are here

class ImageFocusEntropy in Image Focus Crop 8

Class ImageFocusEntropy.

@package Drupal\image_focus\lib

Hierarchy

Expanded class hierarchy of ImageFocusEntropy

1 file declares its use of ImageFocusEntropy
FocusScaleCropImageEffect.php in src/Plugin/ImageEffect/FocusScaleCropImageEffect.php

File

src/lib/ImageFocusEntropy.php, line 10

Namespace

Drupal\image_focus\lib
View source
class ImageFocusEntropy {

  /**
   * Image object.
   *
   * @var mixed
   */
  protected $image;

  /**
   * X.
   *
   * @var mixed
   */
  protected $cx;

  /**
   * Y.
   *
   * @var mixed
   */
  protected $cy;

  /**
   * ImageFocusEntropy constructor.
   *
   * @param mixed $image
   *   Image object.
   */
  public function __construct($image) {
    $this->image = $image;
  }

  /**
   * Calculates the histogram of the zone.
   *
   * From ($x, $y) with a width of $dx and height of $dy.
   */
  protected function calculateHistogram($x, $y, $dx, $dy) {
    $maxx = min(imagesx($this->image), $x + $dx);
    $maxy = min(imagesy($this->image), $y + $dy);
    $histogram = array_fill(0, 768, 0);
    for ($i = $x; $i < $maxx; $i++) {
      for ($j = $y; $j < $maxy; $j++) {
        $rgb = imagecolorat($this->image, $i, $j);
        $r = $rgb >> 16 & 0xff;
        $g = $rgb >> 8 & 0xff;
        $b = $rgb & 0xff;
        $histogram[$r]++;
        $histogram[$g + 256]++;
        $histogram[$b + 512]++;
      }
    }
    return $histogram;
  }

  /**
   * Calculates the entropy of an image zone.
   *
   * Defined as -sum(p.*log2(p)) where p is the
   * histogram counts of a grayscale image.
   */
  protected function calculateEntropy($x, $y, $dx, $dy) {
    $histogram = $this
      ->calculateHistogram($x, $y, $dx, $dy);
    $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;
  }

  /**
   * Calculates the focal point.
   */
  public function calculateFocalPoint() {

    // Split the image into multiple zones and calculate entropy in each one.
    // $d is the size of the zones. This value is chosen emperically.
    // Then calculate the center of mass using entropy.
    $d = (int) max((imagesx($this->image) + imagesy($this->image)) / 50, 10);
    $nx = floor(imagesx($this->image) / $d);
    $ny = floor(imagesy($this->image) / $d);
    $entropies = array_fill(0, $nx * $ny, 0);
    $sum = $wsum_x = $wsum_y = 0;
    for ($i = 0; $i < $nx; $i++) {
      for ($j = 0; $j < $ny; $j++) {
        $entropy = $this
          ->calculateEntropy($i * $d, $j * $d, $d, $d);
        $entropies[$i + $j * $nx] = $entropy;
        $sum += $entropy;
        $wsum_x += $entropy * $i;
        $wsum_y += $entropy * $j;
      }
    }
    $this->cx = $sum ? $wsum_x / $sum * $d : 0;
    $this->cy = $sum ? $wsum_y / $sum * $d : 0;
  }

  /**
   * Get focal point.
   *
   * @return array
   *   Array of points.
   */
  public function getFocalPoint() {
    if (!isset($this->cx)) {
      $this
        ->calculateFocalPoint();
    }
    return [
      $this->cx,
      $this->cy,
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ImageFocusEntropy::$cx protected property X.
ImageFocusEntropy::$cy protected property Y.
ImageFocusEntropy::$image protected property Image object.
ImageFocusEntropy::calculateEntropy protected function Calculates the entropy of an image zone.
ImageFocusEntropy::calculateFocalPoint public function Calculates the focal point.
ImageFocusEntropy::calculateHistogram protected function Calculates the histogram of the zone.
ImageFocusEntropy::getFocalPoint public function Get focal point.
ImageFocusEntropy::__construct public function ImageFocusEntropy constructor.