| 1 |
|
| 2 |
""" |
| 3 |
Copyright (c) 2018 Rolf Hempel, [email protected] |
| 4 |
|
| 5 |
This file is part of the PlanetarySystemStacker tool (PSS). |
| 6 |
https://github.com/Rolf-Hempel/PlanetarySystemStacker |
| 7 |
|
| 8 |
PSS is free software: you can redistribute it and/or modify |
| 9 |
it under the terms of the GNU General Public License as published by |
| 10 |
the Free Software Foundation, either version 3 of the License, or |
| 11 |
(at your option) any later version. |
| 12 |
|
| 13 |
This program is distributed in the hope that it will be useful, |
| 14 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
GNU General Public License for more details. |
| 17 |
|
| 18 |
You should have received a copy of the GNU General Public License |
| 19 |
along with PSS. If not, see <http://www.gnu.org/licenses/>. |
| 20 |
|
| 21 |
Part of this module (in class "AlignmentPointEditor" was copied from |
| 22 |
https://stackoverflow.com/questions/35508711/how-to-enable-pan-and-zoom-in-a-qgraphicsview |
| 23 |
|
| 24 |
""" |
| 25 |
from glob import glob |
| 26 |
from sys import argv, exit |
| 27 |
from time import time |
| 28 |
|
| 29 |
from PyQt5 import QtCore, QtGui, QtWidgets |
| 30 |
from numpy import uint8 |
| 31 |
|
| 32 |
from align_frames import AlignFrames |
| 33 |
from alignment_point_editor_gui import Ui_alignment_point_editor |
| 34 |
from alignment_points import AlignmentPoints |
| 35 |
from configuration import Configuration, ConfigurationParameters |
| 36 |
from exceptions import InternalError, NotSupportedError, Error |
| 37 |
from frame_viewer import FrameViewer |
| 38 |
from frames import Frames |
| 39 |
from rank_frames import RankFrames |
| 40 |
|
| 41 |
|
| 42 |
class GraphicsScene(QtWidgets.QGraphicsScene): |
| 43 |
""" |
| 44 |
Handle mouse events on the graphics scene depicting the image with the alignment points (APs) |
| 45 |
superimposed. The left mouse button can be used to create APs, to move them, and to change their |
| 46 |
sizes. Using the right mouse button APs can be deleted. |
| 47 |
|
| 48 |
""" |
| 49 |
|
| 50 |
def __init__(self, photo_editor, parent=None): |
| 51 |
""" |
| 52 |
Initialize the scene object. |
| 53 |
|
| 54 |
:param photo_editor: the object in which the scene is defined |
| 55 |
:param parent: The parent class |
| 56 |
""" |
| 57 |
|
| 58 |
QtWidgets.QGraphicsScene.__init__(self, parent) |
| 59 |
self.photo_editor = photo_editor |
| 60 |
self.left_button_pressed = False |
| 61 |
self.right_button_pressed = False |
| 62 |
self.moved_ap = None |
| 63 |
self.new_ap = False |
| 64 |
self.remember_ap = None |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
self.single_click_threshold = 5 |
| 70 |
|
| 71 |
|
| 72 |
self.max_match_distance = 5 |
| 73 |
|
| 74 |
self.ap_size_change_factor = 1.05 |
| 75 |
|
| 76 |
def mousePressEvent(self, event): |
| 77 |
""" |
| 78 |
A nouse button is pressed. This can either be a lft or a right button. |
| 79 |
|
| 80 |
:param event: event object |
| 81 |
:return: - |
| 82 |
""" |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
if not self.photo_editor.drag_mode: |
| 87 |
pos = event.lastScenePos() |
| 88 |
x = int(pos.x()) |
| 89 |
y = int(pos.y()) |
| 90 |
|
| 91 |
|
| 92 |
if event.button() == QtCore.Qt.LeftButton: |
| 93 |
self.left_button_pressed = True |
| 94 |
|
| 95 |
|
| 96 |
neighbor_ap, distance = self.photo_editor.aps.find_neighbor(y, x, |
| 97 |
self.photo_editor.aps.alignment_points) |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
if neighbor_ap and distance < self.max_match_distance: |
| 102 |
self.moved_ap = neighbor_ap |
| 103 |
self.remember_ap = neighbor_ap.copy() |
| 104 |
|
| 105 |
|
| 106 |
else: |
| 107 |
|
| 108 |
|
| 109 |
self.remember_ap = self.photo_editor.aps.new_alignment_point(y, x, False, False, |
| 110 |
False, False) |
| 111 |
if self.remember_ap: |
| 112 |
self.new_ap = True |
| 113 |
|
| 114 |
|
| 115 |
elif event.button() == QtCore.Qt.RightButton: |
| 116 |
self.right_button_pressed = True |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
self.right_y_start = pos.y() |
| 121 |
self.right_x_start = pos.x() |
| 122 |
self.remember_sr = None |
| 123 |
|
| 124 |
def mouseReleaseEvent(self, event): |
| 125 |
""" |
| 126 |
A mouse button is released. |
| 127 |
|
| 128 |
:param event: event object |
| 129 |
:return: - |
| 130 |
""" |
| 131 |
|
| 132 |
if not self.photo_editor.drag_mode: |
| 133 |
pos = event.lastScenePos() |
| 134 |
x = int(pos.x()) |
| 135 |
y = int(pos.y()) |
| 136 |
|
| 137 |
|
| 138 |
if event.button() == QtCore.Qt.LeftButton: |
| 139 |
self.left_button_pressed = False |
| 140 |
|
| 141 |
|
| 142 |
if self.moved_ap: |
| 143 |
self.removeItem(self.remember_ap['graphics_item']) |
| 144 |
self.photo_editor.replace_alignment_point(self.moved_ap, |
| 145 |
self.remember_ap) |
| 146 |
self.moved_ap = None |
| 147 |
|
| 148 |
elif self.new_ap: |
| 149 |
self.removeItem(self.remember_ap['graphics_item']) |
| 150 |
self.photo_editor.add_alignment_point(self.remember_ap) |
| 151 |
self.new_ap = False |
| 152 |
|
| 153 |
|
| 154 |
elif event.button() == QtCore.Qt.RightButton: |
| 155 |
self.right_button_pressed = False |
| 156 |
|
| 157 |
|
| 158 |
if max(abs(y - self.right_y_start), |
| 159 |
abs(x - self.right_x_start)) < self.single_click_threshold: |
| 160 |
|
| 161 |
|
| 162 |
ap, dist = self.photo_editor.aps.find_neighbor(y, x, |
| 163 |
self.photo_editor.aps.alignment_points) |
| 164 |
self.photo_editor.remove_alignment_points([ap]) |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
else: |
| 169 |
y_low = min(self.right_y_start, self.right_y_end) |
| 170 |
y_high = max(self.right_y_start, self.right_y_end) |
| 171 |
x_low = min(self.right_x_start, self.right_x_end) |
| 172 |
x_high = max(self.right_x_start, self.right_x_end) |
| 173 |
remove_ap_list = self.photo_editor.aps.find_alignment_points(y_low, y_high, |
| 174 |
x_low, x_high) |
| 175 |
self.removeItem(self.remember_sr) |
| 176 |
self.photo_editor.remove_alignment_points(remove_ap_list) |
| 177 |
self.update() |
| 178 |
|
| 179 |
def mouseMoveEvent(self, event): |
| 180 |
""" |
| 181 |
The mouse is moved while being pressed. |
| 182 |
|
| 183 |
:param event: event object |
| 184 |
:return: - |
| 185 |
""" |
| 186 |
|
| 187 |
pos = event.lastScenePos() |
| 188 |
self.x = int(pos.x()) |
| 189 |
self.y = int(pos.y()) |
| 190 |
|
| 191 |
if not self.photo_editor.drag_mode: |
| 192 |
|
| 193 |
if self.left_button_pressed and (self.moved_ap or self.new_ap): |
| 194 |
|
| 195 |
|
| 196 |
new_ap = self.remember_ap.copy() |
| 197 |
|
| 198 |
|
| 199 |
new_ap = self.photo_editor.aps.move_alignment_point(new_ap, self.y, self.x) |
| 200 |
|
| 201 |
if new_ap: |
| 202 |
|
| 203 |
|
| 204 |
self.removeItem(self.remember_ap['graphics_item']) |
| 205 |
|
| 206 |
new_ap['graphics_item'] = AlignmentPointGraphicsItem(new_ap) |
| 207 |
self.addItem(new_ap['graphics_item']) |
| 208 |
self.remember_ap = new_ap |
| 209 |
|
| 210 |
|
| 211 |
elif self.right_button_pressed: |
| 212 |
self.right_y_end = self.y |
| 213 |
self.right_x_end = self.x |
| 214 |
|
| 215 |
|
| 216 |
new_sr = SelectionRectangleGraphicsItem(self.right_y_start, self.right_x_start, |
| 217 |
self.y, self.x) |
| 218 |
|
| 219 |
|
| 220 |
if self.remember_sr is not None: |
| 221 |
self.removeItem(self.remember_sr) |
| 222 |
self.addItem(new_sr) |
| 223 |
self.remember_sr = new_sr |
| 224 |
self.update() |
| 225 |
|
| 226 |
def wheelEvent(self, event): |
| 227 |
""" |
| 228 |
Handle scroll events. Change the size of the nearest AP. |
| 229 |
|
| 230 |
:param event: wheel event object |
| 231 |
:return: - |
| 232 |
""" |
| 233 |
|
| 234 |
|
| 235 |
self.change_ap_size(event.angleDelta().y()) |
| 236 |
|
| 237 |
def keyPressEvent(self, event): |
| 238 |
""" |
| 239 |
Handle key events as an alternative to wheel events. Change the size of the nearest AP. |
| 240 |
|
| 241 |
:param event: key event |
| 242 |
:return: - |
| 243 |
""" |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
if event.key() == 93 and event.modifiers() & QtCore.Qt.ControlModifier: |
| 248 |
self.change_ap_size(1) |
| 249 |
elif event.key() == QtCore.Qt.Key_Minus and event.modifiers() & QtCore.Qt.ControlModifier: |
| 250 |
self.change_ap_size(-1) |
| 251 |
|
| 252 |
def change_ap_size(self, direction): |
| 253 |
""" |
| 254 |
Change the size of the nearest AP. |
| 255 |
|
| 256 |
:param direction: If > 0, increase the size by a fixed factor. If < 0, decrease its size. |
| 257 |
:return: - |
| 258 |
""" |
| 259 |
|
| 260 |
|
| 261 |
ap, dist = self.photo_editor.aps.find_neighbor(self.y, self.x, |
| 262 |
self.photo_editor.aps.alignment_points) |
| 263 |
|
| 264 |
|
| 265 |
if ap is None: |
| 266 |
return |
| 267 |
|
| 268 |
|
| 269 |
if direction > 0: |
| 270 |
factor = self.ap_size_change_factor |
| 271 |
else: |
| 272 |
factor = 1. / self.ap_size_change_factor |
| 273 |
|
| 274 |
|
| 275 |
new_ap = ap.copy() |
| 276 |
if self.photo_editor.aps.resize_alignment_point(new_ap, factor): |
| 277 |
new_ap['graphics_item'] = AlignmentPointGraphicsItem(new_ap) |
| 278 |
|
| 279 |
self.photo_editor.replace_alignment_point(ap, new_ap) |
| 280 |
|
| 281 |
|
| 282 |
class AlignmentPointGraphicsItem(QtWidgets.QGraphicsItem): |
| 283 |
""" |
| 284 |
This widget represents an AP. It consists of a small red dot at the AP location, a red |
| 285 |
bounding rectangle, and a transparent green filling. |
| 286 |
""" |
| 287 |
|
| 288 |
def __init__(self, ap): |
| 289 |
super(AlignmentPointGraphicsItem, self).__init__() |
| 290 |
|
| 291 |
|
| 292 |
self.color_surface = QtGui.QColor(255, 255, 0, 60) |
| 293 |
|
| 294 |
|
| 295 |
self.color_boundary = QtGui.QColor(255, 0, 0) |
| 296 |
self.y = ap["y"] |
| 297 |
self.x = ap["x"] |
| 298 |
self.patch_y_low = ap["patch_y_low"] |
| 299 |
self.patch_y_high = ap["patch_y_high"] |
| 300 |
self.patch_x_low = ap["patch_x_low"] |
| 301 |
self.patch_x_high = ap["patch_x_high"] |
| 302 |
self.pen_boundary = QtGui.QPen(self.color_boundary) |
| 303 |
self.pen_boundary.setStyle(0) |
| 304 |
self.width_x = self.patch_x_high - self.patch_x_low |
| 305 |
self.width_x_external = self.width_x + self.pen_boundary.width() |
| 306 |
self.width_y = self.patch_y_high - self.patch_y_low |
| 307 |
self.width_y_external = self.width_y + self.pen_boundary.width() |
| 308 |
|
| 309 |
|
| 310 |
self.dot_width = max(1, int(self.width_x / 30)) |
| 311 |
|
| 312 |
def boundingRect(self): |
| 313 |
return QtCore.QRectF(self.patch_x_low, self.patch_y_low, self.width_x_external, |
| 314 |
self.width_y_external) |
| 315 |
|
| 316 |
def paint(self, painter, option, widget): |
| 317 |
painter.setPen(self.pen_boundary) |
| 318 |
painter.setBrush(self.color_boundary) |
| 319 |
painter.drawEllipse(self.x, self.y, self.dot_width, self.dot_width) |
| 320 |
painter.setBrush(self.color_surface) |
| 321 |
painter.drawRect(self.patch_x_low, self.patch_y_low, self.width_x, self.width_y) |
| 322 |
|
| 323 |
|
| 324 |
class SelectionRectangleGraphicsItem(QtWidgets.QGraphicsItem): |
| 325 |
""" |
| 326 |
This widget represents the selection rectangle opening when the mouse is being moved with the |
| 327 |
right mouse button pressed. |
| 328 |
""" |
| 329 |
|
| 330 |
def __init__(self, y_start, x_start, y_end, x_end): |
| 331 |
super(SelectionRectangleGraphicsItem, self).__init__() |
| 332 |
|
| 333 |
|
| 334 |
self.color_surface = QtGui.QColor(0, 0, 255, 40) |
| 335 |
|
| 336 |
|
| 337 |
self.color_boundary = QtGui.QColor(255, 0, 0) |
| 338 |
|
| 339 |
|
| 340 |
self.y_low = min(y_start, y_end) |
| 341 |
self.x_low = min(x_start, x_end) |
| 342 |
self.y_high = max(y_start, y_end) |
| 343 |
self.x_high = max(x_start, x_end) |
| 344 |
self.pen_boundary = QtGui.QPen(self.color_boundary) |
| 345 |
self.pen_boundary.setWidth(1) |
| 346 |
self.width_y = self.y_high - self.y_low |
| 347 |
self.width_x = self.x_high - self.x_low |
| 348 |
self.width_y_external = self.width_y + self.pen_boundary.width() |
| 349 |
self.width_x_external = self.width_x + self.pen_boundary.width() |
| 350 |
|
| 351 |
def boundingRect(self): |
| 352 |
return QtCore.QRectF(self.x_low, self.y_low, self.width_x_external, self.width_y_external) |
| 353 |
|
| 354 |
def paint(self, painter, option, widget): |
| 355 |
painter.setPen(self.pen_boundary) |
| 356 |
painter.setBrush(self.color_surface) |
| 357 |
painter.drawRect(self.x_low, self.y_low, self.width_x, self.width_y) |
| 358 |
|
| 359 |
|
| 360 |
class AlignmentPointEditor(FrameViewer): |
| 361 |
""" |
| 362 |
This widget implements an editor for handling APs superimposed onto an image. It supports two |
| 363 |
modes: |
| 364 |
- In "drag mode" the mouse can be used for panning, and the scroll wheel for zooming. |
| 365 |
- In "alignment point mode" the mouse is used to create/remove APs, to move them or to change |
| 366 |
their sizes. |
| 367 |
The "cntrl" key is used to switch between the two modes. |
| 368 |
""" |
| 369 |
|
| 370 |
def __init__(self, image, alignment_points): |
| 371 |
super(AlignmentPointEditor, self).__init__() |
| 372 |
self.image = image |
| 373 |
|
| 374 |
|
| 375 |
self.undoStack = QtWidgets.QUndoStack(self) |
| 376 |
|
| 377 |
|
| 378 |
self.aps = alignment_points |
| 379 |
|
| 380 |
|
| 381 |
self.setPhoto(self.image) |
| 382 |
|
| 383 |
def initialize_scene(self): |
| 384 |
""" |
| 385 |
Initialize the scene. This object handles mouse events if not in drag mode. In derived |
| 386 |
viewer classes this method is replaced with the instantiation of a custom version of the |
| 387 |
graphics scene. |
| 388 |
|
| 389 |
:return: |
| 390 |
""" |
| 391 |
|
| 392 |
self._scene = GraphicsScene(self, self) |
| 393 |
self._scene.addItem(self._photo) |
| 394 |
self.setScene(self._scene) |
| 395 |
|
| 396 |
def keyPressEvent(self, event): |
| 397 |
""" |
| 398 |
The control key is used to switch between drag and AP modes. |
| 399 |
|
| 400 |
:param event: event object |
| 401 |
:return: - |
| 402 |
""" |
| 403 |
|
| 404 |
|
| 405 |
if event.key() == QtCore.Qt.Key_Z and event.modifiers() & QtCore.Qt.ControlModifier: |
| 406 |
self.undoStack.undo() |
| 407 |
elif event.key() == QtCore.Qt.Key_Y and event.modifiers() & QtCore.Qt.ControlModifier: |
| 408 |
self.undoStack.redo() |
| 409 |
else: |
| 410 |
super(AlignmentPointEditor, self).keyPressEvent(event) |
| 411 |
|
| 412 |
def createApGrid(self): |
| 413 |
""" |
| 414 |
Create an initial alignment point grid, and display it in the viewer. |
| 415 |
|
| 416 |
:return: - |
| 417 |
""" |
| 418 |
|
| 419 |
command = CommandCreateApGrid(self) |
| 420 |
self.undoStack.push(command) |
| 421 |
|
| 422 |
def add_alignment_point(self, ap): |
| 423 |
""" |
| 424 |
Add an AP using the undo framework. |
| 425 |
|
| 426 |
:param ap: AP object in the AP list |
| 427 |
:return: - |
| 428 |
""" |
| 429 |
|
| 430 |
command = CommandAdd(self, self.aps, ap) |
| 431 |
self.undoStack.push(command) |
| 432 |
|
| 433 |
def remove_alignment_points(self, ap_list): |
| 434 |
""" |
| 435 |
Remove an AP using the undo framework. |
| 436 |
|
| 437 |
:param ap: AP object in the AP list |
| 438 |
:return: - |
| 439 |
""" |
| 440 |
|
| 441 |
command = CommandRemove(self, self.aps, ap_list) |
| 442 |
self.undoStack.push(command) |
| 443 |
|
| 444 |
def replace_alignment_point(self, ap_old, ap_new): |
| 445 |
""" |
| 446 |
Replace an AP with another one using the undo framework. |
| 447 |
|
| 448 |
:param ap_old: AP object in the AP list to be replaced |
| 449 |
:param ap_new: new AP object to be added to the AP list |
| 450 |
:return: - |
| 451 |
""" |
| 452 |
|
| 453 |
command = CommandReplace(self, self.aps, ap_old, ap_new) |
| 454 |
self.undoStack.push(command) |
| 455 |
|
| 456 |
|
| 457 |
class CommandCreateApGrid(QtWidgets.QUndoCommand): |
| 458 |
""" |
| 459 |
Undoable command to replace all existing APs with a new AP grid. |
| 460 |
""" |
| 461 |
def __init__(self, photo_editor): |
| 462 |
super(CommandCreateApGrid, self).__init__() |
| 463 |
self.photo_editor = photo_editor |
| 464 |
|
| 465 |
self.old_ap_list = self.photo_editor.aps.alignment_points |
| 466 |
|
| 467 |
self.photo_editor.aps.create_ap_grid() |
| 468 |
|
| 469 |
self.new_ap_list = self.photo_editor.aps.alignment_points |
| 470 |
for ap in self.new_ap_list: |
| 471 |
ap['graphics_item'] = AlignmentPointGraphicsItem(ap) |
| 472 |
|
| 473 |
def redo(self): |
| 474 |
|
| 475 |
for item in self.photo_editor._scene.items(): |
| 476 |
if isinstance(item, AlignmentPointGraphicsItem): |
| 477 |
self.photo_editor._scene.removeItem(item) |
| 478 |
|
| 479 |
|
| 480 |
self.photo_editor.aps.alignment_points = self.new_ap_list.copy() |
| 481 |
|
| 482 |
for ap in self.photo_editor.aps.alignment_points: |
| 483 |
self.photo_editor._scene.addItem(ap['graphics_item']) |
| 484 |
self.photo_editor._scene.update() |
| 485 |
self.photo_editor.setFocus() |
| 486 |
|
| 487 |
def undo(self): |
| 488 |
|
| 489 |
self.photo_editor.aps.alignment_points = self.old_ap_list |
| 490 |
|
| 491 |
|
| 492 |
for item in self.photo_editor._scene.items(): |
| 493 |
if isinstance(item, AlignmentPointGraphicsItem): |
| 494 |
self.photo_editor._scene.removeItem(item) |
| 495 |
|
| 496 |
for ap in self.photo_editor.aps.alignment_points: |
| 497 |
self.photo_editor._scene.addItem(ap['graphics_item']) |
| 498 |
self.photo_editor._scene.update() |
| 499 |
self.photo_editor.setFocus() |
| 500 |
|
| 501 |
|
| 502 |
class CommandAdd(QtWidgets.QUndoCommand): |
| 503 |
""" |
| 504 |
Undoable command to add an AP to the AP list. |
| 505 |
""" |
| 506 |
def __init__(self, photo_editor, aps_object, ap): |
| 507 |
super(CommandAdd, self).__init__() |
| 508 |
self.photo_editor = photo_editor |
| 509 |
self.aps = aps_object |
| 510 |
self.ap = ap |
| 511 |
self.ap['graphics_item'] = AlignmentPointGraphicsItem(ap) |
| 512 |
|
| 513 |
def redo(self): |
| 514 |
self.aps.add_alignment_point(self.ap) |
| 515 |
self.photo_editor._scene.addItem(self.ap['graphics_item']) |
| 516 |
self.photo_editor._scene.update() |
| 517 |
self.photo_editor.setFocus() |
| 518 |
|
| 519 |
def undo(self): |
| 520 |
self.photo_editor._scene.removeItem(self.ap['graphics_item']) |
| 521 |
self.aps.remove_alignment_points([self.ap]) |
| 522 |
self.photo_editor._scene.update() |
| 523 |
self.photo_editor.setFocus() |
| 524 |
|
| 525 |
|
| 526 |
class CommandRemove(QtWidgets.QUndoCommand): |
| 527 |
""" |
| 528 |
Undoable command to remove an AP from the AP list. |
| 529 |
""" |
| 530 |
def __init__(self, photo_editor, aps_object, ap_list): |
| 531 |
super(CommandRemove, self).__init__() |
| 532 |
self.photo_editor = photo_editor |
| 533 |
self.aps = aps_object |
| 534 |
self.ap_list = ap_list |
| 535 |
|
| 536 |
def redo(self): |
| 537 |
for ap in self.ap_list: |
| 538 |
self.photo_editor._scene.removeItem(ap['graphics_item']) |
| 539 |
self.aps.remove_alignment_points(self.ap_list) |
| 540 |
self.photo_editor._scene.update() |
| 541 |
self.photo_editor.setFocus() |
| 542 |
|
| 543 |
def undo(self): |
| 544 |
for ap in self.ap_list: |
| 545 |
self.aps.add_alignment_point(ap) |
| 546 |
self.photo_editor._scene.addItem(ap['graphics_item']) |
| 547 |
self.photo_editor._scene.update() |
| 548 |
self.photo_editor.setFocus() |
| 549 |
|
| 550 |
|
| 551 |
class CommandReplace(QtWidgets.QUndoCommand): |
| 552 |
""" |
| 553 |
Undoable command to replace an AP on the AP list with another one. |
| 554 |
""" |
| 555 |
def __init__(self, photo_editor, aps_object, ap_old, ap_new): |
| 556 |
super(CommandReplace, self).__init__() |
| 557 |
self.photo_editor = photo_editor |
| 558 |
self.aps = aps_object |
| 559 |
self.ap_old = ap_old |
| 560 |
self.ap_new = ap_new |
| 561 |
|
| 562 |
def redo(self): |
| 563 |
self.photo_editor._scene.removeItem(self.ap_old['graphics_item']) |
| 564 |
self.aps.replace_alignment_point(self.ap_old, self.ap_new) |
| 565 |
self.photo_editor._scene.addItem(self.ap_new['graphics_item']) |
| 566 |
self.photo_editor._scene.update() |
| 567 |
self.photo_editor.setFocus() |
| 568 |
|
| 569 |
def undo(self): |
| 570 |
self.photo_editor._scene.removeItem(self.ap_new['graphics_item']) |
| 571 |
self.aps.replace_alignment_point(self.ap_new, self.ap_old) |
| 572 |
self.photo_editor._scene.addItem(self.ap_old['graphics_item']) |
| 573 |
self.photo_editor._scene.update() |
| 574 |
self.photo_editor.setFocus() |
| 575 |
|
| 576 |
|
| 577 |
class AlignmentPointEditorWidget(QtWidgets.QFrame, Ui_alignment_point_editor): |
| 578 |
""" |
| 579 |
This widget implements the AP viewer, to be used as part of the application GUI. |
| 580 |
""" |
| 581 |
def __init__(self, parent_gui, configuration, align_frames, alignment_points, signal_finished, |
| 582 |
parent=None): |
| 583 |
""" |
| 584 |
Initialization of the widget. |
| 585 |
|
| 586 |
:param parent_gui: Parent GUI object |
| 587 |
:param image: Background image on which the APs are superimposed. Usually, the mean frame is |
| 588 |
used for this purpose. |
| 589 |
:param configuration: Configuration object with parameters |
| 590 |
:param align_frames: AlignFrames object with global shift information for all frames |
| 591 |
:param alignment_points: Alignment point object |
| 592 |
:param signal_finished: Qt signal telling the workflow thread that the APs have been |
| 593 |
created successfully |
| 594 |
""" |
| 595 |
|
| 596 |
|
| 597 |
super(AlignmentPointEditorWidget, self).__init__() |
| 598 |
self.setupUi(self) |
| 599 |
|
| 600 |
self.parent_gui = parent_gui |
| 601 |
|
| 602 |
|
| 603 |
if align_frames.mean_frame.dtype != uint8: |
| 604 |
self.mean_frame = align_frames.mean_frame / 256 |
| 605 |
else: |
| 606 |
self.mean_frame = align_frames.mean_frame |
| 607 |
|
| 608 |
self.configuration = configuration |
| 609 |
self.aps = alignment_points |
| 610 |
self.signal_finished = signal_finished |
| 611 |
|
| 612 |
|
| 613 |
self.viewer = AlignmentPointEditor(self.mean_frame, self.aps) |
| 614 |
self.horizontalLayout_2.insertWidget(0, self.viewer) |
| 615 |
self.horizontalLayout_2.setStretch(1, 0) |
| 616 |
|
| 617 |
|
| 618 |
self.initialize_widgets_and_local_parameters( |
| 619 |
self.configuration.alignment_points_half_box_width, |
| 620 |
self.configuration.alignment_points_structure_threshold, |
| 621 |
self.configuration.alignment_points_brightness_threshold) |
| 622 |
|
| 623 |
|
| 624 |
self.aphbw_slider_value.valueChanged['int'].connect(self.aphbw_changed) |
| 625 |
self.apst_slider_value.valueChanged['int'].connect(self.apst_changed) |
| 626 |
self.apbt_slider_value.valueChanged['int'].connect(self.apbt_changed) |
| 627 |
self.restore_standard_values.clicked.connect(self.restore_standard_parameters) |
| 628 |
|
| 629 |
self.btnApGrid.clicked.connect(self.viewer.createApGrid) |
| 630 |
self.btnUndo.clicked.connect(self.viewer.undoStack.undo) |
| 631 |
self.btnRedo.clicked.connect(self.viewer.undoStack.redo) |
| 632 |
self.buttonBox.accepted.connect(self.done) |
| 633 |
self.shape_y = None |
| 634 |
self.shape_x = None |
| 635 |
|
| 636 |
def initialize_widgets_and_local_parameters(self, half_box_width, structure_threshold, |
| 637 |
brightness_threshold): |
| 638 |
""" |
| 639 |
Initialize GUI widgets with current configuration parameter values. |
| 640 |
|
| 641 |
:param half_box_width: Half the width of a standard alignment box. |
| 642 |
:param structure_threshold: Minimum structure value for an alignment point |
| 643 |
(between 0. and 1.) |
| 644 |
:param brightness_threshold: The brightest pixel must be brighter than this value |
| 645 |
(0 < value <256) |
| 646 |
:return: - |
| 647 |
""" |
| 648 |
|
| 649 |
self.aphbw_slider_value.setValue(half_box_width * 2) |
| 650 |
self.aphbw_label_display.setText(str(half_box_width * 2)) |
| 651 |
self.apst_slider_value.setValue(round(structure_threshold * 100)) |
| 652 |
self.apst_label_display.setText(str(structure_threshold)) |
| 653 |
self.apbt_slider_value.setValue(brightness_threshold) |
| 654 |
self.apbt_label_display.setText(str(brightness_threshold)) |
| 655 |
self.label_message.setStyleSheet('color: red') |
| 656 |
|
| 657 |
|
| 658 |
def aphbw_changed(self, value): |
| 659 |
self.configuration.alignment_points_half_box_width = int(value / 2) |
| 660 |
self.configuration.set_derived_parameters() |
| 661 |
|
| 662 |
def apst_changed(self, value): |
| 663 |
self.configuration.alignment_points_structure_threshold = value / 100. |
| 664 |
self.apst_label_display.setText(str(self.configuration.alignment_points_structure_threshold)) |
| 665 |
|
| 666 |
def apbt_changed(self, value): |
| 667 |
self.configuration.alignment_points_brightness_threshold = value |
| 668 |
|
| 669 |
def restore_standard_parameters(self): |
| 670 |
""" |
| 671 |
Reset configuration parameters and GUI widget settings to standard values. |
| 672 |
|
| 673 |
:return: - |
| 674 |
""" |
| 675 |
|
| 676 |
|
| 677 |
config_parameters = ConfigurationParameters() |
| 678 |
config_parameters.set_defaults_ap_editing() |
| 679 |
|
| 680 |
|
| 681 |
self.configuration.alignment_points_half_box_width = \ |
| 682 |
config_parameters.alignment_points_half_box_width |
| 683 |
self.configuration.alignment_points_structure_threshold = \ |
| 684 |
config_parameters.alignment_points_structure_threshold |
| 685 |
self.configuration.alignment_points_brightness_threshold = \ |
| 686 |
config_parameters.alignment_points_brightness_threshold |
| 687 |
|
| 688 |
|
| 689 |
self.initialize_widgets_and_local_parameters( |
| 690 |
self.configuration.alignment_points_half_box_width, |
| 691 |
self.configuration.alignment_points_structure_threshold, |
| 692 |
self.configuration.alignment_points_brightness_threshold) |
| 693 |
|
| 694 |
def done(self): |
| 695 |
|
| 696 |
|
| 697 |
for ap in self.aps.alignment_points: |
| 698 |
if ap['reference_box'] is None: |
| 699 |
self.aps.set_reference_box(ap, self.aps.mean_frame) |
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
if self.parent_gui is not None: |
| 704 |
self.parent_gui.busy = False |
| 705 |
self.parent_gui.update_status() |
| 706 |
|
| 707 |
|
| 708 |
self.signal_finished.emit() |
| 709 |
|
| 710 |
self.close() |
| 711 |
|
| 712 |
|
| 713 |
if __name__ == '__main__': |
| 714 |
|
| 715 |
|
| 716 |
type = 'video' |
| 717 |
if type == 'image': |
| 718 |
names = glob('Images/2012*.tif') |
| 719 |
|
| 720 |
|
| 721 |
else: |
| 722 |
names = 'Videos/another_short_video.avi' |
| 723 |
print(names) |
| 724 |
|
| 725 |
|
| 726 |
configuration = Configuration() |
| 727 |
configuration.initialize_configuration() |
| 728 |
try: |
| 729 |
frames = Frames(configuration, names, type=type) |
| 730 |
print("Number of images read: " + str(frames.number)) |
| 731 |
print("Image shape: " + str(frames.shape)) |
| 732 |
except Error as e: |
| 733 |
print("Error: " + e.message) |
| 734 |
exit() |
| 735 |
|
| 736 |
|
| 737 |
rank_frames = RankFrames(frames, configuration) |
| 738 |
start = time() |
| 739 |
rank_frames.frame_score() |
| 740 |
end = time() |
| 741 |
print('Elapsed time in ranking images: {}'.format(end - start)) |
| 742 |
print("Index of maximum: " + str(rank_frames.frame_ranks_max_index)) |
| 743 |
print("Frame scores: " + str(rank_frames.frame_ranks)) |
| 744 |
print("Frame scores (sorted): " + str( |
| 745 |
[rank_frames.frame_ranks[i] for i in rank_frames.quality_sorted_indices])) |
| 746 |
print("Sorted index list: " + str(rank_frames.quality_sorted_indices)) |
| 747 |
|
| 748 |
|
| 749 |
align_frames = AlignFrames(frames, rank_frames, configuration) |
| 750 |
|
| 751 |
if configuration.align_frames_mode == "Surface": |
| 752 |
start = time() |
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
(y_low_opt, y_high_opt, x_low_opt, x_high_opt) = align_frames.compute_alignment_rect( |
| 757 |
configuration.align_frames_rectangle_scale_factor) |
| 758 |
end = time() |
| 759 |
print('Elapsed time in computing optimal alignment rectangle: {}'.format(end - start)) |
| 760 |
print("optimal alignment rectangle, x_low: " + str(x_low_opt) + ", x_high: " + str( |
| 761 |
x_high_opt) + ", y_low: " + str(y_low_opt) + ", y_high: " + str(y_high_opt)) |
| 762 |
reference_frame_with_alignment_points = frames.frames_mono( |
| 763 |
rank_frames.frame_ranks_max_index).copy() |
| 764 |
reference_frame_with_alignment_points[y_low_opt, |
| 765 |
x_low_opt:x_high_opt] = reference_frame_with_alignment_points[y_high_opt - 1, |
| 766 |
x_low_opt:x_high_opt] = 255 |
| 767 |
reference_frame_with_alignment_points[y_low_opt:y_high_opt, |
| 768 |
x_low_opt] = reference_frame_with_alignment_points[y_low_opt:y_high_opt, |
| 769 |
x_high_opt - 1] = 255 |
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
start = time() |
| 775 |
try: |
| 776 |
align_frames.align_frames() |
| 777 |
except NotSupportedError as e: |
| 778 |
print("Error: " + e.message) |
| 779 |
exit() |
| 780 |
except InternalError as e: |
| 781 |
print("Warning: " + e.message) |
| 782 |
for index, frame_number in enumerate(align_frames.failed_index_list): |
| 783 |
print("Shift computation failed for frame " + str( |
| 784 |
align_frames.failed_index_list[index]) + ", minima list: " + str( |
| 785 |
align_frames.dev_r_list[index])) |
| 786 |
end = time() |
| 787 |
print('Elapsed time in aligning all frames: {}'.format(end - start)) |
| 788 |
print("Frame shifts: " + str(align_frames.frame_shifts)) |
| 789 |
print("Intersection: " + str(align_frames.intersection_shape)) |
| 790 |
|
| 791 |
start = time() |
| 792 |
|
| 793 |
average = align_frames.average_frame() |
| 794 |
|
| 795 |
|
| 796 |
alignment_points = AlignmentPoints(configuration, frames, rank_frames, align_frames) |
| 797 |
end = time() |
| 798 |
print('Elapsed time in computing reference frame: {}'.format(end - start)) |
| 799 |
print("Reference frame computed from the best " + str( |
| 800 |
align_frames.average_frame_number) + " frames.") |
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
app = QtWidgets.QApplication(argv) |
| 805 |
window = AlignmentPointEditorWidget(None, configuration, align_frames, alignment_points, None) |
| 806 |
window.setMinimumSize(800,600) |
| 807 |
window.showMaximized() |
| 808 |
app.exec_() |
| 809 |
|
| 810 |
print("After AP editing, number of APs: " + str( |
| 811 |
len(alignment_points.alignment_points)) + ", aps dropped because too dim: " + str( |
| 812 |
alignment_points.alignment_points_dropped_dim) + ", aps dropped because too little " |
| 813 |
"structure: " + str( |
| 814 |
alignment_points.alignment_points_dropped_structure)) |
| 815 |
|
| 816 |
count_updates = 0 |
| 817 |
for ap in alignment_points.alignment_points: |
| 818 |
if ap['reference_box'] is not None: |
| 819 |
continue |
| 820 |
count_updates += 1 |
| 821 |
AlignmentPoints.set_reference_box(ap, align_frames.mean_frame) |
| 822 |
print("Buffers allocated for " + str(count_updates) + " alignment points.") |
| 823 |
exit() |
| 824 |
|