forked from coraxx/3DCT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TDCT_correlation.py
2223 lines (2091 loc) · 124 KB
/
TDCT_correlation.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
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Extracting 2D and 3D points with subsequent 2D to 3D correlation.
This module can be run as a standalone python application, but is best paired
with the preceding data processing (cubing voxels, merge single image files
to one single stack file, ...).
# @Title : TDCT_correlation
# @Project : 3DCTv2
# @Description : Extracting 2D and 3D points for 2D to 3D correlation
# @Author : Jan Arnold
# @Email : jan.arnold (at) coraxx.net
# @Copyright : Copyright (C) 2016 Jan Arnold
# @License : GPLv3 (see LICENSE file)
# @Credits :
# @Maintainer : Jan Arnold
# @Date : 2016/01
# @Version : 3DCT 2.3.0 module rev. 30
# @Status : stable
# @Usage : part of 3D Correlation Toolbox
# @Notes :
# @Python_version : 2.7.11
"""
# ======================================================================================================================
import sys
import os
import time
import re
import tempfile
from PyQt4 import QtCore, QtGui, uic
import numpy as np
import cv2
import tifffile as tf
import qimage2ndarray
## Colored stdout, custom Qt functions (mostly to handle events), CSV handler
## and correlation algorithm
from tdct import clrmsg, TDCT_debug, QtCustom, csvHandler, correlation
__version__ = 'v2.3.0'
# add working directory temporarily to PYTHONPATH
if getattr(sys, 'frozen', False):
# program runs in a bundle (pyinstaller)
execdir = sys._MEIPASS
else:
execdir = os.path.dirname(os.path.realpath(__file__))
sys.path.append(execdir)
qtCreatorFile_main = os.path.join(execdir, "TDCT_correlation.ui")
Ui_WidgetWindow, QtBaseClass = uic.loadUiType(qtCreatorFile_main)
debug = TDCT_debug.debug
# debug = True
if debug is True: print clrmsg.DEBUG + "Execdir =", execdir
class MainWidget(QtGui.QMainWindow, Ui_WidgetWindow):
def __init__(self, parent=None, leftImage=None, rightImage=None, workingdir=None):
if debug is True: print clrmsg.DEBUG + 'Debug messages enabled'
QtGui.QWidget.__init__(self)
Ui_WidgetWindow.__init__(self)
self.setupUi(self)
self.parent = parent
self.counter = 0 # Just for testing (loop counter for test button)
self.refreshUI = QtGui.QApplication.processEvents
self.currentFocusedWidgetName = QtGui.QApplication.focusWidget()
if workingdir is None:
self.workingdir = execdir
else:
self.workingdir = workingdir
self.lineEdit_workingDir.setText(self.workingdir)
## Stylesheet colors:
self.stylesheet_orange = "color: rgb(255, 120, 0);"
self.stylesheet_green = "color: rgb( 0, 200, 0);"
self.stylesheet_blue = "color: rgb( 0, 190, 255);"
self.stylesheet_red = "color: rgb(255, 0, 0);"
## Marker and POI color
self.markerColor = (0,255,0)
self.poiColor = (0,0,255)
## Tableview and models
self.modelLleft = QtCustom.QStandardItemModelCustom(self)
self.tableView_left.setModel(self.modelLleft)
self.modelLleft.tableview = self.tableView_left
self.modelRight = QtCustom.QStandardItemModelCustom(self)
self.tableView_right.setModel(self.modelRight)
self.modelRight.tableview = self.tableView_right
self.modelResults = QtGui.QStandardItemModel(self)
self.modelResultsProxy = QtCustom.NumberSortModel()
self.modelResultsProxy.setSourceModel(self.modelResults)
self.tableView_results.setModel(self.modelResultsProxy)
## store parameters for resizing
self.parent = parent
self.size = 500
self.leftImage = leftImage
self.rightImage = rightImage
## Initialize parameters
## left
self.selectedLayer_left = 1
self.brightness_left_layer1 = 0
self.brightness_left_layer2 = 0
self.brightness_left_layer3 = 0
self.contrast_left_layer1 = 10
self.contrast_left_layer2 = 10
self.contrast_left_layer3 = 10
self.slice_left = 0
self.mipCHKbox_left = True
self.layer1CHKbox_left = True
self.layer2CHKbox_left = False
self.layer3CHKbox_left = False
self.layer1Color_left = 0
self.layer2Color_left = 0
self.layer3Color_left = 0
self.layer1CustomColor_left = [255,0,255]
self.layer2CustomColor_left = [255,0,255]
self.layer3CustomColor_left = [255,0,255]
self.img_left_overlay = None
# self.img_left_layer1 = None
self.img_left_layer2 = None
self.imgstack_left_layer2 = None
self.img_left_layer3 = None
self.imgstack_left_layer3 = None
## right
self.selectedLayer_right = 1
self.brightness_right_layer1 = 0
self.brightness_right_layer2 = 0
self.brightness_right_layer3 = 0
self.contrast_right_layer1 = 10
self.contrast_right_layer2 = 10
self.contrast_right_layer3 = 10
self.slice_right = 0
self.mipCHKbox_right = True
self.layer1CHKbox_right = True
self.layer2CHKbox_right = False
self.layer3CHKbox_right = False
self.layer1Color_right = 0
self.layer2Color_right = 0
self.layer3Color_right = 0
self.layer1CustomColor_right = [255,0,255]
self.layer2CustomColor_right = [255,0,255]
self.layer3CustomColor_right = [255,0,255]
self.img_right_overlay = None
# self.img_right_layer1 = None
self.img_right_layer2 = None
self.imgstack_right_layer2 = None
self.img_right_layer3 = None
self.imgstack_right_layer3 = None
## Initialize Images and connect image load buttons
self.toolButton_loadLeftImage.clicked.connect(self.openImageLeft)
self.toolButton_loadRightImage.clicked.connect(self.openImageRight)
self.toolButton_resetLeftImage.clicked.connect(lambda: self.resetImageLeft(img=None))
self.toolButton_resetRightImage.clicked.connect(lambda: self.resetImageRight(img=None))
if leftImage is None or rightImage is None:
return
self.initImageLeft()
self.initImageRight()
## connect item change signal to write changes in model back to QGraphicItems as well as highlighting selected points
self.modelLleft.itemChanged.connect(self.tableView_left.updateItems)
self.modelRight.itemChanged.connect(self.tableView_right.updateItems)
self.tableView_left.selectionModel().selectionChanged.connect(self.tableView_left.showSelectedItem)
self.tableView_right.selectionModel().selectionChanged.connect(self.tableView_right.showSelectedItem)
self.tableView_results.selectionModel().selectionChanged.connect(self.showSelectedResidual)
self.tableView_results.doubleClicked.connect(lambda: self.showSelectedResidual(doubleclick=True))
# SpinBoxes
self.spinBox_rot.valueChanged.connect(self.rotateImage)
self.spinBox_markerSize.valueChanged.connect(self.changeMarkerSize)
self.spinBox_slice.valueChanged.connect(self.selectSlice)
self.doubleSpinBox_scatterPlotFrameSize.valueChanged.connect(lambda: self.displayResults(
frame=self.checkBox_scatterPlotFrame.isChecked(),
framesize=self.doubleSpinBox_scatterPlotFrameSize.value()))
## Checkboxes
self.checkBox_scatterPlotFrame.stateChanged.connect(lambda: self.displayResults(
frame=self.checkBox_scatterPlotFrame.isChecked(),
framesize=self.doubleSpinBox_scatterPlotFrameSize.value()))
self.checkBox_resultsAbsolute.stateChanged.connect(lambda: self.displayResults(
frame=self.checkBox_scatterPlotFrame.isChecked(),
framesize=self.doubleSpinBox_scatterPlotFrameSize.value()))
self.checkBox_MIP.stateChanged.connect(self.selectSlice)
self.checkBox_layer1.toggled.connect(lambda: self.layerCtrl('layer1'))
self.checkBox_layer2.toggled.connect(lambda: self.layerCtrl('layer2'))
self.checkBox_layer3.toggled.connect(lambda: self.layerCtrl('layer3'))
## Comboboxes
self.comboBox_channelColorLayer1.currentIndexChanged.connect(self.changeColorChannel)
self.comboBox_channelColorLayer2.currentIndexChanged.connect(self.changeColorChannel)
self.comboBox_channelColorLayer3.currentIndexChanged.connect(self.changeColorChannel)
## Radiobuttons
self.radioButton_layer1.clicked.connect(self.setSliders)
self.radioButton_layer2.clicked.connect(self.setSliders)
self.radioButton_layer3.clicked.connect(self.setSliders)
## Buttons
self.toolButton_rotcw.clicked.connect(lambda: self.rotateImage45(direction='cw'))
self.toolButton_rotccw.clicked.connect(lambda: self.rotateImage45(direction='ccw'))
self.toolButton_brightness_reset.clicked.connect(lambda: self.horizontalSlider_brightness.setValue(0))
self.toolButton_contrast_reset.clicked.connect(lambda: self.horizontalSlider_contrast.setValue(10))
self.toolButton_importPoints.clicked.connect(self.importPoints)
self.toolButton_exportPoints.clicked.connect(self.exportPoints)
self.toolButton_selectWorkingDir.clicked.connect(self.selectWorkingDir)
self.toolButton_selectMarkerColor.clicked.connect(self.getMarkerColor)
self.toolButton_selectPoiColor.clicked.connect(self.getPoiColor)
self.toolButton_saveImage_left.clicked.connect(lambda: self.displayImage(side='left',save=True))
self.toolButton_saveImage_right.clicked.connect(lambda: self.displayImage(side='right',save=True))
self.toolButton_loadLayer2.clicked.connect(lambda: self.layerCtrl('layer2',load=True))
self.toolButton_loadLayer3.clicked.connect(lambda: self.layerCtrl('layer3',load=True))
self.commandLinkButton_correlate.clicked.connect(self.correlate)
## Sliders
self.horizontalSlider_brightness.valueChanged.connect(self.setBrightCont)
self.horizontalSlider_contrast.valueChanged.connect(self.setBrightCont)
## Capture focus change events
QtCore.QObject.connect(QtGui.QApplication.instance(), QtCore.SIGNAL("focusChanged(QWidget *, QWidget *)"), self.changedFocusSlot)
## Pass models and scenes to tableview for easy access
self.tableView_left._model = self.modelLleft
self.tableView_right._model = self.modelRight
self.tableView_left._scene = self.sceneLeft
self.tableView_right._scene = self.sceneRight
self.tableView_results.setContextMenuPolicy(3)
self.tableView_results.customContextMenuRequested.connect(self.cmTableViewResults)
self.lineEdit_workingDir.textChanged.connect(self.updateWorkingDir)
self.activateWindow()
def keyPressEvent(self,event):
"""Filter key press event
Selected table rows can be deleted by pressing the "Del" key
"""
if event.key() == QtCore.Qt.Key_Delete:
if self.currentFocusedWidgetName == 'tableView_left':
if debug is True: print clrmsg.DEBUG + "Deleting item(s) on the left side"
# self.deleteItem(self.tableView_left,self.modelLleft,self.sceneLeft)
self.tableView_left.deleteItem()
# self.updateItems(self.modelLleft,self.sceneLeft)
self.tableView_left.updateItems()
elif self.currentFocusedWidgetName == 'tableView_right':
if debug is True: print clrmsg.DEBUG + "Deleting item(s) on the right side"
# self.deleteItem(self.tableView_right,self.modelRight,self.sceneRight)
self.tableView_right.deleteItem()
# self.updateItems(self.modelRight,self.sceneRight)
self.tableView_right.updateItems()
def closeEvent(self, event):
"""Warning when closing application to prevent unintentional quitting with reminder to save data"""
quit_msg = "Are you sure you want to exit the\n3DCT Correlation?\n\nUnsaved data will be lost!"
reply = QtGui.QMessageBox.question(self, 'Message', quit_msg, QtGui.QMessageBox.Yes, QtGui.QMessageBox.No)
if reply == QtGui.QMessageBox.Yes:
event.accept()
if self.parent:
self.parent.cleanUp()
self.parent.exitstatus = 0
else:
event.ignore()
if self.parent:
self.parent.exitstatus = 1
def selectWorkingDir(self):
path = str(QtGui.QFileDialog.getExistingDirectory(self, "Select working directory", self.workingdir))
self.activateWindow()
if path:
workingdir = self.checkWorkingDirPrivileges(path)
if workingdir:
self.workingdir = workingdir
self.lineEdit_workingDir.setText(self.workingdir)
def updateWorkingDir(self):
if os.path.isdir(self.lineEdit_workingDir.text()):
workingdir = self.checkWorkingDirPrivileges(str(self.lineEdit_workingDir.text()))
if workingdir:
self.workingdir = workingdir
self.lineEdit_workingDir.setText(self.workingdir)
print 'updated working dir to:', self.workingdir
else:
self.lineEdit_workingDir.setText(self.workingdir)
print clrmsg.ERROR + "Dropped object is not a valid path. Returning to {0} as working directory.".format(self.workingdir)
def checkWorkingDirPrivileges(self,path):
try:
testfile = tempfile.TemporaryFile(dir=path)
testfile.close()
return path
except Exception:
QtGui.QMessageBox.critical(
self,"Warning",
"I cannot write to this folder: {0}\nFalling back to {1} as the working directory".format(path, self.workingdir))
return None
def changedFocusSlot(self, former, current):
if debug is True: print clrmsg.DEBUG + "focus changed from/to:", former.objectName() if former else former, \
current.objectName() if current else current
if current:
self.currentFocusedWidgetName = current.objectName()
self.currentFocusedWidget = current
if former:
self.formerFocusedWidgetName = former.objectName()
self.formerFocusedWidget = former
## Label showing selected image
## WORKAROUND: check against empty string, because the popup from the comboboxes emit an empty focus object name string.
if self.currentFocusedWidgetName in [
'spinBox_rot','spinBox_markerSize','spinBox_slice','horizontalSlider_brightness','horizontalSlider_contrast',
'doubleSpinBox_custom_rot_center_x','doubleSpinBox_custom_rot_center_y','doubleSpinBox_custom_rot_center_z',
'checkBox_MIP','checkBox_layer1','checkBox_layer2','checkBox_layer3',
'comboBox_channelColorLayer1','comboBox_channelColorLayer2','comboBox_channelColorLayer3',
'radioButton_layer1','radioButton_layer2','radioButton_layer3','']:
pass
else:
if self.currentFocusedWidgetName != 'graphicsView_left' and self.currentFocusedWidgetName != 'graphicsView_right':
self.borderControl(None,1)
self.label_selimg.setStyleSheet(self.stylesheet_orange)
self.label_selimg.setText('none')
self.label_markerSizeNano.setText('')
self.label_markerSizeNanoUnit.setText('')
self.label_imgpxsize.setText('')
self.label_imgpxsizeUnit.setText('')
self.label_imagetype.setText('')
self.ctrlEnDisAble(False)
elif self.currentFocusedWidgetName == 'graphicsView_left':
self.borderControl(current)
self.label_selimg.setStyleSheet(self.stylesheet_green)
self.label_selimg.setText('left')
self.label_imagetype.setStyleSheet(self.stylesheet_green)
# self.label_imagetype.setText('(2D)' if '{0:b}'.format(self.sceneLeft.imagetype)[-1] == '1' else '(3D)')
if '{0:b}'.format(self.sceneLeft.imagetype)[-1] == '1':
self.label_imagetype.setText('(2D)')
self.widget_sliceSelector.setVisible(False)
else:
self.label_imagetype.setText('(3D)')
self.widget_sliceSelector.setVisible(True)
self.ctrlEnDisAble(True)
if self.mipCHKbox_left is False:
self.spinBox_slice.setEnabled(True)
elif self.currentFocusedWidgetName == 'graphicsView_right':
self.borderControl(current)
self.label_selimg.setStyleSheet(self.stylesheet_blue)
self.label_selimg.setText('right')
self.label_imagetype.setStyleSheet(self.stylesheet_blue)
# self.label_imagetype.setText('(2D)' if '{0:b}'.format(self.sceneRight.imagetype)[-1] == '1' else '(3D)')
if '{0:b}'.format(self.sceneRight.imagetype)[-1] == '1':
self.label_imagetype.setText('(2D)')
self.widget_sliceSelector.setVisible(False)
else:
self.label_imagetype.setText('(3D)')
self.widget_sliceSelector.setVisible(True)
self.ctrlEnDisAble(True)
if self.mipCHKbox_right is False:
self.spinBox_slice.setEnabled(True)
## Label showing selected table
if self.currentFocusedWidgetName != 'tableView_left' and self.currentFocusedWidgetName != 'tableView_right':
self.borderControl(None,2)
self.label_selectedTable.setStyleSheet(self.stylesheet_orange)
self.label_selectedTable.setText('none')
# self.ctrlEnDisAble(False)
elif self.currentFocusedWidgetName == 'tableView_left':
self.borderControl(current)
self.label_selectedTable.setStyleSheet(self.stylesheet_green)
self.label_selectedTable.setText('left')
self.ctrlEnDisAble(False)
elif self.currentFocusedWidgetName == 'tableView_right':
self.borderControl(current)
self.label_selectedTable.setStyleSheet(self.stylesheet_blue)
self.label_selectedTable.setText('right')
self.ctrlEnDisAble(False)
## Feed saved rotation angle/brightness-contrast value from selected image to spinbox/slider
# Block emitting signals for correct setting of BOTH sliders. Otherwise the second one gets overwritten with the old value
self.horizontalSlider_brightness.blockSignals(True)
self.horizontalSlider_contrast.blockSignals(True)
self.spinBox_slice.blockSignals(True)
self.checkBox_MIP.blockSignals(True)
self.checkBox_layer1.blockSignals(True)
self.checkBox_layer2.blockSignals(True)
self.checkBox_layer3.blockSignals(True)
self.comboBox_channelColorLayer1.blockSignals(True)
self.comboBox_channelColorLayer2.blockSignals(True)
self.comboBox_channelColorLayer3.blockSignals(True)
if self.currentFocusedWidgetName == 'graphicsView_left':
self.spinBox_rot.setValue(self.sceneLeft.rotangle)
self.spinBox_markerSize.setValue(self.sceneLeft.markerSize)
self.spinBox_slice.setValue(self.slice_left)
self.checkBox_MIP.setChecked(self.mipCHKbox_left)
self.checkBox_layer1.setChecked(self.layer1CHKbox_left)
self.comboBox_channelColorLayer1.setEnabled(self.layer1CHKbox_left)
self.comboBox_channelColorLayer1.setCurrentIndex(self.layer1Color_left)
self.checkBox_layer2.setChecked(self.layer2CHKbox_left)
self.comboBox_channelColorLayer2.setEnabled(self.layer2CHKbox_left)
self.comboBox_channelColorLayer2.setCurrentIndex(self.layer2Color_left)
self.checkBox_layer3.setChecked(self.layer3CHKbox_left)
self.comboBox_channelColorLayer3.setEnabled(self.layer3CHKbox_left)
self.comboBox_channelColorLayer3.setCurrentIndex(self.layer3Color_left)
if self.selectedLayer_left == 1:
self.radioButton_layer1.setChecked(True)
elif self.selectedLayer_left == 2:
self.radioButton_layer2.setChecked(True)
elif self.selectedLayer_left == 3:
self.radioButton_layer3.setChecked(True)
self.radioButton_layer1.setEnabled(self.layer1CHKbox_left)
self.radioButton_layer2.setEnabled(self.layer2CHKbox_left)
self.radioButton_layer3.setEnabled(self.layer3CHKbox_left)
if self.radioButton_layer1.isChecked():
self.horizontalSlider_brightness.setValue(self.brightness_left_layer1)
self.horizontalSlider_contrast.setValue(self.contrast_left_layer1)
elif self.radioButton_layer2.isChecked():
self.horizontalSlider_brightness.setValue(self.brightness_left_layer2)
self.horizontalSlider_contrast.setValue(self.contrast_left_layer2)
elif self.radioButton_layer3.isChecked():
self.horizontalSlider_brightness.setValue(self.brightness_left_layer3)
self.horizontalSlider_contrast.setValue(self.contrast_left_layer3)
self.label_imgpxsize.setText(str(self.sceneLeft.pixelSize)) # + ' um') # breaks marker size adjustments check
self.label_imgpxsizeUnit.setText('um') if self.sceneLeft.pixelSize else self.label_imgpxsizeUnit.setText('')
elif self.currentFocusedWidgetName == 'graphicsView_right':
self.spinBox_rot.setValue(self.sceneRight.rotangle)
self.spinBox_markerSize.setValue(self.sceneRight.markerSize)
self.spinBox_slice.setValue(self.slice_right)
self.checkBox_MIP.setChecked(self.mipCHKbox_right)
self.checkBox_layer1.setChecked(self.layer1CHKbox_right)
self.comboBox_channelColorLayer1.setEnabled(self.layer1CHKbox_right)
self.comboBox_channelColorLayer1.setCurrentIndex(self.layer1Color_right)
self.checkBox_layer2.setChecked(self.layer2CHKbox_right)
self.comboBox_channelColorLayer2.setEnabled(self.layer2CHKbox_right)
self.comboBox_channelColorLayer2.setCurrentIndex(self.layer2Color_right)
self.checkBox_layer3.setChecked(self.layer3CHKbox_right)
self.comboBox_channelColorLayer3.setEnabled(self.layer3CHKbox_right)
self.comboBox_channelColorLayer3.setCurrentIndex(self.layer3Color_right)
if self.selectedLayer_right == 1:
self.radioButton_layer1.setChecked(True)
elif self.selectedLayer_right == 2:
self.radioButton_layer2.setChecked(True)
elif self.selectedLayer_right == 3:
self.radioButton_layer3.setChecked(True)
self.radioButton_layer1.setEnabled(self.layer1CHKbox_right)
self.radioButton_layer2.setEnabled(self.layer2CHKbox_right)
self.radioButton_layer3.setEnabled(self.layer3CHKbox_right)
if self.radioButton_layer1.isChecked():
self.horizontalSlider_brightness.setValue(self.brightness_right_layer1)
self.horizontalSlider_contrast.setValue(self.contrast_right_layer1)
if self.radioButton_layer2.isChecked():
self.horizontalSlider_brightness.setValue(self.brightness_right_layer2)
self.horizontalSlider_contrast.setValue(self.contrast_right_layer2)
if self.radioButton_layer3.isChecked():
self.horizontalSlider_brightness.setValue(self.brightness_right_layer3)
self.horizontalSlider_contrast.setValue(self.contrast_right_layer3)
self.label_imgpxsize.setText(str(self.sceneRight.pixelSize)) # + ' um') # breaks marker size adjustments check
self.label_imgpxsizeUnit.setText('um') if self.sceneRight.pixelSize else self.label_imgpxsizeUnit.setText('')
# Unblock emitting signals.
self.horizontalSlider_brightness.blockSignals(False)
self.horizontalSlider_contrast.blockSignals(False)
self.spinBox_slice.blockSignals(False)
self.checkBox_MIP.blockSignals(False)
self.checkBox_layer1.blockSignals(False)
self.checkBox_layer2.blockSignals(False)
self.checkBox_layer3.blockSignals(False)
self.comboBox_channelColorLayer1.blockSignals(False)
self.comboBox_channelColorLayer2.blockSignals(False)
self.comboBox_channelColorLayer3.blockSignals(False)
# update marker size in nm
self.changeMarkerSize()
def borderControl(self,widget,idx=0):
if idx == 0:
self.graphicsView_left.setStyleSheet("border: 1px solid rgb(223, 223, 223)")
self.graphicsView_right.setStyleSheet("border: 1px solid rgb(223, 223, 223)")
self.tableView_left.setStyleSheet("border: 1px solid rgb(223, 223, 223)")
self.tableView_right.setStyleSheet("border: 1px solid rgb(223, 223, 223)")
elif idx == 1:
self.graphicsView_left.setStyleSheet("border: 1px solid rgb(223, 223, 223)")
self.graphicsView_right.setStyleSheet("border: 1px solid rgb(223, 223, 223)")
elif idx == 2:
self.tableView_left.setStyleSheet("border: 1px solid rgb(223, 223, 223)")
self.tableView_right.setStyleSheet("border: 1px solid rgb(223, 223, 223)")
if widget is not None:
widget.setStyleSheet("border: 1px solid rgb(100, 140, 220)")
## Function to dis-/enabling the buttons controlling rotation and contrast/brightness
def ctrlEnDisAble(self,status):
self.spinBox_rot.setEnabled(status)
self.spinBox_markerSize.setEnabled(status)
self.spinBox_slice.setEnabled(False)
self.checkBox_MIP.setEnabled(status)
self.checkBox_layer1.setEnabled(status)
self.checkBox_layer2.setEnabled(status)
self.checkBox_layer3.setEnabled(status)
self.comboBox_channelColorLayer1.setEnabled(False)
self.comboBox_channelColorLayer2.setEnabled(False)
self.comboBox_channelColorLayer3.setEnabled(False)
self.radioButton_layer1.setEnabled(False)
self.radioButton_layer2.setEnabled(False)
self.radioButton_layer3.setEnabled(False)
self.horizontalSlider_brightness.setEnabled(status)
self.horizontalSlider_contrast.setEnabled(status)
self.toolButton_brightness_reset.setEnabled(status)
self.toolButton_contrast_reset.setEnabled(status)
self.toolButton_rotcw.setEnabled(status)
self.toolButton_rotccw.setEnabled(status)
self.toolButton_importPoints.setEnabled(not status)
self.toolButton_exportPoints.setEnabled(not status)
self.toolButton_loadLayer2.setEnabled(status)
self.toolButton_loadLayer3.setEnabled(status)
def setSliders(self):
self.horizontalSlider_brightness.blockSignals(True)
self.horizontalSlider_contrast.blockSignals(True)
if self.label_selimg.text() == 'left':
if self.radioButton_layer1.isChecked():
self.horizontalSlider_brightness.setValue(self.brightness_left_layer1)
self.horizontalSlider_contrast.setValue(self.contrast_left_layer1)
self.selectedLayer_left = 1
elif self.radioButton_layer2.isChecked():
self.horizontalSlider_brightness.setValue(self.brightness_left_layer2)
self.horizontalSlider_contrast.setValue(self.contrast_left_layer2)
self.selectedLayer_left = 2
elif self.radioButton_layer3.isChecked():
self.horizontalSlider_brightness.setValue(self.brightness_left_layer3)
self.horizontalSlider_contrast.setValue(self.contrast_left_layer3)
self.selectedLayer_left = 3
if self.label_selimg.text() == 'right':
if self.radioButton_layer1.isChecked():
self.horizontalSlider_brightness.setValue(self.brightness_right_layer1)
self.horizontalSlider_contrast.setValue(self.contrast_right_layer1)
self.selectedLayer_right = 1
if self.radioButton_layer2.isChecked():
self.horizontalSlider_brightness.setValue(self.brightness_right_layer2)
self.horizontalSlider_contrast.setValue(self.contrast_right_layer2)
self.selectedLayer_right = 2
if self.radioButton_layer3.isChecked():
self.horizontalSlider_brightness.setValue(self.brightness_right_layer3)
self.horizontalSlider_contrast.setValue(self.contrast_right_layer3)
self.selectedLayer_right = 3
self.horizontalSlider_brightness.blockSignals(False)
self.horizontalSlider_contrast.blockSignals(False)
def colorModels(self):
rowsLeft = self.modelLleft.rowCount()
rowsRight = self.modelRight.rowCount()
alpha = 100
for row in range(min([rowsLeft,rowsRight])):
color_correlate = (50,220,175,alpha)
if rowsLeft != 0:
try:
self.modelLleft.item(row, 0).setBackground(QtGui.QColor(*color_correlate))
self.modelLleft.item(row, 1).setBackground(QtGui.QColor(*color_correlate))
self.modelLleft.item(row, 2).setBackground(QtGui.QColor(*color_correlate))
except:
if debug is True: print clrmsg.DEBUG + "Model item is None"
if rowsRight != 0:
try:
self.modelRight.item(row, 0).setBackground(QtGui.QColor(*color_correlate))
self.modelRight.item(row, 1).setBackground(QtGui.QColor(*color_correlate))
self.modelRight.item(row, 2).setBackground(QtGui.QColor(*color_correlate))
except:
if debug is True: print clrmsg.DEBUG + "Model item is None"
if rowsLeft > rowsRight:
if '{0:b}'.format(self.sceneLeft.imagetype)[-1] == '0' or '{0:b}'.format(
self.sceneLeft.imagetype)[-1] == '{0:b}'.format(self.sceneRight.imagetype)[-1]:
color_overflow = (105,220,0,alpha) # green if entries are used as POIs
else:
color_overflow = (220,25,105,alpha) # red(ish) color to indicate unbalanced amount of markers for correlation
for row in range(rowsRight,rowsLeft):
try:
self.modelLleft.item(row, 0).setBackground(QtGui.QColor(*color_overflow))
self.modelLleft.item(row, 1).setBackground(QtGui.QColor(*color_overflow))
self.modelLleft.item(row, 2).setBackground(QtGui.QColor(*color_overflow))
except:
if debug is True: print clrmsg.DEBUG + "Model item is None"
elif rowsLeft < rowsRight:
if '{0:b}'.format(self.sceneRight.imagetype)[-1] == '0' or '{0:b}'.format(
self.sceneLeft.imagetype)[-1] == '{0:b}'.format(self.sceneRight.imagetype)[-1]:
color_overflow = (105,220,0,alpha) # green if entries are used as POIs
else:
color_overflow = (220,25,105,alpha) # red(ish) color to indicate unbalanced amount of markers for correlation
for row in range(rowsLeft,rowsRight):
try:
self.modelRight.item(row, 0).setBackground(QtGui.QColor(*color_overflow))
self.modelRight.item(row, 1).setBackground(QtGui.QColor(*color_overflow))
self.modelRight.item(row, 2).setBackground(QtGui.QColor(*color_overflow))
except:
if debug is True: print clrmsg.DEBUG + "Model item is None"
def getMarkerColor(self):
color = QtGui.QColorDialog.getColor()
self.activateWindow()
if color.isValid():
self.markerColor = (color.blue(), color.green(), color.red())
self.label_markerColor.setStyleSheet("background-color: rgb{0};".format((color.red(), color.green(), color.blue())))
def getPoiColor(self):
color = QtGui.QColorDialog.getColor()
self.activateWindow()
if color.isValid():
self.poiColor = (color.blue(), color.green(), color.red())
self.label_poiColor.setStyleSheet("background-color: rgb{0};".format((color.red(), color.green(), color.blue())))
def getCustomChannelColor(self):
color = QtGui.QColorDialog.getColor()
self.activateWindow()
if color.isValid():
return [color.red(), color.green(), color.blue()]
###############################################
###### Image initialization and rotation ######
#################### START ####################
def initImageLeft(self):
if self.leftImage is not None:
## Changed GraphicsSceneLeft(self) to QtCustom.QGraphicsSceneCustom(self.graphicsView_left) to reuse class for both scenes
self.sceneLeft = QtCustom.QGraphicsSceneCustom(self.graphicsView_left,mainWidget=self,side='left',model=self.modelLleft)
## set pen color yellow
self.sceneLeft.pen = QtGui.QPen(QtCore.Qt.red)
## Splash screen message
try:
splashscreen.splash.showMessage("Loading images... "+self.leftImage,color=QtCore.Qt.white)
except Exception as e:
print clrmsg.WARNING, e
pass
QtGui.QApplication.processEvents()
## Get pixel size
self.sceneLeft.pixelSize = self.pxSize(self.leftImage)
self.sceneLeft.pixelSizeUnit = 'um'
## Load image, assign it to scene and store image type information
self.img_left_layer1,self.sceneLeft.imagetype,self.imgstack_left_layer1 = self.imread(self.leftImage)
self.img_left_displayed_layer1 = np.copy(self.img_left_layer1)
self.img_adj_left_layer1 = np.copy(self.img_left_layer1)
## Set slice spinbox maximum
if self.imgstack_left_layer1 is not None:
self.spinBox_slice.setValue(0)
self.slice_left = 0
self.spinBox_slice.setMaximum(self.imgstack_left_layer1.shape[0]-1)
## link image to QTableview for determining z
self.tableView_left.img1 = self.imgstack_left_layer1
self.tableView_left.img2 = self.imgstack_left_layer2
self.tableView_left.img3 = self.imgstack_left_layer3
## check if coloring z values in table is needed (correlation needs z=0 in 2D image, so no checking for valid z
## with 2D images needed)
if self.imgstack_left_layer1 is None:
self.sceneLeft._z = False
else:
self.sceneLeft._z = True
self.setCustomRotCenter(max(self.imgstack_left_layer1.shape))
# self.pixmap_left = QtGui.QPixmap(self.leftImage)
self.pixmap_left = self.cv2Qimage(self.img_left_displayed_layer1)
self.pixmap_item_left = QtGui.QGraphicsPixmapItem(self.pixmap_left, None, self.sceneLeft)
## connect scenes to GUI elements
self.graphicsView_left.setScene(self.sceneLeft)
## reset scaling (needed for reinitialization)
self.graphicsView_left.resetMatrix()
## scaling scene, not image
scaling_factor = float(self.size)/max(self.pixmap_left.width(), self.pixmap_left.height())
self.graphicsView_left.scale(scaling_factor,scaling_factor)
def initImageRight(self):
if self.rightImage is not None:
self.sceneRight = QtCustom.QGraphicsSceneCustom(self.graphicsView_right,mainWidget=self,side='right',model=self.modelRight)
## set pen color yellow
self.sceneRight.pen = QtGui.QPen(QtCore.Qt.yellow)
## Splash screen message
try:
splashscreen.splash.showMessage("Loading images... "+self.rightImage,color=QtCore.Qt.white)
except Exception as e:
print clrmsg.WARNING, e
pass
QtGui.QApplication.processEvents()
## Get pixel size
self.sceneRight.pixelSize = self.pxSize(self.rightImage)
self.sceneRight.pixelSizeUnit = 'um'
## Load image, assign it to scene and store image type information
self.img_right_layer1,self.sceneRight.imagetype,self.imgstack_right_layer1 = self.imread(self.rightImage)
self.img_right_displayed_layer1 = np.copy(self.img_right_layer1)
self.img_adj_right_layer1 = np.copy(self.img_right_layer1)
## Set slice spinbox maximum
if self.imgstack_right_layer1 is not None:
self.spinBox_slice.setValue(0)
self.slice_left = 0
self.spinBox_slice.setMaximum(self.imgstack_right_layer1.shape[0]-1)
## link image to QTableview for determining z
self.tableView_right.img1 = self.imgstack_right_layer1
self.tableView_right.img2 = self.imgstack_right_layer2
self.tableView_right.img3 = self.imgstack_right_layer3
## check if coloring z values in table is needed (correlation needs z=0 in 2D image, so no checking for valid z
## with 2D images needed)
if self.imgstack_right_layer1 is None:
self.sceneRight._z = False
else:
self.sceneRight._z = True
self.setCustomRotCenter(max(self.imgstack_right_layer1.shape))
# self.pixmap_right = QtGui.QPixmap(self.rightImage)
self.pixmap_right = self.cv2Qimage(self.img_right_displayed_layer1)
self.pixmap_item_right = QtGui.QGraphicsPixmapItem(self.pixmap_right, None, self.sceneRight)
## connect scenes to GUI elements
self.graphicsView_right.setScene(self.sceneRight)
## reset scaling (needed for reinitialization)
self.graphicsView_right.resetMatrix()
## scaling scene, not image
scaling_factor = float(self.size)/max(self.pixmap_right.width(), self.pixmap_right.height())
self.graphicsView_right.scale(scaling_factor,scaling_factor)
def openImageLeft(self):
## *.png *.jpg *.bmp not yet supported
path = str(QtGui.QFileDialog.getOpenFileName(
None,"Select image file for correlation", self.workingdir,"Image Files (*.tif *.tiff);; All (*.*)"))
self.activateWindow()
if path != '':
## Set focus to corresponding side to properly reset layer checkboxes
self.graphicsView_left.setFocus()
## reset brightness contrast
self.brightness_left_layer1 = 0
self.brightness_left_layer2 = 0
self.brightness_left_layer3 = 0
self.contrast_left_layer1 = 10
self.contrast_left_layer2 = 10
self.contrast_left_layer3 = 10
self.radioButton_layer1.setChecked(True)
self.horizontalSlider_brightness.setValue(0)
self.horizontalSlider_contrast.setValue(10)
## Reset Layers
self.spinBox_slice.setValue(0)
self.checkBox_MIP.setChecked(True)
self.img_left_layer2,self.img_adj_left_layer2,self.sceneLeft.imagetype_layer2,self.imgstack_left_layer2 = None, None, None, None
self.img_left_layer3,self.img_adj_left_layer3,self.sceneLeft.imagetype_layer3,self.imgstack_left_layer3 = None, None, None, None
self.tableView_left.img2 = self.imgstack_left_layer2
self.tableView_left.img3 = self.imgstack_left_layer3
self.layer2CHKbox_left = False
self.layer3CHKbox_left = False
self.checkBox_layer2.setChecked(False)
self.checkBox_layer3.setChecked(False)
self.comboBox_channelColorLayer1.setCurrentIndex(0)
self.comboBox_channelColorLayer2.setCurrentIndex(0)
self.comboBox_channelColorLayer3.setCurrentIndex(0)
## Load new image
self.leftImage = path
self.sceneLeft.clear()
self.initImageLeft()
self.tableView_left._scene = self.sceneLeft
for i in range(self.tableView_left._model.rowCount()):
self.sceneLeft.addCircle(0.0,0.0,0.0)
self.tableView_left.updateItems()
## Update controls (GUI)
self.tableView_left.setFocus()
self.graphicsView_left.setFocus()
def openImageRight(self):
## *.png *.jpg *.bmp not yet supported
path = str(QtGui.QFileDialog.getOpenFileName(
None,"Select image file for correlation", self.workingdir,"Image Files (*.tif *.tiff);; All (*.*)"))
self.activateWindow()
if path != '':
## Set focus to corresponding side to properly reset layer checkboxes
self.graphicsView_right.setFocus()
## reset brightness contrast
self.brightness_right_layer1 = 0
self.brightness_right_layer2 = 0
self.brightness_right_layer3 = 0
self.contrast_right_layer1 = 10
self.contrast_right_layer2 = 10
self.contrast_right_layer3 = 10
self.radioButton_layer1.setChecked(True)
self.horizontalSlider_brightness.setValue(0)
self.horizontalSlider_contrast.setValue(10)
## Reset Layers
self.spinBox_slice.setValue(0)
self.checkBox_MIP.setChecked(True)
self.img_right_layer2,self.img_adj_right_layer2,self.sceneRight.imagetype_layer2,self.imgstack_right_layer2 = None, None, None, None
self.img_right_layer3,self.img_adj_right_layer3,self.sceneRight.imagetype_layer3,self.imgstack_right_layer3 = None, None, None, None
self.tableView_right.img2 = self.imgstack_right_layer2
self.tableView_right.img3 = self.imgstack_right_layer3
self.layer2CHKbox_right = False
self.layer3CHKbox_right = False
self.checkBox_layer2.setChecked(False)
self.checkBox_layer3.setChecked(False)
self.comboBox_channelColorLayer1.setCurrentIndex(0)
self.comboBox_channelColorLayer2.setCurrentIndex(0)
self.comboBox_channelColorLayer3.setCurrentIndex(0)
## Load new image
self.rightImage = path
self.sceneRight.clear()
self.initImageRight()
self.tableView_right._scene = self.sceneRight
for i in range(self.tableView_right._model.rowCount()):
self.sceneRight.addCircle(0.0,0.0,0.0)
self.tableView_right.updateItems()
## Update controls (GUI)
self.tableView_right.setFocus()
self.graphicsView_right.setFocus()
def resetImageLeft(self,img=None):
if img is None and self.mipCHKbox_left is False:
img = self.imgstack_left_layer1[self.slice_left,:]
## reset brightness contrast
self.brightness_left_layer1 = 0
self.contrast_left_layer1 = 10
self.horizontalSlider_brightness.setValue(0)
self.horizontalSlider_contrast.setValue(10)
elif img is None:
img = self.img_left_layer1
## reset brightness contrast
self.brightness_left_layer1 = 0
self.contrast_left_layer1 = 10
self.horizontalSlider_brightness.setValue(0)
self.horizontalSlider_contrast.setValue(10)
# print img.shape
## Reset Overlay
self.img_left_overlay = None
## Load original
self.img_left_displayed_layer1 = np.copy(img)
self.img_adj_left_layer1 = np.copy(img)
## Display image
self.displayImage(side='left')
self.sceneLeft.deleteArrows()
# self.changeMarkerSize()
def resetImageRight(self,img=None):
if img is None and self.mipCHKbox_right is False:
img = self.imgstack_right_layer1[self.slice_right,:]
## reset brightness contrast
self.brightness_right_layer1 = 0
self.contrast_right_layer1 = 10
self.horizontalSlider_brightness.setValue(0)
self.horizontalSlider_contrast.setValue(10)
elif img is None:
img = self.img_right_layer1
## reset brightness contrast
self.brightness_right_layer1 = 0
self.contrast_right_layer1 = 10
self.horizontalSlider_brightness.setValue(0)
self.horizontalSlider_contrast.setValue(10)
# print img.shape
## Reset Overlay
self.img_right_overlay = None
## Load original
self.img_right_displayed_layer1 = np.copy(img)
self.img_adj_right_layer1 = np.copy(img)
## Display image
self.displayImage(side='right')
self.sceneRight.deleteArrows()
# self.changeMarkerSize()
def rotateImage(self):
if self.label_selimg.text() == 'left':
if int(self.spinBox_rot.value()) == 360:
self.spinBox_rot.setValue(0)
elif int(self.spinBox_rot.value()) == -1:
self.spinBox_rot.setValue(359)
self.graphicsView_left.rotate(int(self.spinBox_rot.value())-self.sceneLeft.rotangle)
self.sceneLeft.rotangle = int(self.spinBox_rot.value())
## Update graphics
self.sceneLeft.enumeratePoints()
elif self.label_selimg.text() == 'right':
if int(self.spinBox_rot.value()) == 360:
self.spinBox_rot.setValue(0)
elif int(self.spinBox_rot.value()) == -1:
self.spinBox_rot.setValue(359)
self.graphicsView_right.rotate(int(self.spinBox_rot.value())-self.sceneRight.rotangle)
self.sceneRight.rotangle = int(self.spinBox_rot.value())
## Update graphics
self.sceneRight.enumeratePoints()
def rotateImage45(self,direction=None):
if direction is None:
print clrmsg.ERROR + "Please specify direction ('cw' or 'ccw')."
# rotate 45 degree clockwise
elif direction == 'cw':
if self.label_selimg.text() == 'left':
self.sceneLeft.rotangle += 45
self.graphicsView_left.rotate(45)
self.sceneLeft.rotangle = self.anglectrl(angle=self.sceneLeft.rotangle)
self.spinBox_rot.setValue(self.sceneLeft.rotangle)
## Update graphics
self.sceneLeft.enumeratePoints()
elif self.label_selimg.text() == 'right':
self.sceneRight.rotangle += 45
self.graphicsView_right.rotate(45)
self.sceneRight.rotangle = self.anglectrl(angle=self.sceneRight.rotangle)
self.spinBox_rot.setValue(self.sceneRight.rotangle)
## Update graphics
self.sceneRight.enumeratePoints()
# rotate 45 degree anticlockwise
elif direction == 'ccw':
if self.label_selimg.text() == 'left':
self.sceneLeft.rotangle -= 45
self.graphicsView_left.rotate(-45)
self.sceneLeft.rotangle = self.anglectrl(angle=self.sceneLeft.rotangle)
self.spinBox_rot.setValue(self.sceneLeft.rotangle)
elif self.label_selimg.text() == 'right':
self.sceneRight.rotangle -= 45
self.graphicsView_right.rotate(-45)
self.sceneRight.rotangle = self.anglectrl(angle=self.sceneRight.rotangle)
self.spinBox_rot.setValue(self.sceneRight.rotangle)
def anglectrl(self,angle=None):
if angle is None:
print clrmsg.ERROR + "Please specify side, e.g. anglectrl(angle=self.sceneLeft.rotangle)"
elif angle >= 360:
angle -= 360
elif angle < 0:
angle += 360
return angle
def changeMarkerSize(self):
if self.label_selimg.text() == 'left':
self.sceneLeft.markerSize = int(self.spinBox_markerSize.value())
## Update graphics
self.sceneLeft.enumeratePoints()
if self.sceneLeft.pixelSize:
if debug is True: print clrmsg.DEBUG + "Doing stuff with image pixelSize (left image).", self.label_imgpxsize.text()
try:
self.label_markerSizeNano.setText(str(self.sceneLeft.markerSize*2*self.sceneLeft.pixelSize))
self.label_markerSizeNanoUnit.setText(self.sceneLeft.pixelSizeUnit)
except:
if debug is True: print clrmsg.DEBUG + "Image pixel size is not a number:", self.label_imgpxsize.text()
self.label_markerSizeNano.setText("NaN")
self.label_markerSizeNanoUnit.setText('')
else:
self.label_markerSizeNano.setText('')
self.label_markerSizeNanoUnit.setText('')
elif self.label_selimg.text() == 'right':
self.sceneRight.markerSize = int(self.spinBox_markerSize.value())
## Update graphics
self.sceneRight.enumeratePoints()
if self.sceneRight.pixelSize:
if debug is True: print clrmsg.DEBUG + "Doing stuff with image pixelSize (right image).", self.label_imgpxsize.text()
try:
self.label_markerSizeNano.setText(str(self.sceneRight.markerSize*2*self.sceneRight.pixelSize))
self.label_markerSizeNanoUnit.setText(self.sceneRight.pixelSizeUnit)
except:
if debug is True: print clrmsg.DEBUG + "Image pixel size is not a number:", self.label_imgpxsize.text()
self.label_markerSizeNano.setText("NaN")
self.label_markerSizeNanoUnit.setText('')
else:
self.label_markerSizeNano.setText('')
self.label_markerSizeNanoUnit.setText('')
def setCustomRotCenter(self,maxdim):
## The default value is set as the center of a cube with an edge length equal to the longest edge of the image volume
halfmaxdim = 0.5 * maxdim
self.doubleSpinBox_custom_rot_center_x.setValue(halfmaxdim)
self.doubleSpinBox_custom_rot_center_y.setValue(halfmaxdim)
self.doubleSpinBox_custom_rot_center_z.setValue(halfmaxdim)
##################### END #####################
###### Image initialization and rotation ######
###############################################
###############################################
###### Image processing functions ######
#################### START ####################
## Read image
def imread(self,path,normalize=True):
"""
Returns a 2D numpy array (maximum intensity projection for stack image files), the kind of image as 5 bit
encoded image property and the original stack file as a numpy array or 'None' if file is 2D image.
return 5 bit encoded image property:
1 = 2D
2 = 3D (always normalized, +16)
4 = gray scale
8 = multicolor/multichannel
16= normalized
"""
if debug is True: print clrmsg.DEBUG + "===== imread"
img = tf.imread(path)
if debug is True: print clrmsg.DEBUG + "Image shape/dtype:", img.shape, img.dtype
## Displaying issues with uint16 images -> convert to uint8
if img.dtype == 'uint16':
img = img*(255.0/img.max())
img = img.astype(dtype=np.uint8)
if debug is True: print clrmsg.DEBUG + "Image dtype converted to:", img.shape, img.dtype
if img.ndim == 4:
if debug is True: print clrmsg.DEBUG + "Calculating multichannel MIP"
## return MIP, code 2+8+16 and image stack
return np.amax(img, axis=1), 26, img
## this can only handle rgb. For more channels set "3" to whatever max number of channels should be handled
elif img.ndim == 3 and any([True for dim in img.shape if dim <= 4]) or img.ndim == 2:
if debug is True: print clrmsg.DEBUG + "Loading regular 2D image... multicolor/normalize:", \
[True for x in [img.ndim] if img.ndim == 3],'/',[normalize]
if normalize is True:
## return normalized 2D image with code 1+4+16 for gray scale normalized 2D image and 1+8+16 for
## multicolor normalized 2D image
return self.norm_img(img), 25 if img.ndim == 3 else 21, None
else:
## return 2D image with code 1+4 for gray scale 2D image and 1+8 for multicolor 2D image
return img, 9 if img.ndim == 3 else 5, None
elif img.ndim == 3:
if debug is True: print clrmsg.DEBUG + "Calculating MIP"
## return MIP and code 2+4+1E6
return np.amax(img, axis=0), 22, img