File: alignment_points.py

package info (click to toggle)
planetary-system-stacker 0.8.32~git20230901.01f3625-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 50,468 kB
  • sloc: python: 14,055; makefile: 3
file content (1034 lines) | stat: -rw-r--r-- 54,253 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
# -*- coding: utf-8; -*-
"""
Copyright (c) 2018 Rolf Hempel, rolf6419@gmx.de

This file is part of the PlanetarySystemStacker tool (PSS).
https://github.com/Rolf-Hempel/PlanetarySystemStacker

PSS is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with PSS.  If not, see <http://www.gnu.org/licenses/>.

"""

from glob import glob
from time import time

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from math import ceil
from numpy import arange, amax, stack, amin, float32, uint8, zeros, sqrt, empty, int32, uint16
from scipy import ndimage
try:
    from skimage.registration import phase_cross_correlation
except ImportError:
    from skimage.feature import register_translation as phase_cross_correlation
from cv2 import meanStdDev, GaussianBlur

from align_frames import AlignFrames
from configuration import Configuration
from exceptions import NotSupportedError, Error
from frames import Frames
from miscellaneous import Miscellaneous
from rank_frames import RankFrames


class AlignmentPoints(object):
    """
        Create a rectangular grid of potential places for alignment points. For each location
        create a small "alignment box" around it. For each alignment box test if there is enough
        structure and brightness in the picture to use it as an alignment point. For all
        alignment points in all frames, compute the local shifts relative to the mean frame.

    """

    def __init__(self, configuration, frames, rank_frames, align_frames, progress_signal=None):
        """
        Initialize the AlignmentPoints object and compute the mean frame.

        :param configuration: Configuration object with parameters
        :param frames: Frames object with all video frames
        :param rank_frames: RankFrames object with global quality ranks (between 0. and 1.,
                            1. being optimal) for all frames
        :param align_frames: AlignFrames object with global shift information for all frames
        :param progress_signal: Either None (no progress signalling), or a signal with the signature
                                (str, int) with the current activity (str) and the progress in
                                percent (int).
        """

        self.configuration = configuration
        self.frames = frames
        self.rank_frames = rank_frames
        self.align_frames = align_frames
        self.progress_signal = progress_signal

        # Apply a low-pass filter on the mean frame as a preparation for shift detection.
        self.mean_frame = GaussianBlur(align_frames.mean_frame.astype(uint16),
                                        (self.configuration.frames_gauss_width,
                                        self.configuration.frames_gauss_width), 0).astype(int32)
        self.num_pixels_y = self.mean_frame.shape[0]
        self.num_pixels_x = self.mean_frame.shape[1]
        self.alignment_points_dropped_dim = None
        self.alignment_points_dropped_structure = None

        # Initialize counters for APs which are dropped because
        # they do not satisfy the brightness or structure condition.
        self.alignment_points = []
        self.alignment_points_dropped_dim = 0
        self.alignment_points_dropped_structure = 0

        # Initialize the number of frames to be stacked at each AP.
        self.stack_size = None

        self.dev_table = empty((2 * self.configuration.alignment_points_search_width + 1,
                               2 * self.configuration.alignment_points_search_width + 1), dtype=float32)

    @staticmethod
    def ap_locations(num_pixels, min_boundary_distance, step_size, even):
        """
        Compute optimal alignment patch coordinates in one coordinate direction. Place boundary
        neighbors as close as possible to the boundary.

        :param num_pixels: Number of pixels in the given coordinate direction
        :param min_boundary_distance: Minimum distance of an AP from the boundary
        :param step_size: Distance of alignment boxes
        :param even: If True, compute locations for even row indices. Otherwise, for odd ones.
        :return: List of alignment point coordinates in the given direction
        """
        # Initialize list of locations.
        locations = []

        # If the frame size is too small for the AP size chosen, no AP locations can be computed.
        # In this case an empty list is returned.
        try:
            # The number of interior alignment boxes in general is not an integer. Round to the next
            # higher number.
            num_interior_odd = int(ceil((num_pixels - 2 * min_boundary_distance) / step_size))
            # Because alignment points are arranged in a staggered grid, in even rows there is one point
            # more.
            num_interior_even = num_interior_odd + 1

            # The precise distance between alignment points will differ slightly from the specified
            # step_size. Compute the exact distance. Integer locations will be rounded later.
            distance_corrected = (num_pixels - 2 * min_boundary_distance) / num_interior_odd

            # Compute the AP locations, separately for even and odd rows.
            if even:
                locations = [int(min_boundary_distance + i * distance_corrected) for i in range(num_interior_even)]
            else:
                locations = [int(min_boundary_distance + 0.5 * distance_corrected +
                                 i * distance_corrected) for i in range(num_interior_odd)]
        except:
            pass
        return locations

    def create_ap_grid(self):
        """
        Create a 2D staggered grid of alignment points. For each AP compute its center coordinates,
        and the coordinate limits of its alignment box and alignment patch. Only alignment points
        which satisfy the conditions on brightness, contrast and structure are eventually added to
        the list.

        :return: List of alignment points.
        """

        # The alignment patch is the area which is stacked after a rigid displacement.
        half_patch_width = self.configuration.alignment_points_half_patch_width
        # The alignment box is the object on which the displacement computation is performed.
        half_box_width = self.configuration.alignment_points_half_box_width
        # Number of pixels in one coordinate direction between alignment points
        step_size = self.configuration.alignment_points_step_size
        # Maximum displacement searched for in the alignment process.
        search_width = self.configuration.alignment_points_search_width
        # Minimum structure value for an alignment point (between 0. and 1.)
        structure_threshold = self.configuration.alignment_points_structure_threshold
        # The brightest pixel must be brighter than this value (0 < value <256). Please note that
        # brightness and contrast values are converted to 16bit resolution.
        brightness_threshold = self.configuration.alignment_points_brightness_threshold * 256
        # The difference between the brightest and darkest pixel values must be larger than this
        # value (0 < value < 256)
        contrast_threshold = self.configuration.alignment_points_contrast_threshold * 256

        # Reset the alignment point list, and initialize counters for APs which are dropped because
        # they do not satisfy the brightness or structure condition.
        self.alignment_points = []
        self.alignment_points_dropped_dim = 0
        self.alignment_points_dropped_structure = 0

        # Compute the minimum distance of an AP from the boundary.
        min_boundary_distance = max(half_box_width + search_width, half_patch_width)

        # Compute y and x coordinate locations of alignment points. Note that the grid is staggered.
        ap_locations_y = self.ap_locations(self.num_pixels_y, min_boundary_distance,
                                           step_size, True)
        ap_locations_x_even = self.ap_locations(self.num_pixels_x, min_boundary_distance,
                                                step_size, True)
        ap_locations_x_odd = self.ap_locations(self.num_pixels_x, min_boundary_distance,
                                               step_size, False)

        # If no AP coordinates fit into the frame, no APs are created.
        if not ap_locations_y or not ap_locations_x_even or not ap_locations_x_odd:
            return

        # Compute the minimum distance of an AP center from the frame boundary.
        min_boundary_distance = max(
            self.configuration.alignment_points_half_box_width + \
            self.configuration.alignment_points_search_width,
            self.configuration.alignment_points_half_patch_width)

        # Create alignment point rows, start with an even one.
        even = True
        for index_y, y in enumerate(ap_locations_y):
            # For the first row extend the patch to the upper frame border, and for the last row
            # to the lower frame border.
            extend_y_low  = (index_y == 0)
            extend_y_high = (index_y == len(ap_locations_y)-1)

            # Create x coordinate, depending on the y row being even or odd (staggered grid).
            if even:
                ap_locations_x = ap_locations_x_even
            else:
                ap_locations_x = ap_locations_x_odd

            # For each location create an alignment point.
            for index_x, x in enumerate(ap_locations_x):
                # For the first point in a row, extend the patch to the left frame border, and for
                # the last point in a row to the right frame border.
                extend_x_low  = (index_x == 0)
                extend_x_high = (index_x == len(ap_locations_x) - 1)

                alignment_point = self.new_alignment_point(y, x, extend_x_low, extend_x_high,
                                                           extend_y_low, extend_y_high)

                # Compute structure and brightness information for the alignment box.
                max_brightness = amax(alignment_point['reference_box'])
                min_brightness = amin(alignment_point['reference_box'])
                # If the alignment box satisfies the brightness conditions, add the AP to the list.
                if max_brightness > brightness_threshold and max_brightness - \
                        min_brightness > contrast_threshold:

                    # Check if the fraction of dark pixels exceeds a threshold.
                    box = alignment_point['reference_box']
                    fraction = (box < brightness_threshold).sum() / (box.shape[0] * box.shape[1])
                    if fraction > self.configuration.alignment_points_dim_fraction_threshold:

                        # Compute the center of mass of the brightness distribution within the box,
                        # and shift the box center to this location.
                        com = ndimage.measurements.center_of_mass(box)
                        y_adapted = y + int(com[0]) - half_box_width
                        x_adapted = x + int(com[1]) - half_box_width

                        y_adapted = max(y_adapted, min_boundary_distance)
                        y_adapted = min(y_adapted, self.num_pixels_y - min_boundary_distance)
                        x_adapted = max(x_adapted, min_boundary_distance)
                        x_adapted = min(x_adapted, self.num_pixels_x - min_boundary_distance)

                        # Replace the alignment point with a new one, using the updated
                        # coordinates.
                        alignment_point = self.new_alignment_point(y_adapted, x_adapted,
                                                                   extend_x_low, extend_x_high,
                                                                   extend_y_low, extend_y_high)

                    alignment_point['structure'] = Miscellaneous.quality_measure(
                        alignment_point['reference_box'])
                    self.alignment_points.append(alignment_point)
                else:
                    # If a point does not satisfy the conditions, increase the counter.
                    self.alignment_points_dropped_dim += 1

            # Switch between even and odd rows.
            even = not even

        # Normalize the structure information for all alignment point boxes by dividing by the
        # maximum value.
        if self.alignment_points:
            structure_max = max(
                alignment_point['structure'] for alignment_point in self.alignment_points)
            alignment_points_dropped_structure_indices = []
            for alignment_point_index, alignment_point in enumerate(self.alignment_points):
                alignment_point['structure'] /= structure_max
                # Remove alignment points with too little structure and increment the counter.
                if alignment_point['structure'] < structure_threshold:
                    alignment_points_dropped_structure_indices.append(alignment_point_index)
                    self.alignment_points_dropped_structure += 1

            # Remove alignment points which do not satisfy the structure condition, if there is any.
            if alignment_points_dropped_structure_indices:
                alignment_points_new = []
                dropped_index = 0
                for alignment_point_index, alignment_point in enumerate(self.alignment_points):
                    if alignment_point_index != alignment_points_dropped_structure_indices[
                        dropped_index]:
                        alignment_points_new.append(alignment_point)
                    elif dropped_index < len(alignment_points_dropped_structure_indices) - 1:
                        dropped_index += 1
                self.alignment_points = alignment_points_new

    def new_alignment_point(self, y, x, extend_x_low, extend_x_high, extend_y_low, extend_y_high):
        """
        Create a new alignment point. This method is called in creating the initial alignment point
        grid. Later it can be invoked by the user to add single alignment points.

        :param y: y coordinate of alignment point center
        :param x: x coordinate of alignment point center
        :param extend_x_low: True, if patch is to be extended to the left frame boundary.
                             False otherwise.
        :param extend_x_high: True, if patch is to be extended to the right frame boundary.
                              False otherwise.
        :param extend_y_low: True, if patch is to be extended to the upper frame boundary.
                             False otherwise.
        :param extend_y_high: True, if patch is to be extended to the lower frame boundary.
                              False otherwise.
        :return: If successful, the alignment point object is returned; otherwise None.
        """

        # If the patch does not fit into the frame, or the AP box is too close to the border,
        # the AP cannot be created at this place.
        min_boundary_distance = max(self.configuration.alignment_points_half_box_width + \
                                self.configuration.alignment_points_search_width,
                                self.configuration.alignment_points_half_patch_width)
        if y<min_boundary_distance or y>self.num_pixels_y-min_boundary_distance or \
            x<min_boundary_distance or x>self.num_pixels_x-min_boundary_distance:
            return None

        alignment_point = {}
        alignment_point['y'] = y
        alignment_point['x'] = x
        alignment_point['half_box_width'] = self.configuration.alignment_points_half_box_width
        alignment_point['box_y_low'] = y - self.configuration.alignment_points_half_box_width
        alignment_point['box_y_high'] = y + self.configuration.alignment_points_half_box_width
        alignment_point['box_x_low'] = x - self.configuration.alignment_points_half_box_width
        alignment_point['box_x_high'] = x + self.configuration.alignment_points_half_box_width

        alignment_point['patch_y_low'] = y - self.configuration.alignment_points_half_patch_width
        alignment_point['patch_y_high'] = y + self.configuration.alignment_points_half_patch_width
        if extend_y_low:
            alignment_point['patch_y_low'] = 0
        elif extend_y_high:
            alignment_point['patch_y_high'] = self.num_pixels_y

        alignment_point['patch_x_low'] = x - self.configuration.alignment_points_half_patch_width
        alignment_point['patch_x_high'] = x + self.configuration.alignment_points_half_patch_width
        if extend_x_low:
            alignment_point['patch_x_low'] = 0
        elif extend_x_high:
            alignment_point['patch_x_high'] = self.num_pixels_x

        # Initialize the reference to the corresponding widget in the AP viewer scene.
        alignment_point['graphics_item'] = None

        # Allocate buffers and fill alignment point box with mean frame.
        AlignmentPoints.set_reference_box(alignment_point, self.mean_frame)

        return alignment_point

    def add_alignment_point(self, ap):
        """
        Add an alignment point to the list.

        :param ap: AP to be added
        :return: -
        """

        self.alignment_points.append(ap)

    def remove_alignment_points(self, ap_list):
        """
        Remove a list of alignment points from the current list.

        :param ap_list: List of point objects to be removed
        :return: -
        """

        # Create list with unique identifiers of all items on the list.
        ap_list_ids = [id(ap_list_item) for ap_list_item in ap_list]

        # Replace the original AP list with the reduced one.
        # If the identifier of an alignment point does not match any list item, keep it.
        self.alignment_points = [ap for ap in self.alignment_points if id(ap) not in ap_list_ids]

    def replace_alignment_point(self, ap_old, ap_new):
        """
        Replace an alignment point with a new one.

        :param ap_old: Existing alignment point to be replaced
        :param ap_new: New alignment point to replace the old one
        :return: True if successful, False otherwise
        """

        ap_old_id = id(ap_old)
        for index, ap in enumerate(self.alignment_points):
            if id(ap) == ap_old_id:
                self.alignment_points[index] = ap_new
                return True
        return False

    def move_alignment_point(self, ap, y_new, x_new):
        """
        Move an existing AP to a different position.

        :param ap: Esisting alignment point to be moved
        :param y_new: New y coordinate of AP center
        :param x_new: New x coordinate of AP center
        :return: The updated AP; or None, if unsuccessful
        """

        # Try to do the changes. If unsuccessful, return None.
        return self.change_alignment_point(ap, y_new, x_new, ap['half_box_width'])

    def resize_alignment_point(self, ap, factor):
        """
        Change the size of an existing alignment point.

        :param ap: Esisting alignment point to be resized
        :param factor: Factor by which the size is to be changed
        :return: The resized alignment point; or None, if unsuccessful
        """

        half_box_width_new = ap['half_box_width'] * factor

        # Try to do the changes. If unsuccessful, return None.
        return self.change_alignment_point(ap, ap["y"], ap["x"], half_box_width_new)

    def change_alignment_point(self, ap, y, x, half_box_width):
        """
        Try to fit a resized or moved AP into the frame.

        :param ap: AP to be resized or moved
        :param y: New y coordinate of center
        :param x: New x coordinate of center
        :param half_box_width: New half width of AP box
        :return: The resized / moved AP; or None, if unsuccessful
        """

        # Test if the AP box is too small.
        if half_box_width < self.configuration.alignment_points_min_half_box_width:
            return None

        # Compute new values for patch and box sizes.
        half_patch_width_new_int = round(half_box_width *
                                             self.configuration.alignment_points_half_patch_width /
                                             self.configuration.alignment_points_half_box_width)
        half_box_width_new_int = round(half_box_width)

        # Compute resized patch bounds. If resizing hits the image boundary on at least one side,
        # the operation is aborted.
        patch_y_low = y - half_patch_width_new_int
        if patch_y_low < 0:
            return None
        patch_y_high = y + half_patch_width_new_int
        if patch_y_high > self.num_pixels_y:
            return None
        patch_x_low = x - half_patch_width_new_int
        if patch_x_low < 0:
            return None
        patch_x_high = x + half_patch_width_new_int
        if patch_x_high > self.num_pixels_x:
            return None

        # Compute resized box bounds. If resizing moves the box closer to the frame border than
        # the search width on at least one side, the operation is aborted.
        box_y_low = y - half_box_width_new_int
        if box_y_low < self.configuration.alignment_points_search_width:
            return None
        box_y_high = y + half_box_width_new_int
        if box_y_high > self.num_pixels_y - self.configuration.alignment_points_search_width:
            return None
        box_x_low = x - half_box_width_new_int
        if box_x_low < self.configuration.alignment_points_search_width:
            return None
        box_x_high = x + half_box_width_new_int
        if box_x_high > self.num_pixels_x - self.configuration.alignment_points_search_width:
            return None

        # Perform the changes.
        ap['y'] = y
        ap['x'] = x
        ap['patch_y_low'] = patch_y_low
        ap['patch_y_high'] = patch_y_high
        ap['patch_x_low'] = patch_x_low
        ap['patch_x_high'] = patch_x_high
        ap['box_y_low'] = box_y_low
        ap['box_y_high'] = box_y_high
        ap['box_x_low'] = box_x_low
        ap['box_x_high'] = box_x_high
        ap['half_box_width'] = half_box_width

        # Invalidate buffers. To save computing time, they are not re-computed at every wheel
        # event.
        ap['graphics_item'] = None
        ap['reference_box'] = None
        ap['stacking_buffer'] = None

        return ap

    @staticmethod
    def set_reference_box(alignment_point, mean_frame):
        """
        For APs which have been changed in the AP editor, buffers have been invalidated. They have
        to be re-computed after AP editing is done.

        :param alignment_point: Alignment_point object
        :param mean_frame: Average frame
        :return: -
        """

        # Cut out the reference box from the mean frame, used in alignment.
        box = mean_frame[alignment_point['box_y_low']:alignment_point['box_y_high'],
              alignment_point['box_x_low']:alignment_point['box_x_high']]
        alignment_point['reference_box'] = box

    def set_reference_boxes_correlation(self):
        """
        This method is used if multilevel-correlation AP matching is selected. In this case two
        reference boxes with different resolution are constructed. The box used in the first phase
        contains only every second pixel of the original frame in each coordinate direction. The box
        used in the second phase has the same resolution as the original frame.

        This method is invoked when all APs are set, just before stacking.

        :return: -
        """

        for ap_index, alignment_point in enumerate(self.alignment_points):

            # Cut the reference box for this alignment point from the mean frame.
            window_second_phase = self.align_frames.mean_frame[
                                  alignment_point['box_y_low']:
                                  alignment_point['box_y_high'],
                                  alignment_point['box_x_low']:
                                  alignment_point['box_x_high']].astype(float32)

            # The reference box with the full resolution is used in the second phase.
            alignment_point['reference_box_second_phase'] = window_second_phase

            # In the first phase a box with half the resolution is constructed.
            alignment_point['reference_box_first_phase'] =  window_second_phase[::2, ::2]

    @staticmethod
    def initialize_ap_stacking_buffer(alignment_point, drizzle_factor, color):
        """
        In the stacking initialization, for each AP a stacking buffer has to be allocated. At the
        same time, drizzled patch index bounds are computed.

        :param alignment_point: Alignment_point object
        :param drizzle_factor: Drizzle factor (integer: 1, 2 or 3)
        :param color: True, if stacking is to be done for color frames. False for monochrome case.
        :return: -
        """

        # Compute drizzled patch coordinates and index bounds.
        alignment_point['y_drizzled'] = alignment_point['y'] * drizzle_factor
        alignment_point['x_drizzled'] = alignment_point['x'] * drizzle_factor
        alignment_point['patch_y_low_drizzled'] = alignment_point['patch_y_low'] * drizzle_factor
        alignment_point['patch_y_high_drizzled'] = alignment_point['patch_y_high'] * drizzle_factor
        alignment_point['patch_x_low_drizzled'] = alignment_point['patch_x_low'] * drizzle_factor
        alignment_point['patch_x_high_drizzled'] = alignment_point['patch_x_high'] * drizzle_factor

        # Allocate space for the stacking buffer.
        if color:
            alignment_point['stacking_buffer'] = zeros(
                [alignment_point['patch_y_high_drizzled'] - alignment_point['patch_y_low_drizzled'],
                 alignment_point['patch_x_high_drizzled'] - alignment_point['patch_x_low_drizzled'],
                 3],
                dtype=float32)
        else:
            alignment_point['stacking_buffer'] = zeros(
                [alignment_point['patch_y_high_drizzled'] - alignment_point['patch_y_low_drizzled'],
                 alignment_point['patch_x_high_drizzled'] - alignment_point[
                     'patch_x_low_drizzled']], dtype=float32)

    def find_alignment_points(self, y_low, y_high, x_low, x_high):
        """
        Find all alignment points the centers of which are within given (y, x) bounds.

        :param y_low: Lower y pixel coordinate bound
        :param y_high: Upper y pixel coordinate bound
        :param x_low: Lower x pixel coordinate bound
        :param x_high: Upper x pixel coordinate bound
        :return: List of all alignment points with centers within the given coordinate bounds.
                 If no AP satisfies the condition, return an empty list.
        """

        return [ap for ap in self.alignment_points if y_low <= ap['y'] <= y_high and
                x_low <= ap['x'] <= x_high]

    @staticmethod
    def find_neighbor(ap_y, ap_x, alignment_points):
        """
        For a given (y, x) position find the closest "real" alignment point.

        :param ap_y: y cocrdinate of location of interest
        :param ap_x: x cocrdinate of location of interest
        :param alignment_points: List of alignment points to be searched

        :return: Alignment point object of closest AP, and distance in pixels. If the list of
                 alignment points is empty, (None, None) is returned.
        """

        # If no APs have been created yet, return None for both results.
        if not alignment_points:
            return None, None

        # Start with an impossibly large distance, and find the closest AP on the list.
        min_distance_squared = 1.e30
        ap_neighbor = None
        for ap in alignment_points:
            distance_squared = (ap['y'] - ap_y) ** 2 + (ap['x'] - ap_x) ** 2
            if distance_squared < min_distance_squared:
                ap_neighbor = ap
                min_distance_squared = distance_squared
        return ap_neighbor, sqrt(min_distance_squared)

    def compute_frame_qualities(self):
        """
        For each alignment point compute a ranking of best frames. Store the list in the
        alignment point dictionary with the key 'best_frame_indices'.

        Consider the special case that sampled-down Laplacians have been stored for frame ranking.
        In this case they can be re-used for ranking the boxes around alignment points (but only
        if "Laplace" has been selected for alignment point ranking).

        :return: -
        """

        # If the user has entered a value for the number of frames, use it.
        if self.configuration.alignment_points_frame_number > 0:
            self.stack_size = self.configuration.alignment_points_frame_number
        # Otherwise compute the stack size from the given percentage. Take at least one frame.
        else:
            self.stack_size = max(int(ceil(
                self.frames.number * self.configuration.alignment_points_frame_percent / 100.)), 1)

        # Select the ranking method.
        if self.configuration.alignment_points_rank_method == "xy gradient":
            method = Miscellaneous.local_contrast
        elif self.configuration.alignment_points_rank_method == "Laplace":
            method = Miscellaneous.local_contrast_laplace
        elif self.configuration.alignment_points_rank_method == "Sobel":
            method = Miscellaneous.local_contrast_sobel
        else:
            raise NotSupportedError(
                "Ranking method " + self.configuration.alignment_points_rank_method +
                " not supported")

        # Compute the frequency of progress signals in the computational loop.
        if self.progress_signal is not None:
            self.signal_loop_length = max(self.frames.number, 1)
            self.signal_step_size = max(round(self.frames.number / 10), 1)

        # Initialize a list which for each AP contains the qualities of all frames at this point.
        for alignment_point in self.alignment_points:
            alignment_point['frame_qualities'] = []

        if self.configuration.rank_frames_method != "Laplace" or \
                self.configuration.alignment_points_rank_method != "Laplace":
            # There are no stored Laplacians, or they cannot be used for the specified method.
            # Cycle through all frames and alignment points:
            for frame_index in range(self.frames.number):
                frame = self.frames.frames_mono_blurred(frame_index)

                # After every "signal_step_size"th frame, send a progress signal to the main GUI.
                if self.progress_signal is not None and frame_index % self.signal_step_size == 1:
                    self.progress_signal.emit("Rank frames at APs",
                                    int(round(10 * frame_index / self.signal_loop_length) * 10))

                for ap_index, alignment_point in enumerate(self.alignment_points):
                    # Compute patch bounds within the current frame.
                    y_low = max(0,
                                alignment_point['patch_y_low'] + self.align_frames.dy[frame_index])
                    y_high = min(self.frames.shape[0],
                                 alignment_point['patch_y_high'] + self.align_frames.dy[
                                     frame_index])
                    x_low = max(0,
                                alignment_point['patch_x_low'] + self.align_frames.dx[frame_index])
                    x_high = min(self.frames.shape[1],
                                 alignment_point['patch_x_high'] + self.align_frames.dx[
                                     frame_index])
                    # Compute the frame quality and append it to the list for this alignment point.
                    if self.configuration.frames_normalization:
                        alignment_point['frame_qualities'].append(
                            method(frame[y_low:y_high, x_low:x_high],
                                   self.configuration.alignment_points_rank_pixel_stride) /
                            self.frames.average_brightness(frame_index))
                    else:
                        alignment_point['frame_qualities'].append(
                            method(frame[y_low:y_high, x_low:x_high],
                                   self.configuration.alignment_points_rank_pixel_stride))
        else:
            # Sampled-down Laplacians of all blurred frames have been computed in
            # "frames.frames_mono_blurred_laplacian". Cut out boxes around alignment points from
            # those objects, rather than computing new Laplacians. Cycle through all frames and
            # alignment points. Use the blurred monochrome image for ranking.
            for frame_index in range(self.frames.number):
                frame = self.frames.frames_mono_blurred_laplacian(frame_index)

                # After every "signal_step_size"th frame, send a progress signal to the main GUI.
                if self.progress_signal is not None and frame_index % self.signal_step_size == 1:
                    self.progress_signal.emit("Rank frames at APs",
                                        int(round(10 * frame_index / self.signal_loop_length) * 10))

                for ap_index, alignment_point in enumerate(self.alignment_points):
                    # Compute patch bounds within the current frame.
                    y_low = int(max(0, alignment_point['patch_y_low'] + self.align_frames.dy[
                        frame_index]) / self.configuration.align_frames_sampling_stride)
                    y_high = int(min(self.frames.shape[0],
                                     alignment_point['patch_y_high'] + self.align_frames.dy[
                                         frame_index]) / self.configuration.align_frames_sampling_stride)
                    x_low = int(max(0, alignment_point['patch_x_low'] + self.align_frames.dx[
                        frame_index]) / self.configuration.align_frames_sampling_stride)
                    x_high = int(min(self.frames.shape[1],
                                     alignment_point['patch_x_high'] + self.align_frames.dx[
                                         frame_index]) / self.configuration.align_frames_sampling_stride)
                    # Compute the frame quality and append it to the list for this alignment point.
                    if self.configuration.frames_normalization:
                        alignment_point['frame_qualities'].append(
                            meanStdDev(frame[y_low:y_high, x_low:x_high])[1][0][0] /
                            self.frames.average_brightness(frame_index))
                    else:
                        alignment_point['frame_qualities'].append(
                            meanStdDev(frame[y_low:y_high, x_low:x_high])[1][0][0])

        if self.progress_signal is not None:
            self.progress_signal.emit("Rank frames at APs", 100)

        # Initialize the alignment point lists for all frames.
        self.frames.reset_alignment_point_lists()
        # For each alignment point sort the computed quality ranks in descending order.
        for alignment_point_index, alignment_point in enumerate(self.alignment_points):
            # Truncate the list to the number of frames to be stacked for each alignmeent point.
            alignment_point['best_frame_indices'] = sorted(range(len(alignment_point['frame_qualities'])),
                                                    key=alignment_point['frame_qualities'].__getitem__, reverse=True)[:self.stack_size]
            # Add this alignment point to the AP lists of those frames where the AP is to be used.
            for frame_index in alignment_point['best_frame_indices']:
                self.frames.used_alignment_points[frame_index].append(alignment_point_index)

    def compute_shift_alignment_point(self, frame_mono_blurred, frame_index, alignment_point_index,
                                      de_warp=True, weight_matrix_first_phase=None,
                                      subpixel_solve=False):
        """
        Compute the [y, x] pixel shift vector at a given alignment point relative to the mean frame.
        Four different methods can be used to compute the shift values:
        - a multilevel cross correlation algorithm (miscellaneous.multilevel_correlation). This
          method was implemented after PSS had shown bad performance for planetary videos captured
          with bad seeing. Pixel shifts can be computed with subpixel accuracy, as specified with
          the parameter "subpixel_solve". This is needed for drizzling.
        - a subpixel algorithm from "skimage.feature"
        - a phase correlation algorithm (miscellaneous.translation)
        - a local search algorithm (spiralling outwards), see method "search_local_match",
          optionally with subpixel accuracy.
        - a local search algorithm, based on steepest descent, see method
          "search_local_match_gradient". This method is faster than the previous one, but it has no
          subpixel option.

        Be careful with the sign of the local shift values. For the first two methods, a positive
        value means that the current frame has to be shifted in the positive coordinate direction
        in order to make objects in the frame match with their counterparts in the reference frame.
        In other words: If the shift is positive, an object in the current frame is at lower pixel
        coordinates as compared to the reference. This is very counter-intuitive, but to make the
        three methods consistent, the same approach was followed in the implementation of the third
        method "search_local_match", contained in this module. There, a pixel box around an
        alignment point in the current frame is moved until the content of the box matches with the
        corresponding box in the reference frame. If at this point the box is shifted towards a
        higher coordinate value, this value is returned with a negative sign as the local shift.

        :param frame_mono_blurred: Gaussian-blurred version of the frame with index "frame_index"
        :param frame_index: Index of the selected frame in the list of frames
        :param alignment_point_index: Index of the selected alignment point
        :param de_warp: If True, include local warp shift computation. If False, only apply
                        global frame shift.
        :param weight_matrix_first_phase: This parameter is only used by the alignment method
                                          "MultiLevelCorrelation". If not None it defines a
                                          weighting array by which the cross correlation results are
                                          multiplied before the maximum value is determined. The
                                          size of this 2D array in each coordinate direction is that
                                          of the reference_box_first_phase plus two times the first
                                          phase search width.
        :param subpixel_solve: Used only for alignment method "MultiLevelCorrelation". If True,
                               in the second phase the optimum is computed with sub-pixel accuracy
                               (i.e. the returned shifts are not integer). If False, shifts are
                               computed as integer values.

        :return: [shift_y, shift_x], success with: [shift_y, shift_x]: Local shift vector
                                         success: True, if computation successful; False, otherwise.
        """

        # Initialize the "success" return value.
        success = True

        alignment_point = self.alignment_points[alignment_point_index]
        y_low = alignment_point['box_y_low']
        y_high = alignment_point['box_y_high']
        x_low = alignment_point['box_x_low']
        x_high = alignment_point['box_x_high']

        # The offsets dy and dx are caused by two effects: First, the mean frame is smaller
        # than the original frames. It only contains their intersection. And second, because the
        # given frame is globally shifted as compared to the mean frame.
        dy = self.align_frames.dy[frame_index]
        dx = self.align_frames.dx[frame_index]

        if de_warp:
            # Use a two-level algorithm based on (weighted) cross correlation. The user-supplied
            # noise level parameter is used as the blurring factor for the first correlation phase.
            if self.configuration.alignment_points_method == 'MultiLevelCorrelation':
                shift_y_local_first_phase, shift_x_local_first_phase, success_first_phase, \
                shift_y_local_second_phase, shift_x_local_second_phase, success_second_phase = \
                    Miscellaneous.multilevel_correlation(
                        alignment_point['reference_box_first_phase'],
                        frame_mono_blurred, self.configuration.frames_gauss_width,
                        alignment_point['reference_box_second_phase'],
                        y_low + dy, y_high + dy, x_low + dx, x_high + dx,
                        self.configuration.alignment_points_search_width,
                        weight_matrix_first_phase=weight_matrix_first_phase,
                        subpixel_solve=subpixel_solve)

                # Compute the combined warp shifts from both phases.
                shift_pixel = [shift_y_local_first_phase + shift_y_local_second_phase,
                               shift_x_local_first_phase + shift_x_local_second_phase]
                success = success_second_phase

            # Use subpixel registration from skimage.feature, with accuracy 1/10 pixels.
            elif self.configuration.alignment_points_method == 'Subpixel':
                # Cut out the alignment box from the given frame. Take into account the offsets
                # explained above.
                box_in_frame = frame_mono_blurred[y_low + dy:y_high + dy,
                               x_low + dx:x_high + dx]
                # The following use of "phase_cross_correlation" replaces the original call to
                # "register_translation" which seems not to be in the package anymore. If this
                # replacement really works has not been tested.
                shift_pixel, error, diffphase = phase_cross_correlation(
                    alignment_point['reference_box'], box_in_frame, upsample_factor=10)

            # Use a simple phase shift computation (contained in module "miscellaneous").
            elif self.configuration.alignment_points_method == 'CrossCorrelation':
                # Cut out the alignment box from the given frame. Take into account the offsets
                # explained above.
                box_in_frame = frame_mono_blurred[y_low + dy:y_high + dy,
                                                  x_low + dx:x_high + dx]
                shift_pixel = Miscellaneous.translation(alignment_point['reference_box'],
                                                        box_in_frame, box_in_frame.shape)

            # Use a local search (see method "search_local_match" below.
            elif self.configuration.alignment_points_method == 'RadialSearch':
                shift_pixel, dev_r = Miscellaneous.search_local_match(alignment_point['reference_box'],
                    frame_mono_blurred, y_low + dy, y_high + dy, x_low + dx, x_high + dx,
                    self.configuration.alignment_points_search_width,
                    self.configuration.alignment_points_sampling_stride,
                    sub_pixel=self.configuration.alignment_points_local_search_subpixel)
                # If a zero shift was returned after a search with radius>2, thie means that
                # the search was not successfu.
                success = len(dev_r)<=2 or shift_pixel!=[0, 0]

            # Use the steepest descent search method.
            elif self.configuration.alignment_points_method == 'SteepestDescent':
                shift_pixel, dev_r = Miscellaneous.search_local_match_gradient(
                    alignment_point['reference_box'],
                    frame_mono_blurred, y_low + dy, y_high + dy, x_low + dx, x_high + dx,
                    self.configuration.alignment_points_search_width,
                    self.configuration.alignment_points_sampling_stride, self.dev_table)
                success = len(dev_r)<=2 or shift_pixel!=[0, 0]
            else:
                raise NotSupportedError("The point shift computation method " +
                                        self.configuration.alignment_points_method +
                                        " is not implemented")

            # Return the computed shift vector.
            return shift_pixel, success
        else:
            # If no de-warping is computed, just return the zero vector.
            return [0, 0], success

    def show_alignment_points(self, image):
        """
        Create an RGB version of a monochrome image and insert red crosses at all alignment
        point locations. Draw green alignment point boxes and white alignment point patches.

        :return: 8-bit RGB image with annotations.
        """

        if len(image.shape) == 3:
            color_image = image.astype(uint8)
        else:
            # Expand the monochrome reference frame to RGB
            color_image = stack((image.astype(uint8),) * 3, -1)

        # For all alignment boxes insert a color-coded cross.
        cross_half_len = 5

        for alignment_point in (self.alignment_points):
            y_center = alignment_point['y']
            x_center = alignment_point['x']
            Miscellaneous.insert_cross(color_image, y_center,
                                       x_center, cross_half_len, 'red')
            box_y_low = max(alignment_point['box_y_low'], 0)
            box_y_high = min(alignment_point['box_y_high'], image.shape[0]) - 1
            box_x_low = max(alignment_point['box_x_low'], 0)
            box_x_high = min(alignment_point['box_x_high'], image.shape[1]) - 1
            for y in arange(box_y_low, box_y_high):
                color_image[y, box_x_low] = [255, 255, 255]
                color_image[y, box_x_high] = [255, 255, 255]
            for x in arange(box_x_low, box_x_high):
                color_image[box_y_low, x] = [255, 255, 255]
                color_image[box_y_high, x] = [255, 255, 255]

            patch_y_low = max(alignment_point['patch_y_low'], 0)
            patch_y_high = min(alignment_point['patch_y_high'], image.shape[0]) - 1
            patch_x_low = max(alignment_point['patch_x_low'], 0)
            patch_x_high = min(alignment_point['patch_x_high'], image.shape[1]) - 1
            for y in arange(patch_y_low, patch_y_high):
                color_image[y, patch_x_low] = [0, int(
                    (255 + color_image[y, patch_x_low][1]) / 2.), 0]
                color_image[y, patch_x_high] = [0, int(
                    (255 + color_image[y, patch_x_high][1]) / 2.), 0]
            for x in arange(patch_x_low, patch_x_high):
                color_image[patch_y_low, x] = [0, int(
                    (255 + color_image[patch_y_low, x][1]) / 2.), 0]
                color_image[patch_y_high, x] = [0, int(
                    (255 + color_image[patch_y_high, x][1]) / 2.), 0]

        return color_image


if __name__ == "__main__":
    # Images can either be extracted from a video file or a batch of single photographs. Select
    # the example for the test run.
    type = 'video'
    if type == 'image':
        names = glob('Images/2012*.tif')
        # names = glob.glob('Images/Moon_Tile-031*ap85_8b.tif')
        # names = glob.glob('Images/Example-3*.jpg')
    else:
        names = 'Videos/short_video.avi'
    print(names)

    # Get configuration parameters.
    configuration = Configuration()
    configuration.initialize_configuration()
    try:
        frames = Frames(configuration, names, type=type)
        print("Number of images read: " + str(frames.number))
        print("Image shape: " + str(frames.shape))
    except Error as e:
        print("Error: " + e.message)
        exit()

    # Rank the frames by their overall local contrast.
    rank_frames = RankFrames(frames, configuration)
    start = time()
    rank_frames.frame_score()
    end = time()
    print('Elapsed time in ranking images: {}'.format(end - start))
    print("Index of maximum: " + str(rank_frames.frame_ranks_max_index))
    print("Frame scores: " + str(rank_frames.frame_ranks))
    print("Frame scores (sorted): " + str(
        [rank_frames.frame_ranks[i] for i in rank_frames.quality_sorted_indices]))
    print("Sorted index list: " + str(rank_frames.quality_sorted_indices))

    # Initialize the frame alignment object.
    align_frames = AlignFrames(frames, rank_frames, configuration)

    if configuration.align_frames_mode == "Surface":
        start = time()
        # Select the local rectangular patch in the image where the L gradient is highest in both x
        # and y direction. The scale factor specifies how much smaller the patch is compared to the
        # whole image frame.
        (y_low_opt, y_high_opt, x_low_opt, x_high_opt) = align_frames.compute_alignment_rect(
            configuration.align_frames_rectangle_scale_factor)
        end = time()
        print('Elapsed time in computing optimal alignment rectangle: {}'.format(end - start))
        print("optimal alignment rectangle, x_low: " + str(x_low_opt) + ", x_high: " + str(
            x_high_opt) + ", y_low: " + str(y_low_opt) + ", y_high: " + str(y_high_opt))
        reference_frame_with_alignment_points = frames.frames_mono(
            align_frames.frame_ranks_max_index).copy()
        reference_frame_with_alignment_points[y_low_opt,
        x_low_opt:x_high_opt] = reference_frame_with_alignment_points[y_high_opt - 1,
                                x_low_opt:x_high_opt] = 255
        reference_frame_with_alignment_points[y_low_opt:y_high_opt,
        x_low_opt] = reference_frame_with_alignment_points[y_low_opt:y_high_opt,
                     x_high_opt - 1] = 255
        # plt.imshow(reference_frame_with_alignment_points, cmap='Greys_r')
        # plt.show()

    # Align all frames globally relative to the frame with the highest score.
    start = time()
    align_frames.align_frames()
    end = time()
    print('Elapsed time in aligning all frames: {}'.format(end - start))
    print("Frame shifts: " + str(align_frames.frame_shifts))
    print("Intersection: " + str(align_frames.intersection_shape))

    start = time()
    # Compute the reference frame by averaging the best frames.
    average = align_frames.average_frame()

    # Initialize the AlignmentPoints object. This includes the computation of the average frame
    # against which the alignment point shifts are measured.

    alignment_points = AlignmentPoints(configuration, frames, rank_frames, align_frames)
    end = time()
    print('Elapsed time in computing reference frame: {}'.format(end - start))
    print("Reference frame computed from the best " + str(
        align_frames.average_frame_number) + " frames.")
    # plt.imshow(align_frames.mean_frame, cmap='Greys_r')
    # plt.show()

    # Create alignment points, and show alignment point boxes and patches.
    alignment_points.create_ap_grid()
    print("Number of alignment points created: " + str(len(alignment_points.alignment_points)) +
          ", number of dropped aps (dim): " + str(
        alignment_points.alignment_points_dropped_dim) +
          ", number of dropped aps (structure): " + str(
        alignment_points.alignment_points_dropped_structure))
    color_image = alignment_points.show_alignment_points(average)

    plt.imshow(color_image)
    plt.show()

    # For each alignment point rank frames by their quality.
    start = time()
    alignment_points.compute_frame_qualities()
    end = time()
    print('Elapsed time in ranking frames for every alignment point: {}'.format(end - start))

    y_low = 490
    y_high = 570
    x_low = 880
    x_high = 960
    found_ap_list = alignment_points.find_alignment_points(y_low, y_high, x_low, x_high)
    print("Removing alignment points between bounds " + str(y_low) + " <= y <= " + str(y_high) +
          ", " + str(x_low) + " <= x <= " + str(x_high) + ":")
    for ap in found_ap_list:
        print("y: " + str(ap['y']) + ", x: " + str(ap['x']))
    alignment_points.remove_alignment_points(found_ap_list)

    y_new = 530
    x_new = 920
    half_box_width_new = 40
    half_patch_width_new = 50
    num_pixels_y = average.shape[0]
    num_pixels_x = average.shape[1]
    alignment_points.alignment_points.append(
        alignment_points.new_alignment_point(y_new, x_new, False, False, False, False))
    print("Added alignment point at y: " + str(y_new) + ", x: " + str(x_new) + ", box size: "
          + str(2 * half_box_width_new) + ", patch size: " + str(2 * half_patch_width_new))

    # Show updated alignment point boxes and patches.
    print("Number of alignment points created: " + str(len(alignment_points.alignment_points)) +
          ", number of dropped aps (dim): " + str(alignment_points.alignment_points_dropped_dim) +
          ", number of dropped aps (structure): " + str(
        alignment_points.alignment_points_dropped_structure))
    color_image = alignment_points.show_alignment_points(average)

    plt.imshow(color_image)
    plt.show()