1 // Copyright 2002, 2004, 2007 David Hilvert <dhilvert@auricle.dyndns.org>,
2 // <dhilvert@ugcs.caltech.edu>
4 /* This file is part of the Anti-Lamenessing Engine.
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
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
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.
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
22 * align.h: Handle alignment of frames.
29 #include "transformation.h"
36 #define D2_ALIGN_IMPRECISE_COVERAGE 0.3
42 * Private data members
45 static ale_pos scale_factor
;
48 * Original frame transformation
50 static transformation orig_t
;
53 * Keep data older than latest
56 static transformation
*kept_t
;
60 * Transformation file handlers
63 static tload_t
*tload
;
64 static tsave_t
*tsave
;
67 * Control point variables
70 static const point
**cp_array
;
71 static unsigned int cp_count
;
74 * Reference rendering to align against
77 static render
*reference
;
78 static filter::scaled_filter
*interpolant
;
79 static const image
*reference_image
;
80 static const image
*reference_defined
;
83 * Per-pixel alignment weight map
86 static const image
*weight_map
;
89 * Frequency-dependent alignment weights
92 static double horiz_freq_cut
;
93 static double vert_freq_cut
;
94 static double avg_freq_cut
;
95 static const char *fw_output
;
98 * Algorithmic alignment weighting
101 static const char *wmx_exec
;
102 static const char *wmx_file
;
103 static const char *wmx_defs
;
106 * Non-certainty alignment weights
109 static image
*alignment_weights
;
112 * Latest transformation.
115 static transformation latest_t
;
118 * Flag indicating whether the latest transformation
119 * resulted in a match.
122 static int latest_ok
;
125 * Frame number most recently aligned.
131 * Exposure registration
133 * 0. Preserve the original exposure of images.
135 * 1. Match exposure between images.
137 * 2. Use only image metadata for registering exposure.
140 static int _exp_register
;
145 * 0. Translation only. Only adjust the x and y position of images.
146 * Do not rotate input images or perform projective transformations.
148 * 1. Euclidean transformations only. Adjust the x and y position
149 * of images and the orientation of the image about the image center.
151 * 2. Perform general projective transformations. See the file gpt.h
152 * for more information about general projective transformations.
155 static int alignment_class
;
158 * Default initial alignment type.
160 * 0. Identity transformation.
162 * 1. Most recently accepted frame's final transformation.
165 static int default_initial_alignment_type
;
168 * Projective group behavior
170 * 0. Perturb in output coordinates.
172 * 1. Perturb in source coordinates
175 static int perturb_type
;
180 * This structure contains variables necessary for handling a
181 * multi-alignment element. The change between the non-default old
182 * initial alignment and old final alignment is used to adjust the
183 * non-default current initial alignment. If either the old or new
184 * initial alignment is a default alignment, the old --follow semantics
189 int is_default
, old_is_default
;
192 transformation old_initial_alignment
;
193 transformation old_final_alignment
;
194 transformation default_initial_alignment
;
195 const image
*input_frame
;
205 * Alignment for failed frames -- default or optimal?
207 * A frame that does not meet the match threshold can be assigned the
208 * best alignment found, or can be assigned its alignment default.
211 static int is_fail_default
;
216 * 0. Align images with an error contribution from each color channel.
218 * 1. Align images with an error contribution only from the green channel.
219 * Other color channels do not affect alignment.
221 * 2. Align images using a summation of channels. May be useful when dealing
222 * with images that have high frequency color ripples due to color aliasing.
225 static int channel_alignment_type
;
228 * Error metric exponent
231 static float metric_exponent
;
237 static float match_threshold
;
240 * Perturbation lower and upper bounds.
243 static ale_pos perturb_lower
;
244 static int perturb_lower_percent
;
245 static ale_pos perturb_upper
;
246 static int perturb_upper_percent
;
249 * Maximum level-of-detail scale factor is 2^lod_max/perturb.
255 * Maximum rotational perturbation
258 static ale_pos rot_max
;
261 * Barrel distortion alignment multiplier
264 static ale_pos bda_mult
;
267 * Barrel distortion maximum adjustment rate
270 static ale_pos bda_rate
;
273 * Alignment match sum
276 static ale_accum match_sum
;
279 * Alignment match count.
282 static int match_count
;
285 * Exposure registration and final match precision flag
287 * 0. Do not require precise calculation.
289 * 1. Require precise exposure registration and final match.
292 static int precise_calculation
;
295 * Certainty weight flag
297 * 0. Don't use certainty weights for alignment.
299 * 1. Use certainty weights for alignment.
302 static int certainty_weights
;
305 * Global search parameter
307 * 0. Local: Local search only.
308 * 1. Inner: Alignment reference image inner region
309 * 2. Outer: Alignment reference image outer region
310 * 3. All: Alignment reference image inner and outer regions.
311 * 4. Central: Inner if possible; else, best of inner and outer.
312 * 5. Points: Align by control points.
318 * Multi-alignment cardinality.
321 static unsigned int _ma_card
;
324 * Multi-alignment contiguity.
327 static double _ma_cont
;
330 * Minimum overlap for global searches
333 static ale_pos _gs_mo
;
334 static int gs_mo_percent
;
340 static exclusion
*ax_parameters
;
344 * Types for scale clusters.
347 struct nl_scale_cluster
{
348 const image
*accum_max
;
349 const image
*accum_min
;
350 const image
*certainty_max
;
351 const image
*certainty_min
;
352 const image
*aweight_max
;
353 const image
*aweight_min
;
354 exclusion
*ax_parameters
;
357 const image
*input_certainty_max
;
358 const image
*input_certainty_min
;
359 const image
*input_max
;
360 const image
*input_min
;
363 struct scale_cluster
{
365 const image
*certainty
;
366 const image
*aweight
;
367 exclusion
*ax_parameters
;
370 const image
*input_certainty
;
373 nl_scale_cluster
*nl_scale_clusters
;
377 * Check for exclusion region coverage in the reference
380 static int ref_excluded(int i
, int j
, point offset
, exclusion
*params
, int param_count
) {
381 for (int idx
= 0; idx
< param_count
; idx
++)
382 if (params
[idx
].type
== exclusion::RENDER
383 && i
+ offset
[0] >= params
[idx
].x
[0]
384 && i
+ offset
[0] <= params
[idx
].x
[1]
385 && j
+ offset
[1] >= params
[idx
].x
[2]
386 && j
+ offset
[1] <= params
[idx
].x
[3])
393 * Check for exclusion region coverage in the input
396 static int input_excluded(ale_pos ti
, ale_pos tj
, exclusion
*params
, int param_count
) {
397 for (int idx
= 0; idx
< param_count
; idx
++)
398 if (params
[idx
].type
== exclusion::FRAME
399 && ti
>= params
[idx
].x
[0]
400 && ti
<= params
[idx
].x
[1]
401 && tj
>= params
[idx
].x
[2]
402 && tj
<= params
[idx
].x
[3])
409 * Overlap function. Determines the number of pixels in areas where
410 * the arrays overlap. Uses the reference array's notion of pixel
413 static unsigned int overlap(struct scale_cluster c
, transformation t
, int ax_count
) {
414 assert (reference_image
);
416 unsigned int result
= 0;
418 point offset
= c
.accum
->offset();
420 for (unsigned int i
= 0; i
< c
.accum
->height(); i
++)
421 for (unsigned int j
= 0; j
< c
.accum
->width(); j
++) {
423 if (ref_excluded(i
, j
, offset
, c
.ax_parameters
, ax_count
))
432 q
= (c
.input_scale
< 1.0 && interpolant
== NULL
)
433 ? t
.scaled_inverse_transform(
434 point(i
+ offset
[0], j
+ offset
[1]))
435 : t
.unscaled_inverse_transform(
436 point(i
+ offset
[0], j
+ offset
[1]));
442 * Check that the transformed coordinates are within
443 * the boundaries of array c.input, and check that the
444 * weight value in the accumulated array is nonzero,
445 * unless we know it is nonzero by virtue of the fact
446 * that it falls within the region of the original
447 * frame (e.g. when we're not increasing image
448 * extents). Also check for frame exclusion.
451 if (input_excluded(ti
, tj
, c
.ax_parameters
, ax_count
))
455 && ti
<= c
.input
->height() - 1
457 && tj
<= c
.input
->width() - 1
458 && c
.certainty
->get_pixel(i
, j
)[0] != 0)
466 * Calculate the region associated with the current multi-alignment
469 static void calculate_element_region(transformation
*t
, scale_cluster si
,
470 int local_ax_count
) {
472 unsigned int i_max
= si
.accum
->height();
473 unsigned int j_max
= si
.accum
->width();
474 point offset
= si
.accum
->offset();
476 if (si
.input_scale
< 1.0 && interpolant
== NULL
)
477 t
->begin_calculate_scaled_region(i_max
, j_max
, offset
);
479 t
->begin_calculate_unscaled_region(i_max
, j_max
, offset
);
481 for (unsigned int i
= 0; i
< i_max
; i
++)
482 for (unsigned int j
= 0; j
< j_max
; j
++) {
484 if (ref_excluded(i
, j
, offset
, si
.ax_parameters
, local_ax_count
))
489 while ((q
= t
->get_query_point((int) (i
+ offset
[0]),
490 (int) (j
+ offset
[1]))).defined()) {
495 if (input_excluded(ti
, tj
, si
.ax_parameters
, ax_count
))
499 && ti
<= si
.input
->height() - 1
501 && tj
<= si
.input
->width() - 1
502 && si
.certainty
->get_pixel(i
, j
)[0] != 0) {
509 t
->end_calculate_region();
513 * Monte carlo iteration class.
515 * Monte Carlo alignment has been used for statistical comparisons in
516 * spatial registration, and is now used for tonal registration
517 * and final match calculation.
521 * We use a random process for which the expected number of sampled
522 * pixels is +/- .000003 from the coverage in the range [.005,.995] for
523 * an image with 100,000 pixels. (The actual number may still deviate
524 * from the expected number by more than this amount, however.) The
525 * method is as follows:
527 * We have coverage == USE/ALL, or (expected # pixels to use)/(# total
528 * pixels). We derive from this SKIP/USE.
530 * SKIP/USE == (SKIP/ALL)/(USE/ALL) == (1 - (USE/ALL))/(USE/ALL)
532 * Once we have SKIP/USE, we know the expected number of pixels to skip
533 * in each iteration. We use a random selection process that provides
534 * SKIP/USE close to this calculated value.
536 * If we can draw uniformly to select the number of pixels to skip, we
537 * do. In this case, the maximum number of pixels to skip is twice the
540 * If we cannot draw uniformly, we still assign equal probability to
541 * each of the integer values in the interval [0, 2 * (SKIP/USE)], but
542 * assign an unequal amount to the integer value ceil(2 * SKIP/USE) +
547 * When reseeding the random number generator, we want the same set of
548 * pixels to be used in cases where two alignment options are compared.
549 * If we wanted to avoid bias from repeatedly utilizing the same seed,
550 * we could seed with the number of the frame most recently aligned:
554 * However, in cursory tests, it seems okay to just use the default
555 * seed of 1, and so we do this, since it is simpler; both of these
556 * approaches to reseeding achieve better results than not reseeding.
557 * (1 is the default seed according to the GNU Manual Page for
560 * For subdomain calculations, we vary the seed by adding the subdomain
567 unsigned int index_max
;
576 mc_iterate(int _i_min
, int _i_max
, int _j_min
, int _j_max
, unsigned int subdomain
)
586 index_max
= (i_max
- i_min
) * (j_max
- j_min
);
588 if (index_max
< 500 || precise_calculation
)
591 coverage
= D2_ALIGN_IMPRECISE_COVERAGE
;
593 ale_pos su
= (1 - coverage
) / coverage
;
595 mc_max
= (floor(2*su
) * (1 + floor(2*su
)) + 2*su
)
596 / (2 + 2 * floor(2*su
) - 2*su
);
598 rng
.seed(1 + subdomain
);
600 index
= -1 + (int) ceil((mc_max
+1)
601 * ( (1 + ((ale_pos
) (rng
.get())) )
602 / (1 + ((ale_pos
) RAND_MAX
)) ));
606 return index
/ (j_max
- j_min
) + i_min
;
610 return index
% (j_max
- j_min
) + j_min
;
613 void operator++(int whats_this_for
) {
614 index
+= (int) ceil((mc_max
+1)
615 * ( (1 + ((ale_pos
) (rng
.get())) )
616 / (1 + ((ale_pos
) RAND_MAX
)) ));
620 return (index
>= index_max
);
625 * Not-quite-symmetric difference function. Determines the difference in areas
626 * where the arrays overlap. Uses the reference array's notion of pixel positions.
628 * For the purposes of determining the difference, this function divides each
629 * pixel value by the corresponding image's average pixel magnitude, unless we
632 * a) Extending the boundaries of the image, or
634 * b) following the previous frame's transform
636 * If we are doing monte-carlo pixel sampling for alignment, we
637 * typically sample a subset of available pixels; otherwise, we sample
646 transformation offset
;
653 ale_accum centroid
[2], centroid_divisor
;
654 ale_accum de_centroid
[2], de_centroid_v
, de_sum
;
661 min
= point::posinf();
662 max
= point::neginf();
666 centroid_divisor
= 0;
676 void init(transformation _offset
, ale_pos _perturb
) {
683 * Required for STL sanity.
689 run(transformation _offset
, ale_pos _perturb
) : offset() {
690 init(_offset
, _perturb
);
693 void add(const run
&_run
) {
694 result
+= _run
.result
;
695 divisor
+= _run
.divisor
;
697 for (int d
= 0; d
< 2; d
++) {
698 if (min
[d
] > _run
.min
[d
])
699 min
[d
] = _run
.min
[d
];
700 if (max
[d
] < _run
.max
[d
])
701 max
[d
] = _run
.max
[d
];
702 centroid
[d
] += _run
.centroid
[d
];
703 de_centroid
[d
] += _run
.de_centroid
[d
];
706 centroid_divisor
+= _run
.centroid_divisor
;
707 de_centroid_v
+= _run
.de_centroid_v
;
708 de_sum
+= _run
.de_sum
;
711 run(const run
&_run
) : offset() {
716 init(_run
.offset
, _run
.perturb
);
724 run
&operator=(const run
&_run
) {
729 init(_run
.offset
, _run
.perturb
);
742 ale_accum
get_error() const {
743 return pow(result
/ divisor
, 1/metric_exponent
);
746 void sample(int f
, scale_cluster c
, int i
, int j
, point t
, point u
,
747 const run
&comparison
) {
749 pixel pa
= c
.accum
->get_pixel(i
, j
);
751 ale_accum this_result
[2] = { 0, 0 };
752 ale_accum this_divisor
[2] = { 0, 0 };
756 weight
[0] = pixel(1, 1, 1);
757 weight
[1] = pixel(1, 1, 1);
759 if (interpolant
!= NULL
) {
760 interpolant
->filtered(i
, j
, &p
[0], &weight
[1], 1, f
);
762 p
[0] = c
.input
->get_bl(t
);
766 p
[1] = c
.input
->get_bl(u
);
774 if (certainty_weights
== 1) {
775 weight
[0] *= c
.input_certainty
->get_bl(t
, 1);
777 weight
[1] *= c
.input_certainty
->get_bl(u
, 1);
778 weight
[0] *= c
.certainty
->get_pixel(i
, j
);
779 weight
[1] *= c
.certainty
->get_pixel(i
, j
);
782 if (c
.aweight
!= NULL
) {
783 weight
[0] *= c
.aweight
->get_pixel(i
, j
);
784 weight
[1] *= c
.aweight
->get_pixel(i
, j
);
788 * Update sampling area statistics
800 centroid
[0] += (weight
[0][0] + weight
[0][1] + weight
[0][2]) * i
;
801 centroid
[1] += (weight
[0][0] + weight
[0][1] + weight
[0][2]) * j
;
802 centroid_divisor
+= (weight
[0][0] + weight
[0][1] + weight
[0][2]);
805 * Determine alignment type.
808 for (int m
= 0; m
< (u
.defined() ? 2 : 1); m
++)
809 if (channel_alignment_type
== 0) {
811 * Align based on all channels.
815 for (int k
= 0; k
< 3; k
++) {
816 ale_real achan
= pa
[k
];
817 ale_real bchan
= p
[m
][k
];
819 this_result
[m
] += weight
[m
][k
] * pow(fabs(achan
- bchan
), metric_exponent
);
820 this_divisor
[m
] += weight
[m
][k
] * pow(achan
> bchan
? achan
: bchan
, metric_exponent
);
822 } else if (channel_alignment_type
== 1) {
824 * Align based on the green channel.
827 ale_real achan
= pa
[1];
828 ale_real bchan
= p
[m
][1];
830 this_result
[m
] = weight
[m
][1] * pow(fabs(achan
- bchan
), metric_exponent
);
831 this_divisor
[m
] = weight
[m
][1] * pow(achan
> bchan
? achan
: bchan
, metric_exponent
);
832 } else if (channel_alignment_type
== 2) {
834 * Align based on the sum of all channels.
841 for (int k
= 0; k
< 3; k
++) {
844 wsum
+= weight
[m
][k
] / 3;
847 this_result
[m
] = wsum
* pow(fabs(asum
- bsum
), metric_exponent
);
848 this_divisor
[m
] = wsum
* pow(asum
> bsum
? asum
: bsum
, metric_exponent
);
852 // ale_accum de = fabs(this_result[0] / this_divisor[0]
853 // - this_result[1] / this_divisor[1]);
854 ale_accum de
= fabs(this_result
[0] - this_result
[1]);
856 de_centroid
[0] += de
* i
;
857 de_centroid
[1] += de
* j
;
859 de_centroid_v
+= de
* t
.lengthto(u
);
864 result
+= (this_result
[0]);
865 divisor
+= (this_divisor
[0]);
868 void rescale(ale_pos scale
) {
869 offset
.rescale(scale
);
871 de_centroid
[0] *= scale
;
872 de_centroid
[1] *= scale
;
873 de_centroid_v
*= scale
;
876 point
get_centroid() {
877 point result
= point(centroid
[0] / centroid_divisor
, centroid
[1] / centroid_divisor
);
879 assert (finite(centroid
[0])
880 && finite(centroid
[1])
881 && (result
.defined() || centroid_divisor
== 0));
886 point
get_error_centroid() {
887 point result
= point(de_centroid
[0] / de_sum
, de_centroid
[1] / de_sum
);
892 ale_pos
get_error_perturb() {
893 ale_pos result
= de_centroid_v
/ de_sum
;
901 * When non-empty, runs.front() is best, runs.back() is
905 std::vector
<run
> runs
;
908 * old_runs stores the latest available perturbation set for
909 * each multi-alignment element.
912 typedef std::pair
<unsigned int, unsigned int> run_index
;
913 std::map
<run_index
, run
> old_runs
;
915 static void *diff_subdomain(void *args
);
917 struct subdomain_args
{
918 struct scale_cluster c
;
919 std::vector
<run
> runs
;
922 int i_min
, i_max
, j_min
, j_max
;
926 int get_current_index() const {
928 return runs
[0].offset
.get_current_index();
931 struct scale_cluster si
;
935 std::vector
<ale_pos
> perturb_multipliers
;
938 void diff(struct scale_cluster c
, ale_pos perturb
,
940 int _ax_count
, int f
) {
942 if (runs
.size() == 2)
945 runs
.push_back(run(t
, perturb
));
948 ax_count
= _ax_count
;
951 ui::get()->d2_align_sample_start();
953 if (interpolant
!= NULL
)
954 interpolant
->set_parameters(t
, c
.input
, c
.accum
->offset());
960 pthread_t
*threads
= (pthread_t
*) malloc(sizeof(pthread_t
) * N
);
961 pthread_attr_t
*thread_attr
= (pthread_attr_t
*) malloc(sizeof(pthread_attr_t
) * N
);
967 subdomain_args
*args
= new subdomain_args
[N
];
969 for (int ti
= 0; ti
< N
; ti
++) {
971 args
[ti
].runs
= runs
;
972 args
[ti
].ax_count
= ax_count
;
974 args
[ti
].i_min
= (c
.accum
->height() * ti
) / N
;
975 args
[ti
].i_max
= (c
.accum
->height() * (ti
+ 1)) / N
;
977 args
[ti
].j_max
= c
.accum
->width();
978 args
[ti
].subdomain
= ti
;
980 pthread_attr_init(&thread_attr
[ti
]);
981 pthread_attr_setdetachstate(&thread_attr
[ti
], PTHREAD_CREATE_JOINABLE
);
982 pthread_create(&threads
[ti
], &thread_attr
[ti
], diff_subdomain
, &args
[ti
]);
984 diff_subdomain(&args
[ti
]);
988 for (int ti
= 0; ti
< N
; ti
++) {
990 pthread_join(threads
[ti
], NULL
);
992 runs
.back().add(args
[ti
].runs
.back());
997 ui::get()->d2_align_sample_stop();
1003 std::vector
<transformation
> t_array
;
1004 std::vector
<ale_pos
> p_array
;
1006 for (unsigned int r
= 0; r
< runs
.size(); r
++) {
1007 t_array
.push_back(runs
[r
].offset
);
1008 p_array
.push_back(runs
[r
].perturb
);
1013 for (unsigned int r
= 0; r
< t_array
.size(); r
++)
1014 diff(si
, p_array
[r
], t_array
[r
], ax_count
, frame
);
1020 assert(runs
.size() >= 2);
1021 assert(runs
[0].offset
.scale() == runs
[1].offset
.scale());
1023 return (runs
[1].get_error() < runs
[0].get_error()
1024 || (!finite(runs
[0].get_error()) && finite(runs
[1].get_error())));
1027 diff_stat_t() : runs(), old_runs(), perturb_multipliers() {
1030 run_index
get_run_index(unsigned int perturb_index
) {
1031 return run_index(get_current_index(), perturb_index
);
1034 run
&get_run(unsigned int perturb_index
) {
1035 run_index index
= get_run_index(perturb_index
);
1037 assert(old_runs
.count(index
));
1038 return old_runs
[index
];
1041 void rescale(ale_pos scale
, scale_cluster _si
) {
1042 assert(runs
.size() == 1);
1046 runs
[0].rescale(scale
);
1051 void push_element() {
1052 assert(runs
.size() == 1);
1054 runs
[0].offset
.push_element();
1059 unsigned int get_current_index() {
1060 assert (runs
.size() > 0);
1062 return runs
[0].offset
.get_current_index();
1065 void set_current_index(unsigned int i
) {
1066 assert(runs
.size() == 1);
1067 runs
[0].offset
.set_current_index(i
);
1071 void calculate_element_region() {
1072 assert(runs
.size() == 1);
1074 if (get_offset().get_current_index() > 0
1075 && get_offset().is_nontrivial())
1076 align::calculate_element_region(&runs
[0].offset
, si
, ax_count
);
1082 diff_stat_t
&operator=(const diff_stat_t
&dst
) {
1084 * Copy run information.
1087 old_runs
= dst
.old_runs
;
1090 * Copy diff variables
1093 ax_count
= dst
.ax_count
;
1095 perturb_multipliers
= dst
.perturb_multipliers
;
1100 diff_stat_t(const diff_stat_t
&dst
) : runs(), old_runs(),
1101 perturb_multipliers() {
1105 ale_accum
get_result() {
1106 assert(runs
.size() == 1);
1107 return runs
[0].result
;
1110 ale_accum
get_divisor() {
1111 assert(runs
.size() == 1);
1112 return runs
[0].divisor
;
1115 transformation
get_offset() {
1116 assert(runs
.size() == 1);
1117 return runs
[0].offset
;
1120 int operator!=(diff_stat_t
¶m
) {
1121 return (get_error() != param
.get_error());
1124 int operator==(diff_stat_t
¶m
) {
1125 return !(operator!=(param
));
1128 ale_pos
get_error_perturb() {
1129 assert(runs
.size() == 1);
1130 return runs
[0].get_error_perturb();
1133 ale_accum
get_error() const {
1134 assert(runs
.size() == 1);
1135 return runs
[0].get_error();
1140 * Get the set of transformations produced by a given perturbation
1142 void get_perturb_set(std::vector
<transformation
> *set
,
1143 ale_pos adj_p
, ale_pos adj_o
, ale_pos adj_b
,
1144 ale_pos
*current_bd
, ale_pos
*modified_bd
,
1145 std::vector
<ale_pos
> multipliers
= std::vector
<ale_pos
>()) {
1147 assert(runs
.size() == 1);
1149 transformation test_t
;
1152 * Translational or euclidean transformation
1155 for (unsigned int i
= 0; i
< 2; i
++)
1156 for (unsigned int s
= 0; s
< 2; s
++) {
1158 if (!multipliers
.size())
1159 multipliers
.push_back(1);
1161 assert(finite(multipliers
[0]));
1163 test_t
= get_offset();
1165 // test_t.eu_modify(i, (s ? -adj_p : adj_p) * multipliers[0]);
1166 test_t
.translate((i
? point(1, 0) : point(0, 1))
1167 * (s
? -adj_p
: adj_p
)
1170 test_t
.snap(adj_p
/ 2);
1172 set
->push_back(test_t
);
1173 multipliers
.erase(multipliers
.begin());
1177 if (alignment_class
> 0)
1178 for (unsigned int s
= 0; s
< 2; s
++) {
1180 if (!multipliers
.size())
1181 multipliers
.push_back(1);
1183 assert(multipliers
.size());
1184 assert(finite(multipliers
[0]));
1186 if (!(adj_o
* multipliers
[0] < rot_max
))
1189 ale_pos adj_s
= (s
? 1 : -1) * adj_o
* multipliers
[0];
1191 test_t
= get_offset();
1193 run_index ori
= get_run_index(set
->size());
1194 point centroid
= point::undefined();
1196 if (!old_runs
.count(ori
))
1197 ori
= get_run_index(0);
1199 if (!centroid
.finite() && old_runs
.count(ori
)) {
1200 centroid
= old_runs
[ori
].get_error_centroid();
1202 if (!centroid
.finite())
1203 centroid
= old_runs
[ori
].get_centroid();
1205 centroid
*= test_t
.scale()
1206 / old_runs
[ori
].offset
.scale();
1209 if (!centroid
.finite() && !test_t
.is_projective()) {
1210 test_t
.eu_modify(2, adj_s
);
1211 } else if (!centroid
.finite()) {
1212 centroid
= point(si
.input
->height() / 2,
1213 si
.input
->width() / 2);
1215 test_t
.rotate(centroid
+ si
.accum
->offset(),
1218 test_t
.rotate(centroid
+ si
.accum
->offset(),
1222 test_t
.snap(adj_p
/ 2);
1224 set
->push_back(test_t
);
1225 multipliers
.erase(multipliers
.begin());
1228 if (alignment_class
== 2) {
1231 * Projective transformation
1234 for (unsigned int i
= 0; i
< 4; i
++)
1235 for (unsigned int j
= 0; j
< 2; j
++)
1236 for (unsigned int s
= 0; s
< 2; s
++) {
1238 if (!multipliers
.size())
1239 multipliers
.push_back(1);
1241 assert(multipliers
.size());
1242 assert(finite(multipliers
[0]));
1244 ale_pos adj_s
= (s
? -1 : 1) * adj_p
* multipliers
[0];
1246 test_t
= get_offset();
1248 if (perturb_type
== 0)
1249 test_t
.gpt_modify(j
, i
, adj_s
);
1250 else if (perturb_type
== 1)
1251 test_t
.gr_modify(j
, i
, adj_s
);
1255 test_t
.snap(adj_p
/ 2);
1257 set
->push_back(test_t
);
1258 multipliers
.erase(multipliers
.begin());
1267 if (bda_mult
!= 0 && adj_b
!= 0) {
1269 for (unsigned int d
= 0; d
< get_offset().bd_count(); d
++)
1270 for (unsigned int s
= 0; s
< 2; s
++) {
1272 if (!multipliers
.size())
1273 multipliers
.push_back(1);
1275 assert (multipliers
.size());
1276 assert (finite(multipliers
[0]));
1278 ale_pos adj_s
= (s
? -1 : 1) * adj_b
* multipliers
[0];
1280 if (bda_rate
> 0 && fabs(modified_bd
[d
] + adj_s
- current_bd
[d
]) > bda_rate
)
1283 transformation test_t
= get_offset();
1285 test_t
.bd_modify(d
, adj_s
);
1287 set
->push_back(test_t
);
1293 assert(runs
.size() == 2);
1299 assert(runs
.size() == 2);
1303 void perturb_test(ale_pos perturb
, ale_pos adj_p
, ale_pos adj_o
, ale_pos adj_b
,
1304 ale_pos
*current_bd
, ale_pos
*modified_bd
, int stable
) {
1306 assert(runs
.size() == 1);
1308 std::vector
<transformation
> t_set
;
1310 if (perturb_multipliers
.size() == 0) {
1311 get_perturb_set(&t_set
, adj_p
, adj_o
, adj_b
,
1312 current_bd
, modified_bd
);
1314 for (unsigned int i
= 0; i
< t_set
.size(); i
++) {
1315 diff_stat_t test
= *this;
1317 test
.diff(si
, perturb
, t_set
[i
], ax_count
, frame
);
1321 if (finite(adj_p
/ test
.get_error_perturb()))
1322 perturb_multipliers
.push_back(adj_p
/ test
.get_error_perturb());
1324 perturb_multipliers
.push_back(1);
1331 get_perturb_set(&t_set
, adj_p
, adj_o
, adj_b
, current_bd
, modified_bd
,
1332 perturb_multipliers
);
1334 int found_unreliable
= 1;
1335 std::vector
<int> tested(t_set
.size(), 0);
1337 for (unsigned int i
= 0; i
< t_set
.size(); i
++) {
1338 run_index ori
= get_run_index(i
);
1341 * Check for stability
1344 && old_runs
.count(ori
)
1345 && old_runs
[ori
].offset
== t_set
[i
])
1349 std::vector
<ale_pos
> perturb_multipliers_original
= perturb_multipliers
;
1351 while (found_unreliable
) {
1353 found_unreliable
= 0;
1355 for (unsigned int i
= 0; i
< t_set
.size(); i
++) {
1360 diff(si
, perturb
, t_set
[i
], ax_count
, frame
);
1362 if (!(i
< perturb_multipliers
.size())
1363 || !finite(perturb_multipliers
[i
])) {
1365 perturb_multipliers
.resize(i
+ 1);
1367 perturb_multipliers
[i
] =
1368 adj_p
/ runs
[1].get_error_perturb();
1370 if (finite(perturb_multipliers
[i
]))
1371 found_unreliable
= 1;
1376 run_index ori
= get_run_index(i
);
1378 if (old_runs
.count(ori
) == 0)
1379 old_runs
.insert(std::pair
<run_index
, run
>(ori
, runs
[1]));
1381 old_runs
[ori
] = runs
[1];
1383 perturb_multipliers
[i
] = perturb_multipliers_original
[i
]
1384 * adj_p
/ runs
[1].get_error_perturb();
1386 if (!finite(perturb_multipliers
[i
]))
1387 perturb_multipliers
[i
] = 1;
1392 && runs
[1].get_error() < runs
[0].get_error()
1393 && perturb_multipliers
[i
]
1394 / perturb_multipliers_original
[i
] < 2) {
1402 if (runs
.size() > 1)
1405 if (!found_unreliable
)
1411 * Attempt to make the current element non-trivial, by finding a nearby
1412 * alignment admitting a non-empty element region.
1414 void make_element_nontrivial(ale_pos adj_p
, ale_pos adj_o
) {
1415 assert(runs
.size() == 1);
1417 transformation
*t
= &runs
[0].offset
;
1419 if (t
->is_nontrivial())
1422 calculate_element_region();
1424 if (t
->is_nontrivial())
1427 std::vector
<transformation
> t_set
;
1428 get_perturb_set(&t_set
, adj_p
, adj_o
, 0, NULL
, NULL
);
1430 for (unsigned int i
= 0; i
< t_set
.size(); i
++) {
1432 align::calculate_element_region(&t_set
[i
], si
, ax_count
);
1434 if (t_set
[i
].is_nontrivial()) {
1445 * Adjust exposure for an aligned frame B against reference A.
1447 * Expects full-LOD images.
1449 * Note: This method does not use any weighting, by certainty or
1450 * otherwise, in the first exposure registration pass, as any bias of
1451 * weighting according to color may also bias the exposure registration
1452 * result; it does use weighting, including weighting by certainty
1453 * (even if certainty weighting is not specified), in the second pass,
1454 * under the assumption that weighting by certainty improves handling
1455 * of out-of-range highlights, and that bias of exposure measurements
1456 * according to color may generally be less harmful after spatial
1457 * registration has been performed.
1459 class exposure_ratio_iterate
: public thread::decompose_domain
{
1464 struct scale_cluster c
;
1469 void prepare_subdomains(unsigned int N
) {
1470 asums
= new pixel_accum
[N
];
1471 bsums
= new pixel_accum
[N
];
1473 void subdomain_algorithm(unsigned int thread
,
1474 int i_min
, int i_max
, int j_min
, int j_max
) {
1476 point offset
= c
.accum
->offset();
1478 for (mc_iterate
m(i_min
, i_max
, j_min
, j_max
, thread
); !m
.done(); m
++) {
1480 unsigned int i
= (unsigned int) m
.get_i();
1481 unsigned int j
= (unsigned int) m
.get_j();
1483 if (ref_excluded(i
, j
, offset
, c
.ax_parameters
, ax_count
))
1492 q
= (c
.input_scale
< 1.0 && interpolant
== NULL
)
1493 ? t
.scaled_inverse_transform(
1494 point(i
+ offset
[0], j
+ offset
[1]))
1495 : t
.unscaled_inverse_transform(
1496 point(i
+ offset
[0], j
+ offset
[1]));
1499 * Check that the transformed coordinates are within
1500 * the boundaries of array c.input, that they are not
1501 * subject to exclusion, and that the weight value in
1502 * the accumulated array is nonzero.
1505 if (input_excluded(q
[0], q
[1], c
.ax_parameters
, ax_count
))
1509 && q
[0] <= c
.input
->height() - 1
1511 && q
[1] <= c
.input
->width() - 1
1512 && c
.certainty
->get_pixel(i
, j
).minabs_norm() != 0) {
1513 pixel a
= c
.accum
->get_pixel(i
, j
);
1516 b
= c
.input
->get_bl(q
);
1518 pixel weight
= ((c
.aweight
&& pass_number
)
1519 ? c
.aweight
->get_pixel(i
, j
)
1522 ? ppow(c
.certainty
->get_pixel(i
, j
)
1523 * c
.input_certainty
->get_bl(q
, 1), 0.5)
1526 asums
[thread
] += a
* weight
;
1527 bsums
[thread
] += b
* weight
;
1532 void finish_subdomains(unsigned int N
) {
1533 for (unsigned int n
= 0; n
< N
; n
++) {
1541 exposure_ratio_iterate(pixel_accum
*_asum
,
1543 struct scale_cluster _c
,
1546 int _pass_number
) : decompose_domain(0, _c
.accum
->height(),
1547 0, _c
.accum
->width()){
1553 ax_count
= _ax_count
;
1554 pass_number
= _pass_number
;
1558 static void set_exposure_ratio(unsigned int m
, struct scale_cluster c
,
1559 transformation t
, int ax_count
, int pass_number
) {
1561 if (_exp_register
== 2) {
1563 * Use metadata only.
1565 ale_real gain_multiplier
= image_rw::exp(m
).get_gain_multiplier();
1566 pixel multiplier
= pixel(gain_multiplier
, gain_multiplier
, gain_multiplier
);
1568 image_rw::exp(m
).set_multiplier(multiplier
);
1569 ui::get()->exp_multiplier(multiplier
[0],
1576 pixel_accum
asum(0, 0, 0), bsum(0, 0, 0);
1578 exposure_ratio_iterate
eri(&asum
, &bsum
, c
, t
, ax_count
, pass_number
);
1581 // std::cerr << (asum / bsum) << " ";
1583 pixel_accum new_multiplier
;
1585 new_multiplier
= asum
/ bsum
* image_rw::exp(m
).get_multiplier();
1587 if (finite(new_multiplier
[0])
1588 && finite(new_multiplier
[1])
1589 && finite(new_multiplier
[2])) {
1590 image_rw::exp(m
).set_multiplier(new_multiplier
);
1591 ui::get()->exp_multiplier(new_multiplier
[0],
1598 * Copy all ax parameters.
1600 static exclusion
*copy_ax_parameters(int local_ax_count
, exclusion
*source
) {
1602 exclusion
*dest
= (exclusion
*) malloc(local_ax_count
* sizeof(exclusion
));
1607 ui::get()->memory_error("exclusion regions");
1609 for (int idx
= 0; idx
< local_ax_count
; idx
++)
1610 dest
[idx
] = source
[idx
];
1616 * Copy ax parameters according to frame.
1618 static exclusion
*filter_ax_parameters(int frame
, int *local_ax_count
) {
1620 exclusion
*dest
= (exclusion
*) malloc(ax_count
* sizeof(exclusion
));
1625 ui::get()->memory_error("exclusion regions");
1627 *local_ax_count
= 0;
1629 for (int idx
= 0; idx
< ax_count
; idx
++) {
1630 if (ax_parameters
[idx
].x
[4] > frame
1631 || ax_parameters
[idx
].x
[5] < frame
)
1634 dest
[*local_ax_count
] = ax_parameters
[idx
];
1636 (*local_ax_count
)++;
1642 static void scale_ax_parameters(int local_ax_count
, exclusion
*ax_parameters
,
1643 ale_pos ref_scale
, ale_pos input_scale
) {
1644 for (int i
= 0; i
< local_ax_count
; i
++) {
1645 ale_pos scale
= (ax_parameters
[i
].type
== exclusion::RENDER
)
1649 for (int n
= 0; n
< 6; n
++) {
1650 ax_parameters
[i
].x
[n
] = ax_parameters
[i
].x
[n
] * scale
;
1656 * Prepare the next level of detail for ordinary images.
1658 static const image
*prepare_lod(const image
*current
) {
1659 if (current
== NULL
)
1662 return current
->scale_by_half("prepare_lod");
1666 * Prepare the next level of detail for definition maps.
1668 static const image
*prepare_lod_def(const image
*current
) {
1669 if (current
== NULL
)
1672 return current
->defined_scale_by_half("prepare_lod_def");
1676 * Initialize scale cluster data structures.
1679 static void init_nl_cluster(struct scale_cluster
*sc
) {
1682 static struct scale_cluster
*init_clusters(int frame
, ale_real scale_factor
,
1683 const image
*input_frame
, unsigned int steps
,
1684 int *local_ax_count
) {
1687 * Allocate memory for the array.
1690 struct scale_cluster
*scale_clusters
=
1691 (struct scale_cluster
*) malloc(steps
* sizeof(struct scale_cluster
));
1693 assert (scale_clusters
);
1695 if (!scale_clusters
)
1696 ui::get()->memory_error("alignment");
1699 * Prepare images and exclusion regions for the highest level
1703 scale_clusters
[0].accum
= reference_image
;
1705 ui::get()->constructing_lod_clusters(0.0);
1706 scale_clusters
[0].input_scale
= scale_factor
;
1707 if (scale_factor
< 1.0 && interpolant
== NULL
)
1708 scale_clusters
[0].input
= input_frame
->scale(scale_factor
, "alignment");
1710 scale_clusters
[0].input
= input_frame
;
1712 scale_clusters
[0].certainty
= reference_defined
->clone("certainty");
1713 scale_clusters
[0].aweight
= alignment_weights
;
1714 scale_clusters
[0].ax_parameters
= filter_ax_parameters(frame
, local_ax_count
);
1717 * Allocate and determine input frame certainty.
1720 if (scale_clusters
[0].input
->get_bayer() != IMAGE_BAYER_NONE
) {
1721 scale_clusters
[0].input_certainty
= new image_bayer_ale_real(
1722 scale_clusters
[0].input
->height(),
1723 scale_clusters
[0].input
->width(),
1724 scale_clusters
[0].input
->depth(),
1725 scale_clusters
[0].input
->get_bayer());
1727 scale_clusters
[0].input_certainty
= scale_clusters
[0].input
->clone("certainty");
1730 for (unsigned int i
= 0; i
< scale_clusters
[0].input_certainty
->height(); i
++)
1731 for (unsigned int j
= 0; j
< scale_clusters
[0].input_certainty
->width(); j
++)
1732 for (unsigned int k
= 0; k
< 3; k
++)
1733 if (scale_clusters
[0].input
->get_channels(i
, j
) & k
)
1734 ((image
*) scale_clusters
[0].input_certainty
)->chan(i
, j
, k
) =
1735 scale_clusters
[0].input
->
1736 exp().confidence(scale_clusters
[0].input
->get_pixel(i
, j
))[k
];
1738 scale_ax_parameters(*local_ax_count
, scale_clusters
[0].ax_parameters
, scale_factor
,
1739 (scale_factor
< 1.0 && interpolant
== NULL
) ? scale_factor
: 1);
1741 init_nl_cluster(&(scale_clusters
[0]));
1744 * Prepare reduced-detail images and exclusion
1748 for (unsigned int step
= 1; step
< steps
; step
++) {
1749 ui::get()->constructing_lod_clusters(step
);
1750 scale_clusters
[step
].accum
= prepare_lod(scale_clusters
[step
- 1].accum
);
1751 scale_clusters
[step
].certainty
= prepare_lod_def(scale_clusters
[step
- 1].certainty
);
1752 scale_clusters
[step
].aweight
= prepare_lod_def(scale_clusters
[step
- 1].aweight
);
1753 scale_clusters
[step
].ax_parameters
1754 = copy_ax_parameters(*local_ax_count
, scale_clusters
[step
- 1].ax_parameters
);
1756 double sf
= scale_clusters
[step
- 1].input_scale
/ 2;
1757 scale_clusters
[step
].input_scale
= sf
;
1759 if (sf
>= 1.0 || interpolant
!= NULL
) {
1760 scale_clusters
[step
].input
= scale_clusters
[step
- 1].input
;
1761 scale_clusters
[step
].input_certainty
= scale_clusters
[step
- 1].input_certainty
;
1762 scale_ax_parameters(*local_ax_count
, scale_clusters
[step
].ax_parameters
, 0.5, 1);
1763 } else if (sf
> 0.5) {
1764 scale_clusters
[step
].input
= scale_clusters
[step
- 1].input
->scale(sf
, "alignment");
1765 scale_clusters
[step
].input_certainty
= scale_clusters
[step
- 1].input
->scale(sf
, "alignment", 1);
1766 scale_ax_parameters(*local_ax_count
, scale_clusters
[step
].ax_parameters
, 0.5, sf
);
1768 scale_clusters
[step
].input
= scale_clusters
[step
- 1].input
->scale(0.5, "alignment");
1769 scale_clusters
[step
].input_certainty
= scale_clusters
[step
- 1].input_certainty
->scale(0.5, "alignment", 1);
1770 scale_ax_parameters(*local_ax_count
, scale_clusters
[step
].ax_parameters
, 0.5, 0.5);
1773 init_nl_cluster(&(scale_clusters
[step
]));
1776 return scale_clusters
;
1780 * Destroy the first element in the scale cluster data structure.
1782 static void final_clusters(struct scale_cluster
*scale_clusters
, ale_real scale_factor
,
1783 unsigned int steps
) {
1785 if (scale_clusters
[0].input_scale
< 1.0)
1786 delete scale_clusters
[0].input
;
1788 free((void *)scale_clusters
[0].ax_parameters
);
1790 delete scale_clusters
[0].certainty
;
1792 for (unsigned int step
= 1; step
< steps
; step
++) {
1793 delete scale_clusters
[step
].accum
;
1794 delete scale_clusters
[step
].certainty
;
1795 delete scale_clusters
[step
].aweight
;
1797 if (scale_clusters
[step
].input_scale
< 1.0)
1798 delete scale_clusters
[step
].input
;
1800 free((void *)scale_clusters
[step
].ax_parameters
);
1803 free(scale_clusters
);
1807 * Calculate the centroid of a control point for the set of frames
1808 * having index lower than m. Divide by any scaling of the output.
1810 static point
unscaled_centroid(unsigned int m
, unsigned int p
) {
1813 point
point_sum(0, 0);
1814 ale_accum divisor
= 0;
1816 for(unsigned int j
= 0; j
< m
; j
++) {
1817 point pp
= cp_array
[p
][j
];
1820 point_sum
+= kept_t
[j
].transform_unscaled(pp
)
1821 / kept_t
[j
].scale();
1827 return point::undefined();
1829 return point_sum
/ divisor
;
1833 * Calculate centroid of this frame, and of all previous frames,
1834 * from points common to both sets.
1836 static void centroids(unsigned int m
, point
*current
, point
*previous
) {
1838 * Calculate the translation
1840 point
other_centroid(0, 0);
1841 point
this_centroid(0, 0);
1842 ale_pos divisor
= 0;
1844 for (unsigned int i
= 0; i
< cp_count
; i
++) {
1845 point other_c
= unscaled_centroid(m
, i
);
1846 point this_c
= cp_array
[i
][m
];
1848 if (!other_c
.defined() || !this_c
.defined())
1851 other_centroid
+= other_c
;
1852 this_centroid
+= this_c
;
1858 *current
= point::undefined();
1859 *previous
= point::undefined();
1863 *current
= this_centroid
/ divisor
;
1864 *previous
= other_centroid
/ divisor
;
1868 * Calculate the RMS error of control points for frame m, with
1869 * transformation t, against control points for earlier frames.
1871 static ale_accum
cp_rms_error(unsigned int m
, transformation t
) {
1875 ale_accum divisor
= 0;
1877 for (unsigned int i
= 0; i
< cp_count
; i
++)
1878 for (unsigned int j
= 0; j
< m
; j
++) {
1879 const point
*p
= cp_array
[i
];
1880 point p_ref
= kept_t
[j
].transform_unscaled(p
[j
]);
1881 point p_cur
= t
.transform_unscaled(p
[m
]);
1883 if (!p_ref
.defined() || !p_cur
.defined())
1886 err
+= p_ref
.lengthtosq(p_cur
);
1890 return sqrt(err
/ divisor
);
1894 * Implement new delta --follow semantics.
1896 * If we have a transformation T such that
1898 * prev_final == T(prev_init)
1902 * current_init_follow == T(current_init)
1904 * We can calculate T as follows:
1906 * T == prev_final(prev_init^-1)
1908 * Where ^-1 is the inverse operator.
1910 static transformation
follow(element_t
*element
, transformation offset
, int lod
) {
1912 transformation new_offset
= offset
;
1915 * Criteria for using following.
1918 if (!element
->old_is_default
&& !element
->is_default
&&
1919 default_initial_alignment_type
== 1) {
1921 * Ensure that the lod for the old initial and final
1922 * alignments are equal to the lod for the new initial
1926 ui::get()->following();
1928 element
->old_final_alignment
.rescale (1 / pow(2, lod
));
1929 element
->old_initial_alignment
.rescale(1 / pow(2, lod
- element
->old_lod
));
1931 for (offset
.set_current_index(0),
1932 element
->old_initial_alignment
.set_current_index(0),
1933 element
->old_final_alignment
.set_current_index(0),
1934 new_offset
.set_current_index(0);
1936 offset
.get_current_index() < _ma_card
;
1938 offset
.push_element(),
1939 new_offset
.push_element()) {
1941 if (alignment_class
== 0) {
1943 * Translational transformations
1946 ale_pos t0
= -element
->old_initial_alignment
.eu_get(0)
1947 + element
->old_final_alignment
.eu_get(0);
1948 ale_pos t1
= -element
->old_initial_alignment
.eu_get(1)
1949 + element
->old_final_alignment
.eu_get(1);
1951 new_offset
.eu_modify(0, t0
);
1952 new_offset
.eu_modify(1, t1
);
1954 } else if (alignment_class
== 1) {
1956 * Euclidean transformations
1959 ale_pos t2
= -element
->old_initial_alignment
.eu_get(2)
1960 + element
->old_final_alignment
.eu_get(2);
1962 new_offset
.eu_modify(2, t2
);
1964 point
p( offset
.scaled_height()/2 + offset
.eu_get(0) - element
->old_initial_alignment
.eu_get(0),
1965 offset
.scaled_width()/2 + offset
.eu_get(1) - element
->old_initial_alignment
.eu_get(1) );
1967 p
= element
->old_final_alignment
.transform_scaled(p
);
1969 new_offset
.eu_modify(0, p
[0] - offset
.scaled_height()/2 - offset
.eu_get(0));
1970 new_offset
.eu_modify(1, p
[1] - offset
.scaled_width()/2 - offset
.eu_get(1));
1972 } else if (alignment_class
== 2) {
1974 * Projective transformations
1979 p
[0] = element
->old_final_alignment
.transform_scaled(element
->old_initial_alignment
1980 . scaled_inverse_transform(offset
.get_current_element().transform_scaled(point( 0 , 0 ))));
1981 p
[1] = element
->old_final_alignment
.transform_scaled(element
->old_initial_alignment
1982 . scaled_inverse_transform(offset
.get_current_element().transform_scaled(point(offset
.scaled_height(), 0 ))));
1983 p
[2] = element
->old_final_alignment
.transform_scaled(element
->old_initial_alignment
1984 . scaled_inverse_transform(offset
.get_current_element().transform_scaled(point(offset
.scaled_height(), offset
.scaled_width()))));
1985 p
[3] = element
->old_final_alignment
.transform_scaled(element
->old_initial_alignment
1986 . scaled_inverse_transform(offset
.get_current_element().transform_scaled(point( 0 , offset
.scaled_width()))));
1988 new_offset
.gpt_set(p
);
1992 ui::get()->set_offset(offset
);
1998 static void test_global(diff_stat_t
*here
, scale_cluster si
, transformation t
,
1999 int local_ax_count
, int m
, ale_pos local_gs_mo
, ale_pos perturb
) {
2001 diff_stat_t
test(*here
);
2003 test
.diff(si
, perturb
, t
, local_ax_count
, m
);
2005 unsigned int ovl
= overlap(si
, t
, local_ax_count
);
2007 if (ovl
>= local_gs_mo
&& test
.better()) {
2010 ui::get()->set_match(here
->get_error());
2011 ui::get()->set_offset(here
->get_offset());
2019 * Get the set of global transformations for a given density
2021 static void test_globals(diff_stat_t
*here
,
2022 scale_cluster si
, transformation t
, int local_gs
, ale_pos adj_p
,
2023 int local_ax_count
, int m
, ale_pos local_gs_mo
, ale_pos perturb
) {
2025 transformation offset
= t
;
2029 transformation offset_p
= offset
;
2031 if (!offset_p
.is_projective())
2032 offset_p
.eu_to_gpt();
2034 min
= max
= offset_p
.gpt_get(0);
2035 for (int p_index
= 1; p_index
< 4; p_index
++) {
2036 point p
= offset_p
.gpt_get(p_index
);
2047 point inner_min_t
= -min
;
2048 point inner_max_t
= -max
+ point(si
.accum
->height(), si
.accum
->width());
2049 point outer_min_t
= -max
+ point(adj_p
- 1, adj_p
- 1);
2050 point outer_max_t
= point(si
.accum
->height(), si
.accum
->width()) - point(adj_p
, adj_p
);
2052 if (local_gs
== 1 || local_gs
== 3 || local_gs
== 4 || local_gs
== 6) {
2058 for (ale_pos i
= inner_min_t
[0]; i
<= inner_max_t
[0]; i
+= adj_p
)
2059 for (ale_pos j
= inner_min_t
[1]; j
<= inner_max_t
[1]; j
+= adj_p
) {
2060 transformation test_t
= offset
;
2061 test_t
.translate(point(i
, j
));
2062 test_global(here
, si
, test_t
, local_ax_count
, m
, local_gs_mo
, perturb
);
2066 if (local_gs
== 2 || local_gs
== 3 || local_gs
== -1 || local_gs
== 6) {
2072 for (ale_pos i
= outer_min_t
[0]; i
<= outer_max_t
[0]; i
+= adj_p
)
2073 for (ale_pos j
= outer_min_t
[1]; j
< inner_min_t
[1]; j
+= adj_p
) {
2074 transformation test_t
= offset
;
2075 test_t
.translate(point(i
, j
));
2076 test_global(here
, si
, test_t
, local_ax_count
, m
, local_gs_mo
, perturb
);
2078 for (ale_pos i
= outer_min_t
[0]; i
<= outer_max_t
[0]; i
+= adj_p
)
2079 for (ale_pos j
= outer_max_t
[1]; j
> inner_max_t
[1]; j
-= adj_p
) {
2080 transformation test_t
= offset
;
2081 test_t
.translate(point(i
, j
));
2082 test_global(here
, si
, test_t
, local_ax_count
, m
, local_gs_mo
, perturb
);
2084 for (ale_pos i
= outer_min_t
[0]; i
< inner_min_t
[0]; i
+= adj_p
)
2085 for (ale_pos j
= outer_min_t
[1]; j
<= outer_max_t
[1]; j
+= adj_p
) {
2086 transformation test_t
= offset
;
2087 test_t
.translate(point(i
, j
));
2088 test_global(here
, si
, test_t
, local_ax_count
, m
, local_gs_mo
, perturb
);
2090 for (ale_pos i
= outer_max_t
[0]; i
> inner_max_t
[0]; i
-= adj_p
)
2091 for (ale_pos j
= outer_min_t
[1]; j
<= outer_max_t
[1]; j
+= adj_p
) {
2092 transformation test_t
= offset
;
2093 test_t
.translate(point(i
, j
));
2094 test_global(here
, si
, test_t
, local_ax_count
, m
, local_gs_mo
, perturb
);
2099 static void get_translational_set(std::vector
<transformation
> *set
,
2100 transformation t
, ale_pos adj_p
) {
2104 transformation offset
= t
;
2105 transformation test_t
;
2107 for (int i
= 0; i
< 2; i
++)
2108 for (adj_s
= -adj_p
; adj_s
<= adj_p
; adj_s
+= 2 * adj_p
) {
2112 test_t
.translate(i
? point(adj_s
, 0) : point(0, adj_s
));
2114 set
->push_back(test_t
);
2118 static int threshold_ok(ale_accum error
) {
2119 if ((1 - error
) * 100 >= match_threshold
)
2122 if (!(match_threshold
>= 0))
2129 * Align frame m against the reference.
2131 * XXX: the transformation class currently combines ordinary
2132 * transformations with scaling. This is somewhat convenient for
2133 * some things, but can also be confusing. This method, _align(), is
2134 * one case where special care must be taken to ensure that the scale
2135 * is always set correctly (by using the 'rescale' method).
2137 static diff_stat_t
_align(int m
, int local_gs
, element_t
*element
) {
2139 const image
*input_frame
= element
->input_frame
;
2142 * Local upper/lower data, possibly dependent on image
2146 ale_pos local_lower
, local_upper
, local_gs_mo
;
2149 * Select the minimum dimension as the reference.
2152 ale_pos reference_size
= input_frame
->height();
2153 if (input_frame
->width() < reference_size
)
2154 reference_size
= input_frame
->width();
2155 ale_pos reference_area
= input_frame
->height()
2156 * input_frame
->width();
2158 if (perturb_lower_percent
)
2159 local_lower
= perturb_lower
2164 local_lower
= perturb_lower
;
2166 if (perturb_upper_percent
)
2167 local_upper
= perturb_upper
2172 local_upper
= perturb_upper
;
2174 local_upper
= pow(2, floor(log(local_upper
) / log(2)));
2177 local_gs_mo
= _gs_mo
2182 local_gs_mo
= _gs_mo
;
2185 * Logarithms aren't exact, so we divide repeatedly to discover
2186 * how many steps will occur, and pass this information to the
2191 double step_variable
= local_upper
;
2192 while (step_variable
>= local_lower
) {
2197 ui::get()->set_steps(step_count
);
2199 ale_pos perturb
= local_upper
;
2203 kept_t
[latest
] = latest_t
;
2204 kept_ok
[latest
] = latest_ok
;
2208 * Maximum level-of-detail. Use a level of detail at most
2209 * 2^lod_diff finer than the adjustment resolution. lod_diff
2210 * is a synonym for lod_max.
2213 const int lod_diff
= lod_max
;
2216 * Determine how many levels of detail should be prepared.
2220 * Plain (unsigned int) casting seems to be broken in some cases.
2223 unsigned int steps
= (perturb
> pow(2, lod_max
))
2224 ? (unsigned int) lrint(log(perturb
) / log(2)) - lod_max
+ 1 : 1;
2227 * Prepare multiple levels of detail.
2231 struct scale_cluster
*scale_clusters
= init_clusters(m
,
2232 scale_factor
, input_frame
, steps
,
2236 * Initialize variables used in the main loop.
2242 * Initialize the default initial transform
2245 if (default_initial_alignment_type
== 0) {
2248 * Follow the transformation of the original frame,
2249 * setting new image dimensions.
2252 // element->default_initial_alignment = orig_t;
2253 element
->default_initial_alignment
.set_current_element(orig_t
.get_element(0));
2254 element
->default_initial_alignment
.set_dimensions(input_frame
);
2256 } else if (default_initial_alignment_type
== 1)
2259 * Follow previous transformation, setting new image
2263 element
->default_initial_alignment
.set_dimensions(input_frame
);
2268 element
->old_is_default
= element
->is_default
;
2271 * Scale default initial transform for lod
2274 element
->default_initial_alignment
.rescale(1 / pow(2, lod
));
2277 * Set the default transformation.
2280 transformation offset
= element
->default_initial_alignment
;
2283 * Load any file-specified transformations
2286 for (offset
.set_current_index(0);
2287 offset
.get_current_index() < _ma_card
;
2288 offset
.push_element()) {
2290 offset
= tload_next(tload
, alignment_class
== 2,
2292 &element
->is_default
,
2293 offset
.get_current_index() == 0);
2297 offset
.set_current_index(0);
2299 ui::get()->set_offset(offset
);
2304 * Apply following logic
2307 transformation new_offset
= follow(element
, offset
, lod
);
2309 new_offset
.set_current_index(0);
2311 element
->old_initial_alignment
= offset
;
2312 element
->old_lod
= lod
;
2313 offset
= new_offset
;
2316 element
->old_initial_alignment
= offset
;
2317 element
->old_lod
= lod
;
2320 struct scale_cluster si
= scale_clusters
[lod
];
2323 * Projective adjustment value
2326 ale_pos adj_p
= (perturb
>= pow(2, lod_diff
))
2327 ? pow(2, lod_diff
) : (double) perturb
;
2330 * Orientational adjustment value in degrees.
2332 * Since rotational perturbation is now specified as an
2333 * arclength, we have to convert.
2336 ale_pos adj_o
= 2 * perturb
2337 / sqrt(pow(scale_clusters
[0].input
->height(), 2)
2338 + pow(scale_clusters
[0].input
->width(), 2))
2343 * Barrel distortion adjustment value
2346 ale_pos adj_b
= perturb
* bda_mult
;
2349 * Global search overlap requirements.
2352 local_gs_mo
/= pow(pow(2, lod
), 2);
2355 * Pre-alignment exposure adjustment
2358 if (_exp_register
) {
2359 ui::get()->exposure_1();
2360 transformation o
= offset
;
2361 for (int k
= lod
; k
> 0; k
--)
2363 set_exposure_ratio(m
, scale_clusters
[0], o
, local_ax_count
, 0);
2367 * Alignment statistics.
2373 * Current difference (error) value
2376 ui::get()->prematching();
2377 here
.diff(si
, perturb
, offset
, local_ax_count
, m
);
2378 ui::get()->set_match(here
.get_error());
2381 * Current and modified barrel distortion parameters
2384 ale_pos current_bd
[BARREL_DEGREE
];
2385 ale_pos modified_bd
[BARREL_DEGREE
];
2386 offset
.bd_get(current_bd
);
2387 offset
.bd_get(modified_bd
);
2390 * Translational global search step
2393 if (perturb
>= local_lower
&& local_gs
!= 0 && local_gs
!= 5
2394 && (local_gs
!= 6 || element
->is_default
)) {
2396 ui::get()->global_alignment(perturb
, lod
);
2397 ui::get()->gs_mo(local_gs_mo
);
2399 test_globals(&here
, si
, here
.get_offset(), local_gs
, adj_p
,
2400 local_ax_count
, m
, local_gs_mo
, perturb
);
2402 ui::get()->set_match(here
.get_error());
2403 ui::get()->set_offset(here
.get_offset());
2407 * Control point alignment
2410 if (local_gs
== 5) {
2412 transformation o
= here
.get_offset();
2414 for (int k
= lod
; k
> 0; k
--)
2418 * Determine centroid data
2421 point current
, previous
;
2422 centroids(m
, ¤t
, &previous
);
2424 if (current
.defined() && previous
.defined()) {
2426 o
.set_dimensions(input_frame
);
2427 o
.translate((previous
- current
) * o
.scale());
2432 * Determine rotation for alignment classes other than translation.
2435 ale_accum lowest_error
= cp_rms_error(m
, o
);
2437 ale_pos rot_lower
= 2 * local_lower
2438 / sqrt(pow(scale_clusters
[0].input
->height(), 2)
2439 + pow(scale_clusters
[0].input
->width(), 2))
2443 if (alignment_class
> 0)
2444 for (ale_pos rot
= 30; rot
> rot_lower
; rot
/= 2)
2445 for (ale_pos srot
= -rot
; srot
< rot
* 1.5; srot
+= rot
* 2) {
2446 int is_improved
= 1;
2447 while (is_improved
) {
2449 transformation test_t
= o
;
2451 * XXX: is this right?
2453 test_t
.rotate(current
* o
.scale(), srot
);
2454 ale_pos test_v
= cp_rms_error(m
, test_t
);
2456 if (test_v
< lowest_error
) {
2457 lowest_error
= test_v
;
2466 * Determine projective parameters through a local
2470 if (alignment_class
== 2) {
2471 ale_accum adj_p
= lowest_error
;
2473 if (adj_p
< local_lower
)
2474 adj_p
= local_lower
;
2476 while (adj_p
>= local_lower
) {
2477 transformation test_t
= o
;
2478 int is_improved
= 1;
2482 while (is_improved
) {
2485 for (int i
= 0; i
< 4; i
++)
2486 for (int j
= 0; j
< 2; j
++)
2487 for (adj_s
= -adj_p
; adj_s
<= adj_p
; adj_s
+= 2 * adj_p
) {
2491 if (perturb_type
== 0)
2492 test_t
.gpt_modify(j
, i
, adj_s
);
2493 else if (perturb_type
== 1)
2494 test_t
.gr_modify(j
, i
, adj_s
);
2498 test_v
= cp_rms_error(m
, test_t
);
2500 if (test_v
< lowest_error
) {
2501 lowest_error
= test_v
;
2513 set_exposure_ratio(m
, scale_clusters
[0], o
, local_ax_count
, 0);
2515 for (int k
= lod
; k
> 0; k
--)
2518 here
.diff(si
, perturb
, o
, local_ax_count
, m
);
2520 ui::get()->set_match(here
.get_error());
2521 ui::get()->set_offset(here
.get_offset());
2525 * Announce perturbation size
2528 ui::get()->aligning(perturb
, lod
);
2531 * Run initial tests to get perturbation multipliers and error
2535 std::vector
<transformation
> t_set
;
2537 here
.get_perturb_set(&t_set
, adj_p
, adj_o
, adj_b
, current_bd
, modified_bd
);
2540 * Perturbation adjustment loop.
2543 int stable_count
= 0;
2545 while (perturb
>= local_lower
) {
2548 * Orientational adjustment value in degrees.
2550 * Since rotational perturbation is now specified as an
2551 * arclength, we have to convert.
2554 ale_pos adj_o
= 2 * perturb
2555 / sqrt(pow(scale_clusters
[0].input
->height(), 2)
2556 + pow(scale_clusters
[0].input
->width(), 2))
2561 * Barrel distortion adjustment value
2564 ale_pos adj_b
= perturb
* bda_mult
;
2566 diff_stat_t old_here
= here
;
2568 here
.perturb_test(perturb
, adj_p
, adj_o
, adj_b
, current_bd
, modified_bd
,
2571 if (here
.get_offset() == old_here
.get_offset())
2576 if (stable_count
== 3) {
2580 here
.calculate_element_region();
2582 if (here
.get_current_index() + 1 < _ma_card
) {
2583 here
.push_element();
2584 here
.make_element_nontrivial(adj_p
, adj_o
);
2585 element
->is_primary
= 0;
2588 here
.set_current_index(0);
2590 element
->is_primary
= 1;
2597 * Work with images twice as large
2601 si
= scale_clusters
[lod
];
2604 * Rescale the transforms.
2607 here
.rescale(2, si
);
2608 element
->default_initial_alignment
.rescale(2);
2618 ui::get()->alignment_perturbation_level(perturb
, lod
);
2623 ui::get()->set_match(here
.get_error());
2624 ui::get()->set_offset(here
.get_offset());
2627 here
.set_current_index(0);
2629 offset
= here
.get_offset();
2632 here
.rescale(pow(2, lod
), scale_clusters
[0]);
2633 element
->default_initial_alignment
.rescale(pow(2, lod
));
2637 * Post-alignment exposure adjustment
2640 if (_exp_register
== 1) {
2641 ui::get()->exposure_2();
2642 set_exposure_ratio(m
, scale_clusters
[0], offset
, local_ax_count
, 1);
2649 ui::get()->postmatching();
2650 offset
.use_full_support();
2651 here
.diff(scale_clusters
[0], perturb
, offset
, local_ax_count
, m
);
2653 offset
.use_restricted_support();
2654 ui::get()->set_match(here
.get_error());
2657 * Free the level-of-detail structures
2660 final_clusters(scale_clusters
, scale_factor
, steps
);
2663 * Ensure that the match meets the threshold.
2666 if (threshold_ok(here
.get_error())) {
2668 * Update alignment variables
2671 element
->default_initial_alignment
= offset
;
2672 element
->old_final_alignment
= offset
;
2673 ui::get()->alignment_match_ok();
2674 } else if (local_gs
== 4) {
2677 * Align with outer starting points.
2681 * XXX: This probably isn't exactly the right thing to do,
2682 * since variables like old_initial_value have been overwritten.
2685 diff_stat_t nested_result
= _align(m
, -1, element
);
2687 if (threshold_ok(nested_result
.get_error())) {
2688 return nested_result
;
2689 } else if (nested_result
.get_error() < here
.get_error()) {
2690 here
= nested_result
;
2693 if (is_fail_default
)
2694 offset
= element
->default_initial_alignment
;
2696 ui::get()->set_match(here
.get_error());
2697 ui::get()->alignment_no_match();
2699 } else if (local_gs
== -1) {
2706 if (is_fail_default
)
2707 offset
= element
->default_initial_alignment
;
2709 ui::get()->alignment_no_match();
2713 * Write the tonal registration multiplier as a comment.
2716 pixel trm
= image_rw::exp(m
).get_multiplier();
2717 tsave_trm(tsave
, trm
[0], trm
[1], trm
[2]);
2720 * Save the transformation information
2723 for (offset
.set_current_index(0);
2724 offset
.get_current_index() < _ma_card
;
2725 offset
.push_element()) {
2727 tsave_next(tsave
, offset
, alignment_class
== 2,
2728 offset
.get_current_index() == 0);
2731 offset
.set_current_index(0);
2736 * Update match statistics.
2739 match_sum
+= (1 - here
.get_error()) * 100;
2748 * High-pass filter for frequency weights
2750 static void hipass(int rows
, int cols
, fftw_complex
*inout
) {
2751 for (int i
= 0; i
< rows
* vert_freq_cut
; i
++)
2752 for (int j
= 0; j
< cols
; j
++)
2753 for (int k
= 0; k
< 2; k
++)
2754 inout
[i
* cols
+ j
][k
] = 0;
2755 for (int i
= 0; i
< rows
; i
++)
2756 for (int j
= 0; j
< cols
* horiz_freq_cut
; j
++)
2757 for (int k
= 0; k
< 2; k
++)
2758 inout
[i
* cols
+ j
][k
] = 0;
2759 for (int i
= 0; i
< rows
; i
++)
2760 for (int j
= 0; j
< cols
; j
++)
2761 for (int k
= 0; k
< 2; k
++)
2762 if (i
/ (double) rows
+ j
/ (double) cols
< 2 * avg_freq_cut
)
2763 inout
[i
* cols
+ j
][k
] = 0;
2769 * Reset alignment weights
2771 static void reset_weights() {
2772 if (alignment_weights
!= NULL
)
2773 delete alignment_weights
;
2775 alignment_weights
= NULL
;
2779 * Initialize alignment weights
2781 static void init_weights() {
2782 if (alignment_weights
!= NULL
)
2785 int rows
= reference_image
->height();
2786 int cols
= reference_image
->width();
2787 int colors
= reference_image
->depth();
2789 alignment_weights
= new image_ale_real(rows
, cols
,
2790 colors
, "alignment_weights");
2792 assert (alignment_weights
);
2794 for (int i
= 0; i
< rows
; i
++)
2795 for (int j
= 0; j
< cols
; j
++)
2796 alignment_weights
->set_pixel(i
, j
, pixel(1, 1, 1));
2800 * Update alignment weights with weight map
2802 static void map_update() {
2804 if (weight_map
== NULL
)
2809 point map_offset
= reference_image
->offset() - weight_map
->offset();
2811 int rows
= reference_image
->height();
2812 int cols
= reference_image
->width();
2814 for (int i
= 0; i
< rows
; i
++)
2815 for (int j
= 0; j
< cols
; j
++) {
2816 point map_weight_position
= map_offset
+ point(i
, j
);
2817 if (map_weight_position
[0] >= 0
2818 && map_weight_position
[1] >= 0
2819 && map_weight_position
[0] <= weight_map
->height() - 1
2820 && map_weight_position
[1] <= weight_map
->width() - 1)
2821 alignment_weights
->pix(i
, j
) *= weight_map
->get_bl(map_weight_position
);
2826 * Update alignment weights with algorithmic weights
2828 static void wmx_update() {
2831 static exposure
*exp_def
= new exposure_default();
2832 static exposure
*exp_bool
= new exposure_boolean();
2834 if (wmx_file
== NULL
|| wmx_exec
== NULL
|| wmx_defs
== NULL
)
2837 unsigned int rows
= reference_image
->height();
2838 unsigned int cols
= reference_image
->width();
2840 image_rw::write_image(wmx_file
, reference_image
);
2841 image_rw::write_image(wmx_defs
, reference_defined
, exp_bool
);
2844 int exit_status
= 1;
2846 execlp(wmx_exec
, wmx_exec
, wmx_file
, wmx_defs
, NULL
);
2847 ui::get()->exec_failure(wmx_exec
, wmx_file
, wmx_defs
);
2853 ui::get()->fork_failure("d2::align");
2855 image
*wmx_weights
= image_rw::read_image(wmx_file
, exp_def
);
2857 if (wmx_weights
->height() != rows
|| wmx_weights
->width() != cols
)
2858 ui::get()->error("algorithmic weighting must not change image size");
2860 if (alignment_weights
== NULL
)
2861 alignment_weights
= wmx_weights
;
2863 for (unsigned int i
= 0; i
< rows
; i
++)
2864 for (unsigned int j
= 0; j
< cols
; j
++)
2865 alignment_weights
->pix(i
, j
) *= wmx_weights
->pix(i
, j
);
2870 * Update alignment weights with frequency weights
2872 static void fw_update() {
2874 if (horiz_freq_cut
== 0
2875 && vert_freq_cut
== 0
2876 && avg_freq_cut
== 0)
2880 * Required for correct operation of --fwshow
2883 assert (alignment_weights
== NULL
);
2885 int rows
= reference_image
->height();
2886 int cols
= reference_image
->width();
2887 int colors
= reference_image
->depth();
2889 alignment_weights
= new image_ale_real(rows
, cols
,
2890 colors
, "alignment_weights");
2892 fftw_complex
*inout
= (fftw_complex
*) fftw_malloc(sizeof(fftw_complex
) * rows
* cols
);
2896 fftw_plan pf
= fftw_plan_dft_2d(rows
, cols
,
2898 FFTW_FORWARD
, FFTW_ESTIMATE
);
2900 fftw_plan pb
= fftw_plan_dft_2d(rows
, cols
,
2902 FFTW_BACKWARD
, FFTW_ESTIMATE
);
2904 for (int k
= 0; k
< colors
; k
++) {
2905 for (int i
= 0; i
< rows
* cols
; i
++) {
2906 inout
[i
][0] = reference_image
->get_pixel(i
/ cols
, i
% cols
)[k
];
2911 hipass(rows
, cols
, inout
);
2914 for (int i
= 0; i
< rows
* cols
; i
++) {
2916 alignment_weights
->pix(i
/ cols
, i
% cols
)[k
] = fabs(inout
[i
][0] / (rows
* cols
));
2918 alignment_weights
->pix(i
/ cols
, i
% cols
)[k
] =
2919 sqrt(pow(inout
[i
][0] / (rows
* cols
), 2)
2920 + pow(inout
[i
][1] / (rows
* cols
), 2));
2925 fftw_destroy_plan(pf
);
2926 fftw_destroy_plan(pb
);
2929 if (fw_output
!= NULL
)
2930 image_rw::write_image(fw_output
, alignment_weights
);
2935 * Update alignment to frame N.
2937 static void update_to(int n
) {
2939 assert (n
<= latest
+ 1);
2942 static std::vector
<element_t
> elements
;
2949 * Handle the initial frame
2952 elements
[0].input_frame
= image_rw::open(n
);
2954 const image
*i
= elements
[0].input_frame
;
2956 transformation result
= alignment_class
== 2
2957 ? transformation::gpt_identity(i
, scale_factor
)
2958 : transformation::eu_identity(i
, scale_factor
);
2959 result
= tload_first(tload
, alignment_class
== 2, result
, &is_default
);
2960 tsave_first(tsave
, result
, alignment_class
== 2);
2963 kept_t
= new transformation
[image_rw::count()];
2964 kept_ok
= (int *) malloc(image_rw::count()
2969 if (!kept_t
|| !kept_ok
)
2970 ui::get()->memory_error("alignment");
2980 elements
[0].default_initial_alignment
= result
;
2986 for (int i
= latest
+ 1; i
<= n
; i
++) {
2990 * Handle supplemental frames.
2993 assert (reference
!= NULL
);
2995 ui::get()->set_arender_current();
2996 reference
->sync(i
- 1);
2997 ui::get()->clear_arender_current();
2998 reference_image
= reference
->get_image();
2999 reference_defined
= reference
->get_defined();
3006 assert (reference_image
!= NULL
);
3007 assert (reference_defined
!= NULL
);
3009 elements
[j
].input_frame
= image_rw::open(i
);
3010 elements
[j
].is_primary
= 1;
3012 _align(i
, _gs
, &elements
[j
]);
3017 if (elements
.size() > _ma_card
)
3018 elements
.resize(_ma_card
);
3024 * Set the control point count
3026 static void set_cp_count(unsigned int n
) {
3027 assert (cp_array
== NULL
);
3030 cp_array
= (const point
**) malloc(n
* sizeof(const point
*));
3034 * Set control points.
3036 static void set_cp(unsigned int i
, const point
*p
) {
3043 static void exp_register() {
3048 * Register exposure only based on metadata
3050 static void exp_meta_only() {
3055 * Don't register exposure
3057 static void exp_noregister() {
3062 * Set alignment class to translation only. Only adjust the x and y
3063 * position of images. Do not rotate input images or perform
3064 * projective transformations.
3066 static void class_translation() {
3067 alignment_class
= 0;
3071 * Set alignment class to Euclidean transformations only. Adjust the x
3072 * and y position of images and the orientation of the image about the
3075 static void class_euclidean() {
3076 alignment_class
= 1;
3080 * Set alignment class to perform general projective transformations.
3081 * See the file gpt.h for more information about general projective
3084 static void class_projective() {
3085 alignment_class
= 2;
3089 * Set the default initial alignment to the identity transformation.
3091 static void initial_default_identity() {
3092 default_initial_alignment_type
= 0;
3096 * Set the default initial alignment to the most recently matched
3097 * frame's final transformation.
3099 static void initial_default_follow() {
3100 default_initial_alignment_type
= 1;
3104 * Perturb output coordinates.
3106 static void perturb_output() {
3111 * Perturb source coordinates.
3113 static void perturb_source() {
3118 * Frames under threshold align optimally
3120 static void fail_optimal() {
3121 is_fail_default
= 0;
3125 * Frames under threshold keep their default alignments.
3127 static void fail_default() {
3128 is_fail_default
= 1;
3132 * Align images with an error contribution from each color channel.
3135 channel_alignment_type
= 0;
3139 * Align images with an error contribution only from the green channel.
3140 * Other color channels do not affect alignment.
3142 static void green() {
3143 channel_alignment_type
= 1;
3147 * Align images using a summation of channels. May be useful when
3148 * dealing with images that have high frequency color ripples due to
3152 channel_alignment_type
= 2;
3156 * Error metric exponent
3159 static void set_metric_exponent(float me
) {
3160 metric_exponent
= me
;
3167 static void set_match_threshold(float mt
) {
3168 match_threshold
= mt
;
3172 * Perturbation lower and upper bounds.
3175 static void set_perturb_lower(ale_pos pl
, int plp
) {
3177 perturb_lower_percent
= plp
;
3180 static void set_perturb_upper(ale_pos pu
, int pup
) {
3182 perturb_upper_percent
= pup
;
3186 * Maximum rotational perturbation.
3189 static void set_rot_max(int rm
) {
3192 * Obtain the largest power of two not larger than rm.
3195 rot_max
= pow(2, floor(log(rm
) / log(2)));
3199 * Barrel distortion adjustment multiplier
3202 static void set_bda_mult(ale_pos m
) {
3207 * Barrel distortion maximum rate of change
3210 static void set_bda_rate(ale_pos m
) {
3218 static void set_lod_max(int lm
) {
3223 * Set the scale factor
3225 static void set_scale(ale_pos s
) {
3230 * Set reference rendering to align against
3232 static void set_reference(render
*r
) {
3237 * Set the interpolant
3239 static void set_interpolant(filter::scaled_filter
*f
) {
3244 * Set alignment weights image
3246 static void set_weight_map(const image
*i
) {
3251 * Set frequency cuts
3253 static void set_frequency_cut(double h
, double v
, double a
) {
3260 * Set algorithmic alignment weighting
3262 static void set_wmx(const char *e
, const char *f
, const char *d
) {
3269 * Show frequency weights
3271 static void set_fl_show(const char *filename
) {
3272 fw_output
= filename
;
3276 * Set transformation file loader.
3278 static void set_tload(tload_t
*tl
) {
3283 * Set transformation file saver.
3285 static void set_tsave(tsave_t
*ts
) {
3290 * Get match statistics for frame N.
3292 static int match(int n
) {
3306 * Message that old alignment data should be kept.
3308 static void keep() {
3309 assert (latest
== -1);
3314 * Get alignment for frame N.
3316 static transformation
of(int n
) {
3329 * Set calculation precision
3331 static void set_precise(int p
) {
3332 precise_calculation
= p
;
3336 * Set the certainty-weighted flag.
3338 static void certainty_weighted(int flag
) {
3339 certainty_weights
= flag
;
3343 * Set the global search type.
3345 static void gs(const char *type
) {
3346 if (!strcmp(type
, "local")) {
3348 } else if (!strcmp(type
, "inner")) {
3350 } else if (!strcmp(type
, "outer")) {
3352 } else if (!strcmp(type
, "all")) {
3354 } else if (!strcmp(type
, "central")) {
3356 } else if (!strcmp(type
, "defaults")) {
3358 } else if (!strcmp(type
, "points")) {
3362 ui::get()->error("bad global search type");
3367 * Multi-alignment contiguity
3369 static void ma_cont(double value
) {
3374 * Multi-alignment cardinality
3376 static void ma_card(unsigned int value
) {
3377 assert (value
>= 1);
3382 * Set the minimum overlap for global searching
3384 static void gs_mo(ale_pos value
, int _gs_mo_percent
) {
3386 gs_mo_percent
= _gs_mo_percent
;
3390 * Set alignment exclusion regions
3392 static void set_exclusion(exclusion
*_ax_parameters
, int _ax_count
) {
3393 ax_count
= _ax_count
;
3394 ax_parameters
= _ax_parameters
;
3398 * Get match summary statistics.
3400 static ale_accum
match_summary() {
3401 return match_sum
/ match_count
;