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
|
# -*- coding: utf-8; -*-
"""
Copyright (c) 2019 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
"""
# The following PyQt5 imports must precede any matplotlib imports. This is a workaround
# for a Matplotlib 2.2.2 bug.
from glob import glob
from sys import argv, exit
from time import time, sleep
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from PyQt5 import QtCore, QtGui, QtWidgets
from cv2 import NORM_MINMAX, normalize, cvtColor, COLOR_GRAY2RGB, circle, line
from matplotlib import patches
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as Canvas
from matplotlib.figure import Figure
from numpy import array, full, uint8, uint16, float64
from align_frames import AlignFrames
from configuration import Configuration
from exceptions import NotSupportedError, InternalError, Error
from frame_viewer_gui import Ui_frame_viewer
from frames import Frames
from miscellaneous import Miscellaneous
from rank_frames import RankFrames
class MatplotlibWidget(Canvas):
"""
This widget creates a plot of frame qualities, either sorted chronologically or by quality.
"""
def __init__(self, configuration, rank_frames, parent=None):
"""
Initialize the widget.
:param configuration: Configuration object with parameters
:param rank_frames: RankFrames object with global quality ranks (between 0. and 1.,
1. being optimal) for all frames
:param parent: Parent object
"""
super(MatplotlibWidget, self).__init__(Figure())
self.setParent(parent)
self.configuration = configuration
self.rank_frames = rank_frames
self.line_chronological = None
self.line_quality = None
self.dot = None
self.line_quality_cutoff = None
self.patch_quality_cutoff = None
plt.rcParams.update({'font.size': 8})
self.fig, self.ax = plt.subplots()
self.ax.invert_xaxis()
plt.subplots_adjust(left=0.23, right=0.95, top=0.98, bottom=0.12)
def renew_plot(self, index, frame_ordering, alignment_points_frame_number):
"""
Update the complete plot, including the quality line, the cutoff line, and the dot at the
position of the current frame.
:param index: Index of the current frame (relative to the selected order)
:param frame_ordering: Ordering of frames, either "chronological" or "quality"
:param alignment_points_frame_number: Number of frames to be stacked at each AP.
:return: -
"""
self.ax.clear()
self.line_quality_cutoff = None
self.patch_quality_cutoff = None
# The quality axis starts with 1. (highest quality).
self.ax.invert_xaxis()
# Remember objects drawn into the plot.
self.line_chronological = None
self.line_quality = None
self.dot = None
self.line_quality_cutoff = None
# Plot the new data.
self.plot_data(frame_ordering)
self.plot_cutoff_lines(frame_ordering, alignment_points_frame_number)
self.plot_dot(index)
def plot_data(self, frame_ordering):
"""
Plot the quality line. Frames (y axis) are ordered as specified with parameter
"frame_ordering".
:param frame_ordering: Ordering of frames, either "chronological" or "quality"
:return: -
"""
self.ax.set_ylim(0, self.rank_frames.number + 1)
self.y = array(range(1, self.rank_frames.number + 1))
if frame_ordering == "chronological":
if self.line_chronological is not None:
self.line_chronological.remove()
self.x = array(self.rank_frames.frame_ranks)
plt.ylabel('Frame numbers ordered chronologically')
plt.gca().invert_yaxis()
plt.xlabel('Quality')
self.line_chronological, = plt.plot(self.x, self.y, lw=1, color='blue')
plt.grid(True)
else:
if self.line_quality is not None:
self.line_quality.remove()
self.x = array(
[self.rank_frames.frame_ranks[i] for i in self.rank_frames.quality_sorted_indices])
plt.ylabel('Frame numbers ordered by quality')
plt.gca().invert_yaxis()
plt.xlabel('Quality')
self.line_quality, = plt.plot(self.x, self.y, lw=1, color='green')
plt.grid(True)
self.fig.canvas.draw()
self.fig.canvas.flush_events()
def plot_cutoff_lines(self, frame_ordering, alignment_points_frame_number):
"""
Plot a horizontal (if frames are ordered by quality) or vertical (if frames are ordered
chronologically) line to separate frames to be stacked from the other ones.
:param frame_ordering: Ordering of frames, either "chronological" or "quality"
:param alignment_points_frame_number: Number of frames to be stacked at each AP.
:return: -
"""
if frame_ordering == "chronological":
if self.line_quality_cutoff is not None:
self.line_quality_cutoff.remove()
if self.patch_quality_cutoff is not None:
self.patch_quality_cutoff.remove()
quality_cutoff = self.rank_frames.frame_ranks[
self.rank_frames.quality_sorted_indices[alignment_points_frame_number-1]]
x_cutoff = full((self.rank_frames.number,), quality_cutoff)
self.line_quality_cutoff, = plt.plot(x_cutoff, self.y, lw=1, color='orange')
width = 1. - quality_cutoff
height = self.rank_frames.number - 1
xy = (quality_cutoff, 1.)
else:
if self.line_quality_cutoff is not None:
self.line_quality_cutoff.remove()
if self.patch_quality_cutoff is not None:
self.patch_quality_cutoff.remove()
y_cutoff = full((self.rank_frames.number,), alignment_points_frame_number)
self.line_quality_cutoff, = plt.plot(self.x, y_cutoff, lw=1, color='orange')
width = 1. - self.x[-1]
height = alignment_points_frame_number
xy = (self.x[-1], 0.)
self.patch_quality_cutoff = patches.Rectangle(xy, width, height, linewidth=None,
facecolor=(0.5, 0.2, 0., 0.15))
self.ax.add_patch(self.patch_quality_cutoff)
self.fig.canvas.draw()
self.fig.canvas.flush_events()
def plot_dot(self, frame_index):
"""
Plot a dot on the quality line at the position of the current frame.
:param frame_index: Frame index
:return: -
"""
if self.dot is not None:
self.dot.remove()
self.dot = plt.scatter(self.x[frame_index], self.y[frame_index], s=20, color='red')
self.fig.canvas.draw()
self.fig.canvas.flush_events()
class FrameViewer(QtWidgets.QGraphicsView):
"""
This widget implements a frame viewer. Panning and zooming is implemented by using the mouse
and scroll wheel.
"""
zoom_factor_signal = QtCore.pyqtSignal(int)
def __init__(self):
super(FrameViewer, self).__init__()
self._empty = True
# Initialize a flag which indicates when an image is being loaded.
self.image_loading_busy = False
# Initialize the photo object. No image is loaded yet.
self._photo = QtWidgets.QGraphicsPixmapItem()
self.shape_y = None
self.shape_x = None
self.viewrect = self.viewport().rect()
self.setTransformationAnchor(QtWidgets.QGraphicsView.AnchorUnderMouse)
self.setResizeAnchor(QtWidgets.QGraphicsView.AnchorUnderMouse)
self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.setBackgroundBrush(QtGui.QBrush(QtGui.QColor(30, 30, 30)))
self.setFrameShape(QtWidgets.QFrame.NoFrame)
self.setDragMode(QtWidgets.QGraphicsView.ScrollHandDrag)
self.drag_mode = True
# Set the focus on the viewer.
self.setFocus()
def resizeEvent(self, event):
"""
This method is called when the window size changes. In this case the image is zoomed so it
fills the entire view.
:param event: Resize event.
:return: -
"""
# Set the rectangle surrounding the current view.
self.viewrect = self.viewport().rect()
self.fitInView()
return super(FrameViewer, self).resizeEvent(event)
def hasPhoto(self):
return not self._empty
def get_zoom_factor(self):
"""
Compute the current zoom factor. It is sent to the GUI widget via the "zoom_factor_signal".
:return: The current zoom factor, rounded to the next int.
"""
zoom_factor = round(self.scenerect.width() / self.photorect.width() * 100.)
self.zoom_factor_signal.emit(zoom_factor)
return zoom_factor
def fitInView(self):
"""
Scale the scene such that it fits into the window completely.
:return: -
"""
if not self.photorect.isNull():
if self.hasPhoto():
factor = min(self.viewrect.width() / self.scenerect.width(),
self.viewrect.height() / self.scenerect.height())
self.scale(factor, factor)
self.scenerect = self.transform().mapRect(self.photorect)
self.get_zoom_factor()
def set_original_scale(self):
"""
Scale the scene to the original size of the photo. If the photo has more pixels than the
current view, only part of the image is displayed.
:return: -
"""
if not self.photorect.isNull():
if self.hasPhoto():
factor = min(self.photorect.width() / self.scenerect.width(),
self.photorect.height() / self.scenerect.height())
self.scale(factor, factor)
self.scenerect = self.transform().mapRect(self.photorect)
self.get_zoom_factor()
def setPhoto(self, image, overlay_exclude_mark=False):
"""
Convert a color or grayscale image to a pixmap and assign it to the photo object.
:param image: Image to be displayed. The image is assumed to be in color or grayscale
format of length uint8 or uint16.
:param overlay_exclude_mark: If True, overlay a crossed-out red circle in the upper left
corner of the image.
:return: -
"""
# Indicate that an image is being loaded.
self.image_loading_busy = True
# Convert the image into uint8 format. If the frame type is uint16, values correspond to
# 16bit resolution.
if image.dtype == uint16:
image_uint8 = (image / 256.).astype(uint8)
elif image.dtype == uint8:
image_uint8 = image.astype(uint8)
elif image.dtype == float64:
image_uint8 = image.astype(uint8)
else:
raise NotSupportedError("Attempt to set a photo in frame viewer with type neither"
" uint8 nor uint16 nor float64")
self.shape_y = image_uint8.shape[0]
self.shape_x = image_uint8.shape[1]
# Normalize the frame brightness.
image_uint8 = normalize(image_uint8, None, alpha=0, beta=255, norm_type=NORM_MINMAX)
# Overlay a crossed-out red circle in the upper left image corner.
if overlay_exclude_mark:
if len(image_uint8.shape) == 2:
image_uint8 = cvtColor(image_uint8, COLOR_GRAY2RGB)
# The position and size of the mark are relative to the image size.
pos_x = int(round(self.shape_x / 10))
pos_y = int(round(self.shape_y / 10))
diameter = int(round(min(pos_x, pos_y) / 4))
circle(image_uint8, (pos_x, pos_y), diameter, (255, 0, 0), 2)
line(image_uint8, (pos_x - diameter, pos_y + diameter),
(pos_x + diameter, pos_y - diameter), (255, 0, 0), 2)
# The image is monochrome:
if len(image_uint8.shape) == 2:
qt_image = QtGui.QImage(image_uint8, self.shape_x, self.shape_y, self.shape_x,
QtGui.QImage.Format_Grayscale8)
# The image is RGB color.
else:
qt_image = QtGui.QImage(image_uint8, self.shape_x,
self.shape_y, 3 * self.shape_x, QtGui.QImage.Format_RGB888)
pixmap = QtGui.QPixmap(qt_image)
if pixmap and not pixmap.isNull():
self._empty = False
self._photo.setPixmap(pixmap)
else:
self._empty = True
self._photo.setPixmap(QtGui.QPixmap())
# Initialize the scene. This object handles mouse events if not in drag mode.
self.initialize_scene()
# Set the new rectangles surrounding the photo and the current scene.
self.photorect = QtCore.QRectF(self._photo.pixmap().rect())
self.scenerect = self.transform().mapRect(self.photorect)
# Release the image loading flag.
self.image_loading_busy = False
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 = QtWidgets.QGraphicsScene()
self._scene.addItem(self._photo)
self.setScene(self._scene)
def wheelEvent(self, event):
"""
Handle scroll events for zooming in and out of the scene. This is only active when a photo
is loaded.
:param event: wheel event object
:return: -
"""
if self.drag_mode:
# Depending of wheel direction, set the direction value to greater or smaller than 1.
self.zoom(event.angleDelta().y())
# If not in drag mode, the wheel event is handled at the scene level.
else:
try:
self._scene.wheelEvent(event)
# This exception occurs if the user uses "Ctrl + Wheel action" in the postproc_editor.
# There this activity does not make any sense, so just ignore the event.
except TypeError:
pass
def zoom(self, direction):
"""
Zoom in or out. This is only active when a photo is loaded. For small images (smaller than
the current view), zooming out is limited by the original photo resolution. For larger
images zooming out stops when the scene fills the entire view.
:param direction: If > 0, zoom in, otherwise zoom out.
:return: -
"""
if self.hasPhoto():
# Depending of direction value, set the zoom factor to greater or smaller than 1.
if direction > 0:
factor = 1.25
else:
min_factor = min(
min(self.photorect.width(), self.viewrect.width()) / self.scenerect.width(),
min(self.photorect.height(), self.viewrect.height()) / self.scenerect.height())
factor = max(0.8, min_factor)
self.scale(factor, factor)
self.scenerect = self.transform().mapRect(self.photorect)
self.get_zoom_factor()
def keyPressEvent(self, event):
"""
The + and - keys are used for zooming.
:param event: event object
:return: -
"""
# If the "+" key is pressed, zoom in. If "-" is pressed, zoom out.
if event.key() == QtCore.Qt.Key_Control:
self.setDragMode(QtWidgets.QGraphicsView.NoDrag)
self.drag_mode = False
elif event.key() == QtCore.Qt.Key_Plus and not event.modifiers() & QtCore.Qt.ControlModifier:
self.zoom(1)
elif event.key() == QtCore.Qt.Key_Minus and not event.modifiers() & QtCore.Qt.ControlModifier:
self.zoom(-1)
elif event.key() == QtCore.Qt.Key_1 and not event.modifiers() & QtCore.Qt.ControlModifier:
self.set_original_scale()
else:
super(FrameViewer, self).keyPressEvent(event)
def keyReleaseEvent(self, event):
# If the control key is released, switch back to "drag mode".
# Use default handling for other keys.
if event.key() == QtCore.Qt.Key_Control:
self.setDragMode(QtWidgets.QGraphicsView.ScrollHandDrag)
self.drag_mode = True
else:
super(FrameViewer, self).keyReleaseEvent(event)
class VideoFrameViewer(FrameViewer):
"""
This widget implements a frame viewer for frames in a video file. Panning and zooming is
implemented by using the mouse and scroll wheel.
"""
def __init__(self, frames, align_frames, frame_index=0):
super(VideoFrameViewer, self).__init__()
self.frames = frames
self.align_frames = align_frames
self.frame_index = frame_index
self.setPhoto(self.frame_index)
def setPhoto(self, index):
"""
Convert a grayscale image to a pixmap and assign it to the photo object.
:param index: Index into the frame list. Frames are assumed to be grayscale image in format
uint8 or uint16.
:return: -
"""
# Indicate that an image is being loaded.
self.image_loading_busy = True
image = self.frames.frames_mono(index)[self.align_frames.intersection_shape[0][0] -
self.align_frames.frame_shifts[index][0]:
self.align_frames.intersection_shape[0][1] -
self.align_frames.frame_shifts[index][0],
self.align_frames.intersection_shape[1][0] -
self.align_frames.frame_shifts[index][1]:
self.align_frames.intersection_shape[1][1] -
self.align_frames.frame_shifts[index][1]]
super(VideoFrameViewer, self).setPhoto(image)
class FrameViewerWidget(QtWidgets.QFrame, Ui_frame_viewer):
"""
This widget implements a frame viewer together with control elements to visualize frame
qualities, and to manipulate the stack limits.
"""
frame_player_start_signal = QtCore.pyqtSignal()
def __init__(self, parent_gui, configuration, frames, rank_frames, align_frames,
stacked_image_log_file, signal_finished, signal_payload):
"""
Initialization of the widget.
:param parent_gui: Parent GUI object
: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 stacked_image_log_file: Log file to be stored with results, or None.
:param signal_finished: Qt signal with signature (str) to trigger the next activity when
the viewer exits.
:param signal_payload: Payload of "signal_finished" (str).
"""
super(FrameViewerWidget, self).__init__(parent_gui)
self.setupUi(self)
# Keep references to upper level objects.
self.parent_gui = parent_gui
self.configuration = configuration
self.stacked_image_log_file = stacked_image_log_file
self.signal_finished = signal_finished
self.signal_payload = signal_payload
self.frames = frames
self.rank_frames = rank_frames
self.align_frames = align_frames
# Be careful: Indices are counted from 0, while widget contents are counted from 1 (to make
# it easier for the user.
self.quality_index = 0
self.frame_index = self.rank_frames.quality_sorted_indices[self.quality_index]
# Set up the frame viewer and put it in the upper left corner.
self.frame_viewer = VideoFrameViewer(self.frames, self.align_frames, self.frame_index)
self.frame_viewer.setObjectName("framewiever")
self.grid_layout.addWidget(self.frame_viewer, 0, 0, 4, 3)
self.widget_elements = [self.spinBox_chronological,
self.spinBox_quality,
self.slider_frames,
self.pushButton_play]
# Initialize variables. The values for "alignment_point_frame_number" and
# "alignment_points_frame_percent" are held as copies in this object. Only if the user
# presses "OK" at the end, the values are copied back into the configuration object.
self.stack_size_changed = None
# The number of frames to be stacked is selected explicitly.
if self.configuration.alignment_points_frame_number > 0:
self.alignment_points_frame_number = min(
self.configuration.alignment_points_frame_number, self.frames.number)
self.alignment_points_frame_percent = max(1, min(round(
self.alignment_points_frame_number / self.frames.number * 100.), 100))
else:
self.alignment_points_frame_percent = self.configuration.alignment_points_frame_percent
self.alignment_points_frame_number = max(1, round(
self.frames.number * self.alignment_points_frame_percent / 100.))
self.frame_ranks = rank_frames.frame_ranks
self.quality_sorted_indices = rank_frames.quality_sorted_indices
# Start with ordering frames by quality. This can be changed by the user using a radio
# button.
self.frame_ordering = "quality"
# Initialize a variable for communication with the frame_player object later.
self.run_player = False
# Create the frame player thread and start it. The player displays frames in succession.
# It is pushed on a different thread because otherwise the user could not stop it before it
# finishes.
self.frame_player = FramePlayer(self)
self.player_thread = QtCore.QThread()
self.frame_player.setParent(None)
self.frame_player.moveToThread(self.player_thread)
self.frame_player.block_widgets_signal.connect(self.block_widgets)
self.frame_player.unblock_widgets_signal.connect(self.unblock_widgets)
self.frame_player.set_photo_signal.connect(self.frame_viewer.setPhoto)
self.frame_player.set_slider_value.connect(self.slider_frames.setValue)
self.frame_player_start_signal.connect(self.frame_player.play)
self.player_thread.start()
# Initialization of GUI elements
self.slider_frames.setMinimum(1)
self.slider_frames.setMaximum(self.frames.number)
self.slider_frames.setValue(self.quality_index + 1)
self.spinBox_chronological.setMinimum(1)
self.spinBox_chronological.setMaximum(self.frames.number)
self.spinBox_chronological.setValue(self.frame_index + 1)
self.spinBox_quality.setMinimum(1)
self.spinBox_quality.setMaximum(self.frames.number)
self.spinBox_quality.setValue(self.quality_index + 1)
self.radioButton_quality.setChecked(True)
self.spinBox_number_frames.setMaximum(self.frames.number)
self.spinBox_number_frames.setValue(self.alignment_points_frame_number)
self.spinBox_percentage_frames.setValue(self.alignment_points_frame_percent)
# Create the Matplotlib widget showing the quality lines.
self.matplotlib_widget = MatplotlibWidget(self.configuration, self.rank_frames)
self.grid_layout.addWidget(Canvas(self.matplotlib_widget.fig), 0, 3, 2, 1)
self.grid_layout.setColumnStretch(0, 5)
self.grid_layout.setColumnStretch(1, 0)
self.grid_layout.setColumnStretch(2, 0)
self.grid_layout.setColumnStretch(3, 1)
self.grid_layout.setRowStretch(0, 1)
self.grid_layout.setRowStretch(1, 0)
self.grid_layout.setRowStretch(2, 0)
self.grid_layout.setRowStretch(3, 0)
# Connect signals with slots.
self.buttonBox.accepted.connect(self.done)
self.buttonBox.rejected.connect(self.reject)
self.slider_frames.valueChanged.connect(self.slider_frames_changed)
self.spinBox_number_frames.valueChanged.connect(self.spinbox_number_frames_changed)
self.spinBox_percentage_frames.valueChanged.connect(self.spinbox_percentage_frames_changed)
self.radioButton_quality.toggled.connect(self.radiobutton_quality_changed)
self.spinBox_chronological.valueChanged.connect(self.spinbox_chronological_changed)
self.spinBox_quality.valueChanged.connect(self.spinbox_quality_changed)
self.pushButton_set_stacking_limit.clicked.connect(
self.pushbutton_set_stacking_limit_clicked)
self.pushButton_play.clicked.connect(self.pushbutton_play_clicked)
self.pushButton_stop.clicked.connect(self.pushbutton_stop_clicked)
# Initialize the Matplotlib widget contents.
self.matplotlib_widget.renew_plot(self.quality_index, self.frame_ordering,
self.alignment_points_frame_number)
@QtCore.pyqtSlot()
def block_widgets(self):
"""
Disable GUI elements to prevent unwanted user interaction while the frame player is running.
:return: -
"""
for element in self.widget_elements:
element.setDisabled(True)
@QtCore.pyqtSlot()
def unblock_widgets(self):
"""
Enable GUI elements which were temporarily blocked when the player stops.
:return: -
"""
for element in self.widget_elements:
element.setDisabled(False)
def slider_frames_changed(self):
"""
The frames slider is changed by the user.
:return: -
"""
# Again, please note the difference between indexing and GUI displays.
index = self.slider_frames.value() - 1
# Differentiate between frame ordering (by quality or chronologically).
if self.frame_ordering == "quality":
self.frame_index = self.rank_frames.quality_sorted_indices[index]
self.quality_index = index
# Plot a dot on the quality line at the position of the current frame.
self.matplotlib_widget.plot_dot(self.quality_index)
else:
self.frame_index = index
self.quality_index = self.rank_frames.quality_sorted_indices.index(self.frame_index)
self.matplotlib_widget.plot_dot(self.frame_index)
# Block signals temporarily to avoid feedback loops. Then update widget contents.
self.spinBox_chronological.blockSignals(True)
self.spinBox_quality.blockSignals(True)
self.spinBox_chronological.setValue(self.frame_index + 1)
self.spinBox_quality.setValue(self.quality_index + 1)
self.spinBox_chronological.blockSignals(False)
self.spinBox_quality.blockSignals(False)
self.frame_viewer.setPhoto(self.frame_index)
def spinbox_number_frames_changed(self):
"""
The user has changed the number of frames to be stacked at each AP.
:return: -
"""
self.alignment_points_frame_number = self.spinBox_number_frames.value()
self.alignment_points_frame_percent = int(
round(self.alignment_points_frame_number * 100. / self.frames.number))
self.spinBox_percentage_frames.blockSignals(True)
self.spinBox_percentage_frames.setValue(self.alignment_points_frame_percent)
self.spinBox_percentage_frames.blockSignals(False)
self.matplotlib_widget.plot_cutoff_lines(self.frame_ordering,
self.alignment_points_frame_number)
self.stack_size_changed = 'number'
def spinbox_percentage_frames_changed(self):
"""
The user has changed the percentage of frames to be stacked at each AP.
:return:
"""
self.alignment_points_frame_percent = self.spinBox_percentage_frames.value()
self.alignment_points_frame_number = max(1, round(
self.frames.number * self.alignment_points_frame_percent / 100.))
self.spinBox_number_frames.blockSignals(True)
self.spinBox_number_frames.setValue(self.alignment_points_frame_number)
self.spinBox_number_frames.blockSignals(False)
self.matplotlib_widget.plot_cutoff_lines(self.frame_ordering,
self.alignment_points_frame_number)
self.stack_size_changed = 'percent'
def radiobutton_quality_changed(self):
"""
Toggle back and forth between frame ordering modes. The frame slider is updated to reflect
the index of the current frame in the new ordering scheme. The Matplotlib widget changes
its appearance.
:return: -
"""
if self.frame_ordering == "quality":
self.frame_ordering = "chronological"
self.slider_frames.setValue(self.frame_index + 1)
self.matplotlib_widget.renew_plot(self.frame_index, self.frame_ordering,
self.alignment_points_frame_number)
else:
self.frame_ordering = "quality"
self.slider_frames.setValue(self.quality_index + 1)
self.matplotlib_widget.renew_plot(self.quality_index, self.frame_ordering,
self.alignment_points_frame_number)
def spinbox_chronological_changed(self):
"""
The user has selected a new frame to be displayed by entering its chronological frame index.
All other widgets which depend on the current frame index are changed.
:return: -
"""
self.frame_index = self.spinBox_chronological.value() - 1
self.quality_index = self.rank_frames.quality_sorted_indices.index(self.frame_index)
self.slider_frames.blockSignals(True)
self.spinBox_quality.blockSignals(True)
self.spinBox_quality.setValue(self.quality_index + 1)
if self.frame_ordering == "quality":
self.slider_frames.setValue(self.quality_index + 1)
self.matplotlib_widget.plot_dot(self.quality_index)
else:
self.slider_frames.setValue(self.frame_index + 1)
self.matplotlib_widget.plot_dot(self.frame_index)
self.slider_frames.blockSignals(False)
self.spinBox_quality.blockSignals(False)
self.frame_viewer.setPhoto(self.frame_index)
def spinbox_quality_changed(self):
"""
The user has selected a new frame to be displayed by entering its quality index.
All other widgets which depend on the current frame index are changed.
:return:
"""
self.quality_index = self.spinBox_quality.value() - 1
self.frame_index = self.rank_frames.quality_sorted_indices[self.quality_index]
self.slider_frames.blockSignals(True)
self.spinBox_chronological.blockSignals(True)
self.spinBox_chronological.setValue(self.frame_index + 1)
if self.frame_ordering == "quality":
self.slider_frames.setValue(self.quality_index + 1)
self.matplotlib_widget.plot_dot(self.quality_index)
else:
self.slider_frames.setValue(self.frame_index + 1)
self.matplotlib_widget.plot_dot(self.frame_index)
self.slider_frames.blockSignals(False)
self.spinBox_chronological.blockSignals(False)
self.frame_viewer.setPhoto(self.frame_index)
def pushbutton_set_stacking_limit_clicked(self):
"""
The current frame defines the stacking limit. Compute the corresponding stack limit
variables and update the widgets. Since the signals are not deactivated before updating
the widgets, the Matplotlib widget gets updated as well.
:return:
"""
self.alignment_points_frame_number = self.quality_index + 1
self.alignment_points_frame_percent = max(1, round(
self.alignment_points_frame_number * 100. / self.frames.number))
self.spinBox_number_frames.setValue(self.alignment_points_frame_number)
self.spinBox_percentage_frames.setValue(self.alignment_points_frame_percent)
self.stack_size_changed = 'number'
@QtCore.pyqtSlot()
def pushbutton_play_clicked(self):
"""
Start the frame player if it is not running already.
:return: -
"""
self.frame_player_start_signal.emit()
def pushbutton_stop_clicked(self):
"""
When the frame player is running, it periodically checks this variable. If it is set to
False, the player stops.
:return:
"""
self.frame_player.run_player = False
def done(self):
"""
On exit from the frame viewer, update the stack frame size and send a completion signal.
:return: -
"""
# Check if a new stack size was selected.
if self.stack_size_changed is not None:
# The number of frames has been set explicitly. Check if it has changed.
if self.stack_size_changed == 'number':
if self.spinBox_number_frames.value() != self.configuration.alignment_points_frame_number:
self.configuration.alignment_points_frame_number = self.spinBox_number_frames.value()
self.configuration.alignment_points_frame_percent = -1
if self.configuration.global_parameters_protocol_level > 1:
Miscellaneous.protocol(
" The user has selected a new stack size: " + str(
self.configuration.alignment_points_frame_number) + " frames.",
self.stacked_image_log_file, precede_with_timestamp=False)
# The percentage of frames has been set. Check if it has changed.
elif self.spinBox_percentage_frames.value() != self.configuration.alignment_points_frame_percent:
self.configuration.alignment_points_frame_number = -1
self.configuration.alignment_points_frame_percent = \
self.spinBox_percentage_frames.value()
if self.configuration.global_parameters_protocol_level > 1:
Miscellaneous.protocol(
" The user has selected a new stack size: " + str(
self.configuration.alignment_points_frame_percent) + "% of all frames.",
self.stacked_image_log_file, precede_with_timestamp=False)
# Send a completion message.
if self.parent_gui is not None:
self.signal_finished.emit(self.signal_payload)
# Close the Window.
self.player_thread.quit()
plt.close()
self.close()
def reject(self):
"""
If the "cancel" button is pressed, the coordinates are not stored.
:return: -
"""
# Send a completion message.
if self.parent_gui is not None:
self.signal_finished.emit(self.signal_payload)
# Close the Window.
self.player_thread.quit()
plt.close()
self.close()
class FramePlayer(QtCore.QObject):
"""
This class implements a video player using the FrameViewer and the control elements of the
FrameViewerWidget. The player is started by the widget on a separate thread. This way the user
can instruct the GUI to stop the running player.
"""
block_widgets_signal = QtCore.pyqtSignal()
unblock_widgets_signal = QtCore.pyqtSignal()
set_photo_signal = QtCore.pyqtSignal(int)
set_slider_value = QtCore.pyqtSignal(int)
def __init__(self, frame_viewer_widget, parent=None):
super(FramePlayer, self).__init__()
# Store a reference of the frame viewer widget.
self.frame_viewer_widget = frame_viewer_widget
# Set the delay time between frames.
self.delay_between_frames = 0.1
# Initialize a variable used to stop the player in the GUI thread.
self.run_player = False
@QtCore.pyqtSlot()
def play(self):
"""
Start the player.
:return: -
"""
# Block signals from GUI elements to avoid cross-talk, and disable them to prevent unwanted
# user interaction.
self.block_widgets_signal.emit()
# Set the plaer running.
self.run_player = True
# The frames are ordered by their quality.
if self.frame_viewer_widget.frame_ordering == "quality":
# The player stops when the end of the video is reached, or when the "run_player"
# variable is set to False in the GUI thread.
while self.frame_viewer_widget.quality_index < self.frame_viewer_widget.frames.number \
- 1 and self.run_player:
# Go to the next frame only if the viewer has finished loading the previous one.
if not self.frame_viewer_widget.frame_viewer.image_loading_busy:
self.frame_viewer_widget.quality_index += 1
self.frame_viewer_widget.frame_index = \
self.frame_viewer_widget.rank_frames.quality_sorted_indices[
self.frame_viewer_widget.quality_index]
self.set_slider_value.emit(self.frame_viewer_widget.quality_index + 1)
self.set_photo_signal.emit(self.frame_viewer_widget.frame_index)
# Insert a short pause to keep the video from running too fast.
sleep(self.delay_between_frames)
else:
# The same for chronological frame ordering.
while self.frame_viewer_widget.frame_index < self.frame_viewer_widget.frames.number \
- 1 and self.run_player:
if not self.frame_viewer_widget.frame_viewer.image_loading_busy:
self.frame_viewer_widget.frame_index += 1
self.frame_viewer_widget.quality_index = \
self.frame_viewer_widget.rank_frames.quality_sorted_indices.index(
self.frame_viewer_widget.frame_index)
self.set_slider_value.emit(self.frame_viewer_widget.frame_index + 1)
self.set_photo_signal.emit(self.frame_viewer_widget.frame_index)
sleep(self.delay_between_frames)
self.run_player = False
# Re-set the GUI elements to their normal state.
self.unblock_widgets_signal.emit()
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":
# 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)
print("optimal alignment rectangle, y_low: " + str(y_low_opt) + ", y_high: " +
str(y_high_opt) + ", x_low: " + str(x_low_opt) + ", x_high: " + str(x_high_opt))
# Align all frames globally relative to the frame with the highest score.
try:
align_frames.align_frames()
except NotSupportedError as e:
print("Error: " + e.message)
exit()
except InternalError as e:
print("Warning: " + e.message)
print("Intersection, y_low: " + str(align_frames.intersection_shape[0][0]) + ", y_high: "
+ str(align_frames.intersection_shape[0][1]) + ", x_low: " \
+ str(align_frames.intersection_shape[1][0]) + ", x_high: " \
+ str(align_frames.intersection_shape[1][1]))
app = QtWidgets.QApplication(argv)
window = FrameViewerWidget(None, configuration, frames, rank_frames, align_frames, None, None,
None)
window.setMinimumSize(800, 600)
window.showMaximized()
app.exec_()
print("Percentage of frames to be stacked: " + str(
configuration.alignment_points_frame_percent) + ", number of frames: " + str(
configuration.alignment_points_frame_number))
exit()
|