1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Stojg\Crop; |
4
|
|
|
|
5
|
|
|
use Imagick; |
6
|
|
|
use ImagickPixel; |
7
|
|
|
|
8
|
|
|
class CropEntropy |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var Imagick |
12
|
|
|
*/ |
13
|
|
|
protected $image = null; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* CropEntropy constructor |
17
|
|
|
* |
18
|
|
|
* @param Imagick|null $image |
19
|
|
|
*/ |
20
|
|
|
public function __construct(Imagick $image = null) |
21
|
|
|
{ |
22
|
|
|
if ($image !== null) { |
23
|
|
|
$this->image = $image; |
24
|
|
|
} else { |
25
|
|
|
$this->image = new Imagick(); |
26
|
|
|
} |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @return Imagick |
31
|
|
|
*/ |
32
|
|
|
public function getImage() |
33
|
|
|
{ |
34
|
|
|
return $this->image; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Get the area in pixels for this image |
39
|
|
|
* |
40
|
|
|
* @return int |
41
|
|
|
*/ |
42
|
|
|
public function area() |
43
|
|
|
{ |
44
|
|
|
$size = $this->image->getImageGeometry(); |
45
|
|
|
return $size['height'] * $size['width']; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param CropEntropy $b |
50
|
|
|
* @return int |
51
|
|
|
*/ |
52
|
|
|
public function compare(CropEntropy $b) |
53
|
|
|
{ |
54
|
|
|
$aValue = $this->getGrayScaleEntropy(); |
55
|
|
|
$bValue = $b->getGrayScaleEntropy(); |
56
|
|
|
|
57
|
|
|
if ($aValue == $bValue) { |
58
|
|
|
return 0; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return ($aValue < $bValue) ? -1 : 1; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Calculate the entropy for this image. |
66
|
|
|
* |
67
|
|
|
* A higher value of entropy means more noise / liveliness / color / business |
68
|
|
|
* |
69
|
|
|
* @return float |
70
|
|
|
* |
71
|
|
|
* @see http://brainacle.com/calculating-image-entropy-with-python-how-and-why.html |
72
|
|
|
* @see http://www.mathworks.com/help/toolbox/images/ref/entropy.html |
73
|
|
|
*/ |
74
|
|
|
public function getGrayScaleEntropy() |
75
|
|
|
{ |
76
|
|
|
$histogram = $this->image->getImageHistogram(); |
77
|
|
|
return $this->getEntropy($histogram, $this->area()); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* |
82
|
|
|
* @param ImagickPixel[] $histogram |
83
|
|
|
* @param int $area |
84
|
|
|
* @return float |
85
|
|
|
*/ |
86
|
|
|
protected function getEntropy($histogram, $area) |
87
|
|
|
{ |
88
|
|
|
$value = 0.0; |
89
|
|
|
foreach ($histogram as $pixel) { |
90
|
|
|
// calculates the percentage of pixels having this color value |
91
|
|
|
$p = $pixel->getColorCount() / $area; |
92
|
|
|
// A common way of representing entropy in scalar |
93
|
|
|
$value += $p * log($p, 2); |
94
|
|
|
} |
95
|
|
|
// $value is always 0.0 or negative, so transform into positive scalar value |
96
|
|
|
return -$value; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param int $width - The width of the region to be extracted |
101
|
|
|
* @param int $height - The height of the region to be extracted |
102
|
|
|
* @param int $x - X-coordinate of the top-left corner of the region to be extracted |
103
|
|
|
* @param int $y -Y-coordinate of the top-left corner of the region to be extracted |
104
|
|
|
* @return CropEntropy |
105
|
|
|
*/ |
106
|
|
|
public function getRegion($width, $height, $x, $y) |
107
|
|
|
{ |
108
|
|
|
return new CropEntropy($this->image->getImageRegion($width, $height, $x, $y)); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|