forked from rhenanbartels/lunge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lunge.m
3004 lines (2412 loc) · 105 KB
/
lunge.m
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
function lunge %%% -- INFO -- the function file name is what matters, so lets make this equal
figObject = createMainFigure();
%Create Navigation Axes
navigationAxesObjects = createNavigationAxes(figObject);
%Create Information Texts
informationTextsObjects = createInformationTexts(navigationAxesObjects.informationAxesObject);
%Create Menus
createMenuObjects(figObject);
createControlSideBar(figObject);
handles.gui = guihandles(figObject);
guidata(figObject, handles);
end
%%%%%%%%%%%% GUI RELATED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function figObject = createMainFigure()
%Get the screen size
screenSize = get(0, 'ScreenSize');
figObject = figure('Tag', 'mainFig',...
'MenuBar', 'None',...
'NumberTitle', 'Off',...
'Name', 'New CT Processing V 0.0.1dev',...
'units', 'normalized', ...
'OuterPosition', [0 0.03 1 0.97], ... %%% -- INFO -- in my laptop I can not see the top of the window, only after manually risize. I think it is a problem with windows, anyway a chaged the dimensions, is ok in your screen?
'Color', 'black',...
'WindowScrollWheelFcn', @refreshSlicePosition,...
'WindowButtonMotionFcn', @mouseMove);
end
function navigationAxesObjectStructure = createNavigationAxes(parentFigureObject)
navigationAxesObjectStructure.navigationAxesObject = axes('Parent', parentFigureObject,...
'Units', 'Normalized',...
'Position', [0.16, 0.15, 0.6, 0.8],...
'Color', 'black',...
'XtickLabel', '',...
'YtickLabel', '',...
'Tag', 'navigationAxes');
navigationAxesObjectStructure.informationAxesObject = axes('Parent', parentFigureObject,...
'Units', 'Normalized',...
'Position', [0.11, 0.08, 0.7, 0.9],...
'XtickLabel', '',...
'YtickLabel', '',...
'Color', 'black');
end
function informationTextsObjectsStructure = createInformationTexts(parentAxesObject)
informationTextsObjectsStructure.patientName = text(0.5, 0.98, 'Patient''s Name',...
'Color', 'white',...
'Fontsize', 12,...
'Fontweight', 'bold',...
'Tag', 'patientNameTag',...
'HorizontalAlignment', 'center',...
'Visible', 'Off');
informationTextsObjectsStructure.slicePosition = text(0.01, 0.02, '1/-',...
'Color', 'white',...
'Fontsize', 12,...
'Fontweight', 'bold',...
'Visible', 'Off',...
'Tag', 'slicePositionTag');
informationTextsObjectsStructure.numberOfRows = text(0.01, 0.06, 'Image Size: -',...
'Color', 'white',...
'Fontsize', 12,...
'Fontweight', 'bold',...
'Visible', 'Off',...
'Tag', 'numberOfRowsTag');
informationTextsObjectsStructure.pixelValue = text(0.14, 0.02, 'Pixel Value = -',...
'Color', 'white',...
'Fontsize', 12,...
'Fontweight', 'bold',...
'Visible', 'Off',...
'Tag', 'pixelValueTag');
end
function createMenuObjects(parentFigureObject)
%Create Menu Objects
%%%FILE MENU
fileMenu = uimenu('Parent', parentFigureObject,...
'Label', 'File');
openGroup = uimenu('Parent', fileMenu, 'Label', 'Open');
%Load Patient Menu
uimenu('Parent', openGroup,...
'Label', 'Open Patient',...
'Acc', 'P',...
'Callback', @openPatient);
%Load Frame Menu
uimenu('Parent', openGroup,...
'Label', 'Open Frame',...
'Acc', 'O',...
'Callback', @openDicom);
%Load Masks Menu
uimenu('Parent', openGroup,...
'Label', 'Open Masks',...
'Acc', 'M',...
'Enable', 'Off',...
'Tag', 'openMaskMenu',...
'Callback', @openMask);
%Save data Menu
uimenu('Parent', fileMenu,...
'Label', 'Save Data',...
'Enable', 'Off',...
'Tag', 'saveDataMenu',...
'Callback', @saveData);
%Close Patient Menu
uimenu('Parent', fileMenu,...
'Label', 'Close Patient',...
'Enable', 'Off',...
'Tag', 'closePatientMenu',...
'Callback', @closePatient);
%Quit Menu
uimenu('Parent', fileMenu,...
'Label', 'Quit',...
'Callback', '');
%%%ANALYSIS MENU
analysisMenu = uimenu('Parent', parentFigureObject,...
'Label', 'Analysis');
massAndVolume = uimenu('Parent', analysisMenu,...
'Label', 'Mass and Volume',... ..
'Enable', 'Off',...
'Tag', 'massAndVolumeCalculation');
uimenu('Parent', massAndVolume,...
'Label', 'Cranio-Caudal',...
'Callback', @massAndVolumeCalculation);
uimenu('Parent', massAndVolume,...
'Label', 'Antero-Posterior',...
'Callback', '');
uimenu('Parent', massAndVolume,...
'Label', 'Latero-Lateral',...
'Callback', '');
uimenu('Parent',analysisMenu,...
'Enable', 'Off',...
'Tag', 'p15Calculation',...
'Callback', @p15Calculation,...
'Label', 'P15');
uimenu('Parent',analysisMenu,...
'Enable', 'Off',...
'Tag', 'toSUV',...
'Callback', @toSUV,...
'Label', 'PET to SUV');
%%%PLUGINS MENU
pluginsMenu = uimenu('Parent', parentFigureObject,...
'Label', 'Plugins');
end
function createControlSideBar(parentFigureObject)
mainPanel = uipanel('Parent', parentFigureObject,...
'Units', 'Normalized',...
'Position', [0.8, 0, 0.2, 1],...'
'BackGroundColor', 'black',...
'Visible', 'On',...
'Tag', 'sideBarMainPanel');
%Side Bar Sliders
uicontrol('Parent', mainPanel,...
'Style', 'Slider',...
'Units', 'Normalized',...
'Position', [0.1, 0.45, 0.1, 0.2],...
'Tag', 'windowWidthSlider',...
'Callback', @windowWidthCallback);
uicontrol('Parent', mainPanel,...
'Style', 'Slider',...
'Units', 'Normalized',...
'Position', [0.35, 0.45, 0.1, 0.2],...
'Tag', 'windowCenterSlider',...
'Callback', @windowCenterCallback);
uicontrol('Parent',mainPanel,...
'Style', 'Edit',...
'Units', 'Normalized',...
'Position', [0.12, 0.67, 0.11, 0.02],...
'HorizontalAlignment', 'Center',...
'String', '0',...
'BackGroundColor', 'black',...
'ForeGroundColor', 'white',...
'Tag', 'windowWidthText',...
'Callback', @windowWidthTextCallback);
uicontrol('Parent',mainPanel,...
'Style', 'Edit',...
'Units', 'Normalized',...
'Position', [0.37, 0.67, 0.11, 0.02],...
'HorizontalAlignment', 'Center',...
'String', '0',...
'BackGroundColor', 'black',...
'ForeGroundColor', 'white',...
'Tag', 'windowCenterText',...
'Callback', @windowCenterTextCallback);
%Window Width and Center Buttons
uicontrol('Parent', mainPanel,...
'Units', 'Normalized',...
'Position', [0.48, 0.45, 0.28, 0.06],...
'String', 'Reset',...
'Callback', @resetWindowWidthCenter);
%HU Ranges textfields
uicontrol('Parent', mainPanel,...
'Units', 'Normalized', ...
'Position', [0.3, 0.2, 0.45, 0.05],...
'Style', 'Text',...
'Backgroundcolor', 'black',...
'Foregroundcolor', 'white',...
'Fontsize', 13,...
'Fontweight', 'bold',...
'String', 'HU Ranges');
uicontrol('Parent', mainPanel,...
'Units', 'Normalized', ...
'Position', [0.01 , 0.16, 0.45, 0.03],...
'Style', 'Text',...
'Backgroundcolor', 'black',...
'Foregroundcolor', 'white',...
'Fontsize', 11,...
'HorizontalAlignment', 'left',...
'String', 'Hyper Aerated:');
uicontrol('Parent', mainPanel,...
'Units', 'Normalized', ...
'Position', [0.01 , 0.12, 0.5, 0.03],...
'Style', 'Text',...
'Backgroundcolor', 'black',...
'Foregroundcolor', 'white',...
'Fontsize', 11,...
'HorizontalAlignment', 'left',...
'String', 'Normally Aerated:');
uicontrol('Parent', mainPanel,...
'Units', 'Normalized', ...
'Position', [0.01 , 0.08, 0.45, 0.03],...
'Style', 'Text',...
'Backgroundcolor', 'black',...
'Foregroundcolor', 'white',...
'Fontsize', 11,...
'HorizontalAlignment', 'left',...
'String', 'Poorly Aerated:');
uicontrol('Parent', mainPanel,...
'Units', 'Normalized', ...
'Position', [0.01 , 0.04, 0.45, 0.03],...
'Style', 'Text',...
'Backgroundcolor', 'black',...
'Foregroundcolor', 'white',...
'Fontsize', 11,...
'HorizontalAlignment', 'left',...
'String', 'Non Aerated:');
uicontrol('Parent', mainPanel,...
'Units', 'Normalized',...
'Position', [0.55, 0.16, 0.15, 0.03],...
'Style', 'Edit',...
'Tag', 'upperHyperValue',...
'String', '-1000');
uicontrol('Parent', mainPanel,...
'Units', 'Normalized',...
'Position', [0.75, 0.16, 0.15, 0.03],...
'Style', 'Edit',...
'Tag', 'lowerHyperValue',...
'String', '-900');
uicontrol('Parent', mainPanel,...
'Units', 'Normalized',...
'Position', [0.55, 0.12, 0.15, 0.03],...
'Style', 'Edit',...
'Tag', 'upperNormallyValue',...
'String', '-900');
uicontrol('Parent', mainPanel,...
'Units', 'Normalized',...
'Position', [0.75, 0.12, 0.15, 0.03],...
'Style', 'Edit',...
'Tag', 'lowerNormallyValue',...
'String', '-500');
uicontrol('Parent', mainPanel,...
'Units', 'Normalized',...
'Position', [0.55, 0.08, 0.15, 0.03],...
'Style', 'Edit',...
'Tag', 'upperPoorlyValue',...
'String', '-500');
uicontrol('Parent', mainPanel,...
'Units', 'Normalized',...
'Position', [0.75, 0.08, 0.15, 0.03],...
'Style', 'Edit',...
'Tag', 'lowerPoorlyValue',...
'String', '-100');
uicontrol('Parent', mainPanel,...
'Units', 'Normalized',...
'Position', [0.55, 0.04, 0.15, 0.03],...
'Style', 'Edit',...
'Tag', 'upperNonValue',...
'String', '-100');
uicontrol('Parent', mainPanel,...
'Units', 'Normalized',...
'Position', [0.75, 0.04, 0.15, 0.03],...
'Style', 'Edit',...
'Tag', 'lowerNonValue',...
'String', '100');
%Side Bar Slice Range
uicontrol('Parent', mainPanel,...
'Units', 'Normalized',...
'Position', [0.3, 0.35, 0.4, 0.05],...
'Style', 'Text',...
'BackGroundColor', 'black',...
'ForeGroundColor', 'white',...
'FontSize', 13,...
'FontWeight', 'bold',...
'String', 'Slice Range');
uicontrol('Parent', mainPanel,...
'Units', 'Normalized',...
'Position', [0.01, 0.33, 0.2, 0.025],...
'Style', 'Text',...
'String', 'From:',...
'BackGroundColor', 'black',...
'ForeGroundColor', 'white');
uicontrol('Parent', mainPanel,...
'Units', 'Normalized',...
'Position', [0.4, 0.33, 0.2, 0.025],...
'Style', 'Text',...
'String', 'To:',...
'BackGroundColor', 'black',...
'ForeGroundColor', 'white');
uicontrol('Parent', mainPanel,...
'Units', 'Normalized',...
'Position', [0.25, 0.33, 0.15, 0.025],...
'Style', 'Edit',...
'String', '1',....
'Tag', 'sliceRangeFrom',...
'Callback', @sliceRangeFromToCallback);
uicontrol('Parent', mainPanel,...
'Units', 'Normalized',...
'Position', [0.6, 0.33, 0.15, 0.025],...
'Style', 'Edit',...
'String', '1',...
'Tag', 'sliceRangeTo',...
'Callback', @sliceRangeFromToCallback);
% Overlay checkboxes
uicontrol('Parent', mainPanel,...
'Units', 'Normalized',...
'Position', [0.1, 0.95, 0.8, 0.025],...
'Style', 'CheckBox',...
'String', 'Show Mask',...
'FontSize', 13,...
'BackGroundColor', 'black',...
'ForeGroundColor', 'white',...
'Enable','off',...
'Tag', 'maskChk',...
'Callback', @maskChkCallback);
uicontrol('Parent', mainPanel,...
'Units', 'Normalized',...
'Position', [0.1, 0.92, 0.8, 0.025],...
'Style', 'CheckBox',...
'String', 'Show PET',...
'FontSize', 13,...
'BackGroundColor', 'black',...
'ForeGroundColor', 'white',...
'Enable','off',...
'Tag', 'petChk',...
'Callback', @petChkCallback);
end
%%%%%%%%%%%% GUI RELATED FUNCTIONS - END %%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function handles = displayCurrentDicom(handles, dicomImage, slicePosition)
axes(handles.gui.navigationAxes)
set(findobj(handles.gui.navigationAxes,'type','image'),'CData',dicomImage(:, :, slicePosition))
set(handles.gui.navigationAxes, 'Clim', [handles.data.displayLow, handles.data.displayHigh]);
colormap(gray)
end
function handles = createDisplayDicom(handles, dicomImage, slicePosition)
axes(handles.gui.navigationAxes)
set(handles.gui.navigationAxes,'nextPlot','replace')
handles.gui.imagePlot = imagesc(dicomImage(:, :, slicePosition));
set(handles.gui.navigationAxes, 'Clim', [handles.data.displayLow, handles.data.displayHigh]);
colormap(gray)
end
function refreshPatientsInfo(handles, info)
patientName = info.PatientName.FamilyName;
%Check if Patient has Give Name
if isfield(info.PatientName, 'GivenName')
patientName = [info.PatientName.GivenName ' ' patientName];
end
set(handles.gui.patientNameTag, 'String', patientName)
set(handles.gui.numberOfRowsTag, 'String', sprintf('Image Size: %d x %d', info.Rows, info.Columns));
end
function refreshSlicePosition(hObject, eventdata)
slicePositionPlaceHolder = '%d/%d';
handles = guidata(hObject);
if isfield(handles, 'data')
nSlices = size(handles.data.dicomImage, 3);
currentSlicePosition = get(handles.gui.slicePositionTag, 'String');
%Get the new slice position based on the displayed values using regexp
newSlicePosition = getSlicePosition(currentSlicePosition,...
eventdata.VerticalScrollCount);
%Make sure that the slice number return to 1 if it is bigger than the
%number of slices
newSlicePosition = mod(newSlicePosition, nSlices);
%Make sure that the slice number return to nSlices if it is smaller than the
%number of slices
if ~newSlicePosition && eventdata.VerticalScrollCount < 0
newSlicePosition = nSlices;
elseif ~newSlicePosition && eventdata.VerticalScrollCount >= 0 %%% -- INFO -- matlab has some hard time with the scroll of my laptop, in this case VerticalScrollCount == 0, it get an error latter in this function and this is error realy I need to restart matlab. I changed > to >=
newSlicePosition = 1;
end
%Refresh slice position information.
set(handles.gui.slicePositionTag, 'String',...
sprintf(slicePositionPlaceHolder, newSlicePosition, nSlices));
if handles.gui.petDisplay
if isfield(handles.dataPET,'CT')
handles = displayCurrentDicom(handles, handles.dataPET.CT, newSlicePosition);
end
else
handles = displayCurrentDicom(handles, handles.data.dicomImage, newSlicePosition);
end
% refresh overlay if needed
if get(handles.gui.maskChk,'value')
if handles.gui.petDisplay
refreshOverlay(handles.dataPET.masks,handles)
else
refreshOverlay(handles.data.masks,handles)
end
elseif get(handles.gui.petChk,'Value') && handles.gui.petDisplay
refreshOverlay(handles.dataPET.dicomImage,handles)
end
%Refresh pixel value information.
refreshPixelPositionInfo(handles, handles.gui.navigationAxes)
guidata(hObject, handles)
end
end
function newSlicePosition = getSlicePosition(slicePositionString, direction)
tempSlicePosition = regexp(slicePositionString, '/', 'split');
if direction > 0
newSlicePosition = str2double(tempSlicePosition(1)) + 1;
else
newSlicePosition = str2double(tempSlicePosition(1)) - 1;
end
end
function openPatient(hObject, eventdata)
handles = guidata(hObject);
if isfield(handles, 'data')
dirPath = uigetdir(handles.data.dicomImagePath, 'Select Patient''s Folder');
else
dirPath = uigetdir('Select Patient''s Folder');
end
if dirPath
%Display Wait window
figObj = createLogFrame();
displayLog(figObj, sprintf('%s', 'Searching patient data files...'), 0)
set(handles.gui.mainFig,'Pointer','watch'); drawnow('expose');
% Look for available CT,mask and PET files
[mask,ct,pet]=find_files(dirPath,{},[],[]);
%Close log frame
close(figObj)
% Now load CT, mask and PET
% TODO no data found case
if ~isempty(ct)
openDicom(hObject, [], fileparts(ct))
end
% TODO case with more than one possible mask
if ~isempty(mask)
[folder name ext] = fileparts(mask{1});
openMask(hObject, [], [name ext], [folder filesep])
end
if ~isempty(pet)
openDicom(hObject, [], fileparts(pet))
end
set(handles.gui.mainFig,'Pointer','arrow'); drawnow('expose');
end
end
function openDicom(hObject, eventdata, dirPath)
handles = guidata(hObject);
if ~exist('dirPath','var')
if isfield(handles, 'data')
dirPath = uigetdir(handles.data.dicomImagePath, 'Select Patient''s Dicom Folder');
else
dirPath = uigetdir('Select Patient''s Dicom Folder');
end
end
if dirPath
%Display Wait window
figObj = createLogFrame();
displayLog(figObj, sprintf('%s', 'Loading Images...'), 0)
set(handles.gui.mainFig,'Pointer','watch'); drawnow('expose');
listOfFiles = dir(dirPath);
%Try to open every file with dicomread. If possible use as a Dicom
nFiles = length(listOfFiles);
found = false;
counter = 0;
%Create a function to insert this piece of code.
while ~found
counter = counter + 1;
fileName = listOfFiles(counter).name;
if ~strcmp(fileName, '.') && ~strcmp(fileName, '..')
completeFileName = [dirPath filesep fileName];
%Try to discover if files without extension are Dicom files
try
info = dicominfo(completeFileName); %%% -- INFO -- I changed the info and handles.data.metadata, because I think this way makes more sense and I need to check is it is a PET image to know were to put the data
handles.gui.petDisplay = strcmpi(info.Modality,'pt');
if handles.gui.petDisplay
% using the string field name we do not need to
% repeat this if in each instance
fieldname = 'dataPET';
else
fieldname = 'data';
end
handles.(fieldname).metadata = dicom_read_header(completeFileName);
%%% -- INFO -- I used a random dicom file I had and it
%%% didn't have the WindowCenter and WindowWidth
%%% infomation - I included the following lines to
%%% garantee
if ~isfield(info,'WindowWidth')
handles.data.metadata.WindowWidth = 1400; % 1400 is a common value used to visualize lung in CT
end
if ~isfield(info,'WindowCenter')
handles.data.metadata.WindowCenter = -600; % -600 is a common value used to visualize lung in CT
end
found = true;
catch
%Do nothing
continue
end
end
end
dicomImage = single(dicom_read_volume(handles.(fieldname).metadata)); %% -- INFO -- for the PET data we can not use int, it will need more memory, but we need single for calculation too
if strcmpi(handles.(fieldname).metadata.Manufacturer,'siemens')
% Siemens data can have different slopes and intercepts for each
% slice (at least in data from Pettsburg)
handles.(fieldname).metadata.RescaleSlope = zeros([1,1,numel(handles.(fieldname).metadata.Filenames)]);
handles.(fieldname).metadata.RescaleIntercept = zeros([1,1,numel(handles.(fieldname).metadata.Filenames)]);
for idx = 1:numel(handles.(fieldname).metadata.Filenames)
aux = dicominfo(handles.(fieldname).metadata.Filenames{idx});
handles.(fieldname).metadata.RescaleSlope(idx) = single(aux.RescaleSlope);
handles.(fieldname).metadata.RescaleIntercept(idx) = single(aux.RescaleIntercept);
end
end
if isfield(info, 'RescaleSlope')
dicomImage = bsxfun(@times,dicomImage,single(handles.(fieldname).metadata.RescaleSlope)); % bsxfun expandes the singleton dimension automaticaly
end
if isfield(info, 'RescaleIntercept')
dicomImage = bsxfun(@plus,dicomImage,single(handles.(fieldname).metadata.RescaleIntercept));
end
% If a PET image was loaded maybe the user want to analyse PET and
% CT together. For this CT must be converted to PET resolution.
if handles.gui.petDisplay && isfield(handles,'data')
displayLog(figObj, sprintf('%s', 'Converting other data to PET resolution...'), 1)
map = CTPET_CreateMap(handles.data.metadata,handles.dataPET.metadata);
handles.dataPET.CT = CTPET_ApplyMap(handles.data.dicomImage,map);
% if a mask also exist, it should be converted too
if isfield(handles.data,'masks')
handles.dataPET.masks = CTPET_ApplyMap(handles.data.masks,map);
end
end
set(handles.gui.mainFig,'Pointer','arrow'); drawnow('expose');
handles.(fieldname).dicomImage = dicomImage;
%Set the Window Width and Window Center
handles = calculateWindowWidthAndCenter(handles);
configureSliders(handles)
%Display First Slice
cla(handles.gui.navigationAxes)
if handles.gui.petDisplay
if isfield(handles.dataPET,'CT')
handles = createDisplayDicom(handles, handles.dataPET.CT, 1);
set(handles.gui.petChk,'Enable','on')
end
set(handles.gui.toSUV,'Enable','on')
else
handles = createDisplayDicom(handles, dicomImage, 1);
set(handles.gui.petChk,'Enable','off')
end
%Display Patients Information
refreshPatientsInfo(handles, handles.(fieldname).metadata)
%Update Interface Appearene
hideShowImageInformation(handles, 'On')
hideShowSideBar(handles, 'On')
set(handles.gui.openMaskMenu, 'Enable', 'On')
set(handles.gui.saveDataMenu, 'Enable', 'On')
set(handles.gui.closePatientMenu, 'Enable', 'On')
%Create a variable to store the Image folder. This way the Masks
%could be easier located.
handles.(fieldname).dicomImagePath = dirPath;
% -- INFO -- This is redundant with createDisplayDicom
% % set(handles.gui.navigationAxes, 'Clim',...
% % [handles.data.displayLow, handles.data.displayHigh])
% Create the saved field as true, because if the user just load
% files the data should not be saved before close. Every data
% processing function should change saved to false
handles.saved = 1;
guidata(hObject, handles)
%Close log frame
close(figObj)
end
end
function openMask(hObject, eventdata, FileName, PathName)
handles = guidata(hObject);
if ~exist('FileName','var')
[FileName PathName] = uigetfile('*.hdr;*.nrrd',...
'Select the file containing the masks', handles.data.dicomImagePath);
end
if FileName
%Display Wait window
figObj = createLogFrame();
displayLog(figObj, sprintf('%s', 'Loading Masks...'), 0)
set(handles.gui.mainFig,'Pointer','watch'); drawnow('expose');
handles = guidata(hObject);
fileName = [PathName FileName];
if strfind(FileName,'hdr')
masks = analyze75read(fileName);
else
masks = nrrd_read(fileName);
end
% Masks created in 3D Slicer using CIP plugin are labeled, ask user
% wich labels he wants to load
if any(masks(:)>255);
labels = unique(masks(:)); labels = labels(2:end); % exclude 0 fram list of labels
list = interpretCIPCodes(labels);
h=lungeDlg('msgbox',{'You opened a labeld mask file.';'Use the next window to choose structure you want to keep!'},'Instruction!!!','help','modal');
%msgbox({'You opened a labeld mask file.';'Use the next window to choose structure you want to keep!'},'Instruction!!!','help','modal');
uiwait(h);
chosen = lungeDlg('listdlg','ListString',list,'Name','Structures to keep');
%listdlg('ListString',list,'Name','Structures to keep');
notchosen = 1:numel(labels);
notchosen=notchosen(~ismember(notchosen,chosen));
for each=notchosen
masks(masks==labels(each))=0;
end
end
% Mask is converted to logical to reduce memory size
handles.data.masks = logical(masks);
% If there is a PET image loaded and the mask is not of the same
% size as PET, convert it
if isfield(handles,'dataPET')
if ~all(size(handles.dataPET.dicomImage) == size(masks))
map = CTPET_CreateMap(handles.data.metadata,hnadles.dataPET.metadata);
handles.dataPET.masks = CTPET_ApplyMap(handles.data.masks,map);
end
end
set(handles.gui.mainFig,'Pointer','arrow'); drawnow('expose');
guidata(hObject, handles)
%UPDATE MENU
set(handles.gui.massAndVolumeCalculation, 'Enable', 'On')
set(handles.gui.p15Calculation, 'Enable', 'On')
set(handles.gui.maskChk, 'Enable', 'On')
%Close wait window
close(figObj);
end
end
function saveData(hObject, eventdata)
handles = guidata(hObject);
if ~isfield(handles.data,'fileName')
[filename, pathname] = uiputfile('*.mat', 'Chose filename');
if isequal(filename,0) || isequal(pathname,0)
return
else
handles.data.fileName = fullfile(pathname, filename);
end
end
%Display Wait window
figObj = createLogFrame();
displayLog(figObj, sprintf('%s', 'Saving data...'), 0)
data = handles.data;
save(handles.data.fileName,'-struct','data')
if isfield(handles,'dataPET')
data = handles.dataPET;
save(handles.data.fileName,'-struct','data','-append')
end
set(handles.gui.saveDataMenu,'Enable','Off')
%Close wait window
close(figObj);
guidata(hObject,handles)
end
function closePatient(hObject, eventdata)
handles = guidata(hObject);
set(handles.gui.mainFig,'Pointer','watch'); drawnow('expose');
if ~handles.saved
% Ask if want to save
btn = lungeDlg('questdlg','You have unsaved data. Want to save before close this patient?','Save','yes','no', 'yes');
%questdlg('You have unsaved data. Want to save before close this patient?','Save','yes','no', 'yes');
if strcmp(btn,'yes')
saveData(hObject)
end
end
handles = rmfield(handles,'data');
if isfield(handles,'dataPET')
handles = rmfield(handles,'dataPET');
end
% Hide patient information and DICOM display
hideShowImageInformation(handles,'off')
cla(handles.gui.navigationAxes)
set(handles.gui.navigationAxes,'color','k')
%UPDATE MENU
set(handles.gui.massAndVolumeCalculation, 'Enable', 'Off')
set(handles.gui.p15Calculation, 'Enable', 'Off')
set(handles.gui.toSUV, 'Enable', 'Off')
set(handles.gui.maskChk, 'Enable', 'Off')
set(handles.gui.petChk, 'Enable', 'Off')
set(handles.gui.openMaskMenu, 'Enable', 'Off')
set(handles.gui.closePatientMenu, 'Enable', 'Off')
set(handles.gui.saveDataMenu,'Enable','Off')
set(handles.gui.mainFig,'Pointer','arrow'); drawnow('expose');
guidata(hObject, handles)
end
function mouseMove(hObject, eventdata)
handles = guidata(hObject);
mainAxes = handles.gui.navigationAxes;
refreshPixelPositionInfo(handles, mainAxes);
end
function refreshPixelPositionInfo(handles, mainAxes)
if isfield(handles, 'data')
C = get(mainAxes,'currentpoint');
xlim = get(mainAxes,'xlim');
ylim = get(mainAxes,'ylim');
row = round(C(1));
col = round(C(1, 2));
%Check if pointer is inside Navigation Axes.
outX = ~any(diff([xlim(1) C(1,1) xlim(2)])<0);
outY = ~any(diff([ylim(1) C(1,2) ylim(2)])<0);
if outX && outY
%Get the current Slice
currentSlicePositionString = get(handles.gui.slicePositionTag, 'String');
tempSlicePosition = regexp(currentSlicePositionString, '/', 'split');
slicePosition = str2double(tempSlicePosition(1));
currentSlice = handles.data.dicomImage(:, :, slicePosition);
pixelValue = currentSlice(col, row);
set(handles.gui.pixelValueTag, 'String', sprintf('Pixel Value = %.2f', double(pixelValue)))
else
set(handles.gui.pixelValueTag, 'String', sprintf('Pixel Value = -'))
end
end
end
function windowWidthCallback(hObject, eventdata)
handles = guidata(hObject);
windowWidth = get(handles.gui.windowWidthSlider, 'Value');
windowCenter = get(handles.gui.windowCenterSlider, 'Value');
if isfield(handles,'data') %%% -- INFO -- if you try to use the sliders before load data there was an error. Other option is to start with disabled sliders
set(handles.gui.windowWidthText, 'String',sprintf('%.2f', windowWidth));
handles = calculateWindowWidthAndCenter(handles,...
windowWidth, windowCenter);
set(handles.gui.navigationAxes, 'Clim', [handles.data.displayLow handles.data.displayHigh])
end
guidata(hObject, handles);
end
function windowWidthTextCallback(hObject, eventdata)
handles = guidata(hObject);
windowWidth = str2double(get(hObject, 'String'));
set(handles.gui.windowWidthSlider, 'Value',windowWidth);
windowCenterCallback(hObject)
end
function windowCenterCallback(hObject, eventdata)
handles = guidata(hObject);
windowWidth = get(handles.gui.windowWidthSlider, 'Value');
windowCenter = get(handles.gui.windowCenterSlider, 'Value');
if isfield(handles,'data') %%% -- INFO -- if you try to use the sliders before load data there was an error. Other option is to start with disabled sliders
set(handles.gui.windowCenterText, 'String',sprintf('%.2f', windowCenter));
handles = calculateWindowWidthAndCenter(handles, windowWidth, windowCenter);
set(handles.gui.navigationAxes, 'Clim', [handles.data.displayLow handles.data.displayHigh])
end
guidata(hObject, handles);
end
function windowCenterTextCallback(hObject, eventdata)
handles = guidata(hObject);
windowCenter = str2double(get(hObject, 'String'));
set(handles.gui.windowCenterSlider, 'Value',windowCenter);
windowCenterCallback(hObject)
end
function resetWindowWidthCenter(hObject, eventdata)
handles = guidata(hObject);
if isfield(handles,'data') %%% -- INFO -- if you try to use the button before load data there was an error. Other option is to start with disabled button
windowWidth = handles.data.metadata.WindowWidth(1);
windowCenter = handles.data.metadata.WindowCenter(1);
handles = calculateWindowWidthAndCenter(handles, windowWidth, windowCenter);
set(handles.gui.navigationAxes, 'Clim', ...
[handles.data.displayLow, handles.data.displayHigh]);
set(handles.gui.windowWidthSlider, 'Value', windowWidth);
set(handles.gui.windowWidthText, 'String',sprintf('%.2f', windowWidth));
set(handles.gui.windowCenterSlider, 'Value', windowCenter);
set(handles.gui.windowCenterText, 'String',sprintf('%.2f', windowCenter));
end
guidata(hObject, handles)
end
function hideShowSideBar(handles, hideOrShow)
set(handles.gui.sideBarMainPanel, 'Visible', hideOrShow)
end
function maskChkCallback(hObject, eventdata)
handles = guidata(hObject);
if get(hObject,'Value')
set(handles.gui.petChk,'enable','off')
if handles.gui.petDisplay
handles.gui.overlayPlot = createOverlay(handles.dataPET.masks,handles);
else
handles.gui.overlayPlot = createOverlay(handles.data.masks,handles);
end
else
if handles.gui.petDisplay
set(handles.gui.petChk,'enable','on')
end
if ishandle(handles.gui.overlayPlot)
set(handles.gui.overlayPlot,'visible','off')
end
end
guidata(hObject,handles)
end
function petChkCallback(hObject, eventdata)
handles = guidata(hObject);
if get(hObject,'Value')
if handles.gui.petDisplay
set(handles.gui.maskChk,'enable','off')
handles.gui.overlayPlot = createOverlay(handles.dataPET.dicomImage,handles);
end
else
set(handles.gui.maskChk,'enable','on')
if ishandle(handles.gui.overlayPlot)
set(handles.gui.overlayPlot,'visible','off')
end
end
guidata(hObject,handles)
end
function hideShowImageInformation(handles, hideOrShow)
set(handles.gui.patientNameTag, 'Visible', hideOrShow)
set(handles.gui.slicePositionTag, 'Visible', hideOrShow)
set(handles.gui.numberOfRowsTag, 'Visible', hideOrShow)
set(handles.gui.pixelValueTag, 'Visible', hideOrShow)
end
function sliceRangeFromToCallback(hObject, eventdata)
handles = guidata(hObject);
%totalNumberOfSlices = size(handles.data.lung, 3);
totalNumberOfSlices = 10;
fromRange = get(handles.gui.sliceRangeFrom, 'String');
toRange = get(handles.gui.sliceRangeTo, 'String');
try
validateSliceRangeValues(handles, fromRange, toRange, totalNumberOfSlices)
catch
lungeDlg('errordlg','Some error ocurred in the Slice Range configuration!',...
'SliceRangeError');
% errordlg('Some error ocurred in the Slice Range configuration!',...