This repository has been archived by the owner on May 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
174 lines (129 loc) · 5.5 KB
/
main.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
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
import sys
from copy import deepcopy
import cv2
import numpy as np
from PyQt5.QtCore import pyqtSignal, Qt, QRect, QRectF
from PyQt5.QtGui import QImage, qRgb, QPixmap
from PyQt5.QtWidgets import QApplication, QMainWindow, QGraphicsScene, QDialog
from gui.mainwindow import Ui_MainWindow
from gui.trainer import Ui_Trainer
from src.InitialPoseDialog import InitialPoseDialog
from src.MultiresFramework import MultiResolutionFramework
from src.datamanager import DataManager
from src.filteringdialog import FilteringDialog
from src.fitterdialog import FitterDialog
from src.interactivegraphicsscene import InteractiveGraphicsScene
from src.pcavisualizerdialog import PcaVisualizerDialog
from src.sampler import Sampler
from src.trainerdialog import TrainerDialog
from src.utils import toQImage
__author__ = "Ivan Sevcik"
class MainWindow(QMainWindow, Ui_MainWindow):
scene = None
data_manager = None
current_sample = 0
current_scale = 0
sampling_level = 0
pca = None
mean_shape = None
def __init__(self):
super(MainWindow, self).__init__()
# Set up the user interface from Designer.
self.setupUi(self)
self.scene = InteractiveGraphicsScene()
# self.scene.clicked.connect()
self.data_manager = DataManager()
self.graphicsView.setScene(self.scene)
self.display_radiograph(self.current_sample)
self.radiographSlider.setValue(self.current_sample + 1)
self.radiographSlider.setRange(1, self.data_manager.number_of_radiographs)
self.radiographSlider.valueChanged.connect(self.set_sample)
self.zoomSlider.setRange(-10, 10)
self.zoomSlider.setValue(self.current_scale)
self.zoomSlider.valueChanged.connect(self.change_scale)
self.levelSlider.setRange(1, MultiResolutionFramework.levels_count)
self.levelSlider.setValue(self.sampling_level + 1)
self.levelSlider.valueChanged.connect(self.change_sampling_level)
self.selectComboBox.addItems(["All teeth", "Upper jaw", "Lower jaw"])
self.selectComboBox.currentIndexChanged.connect(self.selection_changed)
self.trainerButton.clicked.connect(self.open_trainer)
self.filteringButton.clicked.connect(self.open_filtering)
self.pcaVisualizerButton.setEnabled(False)
self.pcaVisualizerButton.clicked.connect(self.open_pca_visulalizer)
self.fitterButton.setEnabled(False)
self.fitterButton.clicked.connect(self.open_fitter)
self.initializationButton.clicked.connect(self.open_initialize_pose)
def open_trainer(self):
dialog = TrainerDialog(self.data_manager)
dialog.trained.connect(self.save_training)
dialog.exec_()
def open_initialize_pose(self):
dialog = InitialPoseDialog(self.data_manager)
dialog.exec_()
def open_filtering(self):
dialog = FilteringDialog(self.data_manager)
dialog.exec_()
def save_training(self, pca):
self.pca = pca
self.pcaVisualizerButton.setEnabled(True)
self.fitterButton.setEnabled(True)
def open_pca_visulalizer(self):
dialog = PcaVisualizerDialog(self.pca, self.data_manager)
dialog.exec_()
def open_fitter(self):
dialog = FitterDialog(self.data_manager, self.pca)
dialog.exec_()
def set_sample(self, sampleId):
self.current_sample = sampleId - 1
self.display_radiograph(self.current_sample)
def change_scale(self, scale):
self.current_scale = scale
self.graphicsView.resetTransform()
real_scale = 1 + scale * (0.1 if scale >= 0 else 0.05)
self.graphicsView.scale(real_scale, real_scale)
def change_sampling_level(self, sampling_level):
self.sampling_level = sampling_level - 1
self.display_radiograph(self.current_sample)
def selection_changed(self, idx):
if idx == 0:
self.data_manager.select_all_teeth()
elif idx == 1:
self.data_manager.select_upper_jaw()
else:
self.data_manager.select_lower_jaw()
# Disable other parts until the pca is trained again
self.pcaVisualizerButton.setEnabled(False)
self.fitterButton.setEnabled(False)
# Show new selection
self.display_radiograph(self.current_sample)
def display_radiograph(self, idx):
self.scene.clear()
radiograph = self.data_manager.radiographs[idx]
image = radiograph.image
teeth = self.data_manager.get_all_teeth_from_radiograph(radiograph, True)
# Apply sampling level
for i in range(0, self.sampling_level):
image, teeth = MultiResolutionFramework.downsample(image, teeth)
# Load and draw image
qimg = toQImage(image)
size = (qimg.width(), qimg.height())
self.scene.addPixmap(QPixmap.fromImage(qimg))
for tooth in teeth:
tooth.draw(self.scene, True, True, True)
# Set generated scene into the view
self.scene.setSceneRect(QRectF(0, 0, size[0], size[1]))
self.graphicsView.resetTransform()
self.graphicsView.centerOn(self.scene.width() / 2, self.scene.height() / 2)
self.current_scale = 0
self.zoomSlider.setValue(0)
self.update_size_label(size)
def update_size_label(self, size):
self.sizeLabel.setText("%d, %d" % size)
# Main Function
if __name__ == '__main__':
# Create main app
myApp = QApplication(sys.argv)
ex2 = MainWindow()
ex2.show()
# Execute the Application and Exit
sys.exit(myApp.exec_())