forked from cgtuebingen/MedicalAnnotationFramework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_display.py
109 lines (87 loc) · 4.11 KB
/
image_display.py
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
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from seg_utils.ui.image_viewer import ImageViewer
from seg_utils.ui.annotation_group import AnnotationGroup
from seg_utils.ui.shape import Shape
from seg_utils.ui.dialogs import ForgotToSaveMessageBox
from seg_utils.utils.qt import get_icon
class CenterDisplayWidget(QWidget):
""" widget to manage the central display in the GUI
controls a QGraphicsView and a QGraphicsScene for drawing on top of a pixmap """
sRequestLabelListUpdate = pyqtSignal(int)
sRequestSave = pyqtSignal()
sChangeFile = pyqtSignal(int)
CREATE, EDIT = 0, 1
def __init__(self, *args):
super(CenterDisplayWidget, self).__init__(*args)
# main components of the display
self.scene = QGraphicsScene()
self.image_viewer = ImageViewer(self.scene)
self.pixmap = QGraphicsPixmapItem()
self.scene.addItem(self.pixmap)
self.annotations = AnnotationGroup()
self.scene.addItem(self.annotations)
# QLabel displaying the patients id/name/alias
self.patient_label = QLabel()
self.patient_label.setContentsMargins(10, 0, 10, 0)
self.hide_button = QPushButton(get_icon("next"), "", self)
self.hide_button.setGeometry(0, 0, 40, 40)
# put the viewer in the ImageDisplay-Frame
self.image_viewer.setFrameShape(QFrame.NoFrame)
self.layout = QVBoxLayout(self)
self.layout.addWidget(self.image_viewer)
self.layout.addWidget(self.patient_label)
def mousePressEvent(self, event: QMouseEvent):
if self.annotations.mode == AnnotationGroup.AnnotationMode.DRAW:
if event.button() == Qt.LeftButton:
self.annotations.create_shape()
event.accept()
def check_for_changes(self, sql_labels: list, new_img_idx: int):
"""compares the annotations in the current image with the stored annotations in the database"""
database_labels = [Shape(image_size=self.image_size,
label_dict=_label,
color=self.annotations.get_color_for_label(_label['label']))
for _label in sql_labels]
current_labels = list(self.annotations.annotations.values())
if database_labels == current_labels:
self.sChangeFile.emit(new_img_idx)
else:
d = ForgotToSaveMessageBox(self)
d.exec()
if d.result() == QMessageBox.AcceptRole or d.result() == QMessageBox.DestructiveRole:
if d.result() == QMessageBox.AcceptRole:
self.sRequestSave.emit()
self.sChangeFile.emit(new_img_idx)
def clear(self):
"""This function deletes all currently stored labels
and triggers the image_viewer to display a default image"""
self.scene.b_isInitialized = False
self.image_viewer.b_isEmpty = True
self.scene.clear()
self.set_labels([])
def get_pixmap_dimensions(self):
return [self.pixmap.pixmap().width(), self.pixmap.pixmap().height()]
def init_image(self, filepath: str, patient: str, labels: list, classes: list):
"""initializes the pixmap to display the image in the center widget
return the current labels as shape objects"""
self.set_initialized()
self.annotations.classes = classes
pixmap = QPixmap(filepath)
self.image_size = pixmap.size()
self.pixmap.setPixmap(pixmap)
labels = [Shape(image_size=self.image_size,
label_dict=_label,
color=self.annotations.get_color_for_label(_label['label']))
for _label in labels]
self.annotations.update_annotations(labels)
self.hide_button.raise_()
rect = QRectF(QPointF(0, 0), QSizeF(self.image_size))
self.image_viewer.fitInView(rect)
self.patient_label.setText(patient)
return labels
def is_empty(self):
return self.image_viewer.b_isEmpty
def set_initialized(self):
self.scene.b_isInitialized = True
self.image_viewer.b_isEmpty = False