You are here

public function ImageFocusEntropy::calculateFocalPoint in Image Focus Crop 6

Same name and namespace in other branches
  1. 7 image_focus.entropy.inc \ImageFocusEntropy::calculateFocalPoint()

Calculates the focal point.

1 call to ImageFocusEntropy::calculateFocalPoint()
ImageFocusEntropy::getFocalPoint in ./image_focus.entropy.inc

File

./image_focus.entropy.inc, line 65
Image entropy calculation.

Class

ImageFocusEntropy
@file Image entropy calculation.

Code

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;
}