File: rectangular_patch_editor.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 (401 lines) | stat: -rw-r--r-- 15,012 bytes parent folder | download | duplicates (2)
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
# -*- 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/>.

Part of this module (in class "AlignmentPointEditor" was copied from
https://stackoverflow.com/questions/35508711/how-to-enable-pan-and-zoom-in-a-qgraphicsview

"""
from glob import glob
from sys import argv, exit
from time import time

from PyQt5 import QtCore, QtWidgets
from numpy import uint8, uint16, int32

from align_frames import AlignFrames
from alignment_point_editor import SelectionRectangleGraphicsItem
from configuration import Configuration
from exceptions import InternalError, NotSupportedError, Error
from frame_viewer import FrameViewer
from frames import Frames
from rank_frames import RankFrames
from rectangular_patch_editor_gui import Ui_rectangular_patch_editor


class GraphicsScene(QtWidgets.QGraphicsScene):
    """
    Handle mouse events on the graphics scene depicting the image with a rectangular patch
    superimposed. The left mouse button can be used to create the patch.

    """

    def __init__(self, photo_editor, parent=None):
        """
        Initialize the scene object.

        :param photo_editor: the object in which the scene is defined
        :param parent: The parent class
        """

        QtWidgets.QGraphicsScene.__init__(self, parent)
        self.photo_editor = photo_editor
        self.left_button_pressed = False
        self.right_button_pressed = False

        self.remember_rp = None

    def mousePressEvent(self, event):
        """
        A mouse button is pressed.

        :param event: event object
        :return: -
        """

        # The following actions are not performed in drag-and-zoom mode. The switch between both
        # modes is handled in the higher-level object "photo_viewer".
        if not self.photo_editor.drag_mode and self.photo_editor.hasPhoto():

            # If a rectangular patch had been drawn before, remove it from the scene.
            if self.remember_rp:
                self.removeItem(self.remember_rp)
                self.remember_rp = None

            pos = event.lastScenePos()
            x = int(pos.x())
            y = int(pos.y())

            # The left button is pressed.
            if event.button() == QtCore.Qt.LeftButton:
                self.left_button_pressed = True

                # Remember the location.
                self.left_y_start = y
                self.left_x_start = x


    def mouseReleaseEvent(self, event):
        """
        A mouse button is released.

        :param event: event object
        :return: -
        """

        if not self.photo_editor.drag_mode and self.photo_editor.hasPhoto():

            # The left button is released.
            if event.button() == QtCore.Qt.LeftButton:
                self.left_button_pressed = False

                # If a rectangular patch has been defined, store its coordinates.
                if self.remember_rp:
                    self.photo_editor.set_selection_rectangle(self.remember_rp.y_low,
                                                              self.remember_rp.y_high,
                                                              self.remember_rp.x_low,
                                                              self.remember_rp.x_high)
                # If the rectangle is void, set all coordinates to zero.
                else:
                    self.photo_editor.set_selection_rectangle(0, 0, 0, 0)



    def mouseMoveEvent(self, event):
        """
        The mouse is moved while being pressed.

        :param event: event object
        :return: -
        """

        pos = event.lastScenePos()
        x = int(pos.x())
        y = int(pos.y())

        if not self.photo_editor.drag_mode and self.photo_editor.hasPhoto():

            # The mouse is moved with the left button pressed.
            if self.left_button_pressed:

                # Compute the new rectangular patch.
                new_sp = SelectionRectangleGraphicsItem(self.left_y_start, self.left_x_start, y, x)

                # If the rectangle was drawn for a previous location, replace it with the new one.
                if self.remember_rp is not None:
                    self.removeItem(self.remember_rp)
                self.addItem(new_sp)
                self.remember_rp = new_sp

            self.update()


class RectangularPatchEditor(FrameViewer):
    """
    This widget implements an editor for handling a rectangular patch superimposed onto an image.
    It supports two modes:
    - In "drag mode" the mouse can be used for panning, and the scroll wheel for zooming.
    - In "alignment point mode" the mouse is used to create/remove APs, to move them or to change
      their sizes.
    The "cntrl" key is used to switch between the two modes.
    """

    def __init__(self, image):
        super(RectangularPatchEditor, self).__init__()

        self.image = image

        # Initialize the rectangular patch.
        self.y_low = None
        self.y_high = None
        self.x_low = None
        self.x_high = None

        self.setPhoto(self.image)

    def initialize_scene(self):
        """
        Initialize the scene. This object handles mouse events if not in drag mode. In derived
        viewer classes this method is replaced with the instantiation of a custom version of the
        graphics scene.

        :return:
        """

        self._scene = GraphicsScene(self, self)
        self._scene.addItem(self._photo)
        self.setScene(self._scene)

    def set_selection_rectangle(self, y_low, y_high, x_low, x_high):
        """
        This method is invoked from the underlying scene object when the left mouse button is
        released, and thus a rectangular patch is defined.

        :param y_low: Lower y bound of patch
        :param y_high: Upper y bound of patch
        :param x_low: Lower x bound of patch
        :param x_high: Upper x bound of patch
        :return: -
        """

        self.y_low = max(0, y_low)
        self.y_high = min(self.shape_y, y_high)
        self.x_low = max(0, x_low)
        self.x_high = min(self.shape_x, x_high)

    def selection_rectangle(self):
        """
        Return the coordinate bounds of the selected rectangle.

        :return: Tuple with 4 ints: (lower y bound, upper y bound, lower x bound, upper x bound)
        """

        if self.y_low is not None:
            return (self.y_low, self.y_high, self.x_low, self.x_high)
        else:
            return (0, 0, 0, 0)


class RectangularPatchEditorWidget(QtWidgets.QFrame, Ui_rectangular_patch_editor):
    """
    This widget implements a rectangular patch editor, to be used for the selection of the
    stabilization patch and the ROI rectangle.
    """

    def __init__(self, parent_gui, frame, message, signal_finished):
        """
        Initialization of the widget.

        :param parent_gui: Parent GUI object
        :param frame: Background image on which the patch is superimposed. Usually, the mean frame
                      is used for this purpose.
        :param message: Message to tell the user what to do.
        :param signal_finished: Qt signal with signature (int, int, int, int) sending the
                                coordinate bounds (y_low, y_high, x_low, x_high) of the patch
                                selected, or (0, 0, 0, 0) if unsuccessful.
        """

        super(RectangularPatchEditorWidget, self).__init__(parent_gui)
        self.setupUi(self)

        self.parent_gui = parent_gui

        # Convert the frame into uint8 format. If the frame type is uint16, values
        # correspond to 16bit resolution.
        if frame.dtype == uint16 or frame.dtype == int32:
            self.frame = (frame / 256).astype(uint8)
        elif frame.dtype == uint8:
            self.frame = frame
        else:
            raise NotSupportedError("Attempt to set a photo in frame viewer with type neither"
                                    " uint8 nor uint16 not int32")

        self.message = message
        self.signal_finished = signal_finished

        self.viewer = RectangularPatchEditor(self.frame)
        self.verticalLayout.insertWidget(0, self.viewer)

        self.messageLabel.setText(self.message)
        self.messageLabel.setStyleSheet('color: red')
        self.buttonBox.accepted.connect(self.done)
        self.buttonBox.rejected.connect(self.reject)
        self.shape_y = None
        self.shape_x = None
        self.y_low = None
        self.y_high = None
        self.x_low = None
        self.x_high = None

    def done(self):
        """
        On exit from the rectangular patch editor, look up the coordinate bounds of the rectangular
        patch.

        :return: -
        """

        if self.viewer.selection_rectangle() is not None:
            self.y_low, self.y_high, self.x_low, self.x_high = self.viewer.selection_rectangle()

        # If the patch was selected successfully, send the patch bounds to the workflow thread.
        # If it was not successful, send (0, 0, 0, 0).
        if self.parent_gui is not None:
            if self.y_low is not None:
                self.signal_finished.emit(self.y_low, self.y_high,
                                                         self.x_low, self.x_high)
            # If the patch is not valid, do frame alignment in automatic mode. This is done if
            # all bounds are zero.
            else:
                self.signal_finished.emit(0, 0, 0, 0)

        # Close the Window.
        self.close()

    def reject(self):
        """
        If the "cancel" button is pressed, the coordinates are not stored.

        :return: -
        """

        # No rectangle was selected. Continue the workflow in automatic mode.
        self.signal_finished.emit(0, 0, 0, 0)
        self.close()


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/another_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(
            rank_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()
    try:
        align_frames.align_frames()
    except NotSupportedError as e:
        print("Error: " + e.message)
        exit()
    except InternalError as e:
        print("Warning: " + e.message)
        for index, frame_number in enumerate(align_frames.failed_index_list):
            print("Shift computation failed for frame " + str(
                align_frames.failed_index_list[index]) + ", minima list: " + str(
                align_frames.dev_r_list[index]))
    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()
    border = configuration.align_frames_search_width
    # border = 100

    app = QtWidgets.QApplication(argv)
    window = RectangularPatchEditorWidget(None, average[border:-border, border:-border],
            "With 'ctrl' and the left mouse button pressed, draw a rectangular patch "
            "to be used for frame alignment. Or just press 'OK / Cancel' (automatic selection).",
            None)
    window.setMinimumSize(800, 600)
    window.showMaximized()
    app.exec_()

    print("Rectangle selected, y_low: " + str(border+window.y_low) + ", y_high: " +
           str(border+window.y_high) + ", x_low: " + str(border+window.x_low) + ", x_high: "
           + str(border+window.x_high))

    exit()