-
Notifications
You must be signed in to change notification settings - Fork 2
/
ImageIAGCWD.class.php
213 lines (189 loc) · 6.02 KB
/
ImageIAGCWD.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php
/**
* Class ImageIAGCWD
*
* PHP implementation of an improved Adaptive Gamma Correction with Weighting Distribution for
* brightness distorted images
*
* Reference:
* - Cao, Gang & Huang, Lihui & Tian, Huawei & Huang, Xianglin & Wang, Yongbin & Zhi, Ruicong. (2017).
* Contrast Enhancement of Brightness-Distorted Images by Improved Adaptive Gamma Correction.
* CAEE. 10.1016/j.compeleceng.2017.09.012.
* - S. Huang, F. Cheng and Y. Chiu, "Efficient Contrast Enhancement Using Adaptive Gamma Correction With
* Weighting Distribution," in IEEE Transactions on Image Processing, vol. 22, no. 3, pp. 1032-1041,
* March 2013. doi: 10.1109/TIP.2012.2226047
*
* @author Jean-Michel Bruenn <[email protected]>
* @copyright 2018 <[email protected]>
* @license https://opensource.org/licenses/MIT The MIT License
* @see https://github.com/chani/AdaptiveGammaCorrection
* @see https://jeanbruenn.info/2018/11/06/adaptive-gamma-correction-for-brightness-distorted-images-with-imagick-and-php/
*/
class ImageIAGCWD extends ImageAGC
{
/**
* @var float
*/
private $adjustingParameter = 0.5;
/**
* @var float
*/
private $brightAdjustingParameter = 0.25;
/**
* @var float
*/
private $dimmedAdjustingParameter = 0.75;
/**
* @var bool
*/
private $useAGCWD = true;
/**
* @return boolean
*/
public function getUseAGCWD()
{
return $this->useAGCWD;
}
/**
* @param boolean $useAGCWD
*/
public function setUseAGCWD($useAGCWD)
{
$this->useAGCWD = $useAGCWD;
}
/**
* @return float
*/
public function getDimmedAdjustingParameter()
{
return $this->dimmedAdjustingParameter;
}
/**
* @param float $dimmedAdjustingParameter
*/
public function setDimmedAdjustingParameter($dimmedAdjustingParameter)
{
$this->dimmedAdjustingParameter = $dimmedAdjustingParameter;
}
/**
* @return float
*/
public function getBrightAdjustingParameter()
{
return $this->brightAdjustingParameter;
}
/**
* @param float $brightAdjustingParameter
*/
public function setBrightAdjustingParameter($brightAdjustingParameter)
{
$this->brightAdjustingParameter = $brightAdjustingParameter;
}
/**
* @return float
*/
public function getAdjustingParameter()
{
return $this->adjustingParameter;
}
/**
* @param float $adjustingParameter
*/
public function setAdjustingParameter($adjustingParameter)
{
$this->adjustingParameter = $adjustingParameter;
}
/**
* @return \Imagick
*/
protected function transform()
{
$pdf_l = [];
$pdf_wl = [];
$cdf_wl = [];
$mn = $this->original->getimagewidth() * $this->original->getimageheight();
$m_l = 0;
$imageIterator = $this->original->getPixelIterator();
foreach ($imageIterator as $pixels) {
/** @var $pixel \ImagickPixel * */
foreach ($pixels as $pixel) {
$c = $pixel->getcolor();
$m_l += $c['b'] / $mn;
}
$imageIterator->syncIterator();
}
// i believe 128 works better than their experimental 112
$t1 = 128;
$rt = 0.3;
$t = ($m_l - $t1) / $t1;
if ($t < ($rt * (-1))) {
$dimmed = true;
$bright = false;
$this->buildHsvWorkingSpace($this->original);
} elseif ($t > $rt) {
$dimmed = false;
$bright = true;
$negated = clone $this->original;
$negated->negateimage(false);
$this->buildHsvWorkingSpace($negated);
} else {
// return original if we shouldn't use AGCWD
if ($this->useAGCWD === false)
return $this->original;
$dimmed = false;
$bright = false;
$this->buildHsvWorkingSpace($this->original);
}
$alpha = $this->adjustingParameter;
if ($bright == true) {
$alpha = $this->brightAdjustingParameter;
} elseif ($dimmed == true) {
$alpha = $this->dimmedAdjustingParameter;
}
$hist = $this->b->getImageHistogram();
/** @var $pixel \ImagickPixel * */
foreach ($hist as $pixel) {
$color = $pixel->getColor();
$pdf_l[$color['b']] = ($pixel->getColorCount() / $mn);
}
ksort($pdf_l);
$minPDF = min($pdf_l);
$maxPDF = max($pdf_l);
$diffPDF = $maxPDF - $minPDF;
$pdf_wl_sum = 0;
foreach ($pdf_l as $intensity => $pdf) {
$pdf_wl[$intensity] = $maxPDF * pow((($pdf_l[$intensity] - $minPDF) / $diffPDF), $alpha);
$cdf_wl[$intensity] = array_sum(array_filter($pdf_wl, function ($k) use ($intensity) {
return $k <= $intensity;
}, ARRAY_FILTER_USE_KEY));
$pdf_wl_sum += $pdf_wl[$intensity];
}
end($pdf_l);
$lmax = key($pdf_l);
$r = 0.5;
$imageIterator = $this->t->getPixelIterator();
foreach ($imageIterator as $pixels) {
/** @var $pixel \ImagickPixel * */
foreach ($pixels as $pixel) {
$c = $pixel->getcolor();
$l = $c['b'];
$cdf = 1 - ($cdf_wl[$l] / $pdf_wl_sum);
if ($dimmed == true) {
$value = $lmax * pow(($l / $lmax), max($r, $cdf));
} else {
$value = $lmax * pow(($l / $lmax), $cdf);
}
$pixel->setColorValue(\Imagick::COLOR_RED, $value / 255);
$pixel->setColorValue(\Imagick::COLOR_BLUE, $value / 255);
$pixel->setColorValue(\Imagick::COLOR_GREEN, $value / 255);
}
$imageIterator->syncIterator();
}
if ($bright == true) {
$res = $this->combine();
$res->negateimage(false);
return $res;
}
return $this->combine();
}
}