Skip to content
This repository has been archived by the owner on Apr 30, 2021. It is now read-only.

Commit

Permalink
adding energy comparison between to two regions in an image
Browse files Browse the repository at this point in the history
  • Loading branch information
Stig Lindqvist committed Mar 12, 2016
1 parent 60c3495 commit 49430ac
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 9 deletions.
25 changes: 22 additions & 3 deletions spec/Stojg/Crop/CropEntropySpec.php
Expand Up @@ -44,14 +44,33 @@ function it_can_calculate_a_grayscale_entropy_value()
$obj = new \Imagick('spec/Stojg/Crop/fixtures/cloud-01.jpg');
$this->beConstructedWith($obj);
$this->getGrayScaleEntropy()->shouldBeFloat();
$this->getGrayScaleEntropy()->shouldBeCloseTo(11.521439369834);
$this->getGrayScaleEntropy()->shouldBeCloseTo(11.5);
}

public function getMatchers()
function it_should_be_able_to_make_sub_region_object()
{
$obj = new \Imagick('spec/Stojg/Crop/fixtures/cloud-01.jpg');
$this->beConstructedWith($obj);
$this->getRegion(500, 600, 0, 0)->area()->shouldReturn(300000);

}

function it_should_be_able_to_compare_energy_between_two_slices_of_an_image()
{
$obj = new \Imagick('spec/Stojg/Crop/fixtures/cloud-01.jpg');
$this->beConstructedWith($obj);
$a = $this->getRegion(500, 600, 0, 0);
$b = $this->getRegion(500, 600, 500, 0);
$a->compare($b)->shouldReturn(1);
$b->compare($a)->shouldReturn(-1);
$a->compare($a)->shouldReturn(0);
}

function getMatchers()
{
return [
'beCloseTo' => function ($subject, $key) {
return (abs(($subject - $key) / $subject) < 0.0000000000001);
return (abs(($subject - $key) / $subject) < 0.01);
},
];
}
Expand Down
43 changes: 37 additions & 6 deletions src/Stojg/Crop/CropEntropy.php
Expand Up @@ -2,29 +2,32 @@

namespace Stojg\Crop;

use Imagick;
use ImagickPixel;

class CropEntropy
{
/**
* @var \Imagick
* @var Imagick
*/
protected $image = null;

/**
* CropEntropy constructor
*
* @param \Imagick|null $image
* @param Imagick|null $image
*/
public function __construct(\Imagick $image = null)
public function __construct(Imagick $image = null)
{
if ($image !== null) {
$this->image = $image;
} else {
$this->image = new \Imagick();
$this->image = new Imagick();
}
}

/**
* @return \Imagick
* @return Imagick
*/
public function getImage()
{
Expand All @@ -42,6 +45,22 @@ public function area()
return $size['height'] * $size['width'];
}

/**
* @param CropEntropy $b
* @return int
*/
public function compare(CropEntropy $b)
{
$aValue = $this->getGrayScaleEntropy();
$bValue = $b->getGrayScaleEntropy();

if ($aValue == $bValue) {
return 0;
}

return ($aValue < $bValue) ? -1 : 1;
}

/**
* Calculate the entropy for this image.
*
Expand All @@ -60,7 +79,7 @@ public function getGrayScaleEntropy()

/**
*
* @param \ImagickPixel[] $histogram
* @param ImagickPixel[] $histogram
* @param int $area
* @return float
*/
Expand All @@ -76,4 +95,16 @@ protected function getEntropy($histogram, $area)
// $value is always 0.0 or negative, so transform into positive scalar value
return -$value;
}

/**
* @param int $width - The width of the region to be extracted
* @param int $height - The height of the region to be extracted
* @param int $x - X-coordinate of the top-left corner of the region to be extracted
* @param int $y -Y-coordinate of the top-left corner of the region to be extracted
* @return CropEntropy
*/
public function getRegion($width, $height, $x, $y)
{
return new CropEntropy($this->image->getImageRegion($width, $height, $x, $y));
}
}

0 comments on commit 49430ac

Please sign in to comment.