-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalc.php
More file actions
275 lines (238 loc) · 9.89 KB
/
Copy pathcalc.php
File metadata and controls
275 lines (238 loc) · 9.89 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#!/usr/bin/php
<?php
//------------- set necessary paramters -------------------------------------
//Unix/Mac: set path to tools here
$convert='/usr/bin/convert';
//$convert='/opt/ImageMagick/bin/convert';
$exiftool='/usr/bin/exiftool';
//set font variable as needed (Mac/Win) for color scale
$font='-font /Library/Fonts/Arial\ Bold.ttf';
$escape='\\';
//color scale
$font_color='white';
$frame_color='black';
//extract embedded palette to
$embpal='palette.png';
//--------------------------------------------------------------------------
$shortopts = "";
$shortopts .= "i:"; // Required value
$shortopts .= "o:";
$shortopts .= "h";
$longopts = array(
"resize:", // Required value
"rmin:",
"rmax:",
"pal:",
"tref:",
"emis:",
"pip::", // opt. value
"clut", // No value
"scale",
"msx",
"shade",
"help",
);
$options = getopt($shortopts, $longopts);
#var_dump($options);
function help()
{
global $argv;
echo <<<EOT
usage: $argv[0] [options] -i ir_file.jpg -o outputimage
Settings:
-i ir_file.jpg flir radiometric image
-o output.jpg save 8 Bit image jpg
-o output.png save 16 Bit image png
Options Summary:
--resize val scale sensor size with "convert -resize val" (val i.e. 600x or 100%, default is 200%)
--tref temp overwrite embedded Reflected Apparent Temperature (degree Celsius)
--emis val overwrite embedded Emissivity (val i.e. 0.95)
--rmin raw_min set min RAW value instead embedded value (set scale min temp)
--rmax raw_ma set max RAW value instead embedded value (set scale max temp)
--pal iron.png use own palette (instead of embedded palette.png)
--clut disable "Color LookUp Table" and color scale (save a grayscale image)
--scale disable color scale on the right edge
--pip[=AxB] input image is a flir PiP radiometric image
overlay embedded "real image" with "ir image"
[optional] crop ir image to size AxB (i.e. --pip=90x90 )
--msx Flir MSX Mode for PiP
--shade Flir MSX Mode for PiP with amboss effect
--help print this help
# source:
# [1] http://u88.n24.queensu.ca/exiftool/forum/index.php/topic,4898.0.html
EOT;
}
if (isset($options['help']) || isset($options['h']))
{
help();
};
if (isset($options['i']))
{
$flirimg="\"".$options['i']."\"";
//if (isset($options['o']))
//{
// $destimg="\"".$options['o']."\"";
//} else {
// print 'Error: No output file specified! "-o filename"'."\n";
// exit(1);
//}
} else {
help();
exit(1);
};
if (isset($options['pal']))
{
$pal="\"".$options['pal']."\"";
} else {
$pal=$embpal;
}
if (isset($options['resize']))
{
$resize='-resize '.$options['resize'];
} else {
// default
$resize="-resize 200%";
}
//get Exif values (syntax for Unix and Windows)
eval('$exif='.shell_exec($exiftool.' -php -flir:all -q '.$flirimg));
//var_dump($exif);
if (isset($options['tref']))
{
$Temp_ref=$options['tref'];
} else {
$tmp=explode(" ",$exif[0]['ReflectedApparentTemperature']);
$Temp_ref = $tmp[0];
}
if (isset($options['emis']))
{
$Emissivity=$options['emis'];
} else {
$Emissivity=$exif[0]['Emissivity'];
}
print("\nReflected Apparent Temperature: ".$Temp_ref." degree Celsius\nEmissivity: ".$Emissivity."\n");
// save Flir values for Plancks Law for better reading in short variables
$R1=$exif[0]['PlanckR1'];
$R2=$exif[0]['PlanckR2'];
$B= $exif[0]['PlanckB'];
$O= $exif[0]['PlanckO'];
$F= $exif[0]['PlanckF'];
print('Plancks values:'."\n".' R1 '.$R1."\n".' R2 '.$R2."\n".' B '.$B."\n".' O '.$O."\n".' F '.$F."\n\n");
// get displayed temp range in RAW values
$RAWmax=$exif[0]['RawValueMedian']+$exif[0]['RawValueRange']/2;
$RAWmin=$RAWmax-$exif[0]['RawValueRange'];
printf("RAW Temp Range FLIR setting: %d %d\n",$RAWmin,$RAWmax);
//overwrite with settings
if (isset($options['rmin'])) $RAWmin=$options['rmin'];
if (isset($options['rmax'])) $RAWmax=$options['rmax'];
printf("RAW Temp Range select : %d %d\n",$RAWmin,$RAWmax);
// calc amount of radiance of reflected objects ( Emissivity < 1 )
//$t11 = $Temp_ref + 273.15;
//printf("t11 %.1f \n",$t11);
//$t12 = $B/$t11;
//printf("t12 %.1f \n",$t12);
//$t13 = exp($t12) - $F;
//printf("t13 %.1f \n",$t13);
//$t14 = ($R2 * $t13);
//printf("t14 %.1f \n", $t14);
//$t15 = $R1 / $t14;
//printf("t15 %.1f \n", $t15);
//$t16 = $t15 - $O;
//printf("t16 %.1f \n", $t16);
$RAWrefl=$R1/($R2*(exp($B/($Temp_ref+273.15))-$F))-$O;
//printf("RAW reflected: %d\n",$RAWrefl);
// get displayed object temp max/min and convert to "%.1f" for printing
$RAWmaxobj=($RAWmax-(1-$Emissivity)*$RAWrefl)/$Emissivity;
//printf("RAW maxobj : %.1f\n", $RAWmaxobj);
$RAWminobj=($RAWmin-(1-$Emissivity)*$RAWrefl)/$Emissivity;
//printf("RAW minobj : %.1f\n", $RAWminobj);
//$t21 = ($RAWminobj + $O);
//printf("t21 %.1f \n", $t21);
//$t22 = $R2 * $t21;
//printf("t22 %.1f \n", $t22);
//$t23 = $R1/($t22) + $F;
//printf("t23 %.1f \n", $t23);
//$t24 = $B/log($t23);
//printf("t24 %.1f \n", $t24);
//$t25 = $t24 - 273.15;
//printf("t25 %.1f \n", $t25);
$Temp_min=sprintf("%.1f", $B/log($R1/($R2*($RAWminobj+$O))+$F)-273.15);
$Temp_max=sprintf("%.1f", $B/log($R1/($R2*($RAWmaxobj+$O))+$F)-273.15);
printf("Temp min: %.1f\n",$Temp_min);
printf("Temp max: %.1f\n",$Temp_max);
// extract color table, swap Cb Cr and expand video pal color table from [16,235] to [0,255]
// best results: Windows -colorspace sRGB | MAC -colorspace RGB
// exec($exiftool.' '.$flirimg.' -b -Palette | '.$convert.' -size "'.$exif[0]['PaletteColors'].'X1" -depth 8 YCbCr:- -separate -swap 1,2 -set colorspace YCbCr -combine -colorspace RGB -auto-level '.$embpal);
// draw color scale
// exec($convert." -size 30x256 gradient: $pal -clut -mattecolor ".$frame_color.' -frame 5x5 -set colorspace rgb gradient.png');
// if your imagemagick have no freetype library remove the next line
// exec($convert." gradient.png -background ".$frame_color." ".$font." -fill ".$font_color." -pointsize 15 label:\"$Temp_max C\" +swap -gravity Center -append label:\"$Temp_min\" -append gradient.png");
//if ($exif[0]['RawThermalImageType'] != "TIFF")
//{
//16 bit PNG: change byte order
// $size=$exif[0]['RawThermalImageWidth']."x".$exif[0]['RawThermalImageHeight'];
// exec($exiftool." -b -RawThermalImage $flirimg | ".$convert." - gray:- | ".$convert." -depth 16 -endian msb -size ".$size." gray:- raw.png");
//}else{
// exec($exiftool." -b -RawThermalImage $flirimg | ".$convert." - raw.png");
//}
// print('RAW Temp Range from sensor : '.exec($convert.' raw.png -format "%[min] %[max]" info:')."\n");
// convert every RAW-16-Bit Pixel with Planck's Law to a Temperature Grayscale value and append temp scale
$Smax=$B/log($R1/($R2*($RAWmax+$O))+$F);
//printf("\nSmax %.1f", $Smax);
$Smin=$B/log($R1/($R2*($RAWmin+$O))+$F);
//printf("\nSmin %.1f", $Smin);
$Sdelta=$Smax-$Smin;
//printf("\nSdelta %.1f", $Sdelta);
exec($convert." raw.png -fx \"($B/ln($R1/($R2*(65535*u+$O))+$F)-$Smin)/$Sdelta\" ir.png");
if ( !isset($options['pip']) )
{
if ( !isset($options['clut']) )
{
if ( !isset($options['scale']) )
{
// with color scale
// exec($convert." ir.png ".$resize." $pal -clut -background ".$frame_color." -flatten +matte gradient.png -gravity East +append $destimg");
}else{
// exec($convert." ir.png ".$resize." $pal -clut ".$destimg);
}
}else{
// only gray picture
// exec($convert." ir.png ".$resize." ".$destimg);
}
}else{
//make PiP
//read embedded image
//exec($exiftool." -b -EmbeddedImage $flirimg | ".$convert." - -set colorspace YCbCr -colorspace RGB embedded.png");
//$geometrie=$exif[0]['OffsetX'].$exif[0]['OffsetY'];
if ( is_string($options['pip']) )
{
//$crop="-gravity Center -crop ".$options['pip']."+0+0";
}
//$resizepercent=100*$exif[0]['EmbeddedImageWidth']/$exif[0]['Real2IR']/$exif[0]['RawThermalImageWidth'];
//$resize="-resize ".$resizepercent.'%';
if ( !isset($options['msx']) && !isset($options['shade']) )
{
//exec($convert." ir.png $crop +repage ".$resize." $pal -clut embedded.png +swap -gravity Center -geometry $geometrie -compose over -composite -background ".$frame_color." -flatten +matte gradient.png -gravity East +append ".$destimg);
}else{
//$cropx=$resizepercent*$exif[0]['RawThermalImageWidth']/100;
//$cropy=$resizepercent*$exif[0]['RawThermalImageHeight']/100;
// $escape: bash/win have different brackets
if ( isset($options['msx']) )
{
// high pass to real image and crop to IR size
//exec($convert." embedded.png -gravity center -crop {$cropx}x{$cropy}{$geometrie} $escape( -clone 0 -blur 0x3 $escape) -compose mathematics -define compose:args=0,-1,+1,0.5 -composite -colorspace gray -sharpen 0x3 -level 30%,70%! embedded1.png");
}else{
// shade filter to real image and crop to IR size
// exec($convert." embedded.png -gravity center -crop {$cropx}x{$cropy}{$geometrie} -auto-level -shade 45x30 -auto-level embedded1.png");
// $gamma=exec($convert." embedded1.png -format \"%[fx:mean]\" info:");
// $gamma=log($gamma)/log(0.5);
// exec($convert." embedded1.png -gamma $gamma embedded1.png");
}
// overlay real with IR
// exec($convert." ir.png ".$resize." $pal -clut embedded1.png +swap -compose overlay -composite ir2.png");
// echo "\n";
#echo($convert." ir.png $crop +repage ".$resize." $pal -clut embedded1.png +swap -gravity Center -geometry $geometrie -compose overlay -composite ".$destimg);
// exec($convert." embedded.png ir2.png -gravity Center -geometry $geometrie -compose over -composite -background ".$frame_color." -flatten +matte gradient.png -gravity East +append ".$destimg);
}
}
// print("wrote $destimg with Temp-Range: $Temp_min / $Temp_max degree Celsius\n");
?>