bugs: Advantages for incremental library separation by analogy with incremental
[Ale.git] / scripts / ale-psf-calibrate
blobc9fedb4ceec880dfc8ceef26af9fa2a5cbcf7cf5
1 #!/usr/bin/perl -w
3 # Copyright 2003, 2004 David Hilvert <dhilvert@auricle.dyndns.org>,
4 # <dhilvert@ugcs.caltech.edu>
6 # This file is part of the Anti-Lamenessing Engine.
8 # The Anti-Lamenessing Engine is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # The Anti-Lamenessing Engine is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with Anti-Lamenessing Engine; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 # This script attempts to calibrate a filter under ALE for characteristics of a
23 # specific device. It uses the stdin IPC device to configure the filter.
25 # The initial filter tested is the box filter, and individual elements
26 # of the filter are increased or decreased to determine the effects of
27 # such changes. When the change increases the match with the desired
28 # output, it is kept. Otherwise, the change is backed out.
30 # The magnitude of change tested is determined by an increment variable,
31 # which starts at a user-defined value and is halved until it is smaller
32 # than 0.1 (an arbitrary value which could easily be changed).
34 # For every increment value, all elements are tested with positive and negative
35 # changes of this magnitude. If an element fails to offer any error
36 # improvement on a given pass, it is skipped on all future passes for the same
37 # increment value.
39 # Passes at a given increment value continue until all elements are skipped, at
40 # which point the magnitude is halved.
42 # It may be a good idea to use this script with relatively small images, as
43 # large images may require a long time to process.
46 # Remove these two lines to run the script.
48 # Note: This script has been manually updated for ALE 0.7.0 from a non-generic
49 # working copy, and hence may contain serious bugs. Bugs may be reported to
50 # ale@ventricle.dyndns.org.
53 print "Edit this script to set relevant parameters for calibration.\n";
54 exit 1;
57 # The diameter of the filter, in pixels. Set nlheight to zero to disable
58 # non-linear filtering.
61 $lheight = 3;
62 $lwidth = 3;
64 $nlheight = 0;
65 $nlwidth = 0;
68 # Filter rows and columns
71 $lrows = 3;
72 $lcols = 3;
74 $nlrows = 0;
75 $nlcols = 0;
78 # These are the input images used for calibration
80 # inputs: input images.
82 # tfile: transformation file indicating the proper transformations for the
83 # input images.
86 $inputs = "*.ppm";
87 $tfile = "b.t";
90 # Factor (>= 1) by which to scale each of the input images.
93 $scale = 5;
96 # Comparison images used for calibration.
98 # Either:
100 # (a) set $comparison to the known scene data, or
102 # (b) set $comparison_inputs for calibration frames taken with the same imaging
103 # device (at a larger scale). $comparison_tfile indicates the alignment of
104 # $comparison_inputs.
107 $comparison = "c-align.png";
108 # $comparison_inputs = "comparison/*.ppm";
109 # $comparison_tfile = "comparison/b.t";
112 # Random (but non-uniform) temporary files
115 $output1 = "/tmp/ale1-$$.png";
116 $temp_file = "/tmp/ale-$$-input";
117 $result_file = "/tmp/ale-$$-results";
118 $comparison = "/tmp/ale-$$-comparison.ppm" if defined $comparison_inputs;
121 # Initialize a box filter.
124 @lfilter = (((1.0) x $lrows) x $lcols) x 3;
125 @nlfilter = (((1.0) x $nlrows) x $nlcols) x 3;
128 # Alternatively, something like this can be used:
130 # @lfilter = (
131 # 1, 1, 1,
132 # 1, 1, 1,
133 # 3, 4, 5.3,
139 # Or:
141 # $lfilter = "1 0.5 ... 3"
143 # @lfilter = split / /, $filter;
147 # Affine colorspace parameters
150 @affine_mul = (1, 1, 1);
151 @affine_add = (0, 0, 0);
154 # When modifying the filter, we start by modifying each element by $increment.
155 # We halve the amount until we reach a value smaller than $increment_lower.
156 # $a*_multiplier scales the increment for @affine_add and @affine_mul.
159 $increment = 0.8;
160 $increment_lower = 0.01;
161 $am_multiplier = 0.01;
162 $aa_multiplier = 0.01;
165 # This array indicates the last direction tried for each index. Since we
166 # haven't tried anything yet, initialize this to +1 for all indices.
168 # +1 ==> positive change
169 # -1 ==> negative change
172 @llast_dir = (((1.0) x $lcols) x $lrows) x 3;
173 @nllast_dir = (((1.0) x $nlcols) x $nlrows) x 3;
176 # Program name to invoke ale
179 $invocation = "ale";
182 # Subroutine to obtain the match value associated with the current filter.
185 sub foo {
186 `echo $lheight > $temp_file`;
187 `echo $lwidth >> $temp_file`;
188 `echo $lrows >> $temp_file`;
189 `echo $lcols >> $temp_file`;
190 foreach $elem (@lfilter) {
191 `echo $elem >> $temp_file`;
194 if ($nlheight > 0) {
195 `echo $nlheight >> $temp_file`;
196 `echo $nlwidth >> $temp_file`;
197 `echo $nlrows >> $temp_file`;
198 `echo $nlcols >> $temp_file`;
199 foreach $elem (@nlfilter) {
200 `echo $elem >> $temp_file`;
204 if ($nlheight > 0) {
205 `$invocation --dchain triangle:2 --no-inc --mc 1 --ips 4 --lpsf stdin --nlpsf stdin --projective --perturb-upper=0 --trans-load=$comparison_tfile $comparison_inputs $comparison < $temp_file 2> /dev/null` if ($init_comparison && $comparison_inputs);
206 `$invocation --dchain triangle:2 --no-inc --mc 1 --scale=$scale --lpsf stdin --nlpsf stdin --psf-match @affine_mul @affine_add --projective --perturb-upper=0 --trans-load=$tfile $inputs $comparison $output1 < $temp_file 2> $result_file`;
207 } else {
208 `$invocation --dchain triangle:2 --no-inc --mc 1 --ips 4 --lpsf stdin --projective --perturb-upper=0 --trans-load=$comparison_tfile $comparison_inputs $comparison < $temp_file 2> /dev/null` if ($init_comparison && $comparison_inputs);
209 `$invocation --dchain triangle:2 --no-inc --mc 1 --scale=$scale --lpsf stdin --psf-match @affine_mul @affine_add --projective --perturb-upper=0 --trans-load=$tfile $inputs $comparison $output1 < $temp_file 2> $result_file`;
212 `rm -f $output1`;
213 `rm -f $temp_file`;
215 $data = `cat $result_file`;
217 `rm -f $result_file`;
219 $data =~ /::\s*(\S+)/ or die "Couldn't get match value.\n";
221 $data = $1;
223 print "Measured Error: " . $data . "\n";
225 $_ = $1;
228 $curval = foo();
230 while ($increment >= $increment_lower) {
231 $changed = 0;
233 for $i (0 .. $lrows - 1) {
234 for $j (0 .. $lcols - 1) {
235 for $k (0 .. 2) {
236 $index = $k + 3 * ($j + $lcols * $i);
238 if ($lskip[$index]) {
239 print "Skipping linear element: " . $index . "\n";
240 next;
241 } else {
242 print "Testing linear element: " . $index . "\n";
245 $dir_incr = $increment * $llast_dir[$index];
247 $lfilter[$index] += $dir_incr;
248 $newval = foo();
249 if ($newval < $curval) {
250 $changed = 1;
251 $curval = $newval;
252 if ($nlheight > 0) {
253 print "Filter: " . "$lheight $lwidth $lrows $lcols @lfilter $nlheight $nlwidth $nlrows $nlcols @nlfilter\n";
254 } else {
255 print "Filter: " . "$lheight $lwidth $lrows $lcols @lfilter\n";
257 } else {
258 $lfilter[$index] -= 2 * $dir_incr;
259 $newval = foo();
260 if ($newval < $curval) {
261 $changed = 1;
262 $curval = $newval;
263 if ($nlheight > 0) {
264 print "Filter: " . "$lheight $lwidth $lrows $lcols @lfilter $nlheight $nlwidth $nlrows $nlcols @nlfilter\n";
265 } else {
266 print "Filter: " . "$lheight $lwidth $lrows $lcols @lfilter\n";
268 $llast_dir[$index] *= -1;
269 } else {
270 $lfilter[$index] += $dir_incr;
271 $lskip[$index] = 1;
276 if ($nlheight > 0) {
277 for $i (0 .. $nlrows - 1) {
278 for $j (0 .. $nlcols - 1) {
279 for $k (0 .. 2) {
280 $index = $k + 3 * ($j + $nlcols * $i);
282 if ($nlskip[$index]) {
283 print "Skipping non-linear element: " . $index . "\n";
284 next;
285 } else {
286 print "Testing non-linear element: " . $index . "\n";
289 $dir_incr = $increment * $nllast_dir[$index];
291 $nlfilter[$index] += $dir_incr;
292 $newval = foo();
293 if ($newval < $curval) {
294 $changed = 1;
295 $curval = $newval;
296 print "Filter: " . "$lheight $lwidth $lrows $lcols @lfilter $nlheight $nlwidth $nlrows $nlcols @nlfilter\n";
297 } else {
298 $nlfilter[$index] -= 2 * $dir_incr;
299 $newval = foo();
300 if ($newval < $curval) {
301 $changed = 1;
302 $curval = $newval;
303 print "Filter: " . "$lheight $lwidth $lrows $lcols @lfilter $nlheight $nlwidth $nlrows $nlcols @nlfilter\n";
304 $nllast_dir[$index] *= -1;
305 } else {
306 $nlfilter[$index] += $dir_incr;
307 $nlskip[$index] = 1;
310 }}}}
312 if ($changed == 0) {
313 @lskip = ();
314 @nlskip = ();
315 $increment /= 2;
316 print "Increment: $increment\n";