| Commit [+] | Author | Date | Line | Data |
|---|---|---|---|---|
| 8a81d3be DH |
David Hilvert | 2007-04-03 18:57:00 +0000 | 1 | // Copyright 2002, 2004, 2007 David Hilvert <dhilvert@auricle.dyndns.org>, |
| 2 | // <dhilvert@ugcs.caltech.edu> | |||
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 3 | |
| 4 | /* This file is part of the Anti-Lamenessing Engine. | |||
| 5 | ||||
| 6 | The Anti-Lamenessing Engine is free software; you can redistribute it and/or modify | |||
| 7 | it under the terms of the GNU General Public License as published by | |||
| 70932f40 | David Hilvert | 2007-07-19 21:14:00 +0000 | 8 | the Free Software Foundation; either version 3 of the License, or |
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 9 | (at your option) any later version. |
| 10 | ||||
| 11 | The Anti-Lamenessing Engine is distributed in the hope that it will be useful, | |||
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
| 14 | GNU General Public License for more details. | |||
| 15 | ||||
| 16 | You should have received a copy of the GNU General Public License | |||
| 17 | along with the Anti-Lamenessing Engine; if not, write to the Free Software | |||
| 18 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |||
| 19 | */ | |||
| 20 | ||||
| 21 | /* | |||
| 22 | * align.h: Handle alignment of frames. | |||
| 23 | */ | |||
| 24 | ||||
| 25 | #ifndef __d2align_h__ | |||
| 26 | #define __d2align_h__ | |||
| 27 | ||||
| 46f9776a | dhilvert | 2005-01-07 06:44:00 +0000 | 28 | #include "filter.h" |
| 29 | #include "transformation.h" | |||
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 30 | #include "image.h" |
| 31 | #include "point.h" | |||
| 32 | #include "render.h" | |||
| 33 | #include "tfile.h" | |||
| 34 | #include "image_rw.h" | |||
| 35 | ||||
| 36 | class align { | |||
| 37 | private: | |||
| 38 | ||||
| 39 | /* | |||
| f922c1c4 DH |
David Hilvert | 2009-06-22 00:04:12 +0000 | 40 | * Alignment properties |
| 41 | */ | |||
| 42 | ||||
| 43 | static ale_align_properties align_properties() { | |||
| 44 | static ale_align_properties data = NULL; | |||
| 45 | ||||
| 46 | if (data == NULL) | |||
| 47 | data = ale_new_align_properties(); | |||
| 48 | ||||
| 49 | assert(data); | |||
| 50 | ||||
| 51 | return data; | |||
| 52 | } | |||
| 53 | ||||
| 54 | /* | |||
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 55 | * Private data members |
| 56 | */ | |||
| 57 | ||||
| 58 | static ale_pos scale_factor; | |||
| 59 | ||||
| 60 | /* | |||
| 46f9776a | dhilvert | 2005-01-07 06:44:00 +0000 | 61 | * Original frame transformation |
| 62 | */ | |||
| 63 | static transformation orig_t; | |||
| 64 | ||||
| 65 | /* | |||
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 66 | * Keep data older than latest |
| 67 | */ | |||
| 68 | static int _keep; | |||
| 69 | static transformation *kept_t; | |||
| 70 | static int *kept_ok; | |||
| 71 | ||||
| 72 | /* | |||
| 73 | * Transformation file handlers | |||
| 74 | */ | |||
| 75 | ||||
| 76 | static tload_t *tload; | |||
| 77 | static tsave_t *tsave; | |||
| 78 | ||||
| 79 | /* | |||
| 04382119 | dhilvert | 2005-04-29 09:23:00 +0000 | 80 | * Control point variables |
| 81 | */ | |||
| 82 | ||||
| 83 | static const point **cp_array; | |||
| 84 | static unsigned int cp_count; | |||
| 85 | ||||
| 86 | /* | |||
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 87 | * Reference rendering to align against |
| 88 | */ | |||
| 89 | ||||
| 90 | static render *reference; | |||
| 46f9776a | dhilvert | 2005-01-07 06:44:00 +0000 | 91 | static filter::scaled_filter *interpolant; |
| 3617b771 | David Hilvert | 2009-05-31 15:07:14 +0000 | 92 | static ale_image reference_image; |
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 93 | |
| 46f9776a | dhilvert | 2005-01-07 06:44:00 +0000 | 94 | /* |
| 2aa417e6 | dhilvert | 2005-01-07 06:44:00 +0000 | 95 | * Per-pixel alignment weight map |
| 46f9776a | dhilvert | 2005-01-07 06:44:00 +0000 | 96 | */ |
| 97 | ||||
| 2f4c0699 | David Hilvert | 2009-06-03 19:47:53 +0000 | 98 | static ale_image weight_map; |
| 46f9776a | dhilvert | 2005-01-07 06:44:00 +0000 | 99 | |
| 100 | /* | |||
| 101 | * Frequency-dependent alignment weights | |||
| 102 | */ | |||
| 103 | ||||
| 104 | static double horiz_freq_cut; | |||
| 105 | static double vert_freq_cut; | |||
| 106 | static double avg_freq_cut; | |||
| 46f9776a | dhilvert | 2005-01-07 06:44:00 +0000 | 107 | static const char *fw_output; |
| 108 | ||||
| 109 | /* | |||
| 110 | * Algorithmic alignment weighting | |||
| 111 | */ | |||
| 112 | ||||
| 113 | static const char *wmx_exec; | |||
| 114 | static const char *wmx_file; | |||
| 115 | static const char *wmx_defs; | |||
| 2aa417e6 | dhilvert | 2005-01-07 06:44:00 +0000 | 116 | |
| 117 | /* | |||
| 19b07c65 | David Hilvert | 2007-09-11 18:07:00 +0000 | 118 | * Non-certainty alignment weights |
| 2aa417e6 | dhilvert | 2005-01-07 06:44:00 +0000 | 119 | */ |
| 120 | ||||
| c2d1a70e | David Hilvert | 2009-05-30 15:37:04 +0000 | 121 | static ale_image alignment_weights; |
| 46f9776a | dhilvert | 2005-01-07 06:44:00 +0000 | 122 | |
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 123 | /* |
| 124 | * Latest transformation. | |||
| 125 | */ | |||
| 126 | ||||
| 127 | static transformation latest_t; | |||
| 128 | ||||
| 129 | /* | |||
| 130 | * Flag indicating whether the latest transformation | |||
| 131 | * resulted in a match. | |||
| 132 | */ | |||
| 133 | ||||
| 134 | static int latest_ok; | |||
| 135 | ||||
| 136 | /* | |||
| 137 | * Frame number most recently aligned. | |||
| 138 | */ | |||
| 139 | ||||
| 140 | static int latest; | |||
| 141 | ||||
| 142 | /* | |||
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 143 | * Exposure registration |
| 144 | * | |||
| 145 | * 0. Preserve the original exposure of images. | |||
| 146 | * | |||
| 147 | * 1. Match exposure between images. | |||
| 3dc20778 | dhilvert | 2005-01-10 23:06:00 +0000 | 148 | * |
| 149 | * 2. Use only image metadata for registering exposure. | |||
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 150 | */ |
| 151 | ||||
| 152 | static int _exp_register; | |||
| 153 | ||||
| 154 | /* | |||
| 155 | * Alignment class. | |||
| 156 | * | |||
| 157 | * 0. Translation only. Only adjust the x and y position of images. | |||
| 158 | * Do not rotate input images or perform projective transformations. | |||
| 159 | * | |||
| 160 | * 1. Euclidean transformations only. Adjust the x and y position | |||
| 161 | * of images and the orientation of the image about the image center. | |||
| 162 | * | |||
| 163 | * 2. Perform general projective transformations. See the file gpt.h | |||
| 164 | * for more information about general projective transformations. | |||
| 165 | */ | |||
| 166 | ||||
| 167 | static int alignment_class; | |||
| 168 | ||||
| 169 | /* | |||
| 0a432b63 | David Hilvert | 2007-03-13 08:03:00 +0000 | 170 | * Default initial alignment type. |
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 171 | * |
| 172 | * 0. Identity transformation. | |||
| 173 | * | |||
| 174 | * 1. Most recently accepted frame's final transformation. | |||
| 175 | */ | |||
| 176 | ||||
| 177 | static int default_initial_alignment_type; | |||
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 178 | |
| 179 | /* | |||
| f09b7254 | dhilvert | 2005-01-07 06:44:00 +0000 | 180 | * Projective group behavior |
| 181 | * | |||
| 182 | * 0. Perturb in output coordinates. | |||
| 183 | * | |||
| 184 | * 1. Perturb in source coordinates | |||
| 185 | */ | |||
| 186 | ||||
| 187 | static int perturb_type; | |||
| 188 | ||||
| 189 | /* | |||
| 46f9776a | dhilvert | 2005-01-07 06:44:00 +0000 | 190 | * Alignment for failed frames -- default or optimal? |
| 191 | * | |||
| 192 | * A frame that does not meet the match threshold can be assigned the | |||
| 193 | * best alignment found, or can be assigned its alignment default. | |||
| 194 | */ | |||
| 195 | ||||
| 196 | static int is_fail_default; | |||
| 197 | ||||
| 198 | /* | |||
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 199 | * Alignment code. |
| 200 | * | |||
| 201 | * 0. Align images with an error contribution from each color channel. | |||
| 202 | * | |||
| 203 | * 1. Align images with an error contribution only from the green channel. | |||
| 204 | * Other color channels do not affect alignment. | |||
| 205 | * | |||
| 206 | * 2. Align images using a summation of channels. May be useful when dealing | |||
| 207 | * with images that have high frequency color ripples due to color aliasing. | |||
| 208 | */ | |||
| 209 | ||||
| 210 | static int channel_alignment_type; | |||
| 211 | ||||
| 212 | /* | |||
| 213 | * Error metric exponent | |||
| 214 | */ | |||
| 215 | ||||
| 42772195 | David Hilvert | 2007-10-21 15:36:00 +0000 | 216 | static ale_real metric_exponent; |
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 217 | |
| 218 | /* | |||
| 219 | * Match threshold | |||
| 220 | */ | |||
| 221 | ||||
| 222 | static float match_threshold; | |||
| 223 | ||||
| 224 | /* | |||
| 225 | * Perturbation lower and upper bounds. | |||
| 226 | */ | |||
| 227 | ||||
| 228 | static ale_pos perturb_lower; | |||
| f09b7254 | dhilvert | 2005-01-07 06:44:00 +0000 | 229 | static int perturb_lower_percent; |
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 230 | static ale_pos perturb_upper; |
| f09b7254 | dhilvert | 2005-01-07 06:44:00 +0000 | 231 | static int perturb_upper_percent; |
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 232 | |
| 233 | /* | |||
| 5292ffa7 | David Hilvert | 2008-05-28 01:17:53 +0000 | 234 | * Preferred level-of-detail scale factor is 2^lod_preferred/perturb. |
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 235 | */ |
| 236 | ||||
| 5292ffa7 DH |
David Hilvert | 2008-05-28 01:17:53 +0000 | 237 | static int lod_preferred; |
| 238 | ||||
| 239 | /* | |||
| 240 | * Minimum dimension for reduced LOD. | |||
| 241 | */ | |||
| 242 | ||||
| 243 | static int min_dimension; | |||
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 244 | |
| 245 | /* | |||
| 246 | * Maximum rotational perturbation | |||
| 247 | */ | |||
| 248 | ||||
| 249 | static ale_pos rot_max; | |||
| 250 | ||||
| 251 | /* | |||
| 46f9776a | dhilvert | 2005-01-07 06:44:00 +0000 | 252 | * Barrel distortion alignment multiplier |
| 253 | */ | |||
| 254 | ||||
| 255 | static ale_pos bda_mult; | |||
| 256 | ||||
| 257 | /* | |||
| 258 | * Barrel distortion maximum adjustment rate | |||
| 259 | */ | |||
| 260 | ||||
| 261 | static ale_pos bda_rate; | |||
| 262 | ||||
| 263 | /* | |||
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 264 | * Alignment match sum |
| 265 | */ | |||
| 266 | ||||
| 267 | static ale_accum match_sum; | |||
| 268 | ||||
| 269 | /* | |||
| 270 | * Alignment match count. | |||
| 271 | */ | |||
| 272 | ||||
| 273 | static int match_count; | |||
| 274 | ||||
| 275 | /* | |||
| bddc9e4d | David Hilvert | 2007-10-01 19:50:00 +0000 | 276 | * Monte Carlo parameter |
| 1c2f7405 DH |
David Hilvert | 2007-09-20 16:58:00 +0000 | 277 | */ |
| 278 | ||||
| bddc9e4d | David Hilvert | 2007-10-01 19:50:00 +0000 | 279 | static ale_pos _mc; |
| 1c2f7405 DH |
David Hilvert | 2007-09-20 16:58:00 +0000 | 280 | |
| 281 | /* | |||
| 07271611 | dhilvert | 2005-01-07 06:48:00 +0000 | 282 | * Certainty weight flag |
| 283 | * | |||
| 284 | * 0. Don't use certainty weights for alignment. | |||
| 285 | * | |||
| 286 | * 1. Use certainty weights for alignment. | |||
| 287 | */ | |||
| 288 | ||||
| 289 | static int certainty_weights; | |||
| 290 | ||||
| 291 | /* | |||
| 2aa417e6 | dhilvert | 2005-01-07 06:44:00 +0000 | 292 | * Global search parameter |
| 293 | * | |||
| 7bcfe5db | dhilvert | 2005-01-07 06:44:00 +0000 | 294 | * 0. Local: Local search only. |
| 2aa417e6 | dhilvert | 2005-01-07 06:44:00 +0000 | 295 | * 1. Inner: Alignment reference image inner region |
| 296 | * 2. Outer: Alignment reference image outer region | |||
| 7bcfe5db | dhilvert | 2005-01-07 06:44:00 +0000 | 297 | * 3. All: Alignment reference image inner and outer regions. |
| 2aa417e6 | dhilvert | 2005-01-07 06:44:00 +0000 | 298 | * 4. Central: Inner if possible; else, best of inner and outer. |
| 04382119 | dhilvert | 2005-04-29 09:23:00 +0000 | 299 | * 5. Points: Align by control points. |
| 2aa417e6 | dhilvert | 2005-01-07 06:44:00 +0000 | 300 | */ |
| 301 | ||||
| 302 | static int _gs; | |||
| 303 | ||||
| 304 | /* | |||
| 4949c031 | dhilvert | 2005-01-07 06:44:00 +0000 | 305 | * Minimum overlap for global searches |
| 306 | */ | |||
| 307 | ||||
| a7882498 | David Hilvert | 2007-10-16 19:48:00 +0000 | 308 | static ale_accum _gs_mo; |
| 326c35b1 | David Hilvert | 2007-04-19 21:28:00 +0000 | 309 | static int gs_mo_percent; |
| 4949c031 | dhilvert | 2005-01-07 06:44:00 +0000 | 310 | |
| 311 | /* | |||
| 903df240 DH |
David Hilvert | 2008-04-24 14:36:35 +0000 | 312 | * Minimum certainty for multi-alignment element registration. |
| 313 | */ | |||
| 314 | ||||
| 315 | static ale_real _ma_cert; | |||
| 316 | ||||
| 317 | /* | |||
| 46f9776a | dhilvert | 2005-01-07 06:44:00 +0000 | 318 | * Exclusion regions |
| 319 | */ | |||
| 320 | ||||
| df55d1a2 | dhilvert | 2006-08-30 07:40:00 +0000 | 321 | static exclusion *ax_parameters; |
| 46f9776a | dhilvert | 2005-01-07 06:44:00 +0000 | 322 | static int ax_count; |
| 323 | ||||
| 324 | /* | |||
| 8bba3df9 DH |
David Hilvert | 2009-07-21 18:14:07 +0000 | 325 | * XXX: note that following in the case of non-primary elements must be |
| 326 | * split, so that Libale performs adjustments according to the | |||
| 327 | * immediate change in the parent element (likely depending on the | |||
| 328 | * alignment properties specified); for this and other reasons, it | |||
| 329 | * would be desirable to rewrite following logic more concisely, using, | |||
| 330 | * e.g., the recently-introduced ale_eval to facilitate abstraction, so | |||
| 331 | * that the split in functionality can be performed more cleanly. | |||
| 3c9742dc DH |
David Hilvert | 2009-07-21 21:12:51 +0000 | 332 | * |
| 333 | * The split in following should probably assign to ALE functionality | |||
| 334 | * like this: | |||
| 335 | * | |||
| 336 | * Working one's way up the hierarchy, | |||
| 337 | * | |||
| 338 | * * If file data is available for both prev. and current transf's, | |||
| 339 | * -> use established 'delta --follow semantics' described below | |||
| 340 | * * If file data is available for only current transf., | |||
| 341 | * -> adjust file current according to parent adjustment rel. to file. | |||
| 342 | * * else (2 cases) | |||
| 343 | * -> use parent calc. adj. by any prev. final against its parent. | |||
| 344 | * | |||
| 345 | * And assign to libale functionality like this: | |||
| 346 | * | |||
| 32f81001 DH |
David Hilvert | 2009-07-21 21:20:11 +0000 | 347 | * * correct each received start element (other than 0th) based on an |
| 348 | * estimated error from alignment-calculated change in the parent | |||
| 349 | * element. (I.e., propagate final alignment calculations to | |||
| 350 | * any children's start values.) | |||
| 8bba3df9 DH |
David Hilvert | 2009-07-21 18:14:07 +0000 | 351 | */ |
| 352 | ||||
| 353 | /* | |||
| 354 | * Alignment state | |||
| 355 | * | |||
| 356 | * This structure contains alignment state information. The change | |||
| 357 | * between the non-default old initial alignment and old final | |||
| 358 | * alignment is used to adjust the non-default current initial | |||
| 359 | * alignment. If either the old or new initial alignment is a default | |||
| 360 | * alignment, the old --follow semantics are preserved. | |||
| 361 | */ | |||
| 362 | ||||
| 363 | class astate_t { | |||
| 364 | ale_trans old_initial_alignment; | |||
| 365 | ale_trans old_final_alignment; | |||
| 366 | ale_trans default_initial_alignment; | |||
| 367 | int old_is_default; | |||
| 368 | std::vector<int> is_default; | |||
| 369 | ale_image input_frame; | |||
| 370 | ||||
| 371 | public: | |||
| 372 | astate_t() : | |||
| 373 | is_default(1) { | |||
| 374 | ||||
| 375 | old_initial_alignment = ale_new_trans(accel::context(), NULL); | |||
| 376 | old_final_alignment = ale_new_trans(accel::context(), NULL); | |||
| 377 | default_initial_alignment = ale_new_trans(accel::context(), NULL); | |||
| 378 | ||||
| 379 | input_frame = NULL; | |||
| 380 | is_default[0] = 1; | |||
| 381 | old_is_default = 1; | |||
| 382 | } | |||
| 383 | ||||
| 384 | ale_image get_input_frame() const { | |||
| 385 | return input_frame; | |||
| 386 | } | |||
| 387 | ||||
| 388 | void set_is_default(unsigned int index, int value) { | |||
| 389 | ||||
| 390 | /* | |||
| 391 | * Expand the array, if necessary. | |||
| 392 | */ | |||
| 393 | if (index == is_default.size()); | |||
| 394 | is_default.resize(index + 1); | |||
| 395 | ||||
| 396 | assert (index < is_default.size()); | |||
| 397 | is_default[index] = value; | |||
| 398 | } | |||
| 399 | ||||
| 400 | int get_is_default(unsigned int index) { | |||
| 401 | assert (index < is_default.size()); | |||
| 402 | return is_default[index]; | |||
| 403 | } | |||
| 404 | ||||
| 405 | ale_trans get_default() { | |||
| 406 | return default_initial_alignment; | |||
| 407 | } | |||
| 408 | ||||
| 409 | void set_default(ale_trans t) { | |||
| 410 | default_initial_alignment = t; | |||
| 411 | } | |||
| 412 | ||||
| 413 | void default_set_original_bounds(ale_image i) { | |||
| 414 | ale_trans_set_original_bounds(default_initial_alignment, i); | |||
| 415 | } | |||
| 416 | ||||
| 417 | void set_final(ale_trans t) { | |||
| 418 | old_final_alignment = t; | |||
| 419 | } | |||
| 420 | ||||
| 421 | void set_input_frame(ale_image i) { | |||
| 422 | input_frame = i; | |||
| 423 | } | |||
| 424 | ||||
| 425 | /* | |||
| 426 | * Implement new delta --follow semantics. | |||
| 427 | * | |||
| 428 | * If we have a transformation T such that | |||
| 429 | * | |||
| 430 | * prev_final == T(prev_init) | |||
| 431 | * | |||
| 432 | * Then we also have | |||
| 433 | * | |||
| 434 | * current_init_follow == T(current_init) | |||
| 435 | * | |||
| 436 | * We can calculate T as follows: | |||
| 437 | * | |||
| 438 | * T == prev_final(prev_init^-1) | |||
| 439 | * | |||
| 440 | * Where ^-1 is the inverse operator. | |||
| 441 | */ | |||
| 442 | static trans_single follow(trans_single a, trans_single b, trans_single c) { | |||
| 443 | trans_single cc = c; | |||
| 444 | ||||
| 445 | if (alignment_class == 0) { | |||
| 446 | /* | |||
| 447 | * Translational transformations | |||
| 448 | */ | |||
| 449 | ||||
| 450 | ale_pos t0 = -a.eu_get(0) + b.eu_get(0); | |||
| 451 | ale_pos t1 = -a.eu_get(1) + b.eu_get(1); | |||
| 452 | ||||
| 453 | cc.eu_modify(0, t0); | |||
| 454 | cc.eu_modify(1, t1); | |||
| 455 | ||||
| 456 | } else if (alignment_class == 1) { | |||
| 457 | /* | |||
| 458 | * Euclidean transformations | |||
| 459 | */ | |||
| 460 | ||||
| 461 | ale_pos t2 = -a.eu_get(2) + b.eu_get(2); | |||
| 462 | ||||
| 463 | cc.eu_modify(2, t2); | |||
| 464 | ||||
| 465 | point p( c.scaled_height()/2 + c.eu_get(0) - a.eu_get(0), | |||
| 466 | c.scaled_width()/2 + c.eu_get(1) - a.eu_get(1) ); | |||
| 467 | ||||
| 468 | p = b.transform_scaled(p); | |||
| 469 | ||||
| 470 | cc.eu_modify(0, p[0] - c.scaled_height()/2 - c.eu_get(0)); | |||
| 471 | cc.eu_modify(1, p[1] - c.scaled_width()/2 - c.eu_get(1)); | |||
| 472 | ||||
| 473 | } else if (alignment_class == 2) { | |||
| 474 | /* | |||
| 475 | * Projective transformations | |||
| 476 | */ | |||
| 477 | ||||
| 478 | point p[4]; | |||
| 479 | ||||
| 480 | p[0] = b.transform_scaled(a | |||
| 481 | . scaled_inverse_transform(c.transform_scaled(point( 0 , 0 )))); | |||
| 482 | p[1] = b.transform_scaled(a | |||
| 483 | . scaled_inverse_transform(c.transform_scaled(point(c.scaled_height(), 0 )))); | |||
| 484 | p[2] = b.transform_scaled(a | |||
| 485 | . scaled_inverse_transform(c.transform_scaled(point(c.scaled_height(), c.scaled_width())))); | |||
| 486 | p[3] = b.transform_scaled(a | |||
| 487 | . scaled_inverse_transform(c.transform_scaled(point( 0 , c.scaled_width())))); | |||
| 488 | ||||
| 489 | cc.gpt_set(p); | |||
| 490 | } | |||
| 491 | ||||
| 492 | return cc; | |||
| 493 | } | |||
| 494 | ||||
| 495 | /* | |||
| 496 | * For multi-alignment following, we use the following approach, not | |||
| 497 | * guaranteed to work with large changes in scene or perspective, but | |||
| 498 | * which should be somewhat flexible: | |||
| 499 | * | |||
| 500 | * For | |||
| 501 | * | |||
| 502 | * t[][] calculated final alignments | |||
| 503 | * s[][] alignments as loaded from file | |||
| 504 | * previous frame n | |||
| 505 | * current frame n+1 | |||
| 506 | * fundamental (primary) 0 | |||
| 507 | * non-fundamental (non-primary) m!=0 | |||
| 508 | * parent element m' | |||
| 509 | * follow(a, b, c) applying the (a, b) delta T=b(a^-1) to c | |||
| 510 | * | |||
| 511 | * following in the case of missing file data might be generated by | |||
| 512 | * | |||
| 513 | * t[n+1][0] = t[n][0] | |||
| 514 | * t[n+1][m!=0] = follow(t[n][m'], t[n+1][m'], t[n][m]) | |||
| 515 | * | |||
| 516 | * cases with all noted file data present might be generated by | |||
| 517 | * | |||
| 518 | * t[n+1][0] = follow(s[n][0], t[n][0], s[n+1][0]) | |||
| 519 | * t[n+1][m!=0] = follow(s[n+1][m'], t[n+1][m'], s[n+1][m]) | |||
| 520 | * | |||
| 521 | * For non-following behavior, or where assigning the above is | |||
| 522 | * impossible, we assign the following default | |||
| 523 | * | |||
| 524 | * t[n+1][0] = Identity | |||
| 525 | * t[n+1][m!=0] = t[n+1][m'] | |||
| 526 | */ | |||
| 527 | ||||
| 528 | void init_frame_alignment_primary(transformation *offset, int lod, ale_pos perturb) { | |||
| 529 | ||||
| 530 | if (perturb > 0 && !old_is_default && !get_is_default(0) | |||
| 531 | && default_initial_alignment_type == 1) { | |||
| 532 | ||||
| 533 | /* | |||
| 534 | * Apply following logic for the primary element. | |||
| 535 | */ | |||
| 536 | ||||
| 537 | ui::get()->following(); | |||
| 538 | ||||
| 539 | trans_single new_offset = follow(old_initial_alignment.get_element(0), | |||
| 540 | old_final_alignment.get_element(0), | |||
| 541 | offset->get_element(0)); | |||
| 542 | ||||
| 543 | old_initial_alignment = *offset; | |||
| 544 | ||||
| 545 | offset->set_element(0, new_offset); | |||
| 546 | ||||
| 547 | ui::get()->set_offset(new_offset); | |||
| 548 | } else { | |||
| 549 | old_initial_alignment = *offset; | |||
| 550 | } | |||
| 551 | ||||
| 552 | is_default.resize(old_initial_alignment.stack_depth()); | |||
| 553 | } | |||
| 554 | ||||
| 555 | void init_frame_alignment_nonprimary(transformation *offset, | |||
| 556 | int lod, ale_pos perturb, unsigned int index) { | |||
| 557 | ||||
| 558 | assert (index > 0); | |||
| 559 | ||||
| 560 | unsigned int parent_index = offset->parent_index(index); | |||
| 561 | ||||
| 562 | if (perturb > 0 | |||
| 563 | && !get_is_default(parent_index) | |||
| 564 | && !get_is_default(index) | |||
| 565 | && default_initial_alignment_type == 1) { | |||
| 566 | ||||
| 567 | /* | |||
| 568 | * Apply file-based following logic for the | |||
| 569 | * given element. | |||
| 570 | */ | |||
| 571 | ||||
| 572 | ui::get()->following(); | |||
| 573 | ||||
| 574 | trans_single new_offset = follow(old_initial_alignment.get_element(parent_index), | |||
| 575 | offset->get_element(parent_index), | |||
| 576 | offset->get_element(index)); | |||
| 577 | ||||
| 578 | old_initial_alignment.set_element(index, offset->get_element(index)); | |||
| 579 | offset->set_element(index, new_offset); | |||
| 580 | ||||
| 581 | ui::get()->set_offset(new_offset); | |||
| 582 | ||||
| 583 | return; | |||
| 584 | } | |||
| 585 | ||||
| 586 | offset->get_coordinate(parent_index); | |||
| 587 | ||||
| 588 | ||||
| 589 | if (perturb > 0 | |||
| 590 | && old_final_alignment.exists(offset->get_coordinate(parent_index)) | |||
| 591 | && old_final_alignment.exists(offset->get_current_coordinate()) | |||
| 592 | && default_initial_alignment_type == 1) { | |||
| 593 | ||||
| 594 | /* | |||
| 595 | * Apply nonfile-based following logic for | |||
| 596 | * the given element. | |||
| 597 | */ | |||
| 598 | ||||
| 599 | ui::get()->following(); | |||
| 600 | ||||
| 601 | /* | |||
| 602 | * XXX: Although it is different, the below | |||
| 603 | * should be equivalent to the comment | |||
| 604 | * description. | |||
| 605 | */ | |||
| 606 | ||||
| 607 | trans_single a = old_final_alignment.get_element(offset->get_coordinate(parent_index)); | |||
| 608 | trans_single b = old_final_alignment.get_element(offset->get_current_coordinate()); | |||
| 609 | trans_single c = offset->get_element(parent_index); | |||
| 610 | ||||
| 611 | trans_single new_offset = follow(a, b, c); | |||
| 612 | ||||
| 613 | offset->set_element(index, new_offset); | |||
| 614 | ui::get()->set_offset(new_offset); | |||
| 615 | ||||
| 616 | return; | |||
| 617 | } | |||
| 618 | ||||
| 619 | /* | |||
| 620 | * Handle other cases. | |||
| 621 | */ | |||
| 622 | ||||
| 623 | if (get_is_default(index)) { | |||
| 624 | offset->set_element(index, offset->get_element(parent_index)); | |||
| 625 | ui::get()->set_offset(offset->get_element(index)); | |||
| 626 | } | |||
| 627 | } | |||
| 628 | ||||
| 629 | void init_default() { | |||
| 630 | ||||
| 631 | if (default_initial_alignment_type == 0) { | |||
| 632 | ||||
| 633 | /* | |||
| 634 | * Follow the transformation of the original frame, | |||
| 635 | * setting new image dimensions. | |||
| 636 | */ | |||
| 637 | ||||
| 638 | // astate->default_initial_alignment = orig_t; | |||
| 639 | default_initial_alignment.set_current_element(orig_t.get_element(0)); | |||
| 640 | default_initial_alignment.set_dimensions(input_frame); | |||
| 641 | ||||
| 642 | } else if (default_initial_alignment_type == 1) | |||
| 643 | ||||
| 644 | /* | |||
| 645 | * Follow previous transformation, setting new image | |||
| 646 | * dimensions. | |||
| 647 | */ | |||
| 648 | ||||
| 649 | default_initial_alignment.set_dimensions(input_frame); | |||
| 650 | ||||
| 651 | else | |||
| 652 | assert(0); | |||
| 653 | ||||
| 654 | old_is_default = get_is_default(0); | |||
| 655 | } | |||
| 656 | }; | |||
| 657 | ||||
| 658 | ||||
| 659 | /* | |||
| 773018a3 | David Hilvert | 2007-09-07 15:14:00 +0000 | 660 | * Types for scale clusters. |
| 2aa417e6 | dhilvert | 2005-01-07 06:44:00 +0000 | 661 | */ |
| 662 | ||||
| 773018a3 DH |
David Hilvert | 2007-09-07 15:14:00 +0000 | 663 | struct nl_scale_cluster { |
| 664 | const image *accum_max; | |||
| 665 | const image *accum_min; | |||
| 580b5321 DH |
David Hilvert | 2007-09-10 17:35:00 +0000 | 666 | const image *certainty_max; |
| 667 | const image *certainty_min; | |||
| 773018a3 DH |
David Hilvert | 2007-09-07 15:14:00 +0000 | 668 | const image *aweight_max; |
| 669 | const image *aweight_min; | |||
| 670 | exclusion *ax_parameters; | |||
| 671 | ||||
| 672 | ale_pos input_scale; | |||
| e7f30dec DH |
David Hilvert | 2007-09-21 19:25:00 +0000 | 673 | const image *input_certainty_max; |
| 674 | const image *input_certainty_min; | |||
| 773018a3 DH |
David Hilvert | 2007-09-07 15:14:00 +0000 | 675 | const image *input_max; |
| 676 | const image *input_min; | |||
| 677 | }; | |||
| 678 | ||||
| 2aa417e6 | dhilvert | 2005-01-07 06:44:00 +0000 | 679 | struct scale_cluster { |
| 680 | const image *accum; | |||
| 580b5321 | David Hilvert | 2007-09-10 17:35:00 +0000 | 681 | const image *certainty; |
| 2aa417e6 | dhilvert | 2005-01-07 06:44:00 +0000 | 682 | const image *aweight; |
| df55d1a2 | dhilvert | 2006-08-30 07:40:00 +0000 | 683 | exclusion *ax_parameters; |
| 07271611 | dhilvert | 2005-01-07 06:48:00 +0000 | 684 | |
| 685 | ale_pos input_scale; | |||
| e7f30dec | David Hilvert | 2007-09-21 19:25:00 +0000 | 686 | const image *input_certainty; |
| 07271611 | dhilvert | 2005-01-07 06:48:00 +0000 | 687 | const image *input; |
| 46cc7d96 DH |
David Hilvert | 2007-09-07 17:18:00 +0000 | 688 | |
| 689 | nl_scale_cluster *nl_scale_clusters; | |||
| 2aa417e6 | dhilvert | 2005-01-07 06:44:00 +0000 | 690 | }; |
| 691 | ||||
| 692 | /* | |||
| df55d1a2 | dhilvert | 2006-08-30 07:40:00 +0000 | 693 | * Check for exclusion region coverage in the reference |
| 694 | * array. | |||
| 695 | */ | |||
| 696 | static int ref_excluded(int i, int j, point offset, exclusion *params, int param_count) { | |||
| 697 | for (int idx = 0; idx < param_count; idx++) | |||
| 698 | if (params[idx].type == exclusion::RENDER | |||
| 699 | && i + offset[0] >= params[idx].x[0] | |||
| 700 | && i + offset[0] <= params[idx].x[1] | |||
| 701 | && j + offset[1] >= params[idx].x[2] | |||
| 702 | && j + offset[1] <= params[idx].x[3]) | |||
| 703 | return 1; | |||
| 704 | ||||
| 705 | return 0; | |||
| 706 | } | |||
| 707 | ||||
| 708 | /* | |||
| 709 | * Check for exclusion region coverage in the input | |||
| 710 | * array. | |||
| 711 | */ | |||
| 712 | static int input_excluded(ale_pos ti, ale_pos tj, exclusion *params, int param_count) { | |||
| 713 | for (int idx = 0; idx < param_count; idx++) | |||
| 714 | if (params[idx].type == exclusion::FRAME | |||
| 715 | && ti >= params[idx].x[0] | |||
| 716 | && ti <= params[idx].x[1] | |||
| 717 | && tj >= params[idx].x[2] | |||
| 718 | && tj <= params[idx].x[3]) | |||
| 719 | return 1; | |||
| 720 | ||||
| 721 | return 0; | |||
| 722 | } | |||
| 723 | ||||
| 724 | /* | |||
| 4949c031 | dhilvert | 2005-01-07 06:44:00 +0000 | 725 | * Overlap function. Determines the number of pixels in areas where |
| 726 | * the arrays overlap. Uses the reference array's notion of pixel | |||
| 727 | * positions. | |||
| 728 | */ | |||
| 729 | static unsigned int overlap(struct scale_cluster c, transformation t, int ax_count) { | |||
| 730 | assert (reference_image); | |||
| 731 | ||||
| 732 | unsigned int result = 0; | |||
| 733 | ||||
| 734 | point offset = c.accum->offset(); | |||
| 735 | ||||
| 736 | for (unsigned int i = 0; i < c.accum->height(); i++) | |||
| 737 | for (unsigned int j = 0; j < c.accum->width(); j++) { | |||
| 738 | ||||
| df55d1a2 | dhilvert | 2006-08-30 07:40:00 +0000 | 739 | if (ref_excluded(i, j, offset, c.ax_parameters, ax_count)) |
| 4949c031 | dhilvert | 2005-01-07 06:44:00 +0000 | 740 | continue; |
| 741 | ||||
| 742 | /* | |||
| 743 | * Transform | |||
| 744 | */ | |||
| 745 | ||||
| 746 | struct point q; | |||
| 747 | ||||
| 07271611 | dhilvert | 2005-01-07 06:48:00 +0000 | 748 | q = (c.input_scale < 1.0 && interpolant == NULL) |
| 749 | ? t.scaled_inverse_transform( | |||
| 750 | point(i + offset[0], j + offset[1])) | |||
| 751 | : t.unscaled_inverse_transform( | |||
| 4949c031 | dhilvert | 2005-01-07 06:44:00 +0000 | 752 | point(i + offset[0], j + offset[1])); |
| 753 | ||||
| 754 | ale_pos ti = q[0]; | |||
| 755 | ale_pos tj = q[1]; | |||
| 756 | ||||
| 757 | /* | |||
| 758 | * Check that the transformed coordinates are within | |||
| 759 | * the boundaries of array c.input, and check that the | |||
| 760 | * weight value in the accumulated array is nonzero, | |||
| 761 | * unless we know it is nonzero by virtue of the fact | |||
| 762 | * that it falls within the region of the original | |||
| 763 | * frame (e.g. when we're not increasing image | |||
| df55d1a2 | dhilvert | 2006-08-30 07:40:00 +0000 | 764 | * extents). Also check for frame exclusion. |
| 4949c031 | dhilvert | 2005-01-07 06:44:00 +0000 | 765 | */ |
| 766 | ||||
| df55d1a2 | dhilvert | 2006-08-30 07:40:00 +0000 | 767 | if (input_excluded(ti, tj, c.ax_parameters, ax_count)) |
| 768 | continue; | |||
| 769 | ||||
| 4949c031 | dhilvert | 2005-01-07 06:44:00 +0000 | 770 | if (ti >= 0 |
| 771 | && ti <= c.input->height() - 1 | |||
| 772 | && tj >= 0 | |||
| 773 | && tj <= c.input->width() - 1 | |||
| 580b5321 | David Hilvert | 2007-09-10 17:35:00 +0000 | 774 | && c.certainty->get_pixel(i, j)[0] != 0) |
| 4949c031 | dhilvert | 2005-01-07 06:44:00 +0000 | 775 | result++; |
| 776 | } | |||
| 777 | ||||
| 778 | return result; | |||
| 779 | } | |||
| 780 | ||||
| 781 | /* | |||
| 699711e2 DH |
David Hilvert | 2007-09-20 21:03:00 +0000 | 782 | * Monte carlo iteration class. |
| 783 | * | |||
| 784 | * Monte Carlo alignment has been used for statistical comparisons in | |||
| 785 | * spatial registration, and is now used for tonal registration | |||
| 786 | * and final match calculation. | |||
| 787 | */ | |||
| 788 | ||||
| 789 | /* | |||
| 790 | * We use a random process for which the expected number of sampled | |||
| 791 | * pixels is +/- .000003 from the coverage in the range [.005,.995] for | |||
| 792 | * an image with 100,000 pixels. (The actual number may still deviate | |||
| 793 | * from the expected number by more than this amount, however.) The | |||
| 794 | * method is as follows: | |||
| 795 | * | |||
| 796 | * We have coverage == USE/ALL, or (expected # pixels to use)/(# total | |||
| 797 | * pixels). We derive from this SKIP/USE. | |||
| 798 | * | |||
| 799 | * SKIP/USE == (SKIP/ALL)/(USE/ALL) == (1 - (USE/ALL))/(USE/ALL) | |||
| 800 | * | |||
| 801 | * Once we have SKIP/USE, we know the expected number of pixels to skip | |||
| 802 | * in each iteration. We use a random selection process that provides | |||
| 803 | * SKIP/USE close to this calculated value. | |||
| 804 | * | |||
| 805 | * If we can draw uniformly to select the number of pixels to skip, we | |||
| 806 | * do. In this case, the maximum number of pixels to skip is twice the | |||
| 807 | * expected number. | |||
| 808 | * | |||
| 809 | * If we cannot draw uniformly, we still assign equal probability to | |||
| 810 | * each of the integer values in the interval [0, 2 * (SKIP/USE)], but | |||
| 811 | * assign an unequal amount to the integer value ceil(2 * SKIP/USE) + | |||
| 812 | * 1. | |||
| 813 | */ | |||
| 814 | ||||
| 815 | /* | |||
| 816 | * When reseeding the random number generator, we want the same set of | |||
| 817 | * pixels to be used in cases where two alignment options are compared. | |||
| 818 | * If we wanted to avoid bias from repeatedly utilizing the same seed, | |||
| 819 | * we could seed with the number of the frame most recently aligned: | |||
| 820 | * | |||
| 821 | * srand(latest); | |||
| 822 | * | |||
| 823 | * However, in cursory tests, it seems okay to just use the default | |||
| 824 | * seed of 1, and so we do this, since it is simpler; both of these | |||
| 825 | * approaches to reseeding achieve better results than not reseeding. | |||
| 826 | * (1 is the default seed according to the GNU Manual Page for | |||
| 827 | * rand(3).) | |||
| 828 | * | |||
| 829 | * For subdomain calculations, we vary the seed by adding the subdomain | |||
| 830 | * index. | |||
| 831 | */ | |||
| 832 | ||||
| 833 | class mc_iterate { | |||
| 834 | ale_pos mc_max; | |||
| 835 | unsigned int index; | |||
| 836 | unsigned int index_max; | |||
| 837 | int i_min; | |||
| 838 | int i_max; | |||
| 839 | int j_min; | |||
| 840 | int j_max; | |||
| 841 | ||||
| 842 | rng_t rng; | |||
| 843 | ||||
| 63f46ff7 | David Hilvert | 2007-09-21 00:03:00 +0000 | 844 | public: |
| f0af1fea DH |
David Hilvert | 2007-09-20 23:22:00 +0000 | 845 | mc_iterate(int _i_min, int _i_max, int _j_min, int _j_max, unsigned int subdomain) |
| 846 | : rng() { | |||
| 699711e2 DH |
David Hilvert | 2007-09-20 21:03:00 +0000 | 847 | |
| 848 | ale_pos coverage; | |||
| 849 | ||||
| 850 | i_min = _i_min; | |||
| 851 | i_max = _i_max; | |||
| 852 | j_min = _j_min; | |||
| 853 | j_max = _j_max; | |||
| 854 | ||||
| a0c28c9a DH |
David Hilvert | 2008-03-23 15:55:54 -0500 | 855 | if (i_max < i_min || j_max < j_min) |
| 856 | index_max = 0; | |||
| 857 | else | |||
| 858 | index_max = (i_max - i_min) * (j_max - j_min); | |||
| 699711e2 | David Hilvert | 2007-09-20 21:03:00 +0000 | 859 | |
| bddc9e4d | David Hilvert | 2007-10-01 19:50:00 +0000 | 860 | if (index_max < 500 || _mc > 100 || _mc <= 0) |
| 699711e2 DH |
David Hilvert | 2007-09-20 21:03:00 +0000 | 861 | coverage = 1; |
| 862 | else | |||
| bddc9e4d | David Hilvert | 2007-10-01 19:50:00 +0000 | 863 | coverage = _mc / 100; |
| 699711e2 | David Hilvert | 2007-09-20 21:03:00 +0000 | 864 | |
| a85f57f9 | David Hilvert | 2007-10-15 17:07:00 +0000 | 865 | double su = (1 - coverage) / coverage; |
| 699711e2 DH |
David Hilvert | 2007-09-20 21:03:00 +0000 | 866 | |
| 867 | mc_max = (floor(2*su) * (1 + floor(2*su)) + 2*su) | |||
| 868 | / (2 + 2 * floor(2*su) - 2*su); | |||
| 869 | ||||
| 870 | rng.seed(1 + subdomain); | |||
| 871 | ||||
| 793fc1a6 DH |
David Hilvert | 2007-10-25 16:36:00 +0000 | 872 | #define FIXED16 3 |
| 873 | #if ALE_COORDINATES == FIXED16 | |||
| 874 | /* | |||
| 875 | * XXX: This calculation might not yield the correct | |||
| 876 | * expected value. | |||
| 877 | */ | |||
| e4a3555e DH |
David Hilvert | 2007-10-24 22:37:00 +0000 | 878 | index = -1 + (int) ceil(((ale_pos) mc_max+1) |
| 879 | / (ale_pos) ( (1 + 0xffffff) | |||
| 880 | / (1 + (rng.get() & 0xffffff)))); | |||
| 793fc1a6 | David Hilvert | 2007-10-25 16:36:00 +0000 | 881 | #else |
| ff364936 | David Hilvert | 2007-10-26 23:35:00 +0000 | 882 | index = -1 + (int) ceil((ale_accum) (mc_max+1) |
| 793fc1a6 DH |
David Hilvert | 2007-10-25 16:36:00 +0000 | 883 | * ( (1 + ((ale_accum) (rng.get())) ) |
| 884 | / (1 + ((ale_accum) RAND_MAX)) )); | |||
| 885 | #endif | |||
| 886 | #undef FIXED16 | |||
| 699711e2 DH |
David Hilvert | 2007-09-20 21:03:00 +0000 | 887 | } |
| 888 | ||||
| 889 | int get_i() { | |||
| 890 | return index / (j_max - j_min) + i_min; | |||
| 891 | } | |||
| 892 | ||||
| 893 | int get_j() { | |||
| 894 | return index % (j_max - j_min) + j_min; | |||
| 895 | } | |||
| 896 | ||||
| 63f46ff7 | David Hilvert | 2007-09-21 00:03:00 +0000 | 897 | void operator++(int whats_this_for) { |
| 793fc1a6 DH |
David Hilvert | 2007-10-25 16:36:00 +0000 | 898 | #define FIXED16 3 |
| 899 | #if ALE_COORDINATES == FIXED16 | |||
| e4a3555e DH |
David Hilvert | 2007-10-24 22:37:00 +0000 | 900 | index += (int) ceil(((ale_pos) mc_max+1) |
| 901 | / (ale_pos) ( (1 + 0xffffff) | |||
| 902 | / (1 + (rng.get() & 0xffffff)))); | |||
| 793fc1a6 | David Hilvert | 2007-10-25 16:36:00 +0000 | 903 | #else |
| ff364936 | David Hilvert | 2007-10-26 23:35:00 +0000 | 904 | index += (int) ceil((ale_accum) (mc_max+1) |
| 793fc1a6 DH |
David Hilvert | 2007-10-25 16:36:00 +0000 | 905 | * ( (1 + ((ale_accum) (rng.get())) ) |
| 906 | / (1 + ((ale_accum) RAND_MAX)) )); | |||
| 907 | #endif | |||
| 908 | #undef FIXED16 | |||
| 699711e2 DH |
David Hilvert | 2007-09-20 21:03:00 +0000 | 909 | } |
| 910 | ||||
| 911 | int done() { | |||
| 912 | return (index >= index_max); | |||
| 913 | } | |||
| 914 | }; | |||
| 915 | ||||
| 916 | /* | |||
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 917 | * Not-quite-symmetric difference function. Determines the difference in areas |
| 4949c031 | dhilvert | 2005-01-07 06:44:00 +0000 | 918 | * where the arrays overlap. Uses the reference array's notion of pixel positions. |
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 919 | * |
| 920 | * For the purposes of determining the difference, this function divides each | |||
| 921 | * pixel value by the corresponding image's average pixel magnitude, unless we | |||
| 922 | * are: | |||
| 923 | * | |||
| 924 | * a) Extending the boundaries of the image, or | |||
| 925 | * | |||
| 926 | * b) following the previous frame's transform | |||
| 927 | * | |||
| 928 | * If we are doing monte-carlo pixel sampling for alignment, we | |||
| 929 | * typically sample a subset of available pixels; otherwise, we sample | |||
| 930 | * all pixels. | |||
| 931 | * | |||
| 932 | */ | |||
| 4707675e | dhilvert | 2006-10-19 10:24:00 +0000 | 933 | |
| f8864302 DH |
David Hilvert | 2008-04-11 18:15:57 +0000 | 934 | template<class diff_trans> |
| 935 | class diff_stat_generic { | |||
| dd7602d7 | David Hilvert | 2008-03-06 18:41:00 +0000 | 936 | |
| dd7602d7 | David Hilvert | 2008-03-06 18:41:00 +0000 | 937 | transformation::elem_bounds_t elem_bounds; |
| e492922d | David Hilvert | 2007-05-09 05:53:00 +0000 | 938 | |
| 1732c1c4 DH |
David Hilvert | 2007-04-30 02:42:00 +0000 | 939 | struct run { |
| 940 | ||||
| dd7602d7 | David Hilvert | 2008-03-06 18:41:00 +0000 | 941 | diff_trans offset; |
| 1732c1c4 DH |
David Hilvert | 2007-04-30 02:42:00 +0000 | 942 | |
| 943 | ale_accum result; | |||
| 944 | ale_accum divisor; | |||
| 945 | ||||
| 946 | point max, min; | |||
| 947 | ale_accum centroid[2], centroid_divisor; | |||
| 948 | ale_accum de_centroid[2], de_centroid_v, de_sum; | |||
| 949 | ||||
| 5d53e401 | David Hilvert | 2007-05-02 03:12:00 +0000 | 950 | void init() { |
| 1732c1c4 DH |
David Hilvert | 2007-04-30 02:42:00 +0000 | 951 | |
| 952 | result = 0; | |||
| 953 | divisor = 0; | |||
| e492922d | David Hilvert | 2007-05-09 05:53:00 +0000 | 954 | |
| 1732c1c4 DH |
David Hilvert | 2007-04-30 02:42:00 +0000 | 955 | min = point::posinf(); |
| 956 | max = point::neginf(); | |||
| 957 | ||||
| 958 | centroid[0] = 0; | |||
| 959 | centroid[1] = 0; | |||
| 960 | centroid_divisor = 0; | |||
| 961 | ||||
| 962 | de_centroid[0] = 0; | |||
| 963 | de_centroid[1] = 0; | |||
| 964 | ||||
| 965 | de_centroid_v = 0; | |||
| 966 | ||||
| 967 | de_sum = 0; | |||
| 968 | } | |||
| 969 | ||||
| 1b980378 | David Hilvert | 2008-07-18 18:30:40 +0000 | 970 | void init(diff_trans _offset) { |
| 5d53e401 DH |
David Hilvert | 2007-05-02 03:12:00 +0000 | 971 | offset = _offset; |
| 972 | init(); | |||
| 973 | } | |||
| 974 | ||||
| 975 | /* | |||
| 976 | * Required for STL sanity. | |||
| 977 | */ | |||
| dd7602d7 | David Hilvert | 2008-03-06 18:41:00 +0000 | 978 | run() : offset(diff_trans::eu_identity()) { |
| 5d53e401 DH |
David Hilvert | 2007-05-02 03:12:00 +0000 | 979 | init(); |
| 980 | } | |||
| 981 | ||||
| 1b980378 DH |
David Hilvert | 2008-07-18 18:30:40 +0000 | 982 | run(diff_trans _offset) : offset(_offset) { |
| 983 | init(_offset); | |||
| e492922d DH |
David Hilvert | 2007-05-09 05:53:00 +0000 | 984 | } |
| 985 | ||||
| 1732c1c4 DH |
David Hilvert | 2007-04-30 02:42:00 +0000 | 986 | void add(const run &_run) { |
| 987 | result += _run.result; | |||
| 988 | divisor += _run.divisor; | |||
| 989 | ||||
| 1732c1c4 DH |
David Hilvert | 2007-04-30 02:42:00 +0000 | 990 | for (int d = 0; d < 2; d++) { |
| 991 | if (min[d] > _run.min[d]) | |||
| 992 | min[d] = _run.min[d]; | |||
| 993 | if (max[d] < _run.max[d]) | |||
| 994 | max[d] = _run.max[d]; | |||
| 995 | centroid[d] += _run.centroid[d]; | |||
| 996 | de_centroid[d] += _run.de_centroid[d]; | |||
| 997 | } | |||
| 998 | ||||
| 999 | centroid_divisor += _run.centroid_divisor; | |||
| 1000 | de_centroid_v += _run.de_centroid_v; | |||
| 1001 | de_sum += _run.de_sum; | |||
| 1002 | } | |||
| 1003 | ||||
| 283c3ecc | David Hilvert | 2008-01-26 01:36:00 +0000 | 1004 | run(const run &_run) : offset(_run.offset) { |
| 1732c1c4 DH |
David Hilvert | 2007-04-30 02:42:00 +0000 | 1005 | |
| 1006 | /* | |||
| 1007 | * Initialize | |||
| 1008 | */ | |||
| 1b980378 | David Hilvert | 2008-07-18 18:30:40 +0000 | 1009 | init(_run.offset); |
| 1732c1c4 DH |
David Hilvert | 2007-04-30 02:42:00 +0000 | 1010 | |
| 1011 | /* | |||
| 1012 | * Add | |||
| 1013 | */ | |||
| 1014 | add(_run); | |||
| 1015 | } | |||
| 1016 | ||||
| 1017 | run &operator=(const run &_run) { | |||
| 1018 | ||||
| 1019 | /* | |||
| 1732c1c4 DH |
David Hilvert | 2007-04-30 02:42:00 +0000 | 1020 | * Initialize |
| 1021 | */ | |||
| 1b980378 | David Hilvert | 2008-07-18 18:30:40 +0000 | 1022 | init(_run.offset); |
| 1732c1c4 DH |
David Hilvert | 2007-04-30 02:42:00 +0000 | 1023 | |
| 1024 | /* | |||
| 1025 | * Add | |||
| 1026 | */ | |||
| 1027 | add(_run); | |||
| 1028 | ||||
| 1029 | return *this; | |||
| 1030 | } | |||
| 1031 | ||||
| 1032 | ~run() { | |||
| 1732c1c4 DH |
David Hilvert | 2007-04-30 02:42:00 +0000 | 1033 | } |
| 1034 | ||||
| 1035 | ale_accum get_error() const { | |||
| c9968081 | David Hilvert | 2007-10-24 02:54:00 +0000 | 1036 | return pow(result / divisor, 1/(ale_accum) metric_exponent); |
| 1732c1c4 DH |
David Hilvert | 2007-04-30 02:42:00 +0000 | 1037 | } |
| 1038 | ||||
| 368d214e DH |
David Hilvert | 2007-05-09 07:57:00 +0000 | 1039 | void sample(int f, scale_cluster c, int i, int j, point t, point u, |
| 1040 | const run &comparison) { | |||
| 1732c1c4 DH |
David Hilvert | 2007-04-30 02:42:00 +0000 | 1041 | |
| 1042 | pixel pa = c.accum->get_pixel(i, j); | |||
| 1043 | ||||
| 42772195 DH |
David Hilvert | 2007-10-21 15:36:00 +0000 | 1044 | ale_real this_result[2] = { 0, 0 }; |
| 1045 | ale_real this_divisor[2] = { 0, 0 }; | |||
| 1732c1c4 | David Hilvert | 2007-04-30 02:42:00 +0000 | 1046 | |
| f064b1a9 DH |
David Hilvert | 2007-09-21 21:21:00 +0000 | 1047 | pixel p[2]; |
| 1048 | pixel weight[2]; | |||
| 1049 | weight[0] = pixel(1, 1, 1); | |||
| 1050 | weight[1] = pixel(1, 1, 1); | |||
| 1051 | ||||
| ca7acd56 | David Hilvert | 2008-06-05 23:43:51 +0000 | 1052 | pixel tm = offset.get_tonal_multiplier(point(i, j) + c.accum->offset()); |
| 28e6b6f7 | David Hilvert | 2008-06-05 02:36:34 +0000 | 1053 | |
| 1e559234 | David Hilvert | 2007-09-11 19:34:00 +0000 | 1054 | if (interpolant != NULL) { |
| 4132897c DH |
David Hilvert | 2007-10-26 18:13:00 +0000 | 1055 | interpolant->filtered(i, j, &p[0], &weight[0], 1, f); |
| 1056 | ||||
| fa3844c7 | David Hilvert | 2007-10-26 18:39:00 +0000 | 1057 | if (weight[0].min_norm() > ale_real_weight_floor) { |
| 4132897c DH |
David Hilvert | 2007-10-26 18:13:00 +0000 | 1058 | p[0] /= weight[0]; |
| 1059 | } else { | |||
| 1060 | return; | |||
| 1061 | } | |||
| 1062 | ||||
| 1e559234 | David Hilvert | 2007-09-11 19:34:00 +0000 | 1063 | } else { |
| e7f30dec | David Hilvert | 2007-09-21 19:25:00 +0000 | 1064 | p[0] = c.input->get_bl(t); |
| 1732c1c4 DH |
David Hilvert | 2007-04-30 02:42:00 +0000 | 1065 | } |
| 1066 | ||||
| 28e6b6f7 | David Hilvert | 2008-06-05 02:36:34 +0000 | 1067 | p[0] *= tm; |
| 64e05da1 | David Hilvert | 2008-05-05 16:21:18 -0500 | 1068 | |
| 1732c1c4 | David Hilvert | 2007-04-30 02:42:00 +0000 | 1069 | if (u.defined()) { |
| 19b07c65 | David Hilvert | 2007-09-11 18:07:00 +0000 | 1070 | p[1] = c.input->get_bl(u); |
| 28e6b6f7 | David Hilvert | 2008-06-05 02:36:34 +0000 | 1071 | p[1] *= tm; |
| 1732c1c4 DH |
David Hilvert | 2007-04-30 02:42:00 +0000 | 1072 | } |
| 1073 | ||||
| 1074 | ||||
| 1075 | /* | |||
| 1076 | * Handle certainty. | |||
| 1077 | */ | |||
| 1078 | ||||
| f064b1a9 | David Hilvert | 2007-09-21 21:21:00 +0000 | 1079 | if (certainty_weights == 1) { |
| 32ec9768 DH |
David Hilvert | 2007-09-21 23:14:00 +0000 | 1080 | |
| 1081 | /* | |||
| 1082 | * For speed, use arithmetic interpolation (get_bl(.)) | |||
| 1083 | * instead of geometric (get_bl(., 1)) | |||
| 1084 | */ | |||
| 1085 | ||||
| 1086 | weight[0] *= c.input_certainty->get_bl(t); | |||
| c4fb894c | David Hilvert | 2007-09-21 22:44:00 +0000 | 1087 | if (u.defined()) |
| 32ec9768 | David Hilvert | 2007-09-21 23:14:00 +0000 | 1088 | weight[1] *= c.input_certainty->get_bl(u); |
| e7f30dec DH |
David Hilvert | 2007-09-21 19:25:00 +0000 | 1089 | weight[0] *= c.certainty->get_pixel(i, j); |
| 1090 | weight[1] *= c.certainty->get_pixel(i, j); | |||
| 1732c1c4 DH |
David Hilvert | 2007-04-30 02:42:00 +0000 | 1091 | } |
| 1092 | ||||
| 1093 | if (c.aweight != NULL) { | |||
| 1094 | weight[0] *= c.aweight->get_pixel(i, j); | |||
| 1095 | weight[1] *= c.aweight->get_pixel(i, j); | |||
| 1096 | } | |||
| 1097 | ||||
| 1098 | /* | |||
| 1099 | * Update sampling area statistics | |||
| 1100 | */ | |||
| 1101 | ||||
| 1102 | if (min[0] > i) | |||
| 1103 | min[0] = i; | |||
| 1104 | if (min[1] > j) | |||
| 1105 | min[1] = j; | |||
| 1106 | if (max[0] < i) | |||
| 1107 | max[0] = i; | |||
| 1108 | if (max[1] < j) | |||
| 1109 | max[1] = j; | |||
| 1110 | ||||
| 1111 | centroid[0] += (weight[0][0] + weight[0][1] + weight[0][2]) * i; | |||
| 1112 | centroid[1] += (weight[0][0] + weight[0][1] + weight[0][2]) * j; | |||
| 1113 | centroid_divisor += (weight[0][0] + weight[0][1] + weight[0][2]); | |||
| 1114 | ||||
| 1115 | /* | |||
| 1116 | * Determine alignment type. | |||
| 1117 | */ | |||
| 1118 | ||||
| 1119 | for (int m = 0; m < (u.defined() ? 2 : 1); m++) | |||
| 1120 | if (channel_alignment_type == 0) { | |||
| 1121 | /* | |||
| 1122 | * Align based on all channels. | |||
| 1123 | */ | |||
| 1124 | ||||
| 1125 | ||||
| 1126 | for (int k = 0; k < 3; k++) { | |||
| 1127 | ale_real achan = pa[k]; | |||
| 1128 | ale_real bchan = p[m][k]; | |||
| 1129 | ||||
| 1130 | this_result[m] += weight[m][k] * pow(fabs(achan - bchan), metric_exponent); | |||
| 1131 | this_divisor[m] += weight[m][k] * pow(achan > bchan ? achan : bchan, metric_exponent); | |||
| 1132 | } | |||
| 1133 | } else if (channel_alignment_type == 1) { | |||
| 1134 | /* | |||
| 1135 | * Align based on the green channel. | |||
| 1136 | */ | |||
| 1137 | ||||
| 1138 | ale_real achan = pa[1]; | |||
| 1139 | ale_real bchan = p[m][1]; | |||
| 1140 | ||||
| 1141 | this_result[m] = weight[m][1] * pow(fabs(achan - bchan), metric_exponent); | |||
| 1142 | this_divisor[m] = weight[m][1] * pow(achan > bchan ? achan : bchan, metric_exponent); | |||
| 1143 | } else if (channel_alignment_type == 2) { | |||
| 1144 | /* | |||
| 1145 | * Align based on the sum of all channels. | |||
| 1146 | */ | |||
| 1147 | ||||
| 1148 | ale_real asum = 0; | |||
| 1149 | ale_real bsum = 0; | |||
| 1150 | ale_real wsum = 0; | |||
| 1151 | ||||
| 1152 | for (int k = 0; k < 3; k++) { | |||
| 1153 | asum += pa[k]; | |||
| 1154 | bsum += p[m][k]; | |||
| 1155 | wsum += weight[m][k] / 3; | |||
| 1156 | } | |||
| 1157 | ||||
| 1158 | this_result[m] = wsum * pow(fabs(asum - bsum), metric_exponent); | |||
| 1159 | this_divisor[m] = wsum * pow(asum > bsum ? asum : bsum, metric_exponent); | |||
| 1160 | } | |||
| 1161 | ||||
| 1162 | if (u.defined()) { | |||
| 42772195 DH |
David Hilvert | 2007-10-21 15:36:00 +0000 | 1163 | // ale_real de = fabs(this_result[0] / this_divisor[0] |
| 1164 | // - this_result[1] / this_divisor[1]); | |||
| 1165 | ale_real de = fabs(this_result[0] - this_result[1]); | |||
| 1732c1c4 | David Hilvert | 2007-04-30 02:42:00 +0000 | 1166 | |
| 42772195 DH |
David Hilvert | 2007-10-21 15:36:00 +0000 | 1167 | de_centroid[0] += de * (ale_real) i; |
| 1168 | de_centroid[1] += de * (ale_real) j; | |||
| 1732c1c4 | David Hilvert | 2007-04-30 02:42:00 +0000 | 1169 | |
| 42772195 | David Hilvert | 2007-10-21 15:36:00 +0000 | 1170 | de_centroid_v += de * (ale_real) t.lengthto(u); |
| 1732c1c4 DH |
David Hilvert | 2007-04-30 02:42:00 +0000 | 1171 | |
| 1172 | de_sum += de; | |||
| 1173 | } | |||
| 1174 | ||||
| 1175 | result += (this_result[0]); | |||
| 1176 | divisor += (this_divisor[0]); | |||
| 1732c1c4 DH |
David Hilvert | 2007-04-30 02:42:00 +0000 | 1177 | } |
| 1178 | ||||
| 1179 | void rescale(ale_pos scale) { | |||
| 1180 | offset.rescale(scale); | |||
| 1181 | ||||
| 1182 | de_centroid[0] *= scale; | |||
| 1183 | de_centroid[1] *= scale; | |||
| 1184 | de_centroid_v *= scale; | |||
| 1185 | } | |||
| 1186 | ||||
| 1187 | point get_centroid() { | |||
| 1188 | point result = point(centroid[0] / centroid_divisor, centroid[1] / centroid_divisor); | |||
| 1189 | ||||
| 1190 | assert (finite(centroid[0]) | |||
| 1191 | && finite(centroid[1]) | |||
| 1192 | && (result.defined() || centroid_divisor == 0)); | |||
| 1193 | ||||
| 1194 | return result; | |||
| 1195 | } | |||
| 1196 | ||||
| 1197 | point get_error_centroid() { | |||
| 1198 | point result = point(de_centroid[0] / de_sum, de_centroid[1] / de_sum); | |||
| 1732c1c4 DH |
David Hilvert | 2007-04-30 02:42:00 +0000 | 1199 | return result; |
| 1200 | } | |||
| 1201 | ||||
| 1202 | ||||
| 1203 | ale_pos get_error_perturb() { | |||
| 1204 | ale_pos result = de_centroid_v / de_sum; | |||
| 1205 | ||||
| 1206 | return result; | |||
| 1207 | } | |||
| 1208 | ||||
| 1209 | }; | |||
| 1210 | ||||
| 5d53e401 DH |
David Hilvert | 2007-05-02 03:12:00 +0000 | 1211 | /* |
| 1212 | * When non-empty, runs.front() is best, runs.back() is | |||
| 1213 | * testing. | |||
| 1214 | */ | |||
| 1215 | ||||
| 1732c1c4 DH |
David Hilvert | 2007-04-30 02:42:00 +0000 | 1216 | std::vector<run> runs; |
| 1217 | ||||
| 5d53e401 DH |
David Hilvert | 2007-05-02 03:12:00 +0000 | 1218 | /* |
| 1219 | * old_runs stores the latest available perturbation set for | |||
| 1220 | * each multi-alignment element. | |||
| 1221 | */ | |||
| 1222 | ||||
| dd7602d7 | David Hilvert | 2008-03-06 18:41:00 +0000 | 1223 | typedef int run_index; |
| 30ea890d | David Hilvert | 2007-05-14 20:54:00 +0000 | 1224 | std::map<run_index, run> old_runs; |
| 5d53e401 | David Hilvert | 2007-05-02 03:12:00 +0000 | 1225 | |
| eb01b1b8 DH |
David Hilvert | 2007-04-24 05:36:00 +0000 | 1226 | static void *diff_subdomain(void *args); |
| 1227 | ||||
| 1228 | struct subdomain_args { | |||
| 1229 | struct scale_cluster c; | |||
| 1732c1c4 | David Hilvert | 2007-04-30 02:42:00 +0000 | 1230 | std::vector<run> runs; |
| eb01b1b8 DH |
David Hilvert | 2007-04-24 05:36:00 +0000 | 1231 | int ax_count; |
| 1232 | int f; | |||
| eb01b1b8 DH |
David Hilvert | 2007-04-24 05:36:00 +0000 | 1233 | int i_min, i_max, j_min, j_max; |
| 1234 | int subdomain; | |||
| 1235 | }; | |||
| 1236 | ||||
| eb01b1b8 DH |
David Hilvert | 2007-04-24 05:36:00 +0000 | 1237 | struct scale_cluster si; |
| 1238 | int ax_count; | |||
| 1239 | int frame; | |||
| 1240 | ||||
| 50a9f51d DH |
David Hilvert | 2007-05-03 05:16:00 +0000 | 1241 | std::vector<ale_pos> perturb_multipliers; |
| 1242 | ||||
| 30ea890d | David Hilvert | 2007-05-14 20:54:00 +0000 | 1243 | public: |
| 1b980378 | David Hilvert | 2008-07-18 18:30:40 +0000 | 1244 | void diff(struct scale_cluster c, const diff_trans &t, |
| afa6d8f6 | David Hilvert | 2007-05-13 03:19:00 +0000 | 1245 | int _ax_count, int f) { |
| 1732c1c4 DH |
David Hilvert | 2007-04-30 02:42:00 +0000 | 1246 | |
| 1247 | if (runs.size() == 2) | |||
| 30ea890d DH |
David Hilvert | 2007-05-14 20:54:00 +0000 | 1248 | runs.pop_back(); |
| 1249 | ||||
| 1b980378 | David Hilvert | 2008-07-18 18:30:40 +0000 | 1250 | runs.push_back(run(t)); |
| eb01b1b8 DH |
David Hilvert | 2007-04-24 05:36:00 +0000 | 1251 | |
| 1252 | si = c; | |||
| eb01b1b8 DH |
David Hilvert | 2007-04-24 05:36:00 +0000 | 1253 | ax_count = _ax_count; |
| 1254 | frame = f; | |||
| 1255 | ||||
| eb01b1b8 DH |
David Hilvert | 2007-04-24 05:36:00 +0000 | 1256 | ui::get()->d2_align_sample_start(); |
| 1257 | ||||
| dd7602d7 DH |
David Hilvert | 2008-03-06 18:41:00 +0000 | 1258 | if (interpolant != NULL) { |
| 1259 | ||||
| 1260 | /* | |||
| 1261 | * XXX: This has not been tested, and may be completely | |||
| 1262 | * wrong. | |||
| 1263 | */ | |||
| 1264 | ||||
| 1265 | transformation tt = transformation::eu_identity(); | |||
| 1266 | tt.set_current_element(t); | |||
| 1267 | interpolant->set_parameters(tt, c.input, c.accum->offset()); | |||
| 1268 | } | |||
| eb01b1b8 DH |
David Hilvert | 2007-04-24 05:36:00 +0000 | 1269 | |
| 1270 | int N; | |||
| 1271 | #ifdef USE_PTHREAD | |||
| 1272 | N = thread::count(); | |||
| 1273 | ||||
| 1274 | pthread_t *threads = (pthread_t *) malloc(sizeof(pthread_t) * N); | |||
| 1275 | pthread_attr_t *thread_attr = (pthread_attr_t *) malloc(sizeof(pthread_attr_t) * N); | |||
| 1276 | ||||
| 1277 | #else | |||
| 1278 | N = 1; | |||
| 1279 | #endif | |||
| 1280 | ||||
| 1281 | subdomain_args *args = new subdomain_args[N]; | |||
| 1282 | ||||
| d790f7da | David Hilvert | 2008-05-02 18:59:43 -0500 | 1283 | transformation::elem_bounds_int_t b = elem_bounds.scale_to_bounds(c.accum->height(), c.accum->width()); |
| dd7602d7 | David Hilvert | 2008-03-06 18:41:00 +0000 | 1284 | |
| a7ee2759 DH |
David Hilvert | 2008-03-17 17:05:06 -0500 | 1285 | // fprintf(stdout, "[%d %d] [%d %d]\n", |
| 1286 | // global_i_min, global_i_max, global_j_min, global_j_max); | |||
| e55e5de1 | David Hilvert | 2008-02-14 01:08:00 +0000 | 1287 | |
| eb01b1b8 DH |
David Hilvert | 2007-04-24 05:36:00 +0000 | 1288 | for (int ti = 0; ti < N; ti++) { |
| 1289 | args[ti].c = c; | |||
| 1732c1c4 | David Hilvert | 2007-04-30 02:42:00 +0000 | 1290 | args[ti].runs = runs; |
| eb01b1b8 DH |
David Hilvert | 2007-04-24 05:36:00 +0000 | 1291 | args[ti].ax_count = ax_count; |
| 1292 | args[ti].f = f; | |||
| d790f7da DH |
David Hilvert | 2008-05-02 18:59:43 -0500 | 1293 | args[ti].i_min = b.imin + ((b.imax - b.imin) * ti) / N; |
| 1294 | args[ti].i_max = b.imin + ((b.imax - b.imin) * (ti + 1)) / N; | |||
| 1295 | args[ti].j_min = b.jmin; | |||
| 1296 | args[ti].j_max = b.jmax; | |||
| eb01b1b8 | David Hilvert | 2007-04-24 05:36:00 +0000 | 1297 | args[ti].subdomain = ti; |
| eb01b1b8 DH |
David Hilvert | 2007-04-24 05:36:00 +0000 | 1298 | #ifdef USE_PTHREAD |
| 1299 | pthread_attr_init(&thread_attr[ti]); | |||
| 1300 | pthread_attr_setdetachstate(&thread_attr[ti], PTHREAD_CREATE_JOINABLE); | |||
| 1301 | pthread_create(&threads[ti], &thread_attr[ti], diff_subdomain, &args[ti]); | |||
| 1302 | #else | |||
| 1303 | diff_subdomain(&args[ti]); | |||
| 1304 | #endif | |||
| 1305 | } | |||
| 1306 | ||||
| 1307 | for (int ti = 0; ti < N; ti++) { | |||
| 1308 | #ifdef USE_PTHREAD | |||
| 1309 | pthread_join(threads[ti], NULL); | |||
| 1310 | #endif | |||
| 1732c1c4 | David Hilvert | 2007-04-30 02:42:00 +0000 | 1311 | runs.back().add(args[ti].runs.back()); |
| eb01b1b8 DH |
David Hilvert | 2007-04-24 05:36:00 +0000 | 1312 | } |
| 1313 | ||||
| 44a1d1de DH |
David Hilvert | 2009-03-30 19:02:57 +0000 | 1314 | #ifdef USE_PTHREAD |
| 1315 | free(threads); | |||
| 1316 | free(thread_attr); | |||
| 1317 | #endif | |||
| 1318 | ||||
| eb01b1b8 DH |
David Hilvert | 2007-04-24 05:36:00 +0000 | 1319 | delete[] args; |
| 1320 | ||||
| 1321 | ui::get()->d2_align_sample_stop(); | |||
| 1322 | ||||
| 1323 | } | |||
| 1324 | ||||
| 1325 | private: | |||
| 1326 | void rediff() { | |||
| dd7602d7 | David Hilvert | 2008-03-06 18:41:00 +0000 | 1327 | std::vector<diff_trans> t_array; |
| b971d971 | David Hilvert | 2006-12-26 05:25:00 +0000 | 1328 | |
| afa6d8f6 | David Hilvert | 2007-05-13 03:19:00 +0000 | 1329 | for (unsigned int r = 0; r < runs.size(); r++) { |
| 1732c1c4 | David Hilvert | 2007-04-30 02:42:00 +0000 | 1330 | t_array.push_back(runs[r].offset); |
| afa6d8f6 | David Hilvert | 2007-05-13 03:19:00 +0000 | 1331 | } |
| b971d971 | David Hilvert | 2006-12-26 05:25:00 +0000 | 1332 | |
| 1732c1c4 | David Hilvert | 2007-04-30 02:42:00 +0000 | 1333 | runs.clear(); |
| eb01b1b8 | David Hilvert | 2007-04-24 05:36:00 +0000 | 1334 | |
| 1732c1c4 | David Hilvert | 2007-04-30 02:42:00 +0000 | 1335 | for (unsigned int r = 0; r < t_array.size(); r++) |
| 1b980378 | David Hilvert | 2008-07-18 18:30:40 +0000 | 1336 | diff(si, t_array[r], ax_count, frame); |
| 86c0d2ba | dhilvert | 2006-10-25 14:42:00 +0000 | 1337 | } |
| 1338 | ||||
| 400c7826 | David Hilvert | 2007-04-20 07:06:00 +0000 | 1339 | |
| 1732c1c4 | David Hilvert | 2007-04-30 02:42:00 +0000 | 1340 | public: |
| 1732c1c4 DH |
David Hilvert | 2007-04-30 02:42:00 +0000 | 1341 | int better() { |
| 1342 | assert(runs.size() >= 2); | |||
| 1343 | assert(runs[0].offset.scale() == runs[1].offset.scale()); | |||
| 86c0d2ba | dhilvert | 2006-10-25 14:42:00 +0000 | 1344 | |
| 1732c1c4 DH |
David Hilvert | 2007-04-30 02:42:00 +0000 | 1345 | return (runs[1].get_error() < runs[0].get_error() |
| 1346 | || (!finite(runs[0].get_error()) && finite(runs[1].get_error()))); | |||
| 08151f52 | dhilvert | 2006-10-25 17:36:00 +0000 | 1347 | } |
| 1348 | ||||
| e0577945 DH |
David Hilvert | 2008-07-19 22:11:55 +0000 | 1349 | int better_defined() { |
| 1350 | assert(runs.size() >= 2); | |||
| 1351 | assert(runs[0].offset.scale() == runs[1].offset.scale()); | |||
| 1352 | ||||
| 1353 | return (runs[1].get_error() < runs[0].get_error()); | |||
| 1354 | } | |||
| 1355 | ||||
| f8864302 | David Hilvert | 2008-04-11 18:15:57 +0000 | 1356 | diff_stat_generic(transformation::elem_bounds_t e) |
| dd7602d7 DH |
David Hilvert | 2008-03-06 18:41:00 +0000 | 1357 | : runs(), old_runs(), perturb_multipliers() { |
| 1358 | elem_bounds = e; | |||
| 30ea890d DH |
David Hilvert | 2007-05-14 20:54:00 +0000 | 1359 | } |
| 1360 | ||||
| 1361 | run_index get_run_index(unsigned int perturb_index) { | |||
| dd7602d7 | David Hilvert | 2008-03-06 18:41:00 +0000 | 1362 | return perturb_index; |
| 30ea890d | David Hilvert | 2007-05-14 20:54:00 +0000 | 1363 | } |
| 86c0d2ba | dhilvert | 2006-10-25 14:42:00 +0000 | 1364 | |
| 30ea890d DH |
David Hilvert | 2007-05-14 20:54:00 +0000 | 1365 | run &get_run(unsigned int perturb_index) { |
| 1366 | run_index index = get_run_index(perturb_index); | |||
| dc426169 | David Hilvert | 2007-04-25 06:50:00 +0000 | 1367 | |
| 30ea890d DH |
David Hilvert | 2007-05-14 20:54:00 +0000 | 1368 | assert(old_runs.count(index)); |
| 1369 | return old_runs[index]; | |||
| 1732c1c4 | David Hilvert | 2007-04-30 02:42:00 +0000 | 1370 | } |
| dc426169 | David Hilvert | 2007-04-25 06:50:00 +0000 | 1371 | |
| 1732c1c4 DH |
David Hilvert | 2007-04-30 02:42:00 +0000 | 1372 | void rescale(ale_pos scale, scale_cluster _si) { |
| 1373 | assert(runs.size() == 1); | |||
| 86c0d2ba | dhilvert | 2006-10-25 14:42:00 +0000 | 1374 | |
| 1732c1c4 | David Hilvert | 2007-04-30 02:42:00 +0000 | 1375 | si = _si; |
| 86c0d2ba | dhilvert | 2006-10-25 14:42:00 +0000 | 1376 | |
| 1732c1c4 DH |
David Hilvert | 2007-04-30 02:42:00 +0000 | 1377 | runs[0].rescale(scale); |
| 1378 | ||||
| 30ea890d DH |
David Hilvert | 2007-05-14 20:54:00 +0000 | 1379 | rediff(); |
| 1380 | } | |||
| 1381 | ||||
| f8864302 | David Hilvert | 2008-04-11 18:15:57 +0000 | 1382 | ~diff_stat_generic() { |
| 1732c1c4 | David Hilvert | 2007-04-30 02:42:00 +0000 | 1383 | } |
| 86c0d2ba | dhilvert | 2006-10-25 14:42:00 +0000 | 1384 | |
| f8864302 | David Hilvert | 2008-04-11 18:15:57 +0000 | 1385 | diff_stat_generic &operator=(const diff_stat_generic &dst) { |
| 1732c1c4 DH |
David Hilvert | 2007-04-30 02:42:00 +0000 | 1386 | /* |
| 1387 | * Copy run information. | |||
| 1388 | */ | |||
| 1389 | runs = dst.runs; | |||
| 82db7fe6 | David Hilvert | 2007-05-05 18:29:00 +0000 | 1390 | old_runs = dst.old_runs; |
| 86c0d2ba | dhilvert | 2006-10-25 14:42:00 +0000 | 1391 | |
| 1732c1c4 DH |
David Hilvert | 2007-04-30 02:42:00 +0000 | 1392 | /* |
| 1393 | * Copy diff variables | |||
| 1394 | */ | |||
| 1732c1c4 DH |
David Hilvert | 2007-04-30 02:42:00 +0000 | 1395 | si = dst.si; |
| 1396 | ax_count = dst.ax_count; | |||
| 1397 | frame = dst.frame; | |||
| 50a9f51d | David Hilvert | 2007-05-03 05:16:00 +0000 | 1398 | perturb_multipliers = dst.perturb_multipliers; |
| dd7602d7 | David Hilvert | 2008-03-06 18:41:00 +0000 | 1399 | elem_bounds = dst.elem_bounds; |
| 86c0d2ba | dhilvert | 2006-10-25 14:42:00 +0000 | 1400 | |
| 1732c1c4 DH |
David Hilvert | 2007-04-30 02:42:00 +0000 | 1401 | return *this; |
| 1402 | } | |||
| dc426169 | David Hilvert | 2007-04-25 06:50:00 +0000 | 1403 | |
| f8864302 | David Hilvert | 2008-04-11 18:15:57 +0000 | 1404 | diff_stat_generic(const diff_stat_generic &dst) : runs(), old_runs(), |
| ca7b6c30 | David Hilvert | 2007-05-06 02:42:00 +0000 | 1405 | perturb_multipliers() { |
| 1732c1c4 DH |
David Hilvert | 2007-04-30 02:42:00 +0000 | 1406 | operator=(dst); |
| 1407 | } | |||
| dc426169 | David Hilvert | 2007-04-25 06:50:00 +0000 | 1408 | |
| 1ed23c0d DH |
David Hilvert | 2008-03-09 11:23:00 +0000 | 1409 | void set_elem_bounds(transformation::elem_bounds_t e) { |
| 1410 | elem_bounds = e; | |||
| 1411 | } | |||
| 1412 | ||||
| 1732c1c4 DH |
David Hilvert | 2007-04-30 02:42:00 +0000 | 1413 | ale_accum get_result() { |
| 1414 | assert(runs.size() == 1); | |||
| 1415 | return runs[0].result; | |||
| 1416 | } | |||
| 86c0d2ba | dhilvert | 2006-10-25 14:42:00 +0000 | 1417 | |
| 1732c1c4 DH |
David Hilvert | 2007-04-30 02:42:00 +0000 | 1418 | ale_accum get_divisor() { |
| 1419 | assert(runs.size() == 1); | |||
| 1420 | return runs[0].divisor; | |||
| 1421 | } | |||
| dc426169 | David Hilvert | 2007-04-25 06:50:00 +0000 | 1422 | |
| dd7602d7 | David Hilvert | 2008-03-06 18:41:00 +0000 | 1423 | diff_trans get_offset() { |
| 1732c1c4 DH |
David Hilvert | 2007-04-30 02:42:00 +0000 | 1424 | assert(runs.size() == 1); |
| 1425 | return runs[0].offset; | |||
| 1426 | } | |||
| dc426169 | David Hilvert | 2007-04-25 06:50:00 +0000 | 1427 | |
| f8864302 | David Hilvert | 2008-04-11 18:15:57 +0000 | 1428 | int operator!=(diff_stat_generic ¶m) { |
| 65886ff7 | David Hilvert | 2007-09-04 02:10:00 +0000 | 1429 | return (get_error() != param.get_error()); |
| 86c0d2ba | dhilvert | 2006-10-25 14:42:00 +0000 | 1430 | } |
| 08151f52 | dhilvert | 2006-10-25 17:36:00 +0000 | 1431 | |
| f8864302 | David Hilvert | 2008-04-11 18:15:57 +0000 | 1432 | int operator==(diff_stat_generic ¶m) { |
| 1732c1c4 DH |
David Hilvert | 2007-04-30 02:42:00 +0000 | 1433 | return !(operator!=(param)); |
| 1434 | } | |||
| 08151f52 | dhilvert | 2006-10-25 17:36:00 +0000 | 1435 | |
| 1732c1c4 DH |
David Hilvert | 2007-04-30 02:42:00 +0000 | 1436 | ale_pos get_error_perturb() { |
| 1437 | assert(runs.size() == 1); | |||
| 1438 | return runs[0].get_error_perturb(); | |||
| 08151f52 | dhilvert | 2006-10-25 17:36:00 +0000 | 1439 | } |
| 86c0d2ba | dhilvert | 2006-10-25 14:42:00 +0000 | 1440 | |
| 1732c1c4 DH |
David Hilvert | 2007-04-30 02:42:00 +0000 | 1441 | ale_accum get_error() const { |
| 1442 | assert(runs.size() == 1); | |||
| 1443 | return runs[0].get_error(); | |||
| eb01b1b8 | David Hilvert | 2007-04-24 05:36:00 +0000 | 1444 | } |
| dda1bf79 | dhilvert | 2006-10-22 18:40:00 +0000 | 1445 | |
| 30ea890d | David Hilvert | 2007-05-14 20:54:00 +0000 | 1446 | public: |
| 1732c1c4 DH |
David Hilvert | 2007-04-30 02:42:00 +0000 | 1447 | /* |
| 1448 | * Get the set of transformations produced by a given perturbation | |||
| 1449 | */ | |||
| dd7602d7 | David Hilvert | 2008-03-06 18:41:00 +0000 | 1450 | void get_perturb_set(std::vector<diff_trans> *set, |
| 1732c1c4 | David Hilvert | 2007-04-30 02:42:00 +0000 | 1451 | ale_pos adj_p, ale_pos adj_o, ale_pos adj_b, |
| 5d53e401 DH |
David Hilvert | 2007-05-02 03:12:00 +0000 | 1452 | ale_pos *current_bd, ale_pos *modified_bd, |
| 1453 | std::vector<ale_pos> multipliers = std::vector<ale_pos>()) { | |||
| dc426169 | David Hilvert | 2007-04-25 06:50:00 +0000 | 1454 | |
| 1732c1c4 | David Hilvert | 2007-04-30 02:42:00 +0000 | 1455 | assert(runs.size() == 1); |
| dda1bf79 | dhilvert | 2006-10-22 18:40:00 +0000 | 1456 | |
| dd7602d7 | David Hilvert | 2008-03-06 18:41:00 +0000 | 1457 | diff_trans test_t(diff_trans::eu_identity()); |
| dda1bf79 | dhilvert | 2006-10-22 18:40:00 +0000 | 1458 | |
| b2e7131e DH |
David Hilvert | 2007-05-02 08:35:00 +0000 | 1459 | /* |
| 1460 | * Translational or euclidean transformation | |||
| 1461 | */ | |||
| 2aa417e6 | dhilvert | 2005-01-07 06:44:00 +0000 | 1462 | |
| b2e7131e DH |
David Hilvert | 2007-05-02 08:35:00 +0000 | 1463 | for (unsigned int i = 0; i < 2; i++) |
| 1464 | for (unsigned int s = 0; s < 2; s++) { | |||
| dc426169 | David Hilvert | 2007-04-25 06:50:00 +0000 | 1465 | |
| b2e7131e DH |
David Hilvert | 2007-05-02 08:35:00 +0000 | 1466 | if (!multipliers.size()) |
| 1467 | multipliers.push_back(1); | |||
| dc426169 | David Hilvert | 2007-04-25 06:50:00 +0000 | 1468 | |
| b2e7131e | David Hilvert | 2007-05-02 08:35:00 +0000 | 1469 | assert(finite(multipliers[0])); |
| 5d53e401 | David Hilvert | 2007-05-02 03:12:00 +0000 | 1470 | |
| b2e7131e | David Hilvert | 2007-05-02 08:35:00 +0000 | 1471 | test_t = get_offset(); |
| dc426169 | David Hilvert | 2007-04-25 06:50:00 +0000 | 1472 | |
| b2e7131e DH |
David Hilvert | 2007-05-02 08:35:00 +0000 | 1473 | // test_t.eu_modify(i, (s ? -adj_p : adj_p) * multipliers[0]); |
| 1474 | test_t.translate((i ? point(1, 0) : point(0, 1)) | |||
| 1475 | * (s ? -adj_p : adj_p) | |||
| 1476 | * multipliers[0]); | |||
| 50a9f51d DH |
David Hilvert | 2007-05-03 05:16:00 +0000 | 1477 | |
| 1478 | test_t.snap(adj_p / 2); | |||
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 1479 | |
| b2e7131e DH |
David Hilvert | 2007-05-02 08:35:00 +0000 | 1480 | set->push_back(test_t); |
| 1481 | multipliers.erase(multipliers.begin()); | |||
| 46f9776a | dhilvert | 2005-01-07 06:44:00 +0000 | 1482 | |
| b2e7131e | David Hilvert | 2007-05-02 08:35:00 +0000 | 1483 | } |
| 4707675e | dhilvert | 2006-10-19 10:24:00 +0000 | 1484 | |
| b2e7131e DH |
David Hilvert | 2007-05-02 08:35:00 +0000 | 1485 | if (alignment_class > 0) |
| 1486 | for (unsigned int s = 0; s < 2; s++) { | |||
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 1487 | |
| b2e7131e DH |
David Hilvert | 2007-05-02 08:35:00 +0000 | 1488 | if (!multipliers.size()) |
| 1489 | multipliers.push_back(1); | |||
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 1490 | |
| b2e7131e DH |
David Hilvert | 2007-05-02 08:35:00 +0000 | 1491 | assert(multipliers.size()); |
| 1492 | assert(finite(multipliers[0])); | |||
| 5d53e401 | David Hilvert | 2007-05-02 03:12:00 +0000 | 1493 | |
| b2e7131e DH |
David Hilvert | 2007-05-02 08:35:00 +0000 | 1494 | if (!(adj_o * multipliers[0] < rot_max)) |
| 1495 | return; | |||
| 4707675e | dhilvert | 2006-10-19 10:24:00 +0000 | 1496 | |
| b2e7131e | David Hilvert | 2007-05-02 08:35:00 +0000 | 1497 | ale_pos adj_s = (s ? 1 : -1) * adj_o * multipliers[0]; |
| 5d53e401 | David Hilvert | 2007-05-02 03:12:00 +0000 | 1498 | |
| b2e7131e | David Hilvert | 2007-05-02 08:35:00 +0000 | 1499 | test_t = get_offset(); |
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 1500 | |
| 30ea890d | David Hilvert | 2007-05-14 20:54:00 +0000 | 1501 | run_index ori = get_run_index(set->size()); |
| b2e7131e | David Hilvert | 2007-05-02 08:35:00 +0000 | 1502 | point centroid = point::undefined(); |
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 1503 | |
| b2e7131e DH |
David Hilvert | 2007-05-02 08:35:00 +0000 | 1504 | if (!old_runs.count(ori)) |
| 1505 | ori = get_run_index(0); | |||
| 5d53e401 | David Hilvert | 2007-05-02 03:12:00 +0000 | 1506 | |
| b2e7131e DH |
David Hilvert | 2007-05-02 08:35:00 +0000 | 1507 | if (!centroid.finite() && old_runs.count(ori)) { |
| 1508 | centroid = old_runs[ori].get_error_centroid(); | |||
| 5d53e401 | David Hilvert | 2007-05-02 03:12:00 +0000 | 1509 | |
| b2e7131e DH |
David Hilvert | 2007-05-02 08:35:00 +0000 | 1510 | if (!centroid.finite()) |
| 1511 | centroid = old_runs[ori].get_centroid(); | |||
| 50a9f51d DH |
David Hilvert | 2007-05-03 05:16:00 +0000 | 1512 | |
| 1513 | centroid *= test_t.scale() | |||
| 1514 | / old_runs[ori].offset.scale(); | |||
| b2e7131e | David Hilvert | 2007-05-02 08:35:00 +0000 | 1515 | } |
| 5d53e401 | David Hilvert | 2007-05-02 03:12:00 +0000 | 1516 | |
| b2e7131e DH |
David Hilvert | 2007-05-02 08:35:00 +0000 | 1517 | if (!centroid.finite() && !test_t.is_projective()) { |
| 1518 | test_t.eu_modify(2, adj_s); | |||
| 1519 | } else if (!centroid.finite()) { | |||
| 1520 | centroid = point(si.input->height() / 2, | |||
| 1521 | si.input->width() / 2); | |||
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 1522 | |
| b2e7131e DH |
David Hilvert | 2007-05-02 08:35:00 +0000 | 1523 | test_t.rotate(centroid + si.accum->offset(), |
| 1524 | adj_s); | |||
| 1525 | } else { | |||
| 1526 | test_t.rotate(centroid + si.accum->offset(), | |||
| 1527 | adj_s); | |||
| 1732c1c4 | David Hilvert | 2007-04-30 02:42:00 +0000 | 1528 | } |
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 1529 | |
| 50a9f51d DH |
David Hilvert | 2007-05-03 05:16:00 +0000 | 1530 | test_t.snap(adj_p / 2); |
| 1531 | ||||
| b2e7131e DH |
David Hilvert | 2007-05-02 08:35:00 +0000 | 1532 | set->push_back(test_t); |
| 1533 | multipliers.erase(multipliers.begin()); | |||
| 1534 | } | |||
| 1535 | ||||
| 1536 | if (alignment_class == 2) { | |||
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 1537 | |
| 1732c1c4 DH |
David Hilvert | 2007-04-30 02:42:00 +0000 | 1538 | /* |
| 1539 | * Projective transformation | |||
| 1540 | */ | |||
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 1541 | |
| 1732c1c4 DH |
David Hilvert | 2007-04-30 02:42:00 +0000 | 1542 | for (unsigned int i = 0; i < 4; i++) |
| 1543 | for (unsigned int j = 0; j < 2; j++) | |||
| 1544 | for (unsigned int s = 0; s < 2; s++) { | |||
| 1545 | ||||
| 5d53e401 DH |
David Hilvert | 2007-05-02 03:12:00 +0000 | 1546 | if (!multipliers.size()) |
| 1547 | multipliers.push_back(1); | |||
| 1548 | ||||
| 1732c1c4 DH |
David Hilvert | 2007-04-30 02:42:00 +0000 | 1549 | assert(multipliers.size()); |
| 1550 | assert(finite(multipliers[0])); | |||
| 46f9776a | dhilvert | 2005-01-07 06:44:00 +0000 | 1551 | |
| 1732c1c4 | David Hilvert | 2007-04-30 02:42:00 +0000 | 1552 | ale_pos adj_s = (s ? -1 : 1) * adj_p * multipliers [0]; |
| eb01b1b8 | David Hilvert | 2007-04-24 05:36:00 +0000 | 1553 | |
| 1732c1c4 | David Hilvert | 2007-04-30 02:42:00 +0000 | 1554 | test_t = get_offset(); |
| 46f9776a | dhilvert | 2005-01-07 06:44:00 +0000 | 1555 | |
| 1732c1c4 | David Hilvert | 2007-04-30 02:42:00 +0000 | 1556 | if (perturb_type == 0) |
| 73f0dc5c | David Hilvert | 2008-08-18 17:37:54 -0500 | 1557 | test_t.gpt_modify_bounded(j, i, adj_s, elem_bounds.scale_to_bounds(si.accum->height(), si.accum->width())); |
| 1732c1c4 DH |
David Hilvert | 2007-04-30 02:42:00 +0000 | 1558 | else if (perturb_type == 1) |
| 1559 | test_t.gr_modify(j, i, adj_s); | |||
| 1560 | else | |||
| 1561 | assert(0); | |||
| dc426169 | David Hilvert | 2007-04-25 06:50:00 +0000 | 1562 | |
| 50a9f51d DH |
David Hilvert | 2007-05-03 05:16:00 +0000 | 1563 | test_t.snap(adj_p / 2); |
| 1564 | ||||
| 1732c1c4 DH |
David Hilvert | 2007-04-30 02:42:00 +0000 | 1565 | set->push_back(test_t); |
| 1566 | multipliers.erase(multipliers.begin()); | |||
| 1567 | } | |||
| dc426169 | David Hilvert | 2007-04-25 06:50:00 +0000 | 1568 | |
| b2e7131e | David Hilvert | 2007-05-02 08:35:00 +0000 | 1569 | } |
| dc426169 | David Hilvert | 2007-04-25 06:50:00 +0000 | 1570 | |
| 49a3a0b4 | David Hilvert | 2007-04-01 07:13:00 +0000 | 1571 | /* |
| 1732c1c4 | David Hilvert | 2007-04-30 02:42:00 +0000 | 1572 | * Barrel distortion |
| 49a3a0b4 DH |
David Hilvert | 2007-04-01 07:13:00 +0000 | 1573 | */ |
| 1574 | ||||
| 1732c1c4 | David Hilvert | 2007-04-30 02:42:00 +0000 | 1575 | if (bda_mult != 0 && adj_b != 0) { |
| 7a696eb1 | David Hilvert | 2007-04-01 06:41:00 +0000 | 1576 | |
| 1732c1c4 DH |
David Hilvert | 2007-04-30 02:42:00 +0000 | 1577 | for (unsigned int d = 0; d < get_offset().bd_count(); d++) |
| 1578 | for (unsigned int s = 0; s < 2; s++) { | |||
| 1579 | ||||
| 5d53e401 DH |
David Hilvert | 2007-05-02 03:12:00 +0000 | 1580 | if (!multipliers.size()) |
| 1581 | multipliers.push_back(1); | |||
| 1582 | ||||
| 1732c1c4 DH |
David Hilvert | 2007-04-30 02:42:00 +0000 | 1583 | assert (multipliers.size()); |
| 1584 | assert (finite(multipliers[0])); | |||
| 1585 | ||||
| 1586 | ale_pos adj_s = (s ? -1 : 1) * adj_b * multipliers[0]; | |||
| 1587 | ||||
| 1588 | if (bda_rate > 0 && fabs(modified_bd[d] + adj_s - current_bd[d]) > bda_rate) | |||
| 1589 | continue; | |||
| 1590 | ||||
| dd7602d7 | David Hilvert | 2008-03-06 18:41:00 +0000 | 1591 | diff_trans test_t = get_offset(); |
| 1732c1c4 DH |
David Hilvert | 2007-04-30 02:42:00 +0000 | 1592 | |
| 1593 | test_t.bd_modify(d, adj_s); | |||
| 1594 | ||||
| 1595 | set->push_back(test_t); | |||
| 1596 | } | |||
| eb01b1b8 | David Hilvert | 2007-04-24 05:36:00 +0000 | 1597 | } |
| 1732c1c4 | David Hilvert | 2007-04-30 02:42:00 +0000 | 1598 | } |
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 1599 | |
| 1732c1c4 DH |
David Hilvert | 2007-04-30 02:42:00 +0000 | 1600 | void confirm() { |
| 1601 | assert(runs.size() == 2); | |||
| 1602 | runs[0] = runs[1]; | |||
| 1603 | runs.pop_back(); | |||
| eb01b1b8 DH |
David Hilvert | 2007-04-24 05:36:00 +0000 | 1604 | } |
| 1605 | ||||
| 1732c1c4 DH |
David Hilvert | 2007-04-30 02:42:00 +0000 | 1606 | void discard() { |
| 1607 | assert(runs.size() == 2); | |||
| 1608 | runs.pop_back(); | |||
| 1609 | } | |||
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 1610 | |
| afa6d8f6 | David Hilvert | 2007-05-13 03:19:00 +0000 | 1611 | void perturb_test(ale_pos perturb, ale_pos adj_p, ale_pos adj_o, ale_pos adj_b, |
| 2077ce22 | David Hilvert | 2007-05-13 09:23:00 +0000 | 1612 | ale_pos *current_bd, ale_pos *modified_bd, int stable) { |
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 1613 | |
| 1732c1c4 | David Hilvert | 2007-04-30 02:42:00 +0000 | 1614 | assert(runs.size() == 1); |
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 1615 | |
| dd7602d7 | David Hilvert | 2008-03-06 18:41:00 +0000 | 1616 | std::vector<diff_trans> t_set; |
| 4707675e | dhilvert | 2006-10-19 10:24:00 +0000 | 1617 | |
| 50a9f51d DH |
David Hilvert | 2007-05-03 05:16:00 +0000 | 1618 | if (perturb_multipliers.size() == 0) { |
| 1619 | get_perturb_set(&t_set, adj_p, adj_o, adj_b, | |||
| 1620 | current_bd, modified_bd); | |||
| 1621 | ||||
| 1622 | for (unsigned int i = 0; i < t_set.size(); i++) { | |||
| f8864302 | David Hilvert | 2008-04-11 18:15:57 +0000 | 1623 | diff_stat_generic test = *this; |
| 50a9f51d | David Hilvert | 2007-05-03 05:16:00 +0000 | 1624 | |
| 1b980378 | David Hilvert | 2008-07-18 18:30:40 +0000 | 1625 | test.diff(si, t_set[i], ax_count, frame); |
| 50a9f51d DH |
David Hilvert | 2007-05-03 05:16:00 +0000 | 1626 | |
| 1627 | test.confirm(); | |||
| 1628 | ||||
| 42772195 | David Hilvert | 2007-10-21 15:36:00 +0000 | 1629 | if (finite(test.get_error_perturb())) |
| 82db7fe6 DH |
David Hilvert | 2007-05-05 18:29:00 +0000 | 1630 | perturb_multipliers.push_back(adj_p / test.get_error_perturb()); |
| 1631 | else | |||
| 1632 | perturb_multipliers.push_back(1); | |||
| 1633 | ||||
| 50a9f51d DH |
David Hilvert | 2007-05-03 05:16:00 +0000 | 1634 | } |
| 1635 | ||||
| 1636 | t_set.clear(); | |||
| 1637 | } | |||
| 1638 | ||||
| 5d53e401 | David Hilvert | 2007-05-02 03:12:00 +0000 | 1639 | get_perturb_set(&t_set, adj_p, adj_o, adj_b, current_bd, modified_bd, |
| 50a9f51d | David Hilvert | 2007-05-03 05:16:00 +0000 | 1640 | perturb_multipliers); |
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 1641 | |
| 1732c1c4 DH |
David Hilvert | 2007-04-30 02:42:00 +0000 | 1642 | int found_unreliable = 1; |
| 1643 | std::vector<int> tested(t_set.size(), 0); | |||
| dc426169 | David Hilvert | 2007-04-25 06:50:00 +0000 | 1644 | |
| 30ea890d DH |
David Hilvert | 2007-05-14 20:54:00 +0000 | 1645 | for (unsigned int i = 0; i < t_set.size(); i++) { |
| 1646 | run_index ori = get_run_index(i); | |||
| 1647 | ||||
| 1648 | /* | |||
| 1649 | * Check for stability | |||
| 1650 | */ | |||
| 1651 | if (stable | |||
| 1652 | && old_runs.count(ori) | |||
| 1653 | && old_runs[ori].offset == t_set[i]) | |||
| 1654 | tested[i] = 1; | |||
| 30ea890d DH |
David Hilvert | 2007-05-14 20:54:00 +0000 | 1655 | } |
| 1656 | ||||
| 1657 | std::vector<ale_pos> perturb_multipliers_original = perturb_multipliers; | |||
| 1658 | ||||
| 1732c1c4 | David Hilvert | 2007-04-30 02:42:00 +0000 | 1659 | while (found_unreliable) { |
| dc426169 | David Hilvert | 2007-04-25 06:50:00 +0000 | 1660 | |
| 1732c1c4 | David Hilvert | 2007-04-30 02:42:00 +0000 | 1661 | found_unreliable = 0; |
| 08151f52 | dhilvert | 2006-10-25 17:36:00 +0000 | 1662 | |
| 1732c1c4 | David Hilvert | 2007-04-30 02:42:00 +0000 | 1663 | for (unsigned int i = 0; i < t_set.size(); i++) { |
| 4d806213 | dhilvert | 2006-10-23 17:58:00 +0000 | 1664 | |
| 1732c1c4 DH |
David Hilvert | 2007-04-30 02:42:00 +0000 | 1665 | if (tested[i]) |
| 1666 | continue; | |||
| b410ef51 | dhilvert | 2006-10-23 15:30:00 +0000 | 1667 | |
| 1b980378 | David Hilvert | 2008-07-18 18:30:40 +0000 | 1668 | diff(si, t_set[i], ax_count, frame); |
| 7e87bd8e | dhilvert | 2006-10-23 06:39:00 +0000 | 1669 | |
| 50a9f51d DH |
David Hilvert | 2007-05-03 05:16:00 +0000 | 1670 | if (!(i < perturb_multipliers.size()) |
| 1671 | || !finite(perturb_multipliers[i])) { | |||
| 5d53e401 | David Hilvert | 2007-05-02 03:12:00 +0000 | 1672 | |
| 50a9f51d | David Hilvert | 2007-05-03 05:16:00 +0000 | 1673 | perturb_multipliers.resize(i + 1); |
| 5d53e401 | David Hilvert | 2007-05-02 03:12:00 +0000 | 1674 | |
| 42772195 DH |
David Hilvert | 2007-10-21 15:36:00 +0000 | 1675 | if (finite(perturb_multipliers[i]) |
| 1676 | && finite(adj_p) | |||
| 1677 | && finite(adj_p / runs[1].get_error_perturb())) { | |||
| 1678 | perturb_multipliers[i] = | |||
| 1679 | adj_p / runs[1].get_error_perturb(); | |||
| 5d53e401 | David Hilvert | 2007-05-02 03:12:00 +0000 | 1680 | |
| 8c886617 | David Hilvert | 2007-05-02 08:39:00 +0000 | 1681 | found_unreliable = 1; |
| 42772195 | David Hilvert | 2007-10-21 15:36:00 +0000 | 1682 | } else |
| 858b0722 | David Hilvert | 2007-10-03 20:44:00 +0000 | 1683 | perturb_multipliers[i] = 1; |
| 5d53e401 DH |
David Hilvert | 2007-05-02 03:12:00 +0000 | 1684 | |
| 1685 | continue; | |||
| 1686 | } | |||
| 1687 | ||||
| 30ea890d DH |
David Hilvert | 2007-05-14 20:54:00 +0000 | 1688 | run_index ori = get_run_index(i); |
| 1689 | ||||
| 1690 | if (old_runs.count(ori) == 0) | |||
| 1691 | old_runs.insert(std::pair<run_index, run>(ori, runs[1])); | |||
| 1692 | else | |||
| 1693 | old_runs[ori] = runs[1]; | |||
| 5d53e401 | David Hilvert | 2007-05-02 03:12:00 +0000 | 1694 | |
| 42772195 DH |
David Hilvert | 2007-10-21 15:36:00 +0000 | 1695 | if (finite(perturb_multipliers_original[i]) |
| 1696 | && finite(runs[1].get_error_perturb()) | |||
| 1697 | && finite(adj_p) | |||
| 1698 | && finite(perturb_multipliers_original[i] * adj_p / runs[1].get_error_perturb())) | |||
| 1699 | perturb_multipliers[i] = perturb_multipliers_original[i] | |||
| 1700 | * adj_p / runs[1].get_error_perturb(); | |||
| 1701 | else | |||
| 50a9f51d | David Hilvert | 2007-05-03 05:16:00 +0000 | 1702 | perturb_multipliers[i] = 1; |
| 5d53e401 | David Hilvert | 2007-05-02 03:12:00 +0000 | 1703 | |
| 1732c1c4 | David Hilvert | 2007-04-30 02:42:00 +0000 | 1704 | tested[i] = 1; |
| dda1bf79 | dhilvert | 2006-10-22 18:40:00 +0000 | 1705 | |
| 1732c1c4 | David Hilvert | 2007-04-30 02:42:00 +0000 | 1706 | if (better() |
| 30ea890d DH |
David Hilvert | 2007-05-14 20:54:00 +0000 | 1707 | && runs[1].get_error() < runs[0].get_error() |
| 1708 | && perturb_multipliers[i] | |||
| 1709 | / perturb_multipliers_original[i] < 2) { | |||
| 9e8e62c9 DH |
David Hilvert | 2007-05-13 10:57:00 +0000 | 1710 | runs[0] = runs[1]; |
| 1711 | runs.pop_back(); | |||
| 1712 | return; | |||
| afa6d8f6 | David Hilvert | 2007-05-13 03:19:00 +0000 | 1713 | } |
| 82db7fe6 | David Hilvert | 2007-05-05 18:29:00 +0000 | 1714 | |
| 1732c1c4 | David Hilvert | 2007-04-30 02:42:00 +0000 | 1715 | } |
| dda1bf79 | dhilvert | 2006-10-22 18:40:00 +0000 | 1716 | |
| 2077ce22 DH |
David Hilvert | 2007-05-13 09:23:00 +0000 | 1717 | if (runs.size() > 1) |
| 1718 | runs.pop_back(); | |||
| 3aa65890 | dhilvert | 2006-10-25 15:26:00 +0000 | 1719 | |
| 1732c1c4 DH |
David Hilvert | 2007-04-30 02:42:00 +0000 | 1720 | if (!found_unreliable) |
| 1721 | return; | |||
| 1732c1c4 | David Hilvert | 2007-04-30 02:42:00 +0000 | 1722 | } |
| eb01b1b8 | David Hilvert | 2007-04-24 05:36:00 +0000 | 1723 | } |
| 08151f52 | dhilvert | 2006-10-25 17:36:00 +0000 | 1724 | |
| eb01b1b8 | David Hilvert | 2007-04-24 05:36:00 +0000 | 1725 | }; |
| 4707675e | dhilvert | 2006-10-19 10:24:00 +0000 | 1726 | |
| f8864302 DH |
David Hilvert | 2008-04-11 18:15:57 +0000 | 1727 | typedef diff_stat_generic<trans_single> diff_stat_t; |
| 1728 | typedef diff_stat_generic<trans_multi> diff_stat_multi; | |||
| 1729 | ||||
| 4707675e | dhilvert | 2006-10-19 10:24:00 +0000 | 1730 | |
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 1731 | /* |
| 1732 | * Adjust exposure for an aligned frame B against reference A. | |||
| 07271611 | dhilvert | 2005-01-07 06:48:00 +0000 | 1733 | * |
| 1734 | * Expects full-LOD images. | |||
| 1735 | * | |||
| 423af06c DH |
David Hilvert | 2007-09-10 17:43:00 +0000 | 1736 | * Note: This method does not use any weighting, by certainty or |
| 1737 | * otherwise, in the first exposure registration pass, as any bias of | |||
| 1738 | * weighting according to color may also bias the exposure registration | |||
| 1739 | * result; it does use weighting, including weighting by certainty | |||
| 1740 | * (even if certainty weighting is not specified), in the second pass, | |||
| 1741 | * under the assumption that weighting by certainty improves handling | |||
| 1742 | * of out-of-range highlights, and that bias of exposure measurements | |||
| 1743 | * according to color may generally be less harmful after spatial | |||
| 1744 | * registration has been performed. | |||
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 1745 | */ |
| 70fb02f9 DH |
David Hilvert | 2007-09-21 03:18:00 +0000 | 1746 | class exposure_ratio_iterate : public thread::decompose_domain { |
| 1747 | pixel_accum *asums; | |||
| 1748 | pixel_accum *bsums; | |||
| 1749 | pixel_accum *asum; | |||
| 1750 | pixel_accum *bsum; | |||
| 1751 | struct scale_cluster c; | |||
| 214d014c | David Hilvert | 2008-05-04 22:08:03 -0500 | 1752 | const transformation &t; |
| 70fb02f9 DH |
David Hilvert | 2007-09-21 03:18:00 +0000 | 1753 | int ax_count; |
| 1754 | int pass_number; | |||
| 1755 | protected: | |||
| 1756 | void prepare_subdomains(unsigned int N) { | |||
| 1757 | asums = new pixel_accum[N]; | |||
| 1758 | bsums = new pixel_accum[N]; | |||
| 1759 | } | |||
| 1760 | void subdomain_algorithm(unsigned int thread, | |||
| 1761 | int i_min, int i_max, int j_min, int j_max) { | |||
| 1762 | ||||
| 1763 | point offset = c.accum->offset(); | |||
| 1764 | ||||
| 1765 | for (mc_iterate m(i_min, i_max, j_min, j_max, thread); !m.done(); m++) { | |||
| 1766 | ||||
| 1767 | unsigned int i = (unsigned int) m.get_i(); | |||
| 1768 | unsigned int j = (unsigned int) m.get_j(); | |||
| 1769 | ||||
| 1770 | if (ref_excluded(i, j, offset, c.ax_parameters, ax_count)) | |||
| 1771 | continue; | |||
| 1772 | ||||
| 1773 | /* | |||
| 1774 | * Transform | |||
| 1775 | */ | |||
| 1776 | ||||
| 1777 | struct point q; | |||
| 1778 | ||||
| 1779 | q = (c.input_scale < 1.0 && interpolant == NULL) | |||
| 1780 | ? t.scaled_inverse_transform( | |||
| 1781 | point(i + offset[0], j + offset[1])) | |||
| 1782 | : t.unscaled_inverse_transform( | |||
| 1783 | point(i + offset[0], j + offset[1])); | |||
| 1784 | ||||
| 1785 | /* | |||
| 1786 | * Check that the transformed coordinates are within | |||
| 1787 | * the boundaries of array c.input, that they are not | |||
| 1788 | * subject to exclusion, and that the weight value in | |||
| 1789 | * the accumulated array is nonzero. | |||
| 1790 | */ | |||
| 1791 | ||||
| 1792 | if (input_excluded(q[0], q[1], c.ax_parameters, ax_count)) | |||
| 1793 | continue; | |||
| 1794 | ||||
| 1795 | if (q[0] >= 0 | |||
| 1796 | && q[0] <= c.input->height() - 1 | |||
| 1797 | && q[1] >= 0 | |||
| 1798 | && q[1] <= c.input->width() - 1 | |||
| e4e7ac02 | David Hilvert | 2007-12-12 23:20:00 +0000 | 1799 | && ((pixel) c.certainty->get_pixel(i, j)).minabs_norm() != 0) { |
| 70fb02f9 DH |
David Hilvert | 2007-09-21 03:18:00 +0000 | 1800 | pixel a = c.accum->get_pixel(i, j); |
| 1801 | pixel b; | |||
| 1802 | ||||
| 1803 | b = c.input->get_bl(q); | |||
| 1804 | ||||
| 1805 | pixel weight = ((c.aweight && pass_number) | |||
| e4e7ac02 | David Hilvert | 2007-12-12 23:20:00 +0000 | 1806 | ? (pixel) c.aweight->get_pixel(i, j) |
| 70fb02f9 DH |
David Hilvert | 2007-09-21 03:18:00 +0000 | 1807 | : pixel(1, 1, 1)) |
| 1808 | * (pass_number | |||
| 5c603c78 DH |
David Hilvert | 2007-10-29 04:51:00 +0000 | 1809 | ? psqrt(c.certainty->get_pixel(i, j) |
| 1810 | * c.input_certainty->get_bl(q, 1)) | |||
| 70fb02f9 DH |
David Hilvert | 2007-09-21 03:18:00 +0000 | 1811 | : pixel(1, 1, 1)); |
| 1812 | ||||
| 1813 | asums[thread] += a * weight; | |||
| 1814 | bsums[thread] += b * weight; | |||
| 1815 | } | |||
| 1816 | } | |||
| 1817 | } | |||
| 1818 | ||||
| 1819 | void finish_subdomains(unsigned int N) { | |||
| 1820 | for (unsigned int n = 0; n < N; n++) { | |||
| 1821 | *asum += asums[n]; | |||
| 1822 | *bsum += bsums[n]; | |||
| 1823 | } | |||
| 34c6efce DH |
David Hilvert | 2007-10-23 01:35:00 +0000 | 1824 | delete[] asums; |
| 1825 | delete[] bsums; | |||
| 70fb02f9 DH |
David Hilvert | 2007-09-21 03:18:00 +0000 | 1826 | } |
| 1827 | public: | |||
| 1828 | exposure_ratio_iterate(pixel_accum *_asum, | |||
| 1829 | pixel_accum *_bsum, | |||
| 1830 | struct scale_cluster _c, | |||
| 214d014c | David Hilvert | 2008-05-04 22:08:03 -0500 | 1831 | const transformation &_t, |
| 70fb02f9 DH |
David Hilvert | 2007-09-21 03:18:00 +0000 | 1832 | int _ax_count, |
| 1833 | int _pass_number) : decompose_domain(0, _c.accum->height(), | |||
| dd761b64 DH |
David Hilvert | 2008-01-26 17:41:00 +0000 | 1834 | 0, _c.accum->width()), |
| 1835 | t(_t) { | |||
| 70fb02f9 DH |
David Hilvert | 2007-09-21 03:18:00 +0000 | 1836 | |
| 1837 | asum = _asum; | |||
| 1838 | bsum = _bsum; | |||
| 1839 | c = _c; | |||
| 214d014c DH |
David Hilvert | 2008-05-04 22:08:03 -0500 | 1840 | ax_count = _ax_count; |
| 1841 | pass_number = _pass_number; | |||
| 1842 | } | |||
| 1843 | ||||
| 1844 | exposure_ratio_iterate(pixel_accum *_asum, | |||
| 1845 | pixel_accum *_bsum, | |||
| 1846 | struct scale_cluster _c, | |||
| 1847 | const transformation &_t, | |||
| 1848 | int _ax_count, | |||
| 1849 | int _pass_number, | |||
| 1850 | transformation::elem_bounds_int_t b) : decompose_domain(b.imin, b.imax, | |||
| 1851 | b.jmin, b.jmax), | |||
| 1852 | t(_t) { | |||
| 1853 | ||||
| 1854 | asum = _asum; | |||
| 1855 | bsum = _bsum; | |||
| 1856 | c = _c; | |||
| 70fb02f9 DH |
David Hilvert | 2007-09-21 03:18:00 +0000 | 1857 | ax_count = _ax_count; |
| 1858 | pass_number = _pass_number; | |||
| 1859 | } | |||
| 1860 | }; | |||
| 1861 | ||||
| 2aa417e6 | dhilvert | 2005-01-07 06:44:00 +0000 | 1862 | static void set_exposure_ratio(unsigned int m, struct scale_cluster c, |
| 993fde09 | David Hilvert | 2008-05-05 05:28:56 +0000 | 1863 | const transformation &t, int ax_count, int pass_number) { |
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 1864 | |
| 3dc20778 | dhilvert | 2005-01-10 23:06:00 +0000 | 1865 | if (_exp_register == 2) { |
| 1866 | /* | |||
| 1867 | * Use metadata only. | |||
| 1868 | */ | |||
| 1869 | ale_real gain_multiplier = image_rw::exp(m).get_gain_multiplier(); | |||
| 1870 | pixel multiplier = pixel(gain_multiplier, gain_multiplier, gain_multiplier); | |||
| 1871 | ||||
| 1872 | image_rw::exp(m).set_multiplier(multiplier); | |||
| a9d66b26 | dhilvert | 2005-01-10 23:15:00 +0000 | 1873 | ui::get()->exp_multiplier(multiplier[0], |
| 1874 | multiplier[1], | |||
| 1875 | multiplier[2]); | |||
| 3d7fd555 | dhilvert | 2005-01-10 23:10:00 +0000 | 1876 | |
| 1877 | return; | |||
| 3dc20778 | dhilvert | 2005-01-10 23:06:00 +0000 | 1878 | } |
| 1879 | ||||
| 70fb02f9 | David Hilvert | 2007-09-21 03:18:00 +0000 | 1880 | pixel_accum asum(0, 0, 0), bsum(0, 0, 0); |
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 1881 | |
| 70fb02f9 DH |
David Hilvert | 2007-09-21 03:18:00 +0000 | 1882 | exposure_ratio_iterate eri(&asum, &bsum, c, t, ax_count, pass_number); |
| 1883 | eri.run(); | |||
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 1884 | |
| 1885 | // std::cerr << (asum / bsum) << " "; | |||
| 07271611 | dhilvert | 2005-01-07 06:48:00 +0000 | 1886 | |
| 1887 | pixel_accum new_multiplier; | |||
| 1888 | ||||
| 1889 | new_multiplier = asum / bsum * image_rw::exp(m).get_multiplier(); | |||
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 1890 | |
| 07271611 | dhilvert | 2005-01-07 06:48:00 +0000 | 1891 | if (finite(new_multiplier[0]) |
| 1892 | && finite(new_multiplier[1]) | |||
| 1893 | && finite(new_multiplier[2])) { | |||
| 1894 | image_rw::exp(m).set_multiplier(new_multiplier); | |||
| 1895 | ui::get()->exp_multiplier(new_multiplier[0], | |||
| 1896 | new_multiplier[1], | |||
| 1897 | new_multiplier[2]); | |||
| 1898 | } | |||
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 1899 | } |
| 1900 | ||||
| df55d1a2 | dhilvert | 2006-08-30 07:40:00 +0000 | 1901 | /* |
| 1902 | * Copy all ax parameters. | |||
| 1903 | */ | |||
| 1904 | static exclusion *copy_ax_parameters(int local_ax_count, exclusion *source) { | |||
| 2aa417e6 | dhilvert | 2005-01-07 06:44:00 +0000 | 1905 | |
| df55d1a2 | dhilvert | 2006-08-30 07:40:00 +0000 | 1906 | exclusion *dest = (exclusion *) malloc(local_ax_count * sizeof(exclusion)); |
| 2aa417e6 | dhilvert | 2005-01-07 06:44:00 +0000 | 1907 | |
| df55d1a2 | dhilvert | 2006-08-30 07:40:00 +0000 | 1908 | assert (dest); |
| 2aa417e6 | dhilvert | 2005-01-07 06:44:00 +0000 | 1909 | |
| df55d1a2 | dhilvert | 2006-08-30 07:40:00 +0000 | 1910 | if (!dest) |
| 07271611 | dhilvert | 2005-01-07 06:48:00 +0000 | 1911 | ui::get()->memory_error("exclusion regions"); |
| 2aa417e6 | dhilvert | 2005-01-07 06:44:00 +0000 | 1912 | |
| df55d1a2 | dhilvert | 2006-08-30 07:40:00 +0000 | 1913 | for (int idx = 0; idx < local_ax_count; idx++) |
| 1914 | dest[idx] = source[idx]; | |||
| 2aa417e6 | dhilvert | 2005-01-07 06:44:00 +0000 | 1915 | |
| df55d1a2 | dhilvert | 2006-08-30 07:40:00 +0000 | 1916 | return dest; |
| 2aa417e6 | dhilvert | 2005-01-07 06:44:00 +0000 | 1917 | } |
| 1918 | ||||
| df55d1a2 | dhilvert | 2006-08-30 07:40:00 +0000 | 1919 | /* |
| 1920 | * Copy ax parameters according to frame. | |||
| 1921 | */ | |||
| 1922 | static exclusion *filter_ax_parameters(int frame, int *local_ax_count) { | |||
| 2aa417e6 | dhilvert | 2005-01-07 06:44:00 +0000 | 1923 | |
| df55d1a2 | dhilvert | 2006-08-30 07:40:00 +0000 | 1924 | exclusion *dest = (exclusion *) malloc(ax_count * sizeof(exclusion)); |
| 46f9776a | dhilvert | 2005-01-07 06:44:00 +0000 | 1925 | |
| df55d1a2 | dhilvert | 2006-08-30 07:40:00 +0000 | 1926 | assert (dest); |
| 46f9776a | dhilvert | 2005-01-07 06:44:00 +0000 | 1927 | |
| df55d1a2 | dhilvert | 2006-08-30 07:40:00 +0000 | 1928 | if (!dest) |
| 07271611 | dhilvert | 2005-01-07 06:48:00 +0000 | 1929 | ui::get()->memory_error("exclusion regions"); |
| 46f9776a | dhilvert | 2005-01-07 06:44:00 +0000 | 1930 | |
| 1931 | *local_ax_count = 0; | |||
| 1932 | ||||
| 1933 | for (int idx = 0; idx < ax_count; idx++) { | |||
| df55d1a2 | dhilvert | 2006-08-30 07:40:00 +0000 | 1934 | if (ax_parameters[idx].x[4] > frame |
| 1935 | || ax_parameters[idx].x[5] < frame) | |||
| 46f9776a | dhilvert | 2005-01-07 06:44:00 +0000 | 1936 | continue; |
| 1937 | ||||
| df55d1a2 | dhilvert | 2006-08-30 07:40:00 +0000 | 1938 | dest[*local_ax_count] = ax_parameters[idx]; |
| 46f9776a | dhilvert | 2005-01-07 06:44:00 +0000 | 1939 | |
| 1940 | (*local_ax_count)++; | |||
| 1941 | } | |||
| 1942 | ||||
| df55d1a2 | dhilvert | 2006-08-30 07:40:00 +0000 | 1943 | return dest; |
| 1944 | } | |||
| 46f9776a | dhilvert | 2005-01-07 06:44:00 +0000 | 1945 | |
| df55d1a2 | dhilvert | 2006-08-30 07:40:00 +0000 | 1946 | static void scale_ax_parameters(int local_ax_count, exclusion *ax_parameters, |
| 1947 | ale_pos ref_scale, ale_pos input_scale) { | |||
| 1948 | for (int i = 0; i < local_ax_count; i++) { | |||
| 1949 | ale_pos scale = (ax_parameters[i].type == exclusion::RENDER) | |||
| 1950 | ? ref_scale | |||
| 1951 | : input_scale; | |||
| 2aa417e6 | dhilvert | 2005-01-07 06:44:00 +0000 | 1952 | |
| df55d1a2 | dhilvert | 2006-08-30 07:40:00 +0000 | 1953 | for (int n = 0; n < 6; n++) { |
| e1eaf84c | David Hilvert | 2007-07-24 02:50:00 +0000 | 1954 | ax_parameters[i].x[n] = ax_parameters[i].x[n] * scale; |
| df55d1a2 | dhilvert | 2006-08-30 07:40:00 +0000 | 1955 | } |
| 1956 | } | |||
| 46f9776a | dhilvert | 2005-01-07 06:44:00 +0000 | 1957 | } |
| 1958 | ||||
| 2aa417e6 | dhilvert | 2005-01-07 06:44:00 +0000 | 1959 | /* |
| 1960 | * Prepare the next level of detail for ordinary images. | |||
| 1961 | */ | |||
| 1962 | static const image *prepare_lod(const image *current) { | |||
| 1963 | if (current == NULL) | |||
| 1964 | return NULL; | |||
| 46f9776a | dhilvert | 2005-01-07 06:44:00 +0000 | 1965 | |
| 2aa417e6 | dhilvert | 2005-01-07 06:44:00 +0000 | 1966 | return current->scale_by_half("prepare_lod"); |
| 1967 | } | |||
| 46f9776a | dhilvert | 2005-01-07 06:44:00 +0000 | 1968 | |
| 2aa417e6 | dhilvert | 2005-01-07 06:44:00 +0000 | 1969 | /* |
| 2aa417e6 | dhilvert | 2005-01-07 06:44:00 +0000 | 1970 | * Prepare the next level of detail for definition maps. |
| 1971 | */ | |||
| 1972 | static const image *prepare_lod_def(const image *current) { | |||
| 7a19e165 DH |
David Hilvert | 2007-09-10 21:16:00 +0000 | 1973 | if (current == NULL) |
| 1974 | return NULL; | |||
| 2aa417e6 | dhilvert | 2005-01-07 06:44:00 +0000 | 1975 | |
| 1976 | return current->defined_scale_by_half("prepare_lod_def"); | |||
| 1977 | } | |||
| 1978 | ||||
| 1979 | /* | |||
| 8f2ed1fd | David Hilvert | 2007-09-07 18:36:00 +0000 | 1980 | * Initialize scale cluster data structures. |
| 2aa417e6 | dhilvert | 2005-01-07 06:44:00 +0000 | 1981 | */ |
| 8f2ed1fd DH |
David Hilvert | 2007-09-07 18:36:00 +0000 | 1982 | |
| 1983 | static void init_nl_cluster(struct scale_cluster *sc) { | |||
| 1984 | } | |||
| 1985 | ||||
| 2aa417e6 | dhilvert | 2005-01-07 06:44:00 +0000 | 1986 | /* |
| 1987 | * Destroy the first element in the scale cluster data structure. | |||
| 1988 | */ | |||
| a85f57f9 | David Hilvert | 2007-10-15 17:07:00 +0000 | 1989 | static void final_clusters(struct scale_cluster *scale_clusters, ale_pos scale_factor, |
| 2aa417e6 | dhilvert | 2005-01-07 06:44:00 +0000 | 1990 | unsigned int steps) { |
| 1991 | ||||
| c6e3d33a | David Hilvert | 2007-10-02 15:57:00 +0000 | 1992 | if (scale_clusters[0].input_scale < 1.0) { |
| 2aa417e6 | dhilvert | 2005-01-07 06:44:00 +0000 | 1993 | delete scale_clusters[0].input; |
| c6e3d33a | David Hilvert | 2007-10-02 15:57:00 +0000 | 1994 | } |
| 2aa417e6 | dhilvert | 2005-01-07 06:44:00 +0000 | 1995 | |
| 44a1d1de DH |
David Hilvert | 2009-03-30 19:02:57 +0000 | 1996 | delete scale_clusters[0].input_certainty; |
| 1997 | ||||
| 2aa417e6 | dhilvert | 2005-01-07 06:44:00 +0000 | 1998 | free((void *)scale_clusters[0].ax_parameters); |
| 1999 | ||||
| 2000 | for (unsigned int step = 1; step < steps; step++) { | |||
| 2001 | delete scale_clusters[step].accum; | |||
| 580b5321 | David Hilvert | 2007-09-10 17:35:00 +0000 | 2002 | delete scale_clusters[step].certainty; |
| 2aa417e6 | dhilvert | 2005-01-07 06:44:00 +0000 | 2003 | delete scale_clusters[step].aweight; |
| 2004 | ||||
| c6e3d33a | David Hilvert | 2007-10-02 15:57:00 +0000 | 2005 | if (scale_clusters[step].input_scale < 1.0) { |
| 07271611 | dhilvert | 2005-01-07 06:48:00 +0000 | 2006 | delete scale_clusters[step].input; |
| c6e3d33a DH |
David Hilvert | 2007-10-02 15:57:00 +0000 | 2007 | delete scale_clusters[step].input_certainty; |
| 2008 | } | |||
| 07271611 | dhilvert | 2005-01-07 06:48:00 +0000 | 2009 | |
| 2aa417e6 | dhilvert | 2005-01-07 06:44:00 +0000 | 2010 | free((void *)scale_clusters[step].ax_parameters); |
| 2011 | } | |||
| 2012 | ||||
| 2013 | free(scale_clusters); | |||
| 46f9776a | dhilvert | 2005-01-07 06:44:00 +0000 | 2014 | } |
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 2015 | |
| 2016 | /* | |||
| c052e200 | dhilvert | 2005-05-08 16:16:00 +0000 | 2017 | * Calculate the centroid of a control point for the set of frames |
| 2018 | * having index lower than m. Divide by any scaling of the output. | |||
| 2019 | */ | |||
| 2020 | static point unscaled_centroid(unsigned int m, unsigned int p) { | |||
| 2021 | assert(_keep); | |||
| 2022 | ||||
| 2023 | point point_sum(0, 0); | |||
| 2024 | ale_accum divisor = 0; | |||
| 2025 | ||||
| 2026 | for(unsigned int j = 0; j < m; j++) { | |||
| 2027 | point pp = cp_array[p][j]; | |||
| 2028 | ||||
| 2029 | if (pp.defined()) { | |||
| 2030 | point_sum += kept_t[j].transform_unscaled(pp) | |||
| 2031 | / kept_t[j].scale(); | |||
| 2032 | divisor += 1; | |||
| 2033 | } | |||
| 2034 | } | |||
| 2035 | ||||
| 2036 | if (divisor == 0) | |||
| 2037 | return point::undefined(); | |||
| 2038 | ||||
| 2039 | return point_sum / divisor; | |||
| 2040 | } | |||
| 2041 | ||||
| 2042 | /* | |||
| 2043 | * Calculate centroid of this frame, and of all previous frames, | |||
| 2044 | * from points common to both sets. | |||
| 2045 | */ | |||
| 2046 | static void centroids(unsigned int m, point *current, point *previous) { | |||
| 2047 | /* | |||
| 2048 | * Calculate the translation | |||
| 2049 | */ | |||
| 2050 | point other_centroid(0, 0); | |||
| 2051 | point this_centroid(0, 0); | |||
| 2052 | ale_pos divisor = 0; | |||
| 2053 | ||||
| 2054 | for (unsigned int i = 0; i < cp_count; i++) { | |||
| 2055 | point other_c = unscaled_centroid(m, i); | |||
| 2056 | point this_c = cp_array[i][m]; | |||
| 2057 | ||||
| 2058 | if (!other_c.defined() || !this_c.defined()) | |||
| 2059 | continue; | |||
| 2060 | ||||
| 2061 | other_centroid += other_c; | |||
| 2062 | this_centroid += this_c; | |||
| 2063 | divisor += 1; | |||
| 2064 | ||||
| 2065 | } | |||
| 2066 | ||||
| 2067 | if (divisor == 0) { | |||
| 2068 | *current = point::undefined(); | |||
| 2069 | *previous = point::undefined(); | |||
| 2070 | return; | |||
| 2071 | } | |||
| 2072 | ||||
| 2073 | *current = this_centroid / divisor; | |||
| 2074 | *previous = other_centroid / divisor; | |||
| 2075 | } | |||
| 2076 | ||||
| 2077 | /* | |||
| b386e644 | dhilvert | 2005-04-30 09:28:00 +0000 | 2078 | * Calculate the RMS error of control points for frame m, with |
| 2079 | * transformation t, against control points for earlier frames. | |||
| 2080 | */ | |||
| e812ee44 | David Hilvert | 2007-10-18 18:24:00 +0000 | 2081 | static ale_pos cp_rms_error(unsigned int m, transformation t) { |
| b386e644 | dhilvert | 2005-04-30 09:28:00 +0000 | 2082 | assert (_keep); |
| 2083 | ||||
| 2084 | ale_accum err = 0; | |||
| 2085 | ale_accum divisor = 0; | |||
| 2086 | ||||
| 2087 | for (unsigned int i = 0; i < cp_count; i++) | |||
| 2088 | for (unsigned int j = 0; j < m; j++) { | |||
| 2089 | const point *p = cp_array[i]; | |||
| 936ff6ec | dhilvert | 2005-05-07 20:02:00 +0000 | 2090 | point p_ref = kept_t[j].transform_unscaled(p[j]); |
| 2091 | point p_cur = t.transform_unscaled(p[m]); | |||
| b386e644 | dhilvert | 2005-04-30 09:28:00 +0000 | 2092 | |
| 2093 | if (!p_ref.defined() || !p_cur.defined()) | |||
| 2094 | continue; | |||
| 2095 | ||||
| 2096 | err += p_ref.lengthtosq(p_cur); | |||
| 2097 | divisor += 1; | |||
| 2098 | } | |||
| 2099 | ||||
| e812ee44 | David Hilvert | 2007-10-18 18:24:00 +0000 | 2100 | return (ale_pos) sqrt(err / divisor); |
| b386e644 | dhilvert | 2005-04-30 09:28:00 +0000 | 2101 | } |
| 2102 | ||||
| 6126fb3c | David Hilvert | 2007-04-03 08:15:00 +0000 | 2103 | |
| 59e5857b | David Hilvert | 2007-05-08 12:15:00 +0000 | 2104 | static void test_global(diff_stat_t *here, scale_cluster si, transformation t, |
| a7882498 | David Hilvert | 2007-10-16 19:48:00 +0000 | 2105 | int local_ax_count, int m, ale_accum local_gs_mo, ale_pos perturb) { |
| 59e5857b DH |
David Hilvert | 2007-05-08 12:15:00 +0000 | 2106 | |
| 2107 | diff_stat_t test(*here); | |||
| 2108 | ||||
| 1b980378 | David Hilvert | 2008-07-18 18:30:40 +0000 | 2109 | test.diff(si, t.get_current_element(), local_ax_count, m); |
| 59e5857b DH |
David Hilvert | 2007-05-08 12:15:00 +0000 | 2110 | |
| 2111 | unsigned int ovl = overlap(si, t, local_ax_count); | |||
| 2112 | ||||
| 2113 | if (ovl >= local_gs_mo && test.better()) { | |||
| 2114 | test.confirm(); | |||
| 2115 | *here = test; | |||
| 2116 | ui::get()->set_match(here->get_error()); | |||
| 2117 | ui::get()->set_offset(here->get_offset()); | |||
| 2118 | } else { | |||
| 2119 | test.discard(); | |||
| 2120 | } | |||
| 2121 | ||||
| 2122 | } | |||
| 2123 | ||||
| f2d60fe2 | David Hilvert | 2007-04-13 23:28:00 +0000 | 2124 | /* |
| eb01b1b8 | David Hilvert | 2007-04-24 05:36:00 +0000 | 2125 | * Get the set of global transformations for a given density |
| f2d60fe2 | David Hilvert | 2007-04-13 23:28:00 +0000 | 2126 | */ |
| 59e5857b DH |
David Hilvert | 2007-05-08 12:15:00 +0000 | 2127 | static void test_globals(diff_stat_t *here, |
| 2128 | scale_cluster si, transformation t, int local_gs, ale_pos adj_p, | |||
| a7882498 | David Hilvert | 2007-10-16 19:48:00 +0000 | 2129 | int local_ax_count, int m, ale_accum local_gs_mo, ale_pos perturb) { |
| f2d60fe2 | David Hilvert | 2007-04-13 23:28:00 +0000 | 2130 | |
| eb01b1b8 | David Hilvert | 2007-04-24 05:36:00 +0000 | 2131 | transformation offset = t; |
| f2d60fe2 | David Hilvert | 2007-04-13 23:28:00 +0000 | 2132 | |
| eb01b1b8 | David Hilvert | 2007-04-24 05:36:00 +0000 | 2133 | point min, max; |
| f2d60fe2 | David Hilvert | 2007-04-13 23:28:00 +0000 | 2134 | |
| eb01b1b8 | David Hilvert | 2007-04-24 05:36:00 +0000 | 2135 | transformation offset_p = offset; |
| f2d60fe2 | David Hilvert | 2007-04-13 23:28:00 +0000 | 2136 | |
| eb01b1b8 DH |
David Hilvert | 2007-04-24 05:36:00 +0000 | 2137 | if (!offset_p.is_projective()) |
| 2138 | offset_p.eu_to_gpt(); | |||
| f2d60fe2 | David Hilvert | 2007-04-13 23:28:00 +0000 | 2139 | |
| eb01b1b8 DH |
David Hilvert | 2007-04-24 05:36:00 +0000 | 2140 | min = max = offset_p.gpt_get(0); |
| 2141 | for (int p_index = 1; p_index < 4; p_index++) { | |||
| 2142 | point p = offset_p.gpt_get(p_index); | |||
| 2143 | if (p[0] < min[0]) | |||
| 2144 | min[0] = p[0]; | |||
| 2145 | if (p[1] < min[1]) | |||
| 2146 | min[1] = p[1]; | |||
| 2147 | if (p[0] > max[0]) | |||
| 2148 | max[0] = p[0]; | |||
| 2149 | if (p[1] > max[1]) | |||
| 2150 | max[1] = p[1]; | |||
| 2151 | } | |||
| f2d60fe2 | David Hilvert | 2007-04-13 23:28:00 +0000 | 2152 | |
| eb01b1b8 DH |
David Hilvert | 2007-04-24 05:36:00 +0000 | 2153 | point inner_min_t = -min; |
| 2154 | point inner_max_t = -max + point(si.accum->height(), si.accum->width()); | |||
| 2155 | point outer_min_t = -max + point(adj_p - 1, adj_p - 1); | |||
| 2156 | point outer_max_t = point(si.accum->height(), si.accum->width()) - point(adj_p, adj_p); | |||
| f2d60fe2 | David Hilvert | 2007-04-13 23:28:00 +0000 | 2157 | |
| 02eb92ab | David Hilvert | 2007-05-08 07:11:00 +0000 | 2158 | if (local_gs == 1 || local_gs == 3 || local_gs == 4 || local_gs == 6) { |
| f2d60fe2 | David Hilvert | 2007-04-13 23:28:00 +0000 | 2159 | |
| eb01b1b8 DH |
David Hilvert | 2007-04-24 05:36:00 +0000 | 2160 | /* |
| 2161 | * Inner | |||
| 2162 | */ | |||
| f2d60fe2 | David Hilvert | 2007-04-13 23:28:00 +0000 | 2163 | |
| eb01b1b8 DH |
David Hilvert | 2007-04-24 05:36:00 +0000 | 2164 | for (ale_pos i = inner_min_t[0]; i <= inner_max_t[0]; i += adj_p) |
| 2165 | for (ale_pos j = inner_min_t[1]; j <= inner_max_t[1]; j += adj_p) { | |||
| 2166 | transformation test_t = offset; | |||
| 2167 | test_t.translate(point(i, j)); | |||
| afa6d8f6 | David Hilvert | 2007-05-13 03:19:00 +0000 | 2168 | test_global(here, si, test_t, local_ax_count, m, local_gs_mo, perturb); |
| eb01b1b8 DH |
David Hilvert | 2007-04-24 05:36:00 +0000 | 2169 | } |
| 2170 | } | |||
| 2171 | ||||
| 02eb92ab | David Hilvert | 2007-05-08 07:11:00 +0000 | 2172 | if (local_gs == 2 || local_gs == 3 || local_gs == -1 || local_gs == 6) { |
| f2d60fe2 | David Hilvert | 2007-04-13 23:28:00 +0000 | 2173 | |
| eb01b1b8 DH |
David Hilvert | 2007-04-24 05:36:00 +0000 | 2174 | /* |
| 2175 | * Outer | |||
| 2176 | */ | |||
| 2177 | ||||
| 2178 | for (ale_pos i = outer_min_t[0]; i <= outer_max_t[0]; i += adj_p) | |||
| 2179 | for (ale_pos j = outer_min_t[1]; j < inner_min_t[1]; j += adj_p) { | |||
| 2180 | transformation test_t = offset; | |||
| 2181 | test_t.translate(point(i, j)); | |||
| afa6d8f6 | David Hilvert | 2007-05-13 03:19:00 +0000 | 2182 | test_global(here, si, test_t, local_ax_count, m, local_gs_mo, perturb); |
| eb01b1b8 DH |
David Hilvert | 2007-04-24 05:36:00 +0000 | 2183 | } |
| 2184 | for (ale_pos i = outer_min_t[0]; i <= outer_max_t[0]; i += adj_p) | |||
| 2185 | for (ale_pos j = outer_max_t[1]; j > inner_max_t[1]; j -= adj_p) { | |||
| 2186 | transformation test_t = offset; | |||
| 2187 | test_t.translate(point(i, j)); | |||
| afa6d8f6 | David Hilvert | 2007-05-13 03:19:00 +0000 | 2188 | test_global(here, si, test_t, local_ax_count, m, local_gs_mo, perturb); |
| eb01b1b8 DH |
David Hilvert | 2007-04-24 05:36:00 +0000 | 2189 | } |
| 2190 | for (ale_pos i = outer_min_t[0]; i < inner_min_t[0]; i += adj_p) | |||
| 2191 | for (ale_pos j = outer_min_t[1]; j <= outer_max_t[1]; j += adj_p) { | |||
| 2192 | transformation test_t = offset; | |||
| 2193 | test_t.translate(point(i, j)); | |||
| afa6d8f6 | David Hilvert | 2007-05-13 03:19:00 +0000 | 2194 | test_global(here, si, test_t, local_ax_count, m, local_gs_mo, perturb); |
| eb01b1b8 DH |
David Hilvert | 2007-04-24 05:36:00 +0000 | 2195 | } |
| 2196 | for (ale_pos i = outer_max_t[0]; i > inner_max_t[0]; i -= adj_p) | |||
| 2197 | for (ale_pos j = outer_min_t[1]; j <= outer_max_t[1]; j += adj_p) { | |||
| 2198 | transformation test_t = offset; | |||
| 2199 | test_t.translate(point(i, j)); | |||
| afa6d8f6 | David Hilvert | 2007-05-13 03:19:00 +0000 | 2200 | test_global(here, si, test_t, local_ax_count, m, local_gs_mo, perturb); |
| f2d60fe2 DH |
David Hilvert | 2007-04-13 23:28:00 +0000 | 2201 | } |
| 2202 | } | |||
| cc71efb2 DH |
David Hilvert | 2007-04-08 16:37:00 +0000 | 2203 | } |
| 2204 | ||||
| a9e10df7 DH |
David Hilvert | 2007-04-25 12:39:00 +0000 | 2205 | static void get_translational_set(std::vector<transformation> *set, |
| 2206 | transformation t, ale_pos adj_p) { | |||
| 2207 | ||||
| 2208 | ale_pos adj_s; | |||
| 2209 | ||||
| 2210 | transformation offset = t; | |||
| dd761b64 | David Hilvert | 2008-01-26 17:41:00 +0000 | 2211 | transformation test_t(transformation::eu_identity()); |
| a9e10df7 DH |
David Hilvert | 2007-04-25 12:39:00 +0000 | 2212 | |
| 2213 | for (int i = 0; i < 2; i++) | |||
| 2214 | for (adj_s = -adj_p; adj_s <= adj_p; adj_s += 2 * adj_p) { | |||
| 2215 | ||||
| 2216 | test_t = offset; | |||
| 2217 | ||||
| 5b7749b0 | David Hilvert | 2007-04-26 04:36:00 +0000 | 2218 | test_t.translate(i ? point(adj_s, 0) : point(0, adj_s)); |
| a9e10df7 DH |
David Hilvert | 2007-04-25 12:39:00 +0000 | 2219 | |
| 2220 | set->push_back(test_t); | |||
| 2221 | } | |||
| 2222 | } | |||
| 2223 | ||||
| cd621c15 | David Hilvert | 2007-07-23 20:38:00 +0000 | 2224 | static int threshold_ok(ale_accum error) { |
| 34add5e1 | David Hilvert | 2007-10-17 21:47:00 +0000 | 2225 | if ((1 - error) * (ale_accum) 100 >= match_threshold) |
| cd621c15 DH |
David Hilvert | 2007-07-23 20:38:00 +0000 | 2226 | return 1; |
| 2227 | ||||
| 2228 | if (!(match_threshold >= 0)) | |||
| 2229 | return 1; | |||
| 2230 | ||||
| 2231 | return 0; | |||
| 2232 | } | |||
| 228e156a DH |
David Hilvert | 2007-04-22 23:17:00 +0000 | 2233 | |
| 2234 | /* | |||
| 3fa727c5 DH |
David Hilvert | 2008-04-26 00:41:37 +0000 | 2235 | * Check for satisfaction of the certainty threshold. |
| 2236 | */ | |||
| 2237 | static int ma_cert_satisfied(const scale_cluster &c, const transformation &t, unsigned int i) { | |||
| d790f7da | David Hilvert | 2008-05-02 18:59:43 -0500 | 2238 | transformation::elem_bounds_int_t b = t.elem_bounds().scale_to_bounds(c.accum->height(), c.accum->width()); |
| 3fa727c5 DH |
David Hilvert | 2008-04-26 00:41:37 +0000 | 2239 | ale_accum sum[3] = {0, 0, 0}; |
| 2240 | ||||
| d790f7da DH |
David Hilvert | 2008-05-02 18:59:43 -0500 | 2241 | for (unsigned int ii = b.imin; ii < b.imax; ii++) |
| 2242 | for (unsigned int jj = b.jmin; jj < b.jmax; jj++) { | |||
| 3fa727c5 DH |
David Hilvert | 2008-04-26 00:41:37 +0000 | 2243 | pixel p = c.accum->get_pixel(ii, jj); |
| 2244 | sum[0] += p[0]; | |||
| 2245 | sum[1] += p[1]; | |||
| 2246 | sum[2] += p[2]; | |||
| 2247 | } | |||
| 2248 | ||||
| d790f7da | David Hilvert | 2008-05-02 18:59:43 -0500 | 2249 | unsigned int count = (b.jmax - b.jmin) * (b.imax - b.imin); |
| 3fa727c5 DH |
David Hilvert | 2008-04-26 00:41:37 +0000 | 2250 | |
| 2251 | for (int k = 0; k < 3; k++) | |||
| 2252 | if (sum[k] / count < _ma_cert) | |||
| 2253 | return 0; | |||
| 2254 | ||||
| 2255 | return 1; | |||
| 2256 | } | |||
| 2257 | ||||
| 1b980378 DH |
David Hilvert | 2008-07-18 18:30:40 +0000 | 2258 | static diff_stat_t check_ancestor_path(const trans_multi &offset, const scale_cluster &si, diff_stat_t here, int local_ax_count, int frame) { |
| 2259 | ||||
| 2260 | if (offset.get_current_index() > 0) | |||
| 2261 | for (int i = offset.parent_index(offset.get_current_index()); i >= 0; i = (i > 0) ? (int) offset.parent_index(i) : -1) { | |||
| 2262 | trans_single t = offset.get_element(i); | |||
| 2263 | t.rescale(offset.get_current_element().scale()); | |||
| 2264 | ||||
| 2265 | here.diff(si, t, local_ax_count, frame); | |||
| 2266 | ||||
| e0577945 | David Hilvert | 2008-07-19 22:11:55 +0000 | 2267 | if (here.better_defined()) |
| 1b980378 DH |
David Hilvert | 2008-07-18 18:30:40 +0000 | 2268 | here.confirm(); |
| 2269 | else | |||
| 2270 | here.discard(); | |||
| 2271 | } | |||
| 2272 | ||||
| 2273 | return here; | |||
| 2274 | } | |||
| 2275 | ||||
| 46f9776a | dhilvert | 2005-01-07 06:44:00 +0000 | 2276 | #ifdef USE_FFTW |
| 2277 | /* | |||
| 2278 | * High-pass filter for frequency weights | |||
| 2279 | */ | |||
| 2280 | static void hipass(int rows, int cols, fftw_complex *inout) { | |||
| 2281 | for (int i = 0; i < rows * vert_freq_cut; i++) | |||
| 2282 | for (int j = 0; j < cols; j++) | |||
| 2283 | for (int k = 0; k < 2; k++) | |||
| 2284 | inout[i * cols + j][k] = 0; | |||
| 2285 | for (int i = 0; i < rows; i++) | |||
| 2286 | for (int j = 0; j < cols * horiz_freq_cut; j++) | |||
| 2287 | for (int k = 0; k < 2; k++) | |||
| 2288 | inout[i * cols + j][k] = 0; | |||
| 2289 | for (int i = 0; i < rows; i++) | |||
| 2290 | for (int j = 0; j < cols; j++) | |||
| 2291 | for (int k = 0; k < 2; k++) | |||
| 2292 | if (i / (double) rows + j / (double) cols < 2 * avg_freq_cut) | |||
| 2293 | inout[i * cols + j][k] = 0; | |||
| 2294 | } | |||
| 2295 | #endif | |||
| 2296 | ||||
| 2aa417e6 | dhilvert | 2005-01-07 06:44:00 +0000 | 2297 | |
| 2298 | /* | |||
| 2299 | * Reset alignment weights | |||
| 2300 | */ | |||
| 2301 | static void reset_weights() { | |||
| 07271611 | dhilvert | 2005-01-07 06:48:00 +0000 | 2302 | if (alignment_weights != NULL) |
| c2d1a70e | David Hilvert | 2009-05-30 15:37:04 +0000 | 2303 | ale_image_release(alignment_weights); |
| 07271611 | dhilvert | 2005-01-07 06:48:00 +0000 | 2304 | |
| 2305 | alignment_weights = NULL; | |||
| 2aa417e6 | dhilvert | 2005-01-07 06:44:00 +0000 | 2306 | } |
| 2307 | ||||
| 2308 | /* | |||
| 2309 | * Initialize alignment weights | |||
| 2310 | */ | |||
| 2311 | static void init_weights() { | |||
| 2312 | if (alignment_weights != NULL) | |||
| 2313 | return; | |||
| 2314 | ||||
| 3617b771 | David Hilvert | 2009-05-31 15:07:14 +0000 | 2315 | alignment_weights = ale_new_image(accel::context(), ALE_IMAGE_RGB, ale_image_get_type(reference_image)); |
| 2aa417e6 | dhilvert | 2005-01-07 06:44:00 +0000 | 2316 | |
| 2317 | assert (alignment_weights); | |||
| 2318 | ||||
| e28f8ff7 | David Hilvert | 2009-06-02 22:23:44 +0000 | 2319 | assert (!ale_resize_image(alignment_weights, 0, 0, ale_image_get_width(reference_image), ale_image_get_height(reference_image))); |
| 3617b771 | David Hilvert | 2009-05-31 15:07:14 +0000 | 2320 | |
| bccf19c9 | David Hilvert | 2009-07-18 19:30:20 +0000 | 2321 | ale_eval("MAP_PIXEL(%0I, p, PIXEL(1, 1, 1))", alignment_weights); |
| 2aa417e6 | dhilvert | 2005-01-07 06:44:00 +0000 | 2322 | } |
| 2323 | ||||
| 46f9776a | dhilvert | 2005-01-07 06:44:00 +0000 | 2324 | /* |
| 2aa417e6 | dhilvert | 2005-01-07 06:44:00 +0000 | 2325 | * Update alignment weights with weight map |
| 2326 | */ | |||
| 2327 | static void map_update() { | |||
| 2328 | ||||
| 2329 | if (weight_map == NULL) | |||
| 2330 | return; | |||
| 2331 | ||||
| 2332 | init_weights(); | |||
| 2333 | ||||
| bccf19c9 | David Hilvert | 2009-07-18 19:30:20 +0000 | 2334 | ale_eval("MAP_PIXEL(%0I, p, GET_PIXEL(%0i, p) * GET_PIXEL_BG(%1i, p))", alignment_weights, weight_map); |
| 2aa417e6 | dhilvert | 2005-01-07 06:44:00 +0000 | 2335 | } |
| 2336 | ||||
| 2337 | /* | |||
| 2338 | * Update alignment weights with algorithmic weights | |||
| 46f9776a | dhilvert | 2005-01-07 06:44:00 +0000 | 2339 | */ |
| 2340 | static void wmx_update() { | |||
| 2341 | #ifdef USE_UNIX | |||
| 2342 | ||||
| 2343 | static exposure *exp_def = new exposure_default(); | |||
| 2344 | static exposure *exp_bool = new exposure_boolean(); | |||
| 2345 | ||||
| 46f9776a | dhilvert | 2005-01-07 06:44:00 +0000 | 2346 | if (wmx_file == NULL || wmx_exec == NULL || wmx_defs == NULL) |
| 2347 | return; | |||
| 2348 | ||||
| 33a3cc28 DH |
David Hilvert | 2009-06-03 19:51:46 +0000 | 2349 | unsigned int rows = ale_image_get_height(reference_image); |
| 2350 | unsigned int cols = ale_image_get_width(reference_image); | |||
| 46f9776a | dhilvert | 2005-01-07 06:44:00 +0000 | 2351 | |
| 2352 | image_rw::write_image(wmx_file, reference_image); | |||
| ac4577d5 | David Hilvert | 2009-06-14 19:02:25 +0000 | 2353 | image_rw::write_image(wmx_defs, reference_image, exp_bool->get_gamma(), 0, 0, 1); |
| 46f9776a | dhilvert | 2005-01-07 06:44:00 +0000 | 2354 | |
| 2355 | /* execute ... */ | |||
| 2356 | int exit_status = 1; | |||
| 2357 | if (!fork()) { | |||
| 2358 | execlp(wmx_exec, wmx_exec, wmx_file, wmx_defs, NULL); | |||
| 07271611 | dhilvert | 2005-01-07 06:48:00 +0000 | 2359 | ui::get()->exec_failure(wmx_exec, wmx_file, wmx_defs); |
| 46f9776a | dhilvert | 2005-01-07 06:44:00 +0000 | 2360 | } |
| 2361 | ||||
| 2362 | wait(&exit_status); | |||
| 2363 | ||||
| 07271611 | dhilvert | 2005-01-07 06:48:00 +0000 | 2364 | if (exit_status) |
| 2365 | ui::get()->fork_failure("d2::align"); | |||
| 46f9776a | dhilvert | 2005-01-07 06:44:00 +0000 | 2366 | |
| c2d1a70e | David Hilvert | 2009-05-30 15:37:04 +0000 | 2367 | ale_image wmx_weights = image_rw::read_image(wmx_file, exp_def); |
| 46f9776a | dhilvert | 2005-01-07 06:44:00 +0000 | 2368 | |
| 35c1c6e3 DH |
David Hilvert | 2009-06-14 21:17:14 +0000 | 2369 | ale_image_set_x_offset(wmx_weights, ale_image_get_x_offset(reference_image)); |
| 2370 | ale_image_set_y_offset(wmx_weights, ale_image_get_y_offset(reference_image)); | |||
| 5073b97e | David Hilvert | 2009-06-14 20:59:56 +0000 | 2371 | |
| c2d1a70e | David Hilvert | 2009-05-30 15:37:04 +0000 | 2372 | if (ale_image_get_height(wmx_weights) != rows || ale_image_get_width(wmx_weights) != cols) |
| 07271611 | dhilvert | 2005-01-07 06:48:00 +0000 | 2373 | ui::get()->error("algorithmic weighting must not change image size"); |
| 2aa417e6 | dhilvert | 2005-01-07 06:44:00 +0000 | 2374 | |
| 2375 | if (alignment_weights == NULL) | |||
| 2376 | alignment_weights = wmx_weights; | |||
| 2377 | else | |||
| bccf19c9 | David Hilvert | 2009-07-18 19:30:20 +0000 | 2378 | ale_eval("MAP_PIXEL(%0I, p, GET_PIXEL(%0i, p) * GET_PIXEL(%1i, p))", alignment_weights, wmx_weights); |
| 46f9776a | dhilvert | 2005-01-07 06:44:00 +0000 | 2379 | #endif |
| 2380 | } | |||
| 2381 | ||||
| 2382 | /* | |||
| 2aa417e6 | dhilvert | 2005-01-07 06:44:00 +0000 | 2383 | * Update alignment weights with frequency weights |
| 46f9776a | dhilvert | 2005-01-07 06:44:00 +0000 | 2384 | */ |
| 2385 | static void fw_update() { | |||
| 2386 | #ifdef USE_FFTW | |||
| 46f9776a | dhilvert | 2005-01-07 06:44:00 +0000 | 2387 | if (horiz_freq_cut == 0 |
| 2388 | && vert_freq_cut == 0 | |||
| 2389 | && avg_freq_cut == 0) | |||
| 2390 | return; | |||
| 2391 | ||||
| 2aa417e6 | dhilvert | 2005-01-07 06:44:00 +0000 | 2392 | /* |
| 2393 | * Required for correct operation of --fwshow | |||
| 2394 | */ | |||
| 2395 | ||||
| 2396 | assert (alignment_weights == NULL); | |||
| 2397 | ||||
| 46f9776a | dhilvert | 2005-01-07 06:44:00 +0000 | 2398 | int rows = reference_image->height(); |
| 2399 | int cols = reference_image->width(); | |||
| 2aa417e6 | dhilvert | 2005-01-07 06:44:00 +0000 | 2400 | int colors = reference_image->depth(); |
| 46f9776a | dhilvert | 2005-01-07 06:44:00 +0000 | 2401 | |
| 7cc12274 | David Hilvert | 2007-12-11 04:50:00 +0000 | 2402 | alignment_weights = new_image_ale_real(rows, cols, |
| 2aa417e6 | dhilvert | 2005-01-07 06:44:00 +0000 | 2403 | colors, "alignment_weights"); |
| 46f9776a | dhilvert | 2005-01-07 06:44:00 +0000 | 2404 | |
| 2405 | fftw_complex *inout = (fftw_complex *) fftw_malloc(sizeof(fftw_complex) * rows * cols); | |||
| 2406 | ||||
| 2407 | assert (inout); | |||
| 2408 | ||||
| 2409 | fftw_plan pf = fftw_plan_dft_2d(rows, cols, | |||
| 2410 | inout, inout, | |||
| 2411 | FFTW_FORWARD, FFTW_ESTIMATE); | |||
| 2412 | ||||
| 2413 | fftw_plan pb = fftw_plan_dft_2d(rows, cols, | |||
| 2414 | inout, inout, | |||
| 2415 | FFTW_BACKWARD, FFTW_ESTIMATE); | |||
| 2416 | ||||
| 2aa417e6 | dhilvert | 2005-01-07 06:44:00 +0000 | 2417 | for (int k = 0; k < colors; k++) { |
| 46f9776a | dhilvert | 2005-01-07 06:44:00 +0000 | 2418 | for (int i = 0; i < rows * cols; i++) { |
| 2419 | inout[i][0] = reference_image->get_pixel(i / cols, i % cols)[k]; | |||
| 2420 | inout[i][1] = 0; | |||
| 2421 | } | |||
| 2422 | ||||
| 2423 | fftw_execute(pf); | |||
| 2424 | hipass(rows, cols, inout); | |||
| 2425 | fftw_execute(pb); | |||
| 2426 | ||||
| 2427 | for (int i = 0; i < rows * cols; i++) { | |||
| 2428 | #if 0 | |||
| 2aa417e6 | dhilvert | 2005-01-07 06:44:00 +0000 | 2429 | alignment_weights->pix(i / cols, i % cols)[k] = fabs(inout[i][0] / (rows * cols)); |
| 46f9776a | dhilvert | 2005-01-07 06:44:00 +0000 | 2430 | #else |
| e4e7ac02 | David Hilvert | 2007-12-12 23:20:00 +0000 | 2431 | alignment_weights->set_chan(i / cols, i % cols, k, |
| 46f9776a | dhilvert | 2005-01-07 06:44:00 +0000 | 2432 | sqrt(pow(inout[i][0] / (rows * cols), 2) |
| e4e7ac02 | David Hilvert | 2007-12-12 23:20:00 +0000 | 2433 | + pow(inout[i][1] / (rows * cols), 2))); |
| 46f9776a | dhilvert | 2005-01-07 06:44:00 +0000 | 2434 | #endif |
| 2435 | } | |||
| 2436 | } | |||
| 2437 | ||||
| 2438 | fftw_destroy_plan(pf); | |||
| 2439 | fftw_destroy_plan(pb); | |||
| 2440 | fftw_free(inout); | |||
| 2441 | ||||
| 2442 | if (fw_output != NULL) | |||
| 2aa417e6 | dhilvert | 2005-01-07 06:44:00 +0000 | 2443 | image_rw::write_image(fw_output, alignment_weights); |
| 46f9776a | dhilvert | 2005-01-07 06:44:00 +0000 | 2444 | #endif |
| 2445 | } | |||
| 2446 | ||||
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 2447 | /* |
| 2448 | * Update alignment to frame N. | |||
| 2449 | */ | |||
| 2450 | static void update_to(int n) { | |||
| 0e4ec247 | David Hilvert | 2007-03-13 04:51:00 +0000 | 2451 | |
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 2452 | assert (n <= latest + 1); |
| 0a432b63 | David Hilvert | 2007-03-13 08:03:00 +0000 | 2453 | assert (n >= 0); |
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 2454 | |
| f922c1c4 | David Hilvert | 2009-06-22 00:04:12 +0000 | 2455 | ale_align_properties astate = align_properties(); |
| f65325e3 | David Hilvert | 2007-03-15 06:24:00 +0000 | 2456 | |
| 724b1c71 DH |
David Hilvert | 2008-07-01 15:20:56 +0000 | 2457 | ui::get()->set_frame_num(n); |
| 2458 | ||||
| 46f9776a | dhilvert | 2005-01-07 06:44:00 +0000 | 2459 | if (latest < 0) { |
| 0a432b63 DH |
David Hilvert | 2007-03-13 08:03:00 +0000 | 2460 | |
| 2461 | /* | |||
| 2462 | * Handle the initial frame | |||
| 2463 | */ | |||
| 2464 | ||||
| 8aaa492c DH |
David Hilvert | 2009-06-28 01:59:05 +0000 | 2465 | astate.set_input_frame(image_rw::open(n)); |
| 2466 | ||||
| 2467 | const image *i = astate.get_input_frame(); | |||
| 46f9776a | dhilvert | 2005-01-07 06:44:00 +0000 | 2468 | int is_default; |
| 2469 | transformation result = alignment_class == 2 | |||
| 2470 | ? transformation::gpt_identity(i, scale_factor) | |||
| 2471 | : transformation::eu_identity(i, scale_factor); | |||
| 2472 | result = tload_first(tload, alignment_class == 2, result, &is_default); | |||
| 2473 | tsave_first(tsave, result, alignment_class == 2); | |||
| 46f9776a | dhilvert | 2005-01-07 06:44:00 +0000 | 2474 | |
| 2475 | if (_keep > 0) { | |||
| dd761b64 | David Hilvert | 2008-01-26 17:41:00 +0000 | 2476 | kept_t = transformation::new_eu_identity_array(image_rw::count()); |
| 46f9776a | dhilvert | 2005-01-07 06:44:00 +0000 | 2477 | kept_ok = (int *) malloc(image_rw::count() |
| 2478 | * sizeof(int)); | |||
| 2479 | assert (kept_t); | |||
| 2480 | assert (kept_ok); | |||
| 2481 | ||||
| 07271611 | dhilvert | 2005-01-07 06:48:00 +0000 | 2482 | if (!kept_t || !kept_ok) |
| 2483 | ui::get()->memory_error("alignment"); | |||
| 46f9776a | dhilvert | 2005-01-07 06:44:00 +0000 | 2484 | |
| 2485 | kept_ok[0] = 1; | |||
| 2486 | kept_t[0] = result; | |||
| 2487 | } | |||
| 2488 | ||||
| 2489 | latest = 0; | |||
| 2490 | latest_ok = 1; | |||
| 2491 | latest_t = result; | |||
| 2492 | ||||
| 0467efe3 | David Hilvert | 2008-04-09 21:14:38 +0000 | 2493 | astate.set_default(result); |
| 46f9776a | dhilvert | 2005-01-07 06:44:00 +0000 | 2494 | orig_t = result; |
| 0a432b63 DH |
David Hilvert | 2007-03-13 08:03:00 +0000 | 2495 | |
| 2496 | image_rw::close(n); | |||
| 46f9776a | dhilvert | 2005-01-07 06:44:00 +0000 | 2497 | } |
| 2498 | ||||
| bbc7699c | David Hilvert | 2007-04-02 03:05:00 +0000 | 2499 | for (int i = latest + 1; i <= n; i++) { |
| 0a432b63 DH |
David Hilvert | 2007-03-13 08:03:00 +0000 | 2500 | |
| 2501 | /* | |||
| 2502 | * Handle supplemental frames. | |||
| 2503 | */ | |||
| 2504 | ||||
| 46f9776a | dhilvert | 2005-01-07 06:44:00 +0000 | 2505 | assert (reference != NULL); |
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 2506 | |
| 07271611 | dhilvert | 2005-01-07 06:48:00 +0000 | 2507 | ui::get()->set_arender_current(); |
| 46f9776a | dhilvert | 2005-01-07 06:44:00 +0000 | 2508 | reference->sync(i - 1); |
| 07271611 | dhilvert | 2005-01-07 06:48:00 +0000 | 2509 | ui::get()->clear_arender_current(); |
| 46f9776a | dhilvert | 2005-01-07 06:44:00 +0000 | 2510 | reference_image = reference->get_image(); |
| 2511 | reference_defined = reference->get_defined(); | |||
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 2512 | |
| f2fc9b99 | David Hilvert | 2008-02-14 17:35:00 +0000 | 2513 | if (i == 1) |
| 0467efe3 | David Hilvert | 2008-04-09 21:14:38 +0000 | 2514 | astate.default_set_original_bounds(reference_image); |
| f2fc9b99 | David Hilvert | 2008-02-14 17:35:00 +0000 | 2515 | |
| 2aa417e6 | dhilvert | 2005-01-07 06:44:00 +0000 | 2516 | reset_weights(); |
| 46f9776a | dhilvert | 2005-01-07 06:44:00 +0000 | 2517 | fw_update(); |
| 2518 | wmx_update(); | |||
| 2aa417e6 | dhilvert | 2005-01-07 06:44:00 +0000 | 2519 | map_update(); |
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 2520 | |
| 46f9776a | dhilvert | 2005-01-07 06:44:00 +0000 | 2521 | assert (reference_image != NULL); |
| 2522 | assert (reference_defined != NULL); | |||
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 2523 | |
| 0467efe3 | David Hilvert | 2008-04-09 21:14:38 +0000 | 2524 | astate.set_input_frame(image_rw::open(i)); |
| 0a432b63 | David Hilvert | 2007-03-13 08:03:00 +0000 | 2525 | |
| e580d9d3 | David Hilvert | 2007-12-19 16:59:00 +0000 | 2526 | _align(i, _gs, &astate); |
| 0a432b63 DH |
David Hilvert | 2007-03-13 08:03:00 +0000 | 2527 | |
| 2528 | image_rw::close(n); | |||
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 2529 | } |
| 2530 | } | |||
| 2531 | ||||
| 2532 | public: | |||
| 2533 | ||||
| 2534 | /* | |||
| 04382119 | dhilvert | 2005-04-29 09:23:00 +0000 | 2535 | * Set the control point count |
| 2536 | */ | |||
| 2537 | static void set_cp_count(unsigned int n) { | |||
| 2538 | assert (cp_array == NULL); | |||
| 2539 | ||||
| 2540 | cp_count = n; | |||
| 2541 | cp_array = (const point **) malloc(n * sizeof(const point *)); | |||
| 2542 | } | |||
| 2543 | ||||
| 2544 | /* | |||
| 2545 | * Set control points. | |||
| 2546 | */ | |||
| 2547 | static void set_cp(unsigned int i, const point *p) { | |||
| 2548 | cp_array[i] = p; | |||
| 2549 | } | |||
| 2550 | ||||
| 2551 | /* | |||
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 2552 | * Register exposure |
| 2553 | */ | |||
| 2554 | static void exp_register() { | |||
| 2555 | _exp_register = 1; | |||
| 2556 | } | |||
| 2557 | ||||
| 2558 | /* | |||
| 3dc20778 | dhilvert | 2005-01-10 23:06:00 +0000 | 2559 | * Register exposure only based on metadata |
| 2560 | */ | |||
| 2561 | static void exp_meta_only() { | |||
| 2562 | _exp_register = 2; | |||
| 2563 | } | |||
| 2564 | ||||
| 2565 | /* | |||
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 2566 | * Don't register exposure |
| 2567 | */ | |||
| 2568 | static void exp_noregister() { | |||
| 2569 | _exp_register = 0; | |||
| 2570 | } | |||
| 2571 | ||||
| 2572 | /* | |||
| 2573 | * Set alignment class to translation only. Only adjust the x and y | |||
| 2574 | * position of images. Do not rotate input images or perform | |||
| 2575 | * projective transformations. | |||
| 2576 | */ | |||
| 2577 | static void class_translation() { | |||
| 2578 | alignment_class = 0; | |||
| 2579 | } | |||
| 2580 | ||||
| 2581 | /* | |||
| 2582 | * Set alignment class to Euclidean transformations only. Adjust the x | |||
| 2583 | * and y position of images and the orientation of the image about the | |||
| 2584 | * image center. | |||
| 2585 | */ | |||
| 2586 | static void class_euclidean() { | |||
| 2587 | alignment_class = 1; | |||
| 2588 | } | |||
| 2589 | ||||
| 2590 | /* | |||
| 2591 | * Set alignment class to perform general projective transformations. | |||
| 2592 | * See the file gpt.h for more information about general projective | |||
| 2593 | * transformations. | |||
| 2594 | */ | |||
| 2595 | static void class_projective() { | |||
| 2596 | alignment_class = 2; | |||
| 2597 | } | |||
| 2598 | ||||
| 2599 | /* | |||
| 2600 | * Set the default initial alignment to the identity transformation. | |||
| 2601 | */ | |||
| 2602 | static void initial_default_identity() { | |||
| 2603 | default_initial_alignment_type = 0; | |||
| 2604 | } | |||
| 2605 | ||||
| 2606 | /* | |||
| 2607 | * Set the default initial alignment to the most recently matched | |||
| 2608 | * frame's final transformation. | |||
| 2609 | */ | |||
| 2610 | static void initial_default_follow() { | |||
| 2611 | default_initial_alignment_type = 1; | |||
| 2612 | } | |||
| 2613 | ||||
| 2614 | /* | |||
| f09b7254 | dhilvert | 2005-01-07 06:44:00 +0000 | 2615 | * Perturb output coordinates. |
| 2616 | */ | |||
| 2617 | static void perturb_output() { | |||
| 2618 | perturb_type = 0; | |||
| 2619 | } | |||
| 2620 | ||||
| 2621 | /* | |||
| 2622 | * Perturb source coordinates. | |||
| 2623 | */ | |||
| 2624 | static void perturb_source() { | |||
| 2625 | perturb_type = 1; | |||
| 2626 | } | |||
| 2627 | ||||
| 2628 | /* | |||
| 46f9776a | dhilvert | 2005-01-07 06:44:00 +0000 | 2629 | * Frames under threshold align optimally |
| 2630 | */ | |||
| 2631 | static void fail_optimal() { | |||
| 2632 | is_fail_default = 0; | |||
| 2633 | } | |||
| 2634 | ||||
| 2635 | /* | |||
| 2636 | * Frames under threshold keep their default alignments. | |||
| 2637 | */ | |||
| 2638 | static void fail_default() { | |||
| 2639 | is_fail_default = 1; | |||
| 2640 | } | |||
| 2641 | ||||
| 2642 | /* | |||
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 2643 | * Align images with an error contribution from each color channel. |
| 2644 | */ | |||
| 2645 | static void all() { | |||
| 2646 | channel_alignment_type = 0; | |||
| 2647 | } | |||
| 2648 | ||||
| 2649 | /* | |||
| 2650 | * Align images with an error contribution only from the green channel. | |||
| 2651 | * Other color channels do not affect alignment. | |||
| 2652 | */ | |||
| 2653 | static void green() { | |||
| 2654 | channel_alignment_type = 1; | |||
| 2655 | } | |||
| 2656 | ||||
| 2657 | /* | |||
| 2658 | * Align images using a summation of channels. May be useful when | |||
| 2659 | * dealing with images that have high frequency color ripples due to | |||
| 2660 | * color aliasing. | |||
| 2661 | */ | |||
| 2662 | static void sum() { | |||
| 2663 | channel_alignment_type = 2; | |||
| 2664 | } | |||
| 2665 | ||||
| 2666 | /* | |||
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 2667 | * Error metric exponent |
| 2668 | */ | |||
| 2669 | ||||
| 2670 | static void set_metric_exponent(float me) { | |||
| 2671 | metric_exponent = me; | |||
| 2672 | } | |||
| 2673 | ||||
| 2674 | /* | |||
| 2675 | * Match threshold | |||
| 2676 | */ | |||
| 2677 | ||||
| 2678 | static void set_match_threshold(float mt) { | |||
| 2679 | match_threshold = mt; | |||
| 2680 | } | |||
| 2681 | ||||
| 2682 | /* | |||
| 2683 | * Perturbation lower and upper bounds. | |||
| 2684 | */ | |||
| 2685 | ||||
| f09b7254 | dhilvert | 2005-01-07 06:44:00 +0000 | 2686 | static void set_perturb_lower(ale_pos pl, int plp) { |
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 2687 | perturb_lower = pl; |
| f09b7254 | dhilvert | 2005-01-07 06:44:00 +0000 | 2688 | perturb_lower_percent = plp; |
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 2689 | } |
| 2690 | ||||
| f09b7254 | dhilvert | 2005-01-07 06:44:00 +0000 | 2691 | static void set_perturb_upper(ale_pos pu, int pup) { |
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 2692 | perturb_upper = pu; |
| f09b7254 | dhilvert | 2005-01-07 06:44:00 +0000 | 2693 | perturb_upper_percent = pup; |
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 2694 | } |
| 2695 | ||||
| 2696 | /* | |||
| 2697 | * Maximum rotational perturbation. | |||
| 2698 | */ | |||
| 2699 | ||||
| 2700 | static void set_rot_max(int rm) { | |||
| 2701 | ||||
| 2702 | /* | |||
| 2703 | * Obtain the largest power of two not larger than rm. | |||
| 2704 | */ | |||
| 2705 | ||||
| 2706 | rot_max = pow(2, floor(log(rm) / log(2))); | |||
| 2707 | } | |||
| 2708 | ||||
| 2709 | /* | |||
| 46f9776a | dhilvert | 2005-01-07 06:44:00 +0000 | 2710 | * Barrel distortion adjustment multiplier |
| 2711 | */ | |||
| 2712 | ||||
| 2713 | static void set_bda_mult(ale_pos m) { | |||
| 2714 | bda_mult = m; | |||
| 2715 | } | |||
| 2716 | ||||
| 2717 | /* | |||
| 2718 | * Barrel distortion maximum rate of change | |||
| 2719 | */ | |||
| 2720 | ||||
| 2721 | static void set_bda_rate(ale_pos m) { | |||
| 2722 | bda_rate = m; | |||
| 2723 | } | |||
| 2724 | ||||
| 2725 | /* | |||
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 2726 | * Level-of-detail |
| 2727 | */ | |||
| 2728 | ||||
| 5292ffa7 DH |
David Hilvert | 2008-05-28 01:17:53 +0000 | 2729 | static void set_lod_preferred(int lm) { |
| 2730 | lod_preferred = lm; | |||
| 2731 | } | |||
| 2732 | ||||
| 2733 | /* | |||
| 2734 | * Minimum dimension for reduced level-of-detail. | |||
| 2735 | */ | |||
| 2736 | ||||
| 2737 | static void set_min_dimension(int md) { | |||
| 2738 | min_dimension = md; | |||
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 2739 | } |
| 2740 | ||||
| 2741 | /* | |||
| 2742 | * Set the scale factor | |||
| 2743 | */ | |||
| 2744 | static void set_scale(ale_pos s) { | |||
| 2745 | scale_factor = s; | |||
| 2746 | } | |||
| 2747 | ||||
| 2748 | /* | |||
| 2749 | * Set reference rendering to align against | |||
| 2750 | */ | |||
| 2751 | static void set_reference(render *r) { | |||
| 2752 | reference = r; | |||
| 2753 | } | |||
| 2754 | ||||
| 2755 | /* | |||
| 46f9776a | dhilvert | 2005-01-07 06:44:00 +0000 | 2756 | * Set the interpolant |
| 2757 | */ | |||
| 2758 | static void set_interpolant(filter::scaled_filter *f) { | |||
| 2759 | interpolant = f; | |||
| 2760 | } | |||
| 2761 | ||||
| 2762 | /* | |||
| 2763 | * Set alignment weights image | |||
| 2764 | */ | |||
| 2aa417e6 | dhilvert | 2005-01-07 06:44:00 +0000 | 2765 | static void set_weight_map(const image *i) { |
| 2766 | weight_map = i; | |||
| 46f9776a | dhilvert | 2005-01-07 06:44:00 +0000 | 2767 | } |
| 2768 | ||||
| 2769 | /* | |||
| 2770 | * Set frequency cuts | |||
| 2771 | */ | |||
| 2772 | static void set_frequency_cut(double h, double v, double a) { | |||
| 2773 | horiz_freq_cut = h; | |||
| 2774 | vert_freq_cut = v; | |||
| 2775 | avg_freq_cut = a; | |||
| 2776 | } | |||
| 2777 | ||||
| 2778 | /* | |||
| 2779 | * Set algorithmic alignment weighting | |||
| 2780 | */ | |||
| 2781 | static void set_wmx(const char *e, const char *f, const char *d) { | |||
| 2782 | wmx_exec = e; | |||
| 2783 | wmx_file = f; | |||
| 2784 | wmx_defs = d; | |||
| 2785 | } | |||
| 2786 | ||||
| 2787 | /* | |||
| 2788 | * Show frequency weights | |||
| 2789 | */ | |||
| 2790 | static void set_fl_show(const char *filename) { | |||
| 2791 | fw_output = filename; | |||
| 2792 | } | |||
| 2793 | ||||
| 2794 | /* | |||
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 2795 | * Set transformation file loader. |
| 2796 | */ | |||
| 2797 | static void set_tload(tload_t *tl) { | |||
| 2798 | tload = tl; | |||
| 2799 | } | |||
| 2800 | ||||
| 2801 | /* | |||
| 2802 | * Set transformation file saver. | |||
| 2803 | */ | |||
| 2804 | static void set_tsave(tsave_t *ts) { | |||
| 2805 | tsave = ts; | |||
| 2806 | } | |||
| 2807 | ||||
| 2808 | /* | |||
| 2809 | * Get match statistics for frame N. | |||
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 2810 | */ |
| 2811 | static int match(int n) { | |||
| 2812 | update_to(n); | |||
| 2813 | ||||
| 46f9776a | dhilvert | 2005-01-07 06:44:00 +0000 | 2814 | if (n == latest) |
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 2815 | return latest_ok; |
| 2816 | else if (_keep) | |||
| 2817 | return kept_ok[n]; | |||
| 46f9776a | dhilvert | 2005-01-07 06:44:00 +0000 | 2818 | else { |
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 2819 | assert(0); |
| 46f9776a | dhilvert | 2005-01-07 06:44:00 +0000 | 2820 | exit(1); |
| 2821 | } | |||
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 2822 | } |
| 2823 | ||||
| 2824 | /* | |||
| 2825 | * Message that old alignment data should be kept. | |||
| 2826 | */ | |||
| 2827 | static void keep() { | |||
| 46f9776a | dhilvert | 2005-01-07 06:44:00 +0000 | 2828 | assert (latest == -1); |
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 2829 | _keep = 1; |
| 2830 | } | |||
| 2831 | ||||
| 2832 | /* | |||
| 2833 | * Get alignment for frame N. | |||
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 2834 | */ |
| 2835 | static transformation of(int n) { | |||
| 2836 | update_to(n); | |||
| 46f9776a | dhilvert | 2005-01-07 06:44:00 +0000 | 2837 | if (n == latest) |
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 2838 | return latest_t; |
| 2839 | else if (_keep) | |||
| 2840 | return kept_t[n]; | |||
| 2841 | else { | |||
| 2842 | assert(0); | |||
| 2843 | exit(1); | |||
| 2844 | } | |||
| 2845 | } | |||
| 2846 | ||||
| 2847 | /* | |||
| bddc9e4d | David Hilvert | 2007-10-01 19:50:00 +0000 | 2848 | * Use Monte Carlo alignment sampling with argument N. |
| 1c2f7405 | David Hilvert | 2007-09-20 16:58:00 +0000 | 2849 | */ |
| bddc9e4d DH |
David Hilvert | 2007-10-01 19:50:00 +0000 | 2850 | static void mc(ale_pos n) { |
| 2851 | _mc = n; | |||
| 1c2f7405 DH |
David Hilvert | 2007-09-20 16:58:00 +0000 | 2852 | } |
| 2853 | ||||
| 2854 | /* | |||
| 07271611 | dhilvert | 2005-01-07 06:48:00 +0000 | 2855 | * Set the certainty-weighted flag. |
| 2856 | */ | |||
| 2857 | static void certainty_weighted(int flag) { | |||
| 2858 | certainty_weights = flag; | |||
| 2859 | } | |||
| 2860 | ||||
| 2861 | /* | |||
| 2aa417e6 | dhilvert | 2005-01-07 06:44:00 +0000 | 2862 | * Set the global search type. |
| 2863 | */ | |||
| 2864 | static void gs(const char *type) { | |||
| 7bcfe5db | dhilvert | 2005-01-07 06:44:00 +0000 | 2865 | if (!strcmp(type, "local")) { |
| 2aa417e6 | dhilvert | 2005-01-07 06:44:00 +0000 | 2866 | _gs = 0; |
| 2867 | } else if (!strcmp(type, "inner")) { | |||
| 2868 | _gs = 1; | |||
| 2869 | } else if (!strcmp(type, "outer")) { | |||
| 2870 | _gs = 2; | |||
| 2871 | } else if (!strcmp(type, "all")) { | |||
| 2872 | _gs = 3; | |||
| 2873 | } else if (!strcmp(type, "central")) { | |||
| 2874 | _gs = 4; | |||
| 842afc18 DH |
David Hilvert | 2007-05-08 06:55:00 +0000 | 2875 | } else if (!strcmp(type, "defaults")) { |
| 2876 | _gs = 6; | |||
| 04382119 | dhilvert | 2005-04-29 09:23:00 +0000 | 2877 | } else if (!strcmp(type, "points")) { |
| 2878 | _gs = 5; | |||
| b386e644 | dhilvert | 2005-04-30 09:28:00 +0000 | 2879 | keep(); |
| 2aa417e6 | dhilvert | 2005-01-07 06:44:00 +0000 | 2880 | } else { |
| 07271611 | dhilvert | 2005-01-07 06:48:00 +0000 | 2881 | ui::get()->error("bad global search type"); |
| 2aa417e6 | dhilvert | 2005-01-07 06:44:00 +0000 | 2882 | } |
| 2883 | } | |||
| 2884 | ||||
| 2885 | /* | |||
| 4949c031 | dhilvert | 2005-01-07 06:44:00 +0000 | 2886 | * Set the minimum overlap for global searching |
| 2887 | */ | |||
| 326c35b1 | David Hilvert | 2007-04-19 21:28:00 +0000 | 2888 | static void gs_mo(ale_pos value, int _gs_mo_percent) { |
| 4949c031 | dhilvert | 2005-01-07 06:44:00 +0000 | 2889 | _gs_mo = value; |
| 326c35b1 | David Hilvert | 2007-04-19 21:28:00 +0000 | 2890 | gs_mo_percent = _gs_mo_percent; |
| 4949c031 | dhilvert | 2005-01-07 06:44:00 +0000 | 2891 | } |
| 2892 | ||||
| 2893 | /* | |||
| 903df240 DH |
David Hilvert | 2008-04-24 14:36:35 +0000 | 2894 | * Set mutli-alignment certainty lower bound. |
| 2895 | */ | |||
| 2896 | static void set_ma_cert(ale_real value) { | |||
| 2897 | _ma_cert = value; | |||
| 2898 | } | |||
| 2899 | ||||
| 2900 | /* | |||
| 46f9776a | dhilvert | 2005-01-07 06:44:00 +0000 | 2901 | * Set alignment exclusion regions |
| 2902 | */ | |||
| df55d1a2 | dhilvert | 2006-08-30 07:40:00 +0000 | 2903 | static void set_exclusion(exclusion *_ax_parameters, int _ax_count) { |
| 46f9776a | dhilvert | 2005-01-07 06:44:00 +0000 | 2904 | ax_count = _ax_count; |
| 2905 | ax_parameters = _ax_parameters; | |||
| 2906 | } | |||
| 2907 | ||||
| 2908 | /* | |||
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 2909 | * Get match summary statistics. |
| 2910 | */ | |||
| 2911 | static ale_accum match_summary() { | |||
| 34add5e1 | David Hilvert | 2007-10-17 21:47:00 +0000 | 2912 | return match_sum / (ale_accum) match_count; |
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 2913 | } |
| 2914 | }; | |||
| 2915 | ||||
| f8864302 DH |
David Hilvert | 2008-04-11 18:15:57 +0000 | 2916 | template<class diff_trans> |
| 2917 | void *d2::align::diff_stat_generic<diff_trans>::diff_subdomain(void *args) { | |||
| 2918 | ||||
| 2919 | subdomain_args *sargs = (subdomain_args *) args; | |||
| 2920 | ||||
| 2921 | struct scale_cluster c = sargs->c; | |||
| 2922 | std::vector<run> runs = sargs->runs; | |||
| 2923 | int ax_count = sargs->ax_count; | |||
| 2924 | int f = sargs->f; | |||
| 2925 | int i_min = sargs->i_min; | |||
| 2926 | int i_max = sargs->i_max; | |||
| 2927 | int j_min = sargs->j_min; | |||
| 2928 | int j_max = sargs->j_max; | |||
| 2929 | int subdomain = sargs->subdomain; | |||
| 2930 | ||||
| 2931 | assert (reference_image); | |||
| 2932 | ||||
| 2933 | point offset = c.accum->offset(); | |||
| 2934 | ||||
| 2935 | for (mc_iterate m(i_min, i_max, j_min, j_max, subdomain); !m.done(); m++) { | |||
| 2936 | ||||
| 2937 | int i = m.get_i(); | |||
| 2938 | int j = m.get_j(); | |||
| 2939 | ||||
| 2940 | /* | |||
| 2941 | * Check reference frame definition. | |||
| 2942 | */ | |||
| 2943 | ||||
| 2944 | if (!((pixel) c.accum->get_pixel(i, j)).finite() | |||
| 2945 | || !(((pixel) c.certainty->get_pixel(i, j)).minabs_norm() > 0)) | |||
| 2946 | continue; | |||
| 2947 | ||||
| 2948 | /* | |||
| 2949 | * Check for exclusion in render coordinates. | |||
| 2950 | */ | |||
| 2951 | ||||
| 2952 | if (ref_excluded(i, j, offset, c.ax_parameters, ax_count)) | |||
| 2953 | continue; | |||
| 2954 | ||||
| 2955 | /* | |||
| 2956 | * Transform | |||
| 2957 | */ | |||
| 2958 | ||||
| 2959 | struct point q, r = point::undefined(); | |||
| 2960 | ||||
| 2961 | q = (c.input_scale < 1.0 && interpolant == NULL) | |||
| 2962 | ? runs.back().offset.scaled_inverse_transform( | |||
| 2963 | point(i + offset[0], j + offset[1])) | |||
| 2964 | : runs.back().offset.unscaled_inverse_transform( | |||
| 2965 | point(i + offset[0], j + offset[1])); | |||
| 2966 | ||||
| 2967 | if (runs.size() == 2) { | |||
| 2968 | r = (c.input_scale < 1.0) | |||
| 2969 | ? runs.front().offset.scaled_inverse_transform( | |||
| 2970 | point(i + offset[0], j + offset[1])) | |||
| 2971 | : runs.front().offset.unscaled_inverse_transform( | |||
| 2972 | point(i + offset[0], j + offset[1])); | |||
| 2973 | } | |||
| 2974 | ||||
| 2975 | ale_pos ti = q[0]; | |||
| 2976 | ale_pos tj = q[1]; | |||
| 2977 | ||||
| 2978 | /* | |||
| 2979 | * Check that the transformed coordinates are within | |||
| 2980 | * the boundaries of array c.input and that they | |||
| 2981 | * are not subject to exclusion. | |||
| 2982 | * | |||
| 2983 | * Also, check that the weight value in the accumulated array | |||
| 2984 | * is nonzero, unless we know it is nonzero by virtue of the | |||
| 2985 | * fact that it falls within the region of the original frame | |||
| 2986 | * (e.g. when we're not increasing image extents). | |||
| 2987 | */ | |||
| 2988 | ||||
| 2989 | if (input_excluded(ti, tj, c.ax_parameters, ax_count)) | |||
| 2990 | continue; | |||
| 2991 | ||||
| 2992 | if (input_excluded(r[0], r[1], c.ax_parameters, ax_count)) | |||
| 2993 | r = point::undefined(); | |||
| 2994 | ||||
| 2995 | /* | |||
| 2996 | * Check the boundaries of the input frame | |||
| 2997 | */ | |||
| 2998 | ||||
| 2999 | if (!(ti >= 0 | |||
| 3000 | && ti <= c.input->height() - 1 | |||
| 3001 | && tj >= 0 | |||
| 3002 | && tj <= c.input->width() - 1)) | |||
| 3003 | continue; | |||
| 3004 | ||||
| 3005 | if (!(r[0] >= 0 | |||
| 3006 | && r[0] <= c.input->height() - 1 | |||
| 3007 | && r[1] >= 0 | |||
| 3008 | && r[1] <= c.input->width() - 1)) | |||
| 3009 | r = point::undefined(); | |||
| 3010 | ||||
| 3011 | sargs->runs.back().sample(f, c, i, j, q, r, runs.front()); | |||
| 3012 | } | |||
| 3013 | ||||
| 3014 | return NULL; | |||
| 3015 | } | |||
| 3016 | ||||
| 30afe4b6 | dhilvert | 2005-01-07 06:42:00 +0000 | 3017 | #endif |