-
Notifications
You must be signed in to change notification settings - Fork 0
/
Register_PM_MRI.m
1993 lines (1669 loc) · 84.7 KB
/
Register_PM_MRI.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 varargout = Register_PM_MRI(varargin)
% REGISTER_PM_MRI MATLAB code for Register_PM_MRI.fig
% REGISTER_PM_MRI, by itself, creates a new REGISTER_PM_MRI or raises the existing
% singleton*.
%
% H = REGISTER_PM_MRI returns the handle to a new REGISTER_PM_MRI or the handle to
% the existing singleton*.
%
% REGISTER_PM_MRI('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in REGISTER_PM_MRI.M with the given input arguments.
%
% REGISTER_PM_MRI('Property','Value',...) creates a new REGISTER_PM_MRI or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before Register_PM_MRI_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to Register_PM_MRI_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
%
% Created by: Victor Gonzalez Castro
% Funded by: Row Fogo Charitable Trust
%
% Edit the above text to modify the response to help Register_PM_MRI
% Last Modified by GUIDE v2.5 05-Oct-2016 17:23:30
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Register_PM_MRI_OpeningFcn, ...
'gui_OutputFcn', @Register_PM_MRI_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
end
% *************************************************************************
% --- Executes just before Register_PM_MRI is made visible.
function Register_PM_MRI_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to Register_PM_MRI (see VARARGIN)
% ADD B-SPLINE FUNCTIONS TO PATH
addpath('./B-spline/');
addpath('./B-spline/functions');
addpath('./B-spline/functions_affine');
addpath('./B-spline/functions_nonrigid');
maxMRIImages = 4;
% Choose default command line output for Register_PM_MRI
handles.output = hObject;
% Add additional data as a new field inside axes1. Specifically:
% - A cell array to store the MRI images
data = guidata(handles.axes1);
data.data.MRIImage = cell(3,maxMRIImages); % Cell array for the MR images
data.data.tableCoordinatesMRI = zeros(1,2);
data.data.tableCoordinatesHistol = zeros(1,2);
%data.indexMRILoaded = zeros(1, maxMRIImages);
% Add additional data as a new field inside axes2. Specifically:
% - A cell array to store the histology images
data.data.histologyImage = cell(1,3); % Cell array for the MR images
guidata(handles.axes1, data);
% guidata(handles.axes2, dataHistology);
% Update handles structure
%guidata(hObject, handles);
% UIWAIT makes Register_PM_MRI wait for user response (see UIRESUME)
% uiwait(handles.figRegistration);
% Set all buttons of the MRI except for the FLAIR to invisible
set(handles.buttonT1w, 'Visible', 'off');
set(handles.buttonT2w, 'Visible', 'off');
set(handles.buttonT2star, 'Visible', 'off');
% Set string of the text panel above the histology panel
set(handles.text5, 'String', '');
% Set properties of uitables
set(handles.uitablePointsMRI, 'ColumnEditable', [false, false]);
set(handles.uitablePointsHistol, 'ColumnEditable', [false, false]);
% Set uitables to empty
dataTableMRI = get(handles.uitablePointsMRI, 'Data');
% dataTableMRI(4,:) = [];
% dataTableMRI(3,:) = [];
% dataTableMRI(2,:) = [];
% dataTableMRI(1,:) = [];
numPointsMRI = size(dataTableMRI,1);
for nP=1:numPointsMRI
dataTableMRI(1,:) = [];
end
set(handles.uitablePointsMRI, 'Data', dataTableMRI);
dataTableHistol = get(handles.uitablePointsHistol, 'Data');
% dataTableHistol(4,:) = [];
% dataTableHistol(3,:) = [];
% dataTableHistol(2,:) = [];
% dataTableHistol(1,:) = [];
numPointsHistol = size(dataTableHistol,1);
for nP=1:numPointsHistol
dataTableHistol(1,:) = [];
end
set(handles.uitablePointsHistol, 'Data', dataTableHistol);
% Put actual directory path in edit3
set(handles.edit3, 'String', pwd);
end
% *************************************************************************
% --- Outputs from this function are returned to the command line.
function varargout = Register_PM_MRI_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
%varargout{1} = handles.output;
end
% *************************************************************************
% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
end
% *************************************************************************
% --- Executes during object creation, after setting all properties.
function axes1_CreateFcn(hObject, eventdata, handles)
% hObject handle to the selected object in uibuttongroup1
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
end
% *************************************************************************
function edit2_Callback(hObject, eventdata, handles)
% hObject handle to edit2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit2 as text
% str2double(get(hObject,'String')) returns contents of edit2 as a double
end
% *************************************************************************
% --- Executes on button press in buttonFLAIR.
function buttonFLAIR_Callback(hObject, eventdata, handles)
% hObject handle to buttonFLAIR (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
valueFLAIR = 1;
% GET WORKING DIRECTORY
workDir = get(handles.edit3, 'String');
% Get MRI file name
[fileImage, pathImage] = uigetfile({'*.dcm;*.jpg;*.jpeg',...
'Image Files (*.dcm,*.jpg,*.jpeg)';
'*.dcm', 'DICOM (*.dcm)'; ...
'*.jpg;*.jpeg','JPEG image (*.jpg;*.jpeg)'; ...
'*.*', 'All Files (*.*)'}, ...
'Choose the FLAIR image', ...
workDir);
% READ IMAGE FILE
try
[~,~,ext] = fileparts(fileImage);
if(strcmp(ext,'.dcm'))
image = dicomread([pathImage, fileImage]);
else
image = imread([pathImage, fileImage]);
end
% SEGMENT THE FLAIR IMAGE (in a external function)
[imgCropped, maskCrop] = segmentMR_Block(image, 'FLAIR');
% ADD TO THE AXIS. IF IT IS NOT THE FIRST ONE, CONTROL THE SLIDER
% Store in the cell array inside axis1
data = guidata(handles.axes1);
%data.MRIImage{valueFLAIR} = image;
data.data.MRIImage{1,valueFLAIR} = image;
data.data.MRIImage{2,valueFLAIR} = imgCropped;
data.data.MRIImage{3,valueFLAIR} = maskCrop;
guidata(handles.axes1, data);
%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% SHOW IN THE AXIS %%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%
% Next, set your image display axes as the current axes:
axes(handles.axes1);
% Get axes1 tag
tag = get(handles.axes1, 'Tag');
% % Display image in axes1 using bicubic interpolation
hFLAIR = imshow(data.data.MRIImage{2,valueFLAIR},[]);
% resizePos = get(handles.axes1, 'Position');
% scaleRes = resizePos(3)/max(size(data.data.MRIImage{2,valueFLAIR})); % Axis is squared
% %imageRes = imresize(data.data.MRIImage{2,valueFLAIR}, [resizePos(4) resizePos(3)]);
% imageRes = imresize(data.data.MRIImage{2,valueFLAIR}, scaleRes, 'bicubic');
% hFLAIR = imshow(imageRes);
% Set again the properties lost in axes1
set(handles.axes1, 'Tag', tag);
%set(handles.axes1, 'ButtonDownFcn', @axes1_ButtonDownFcn);
set(hFLAIR, 'ButtonDownFcn', {@getClicksMRI, handles});
% ACTIVATE THE CORRESPONDING RADIOBUTTON
set(handles.rbFLAIR, 'Value', 1);
% IF THE IMAGE IS WELL READ, SET THE BUTTONS FOR THE OTHER
% MODALITIES TO VISIBLE
set(handles.buttonT1w, 'Visible', 'on');
set(handles.buttonT2w, 'Visible', 'on');
set(handles.buttonT2star, 'Visible', 'on');
catch
if(~isempty(fileImage))
errordlg('Choose a DICOM or a JPEG image', 'Error reading the image');
end
end
end
% *************************************************************************
% --- Executes on button press in buttonT1w.
function buttonT1w_Callback(hObject, eventdata, handles)
% hObject handle to buttonT1w (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
valueT1w = 2;
% GET WORKING DIRECTORY
workDir = get(handles.edit3, 'String');
% Get MRI file name
[fileImage, pathImage] = uigetfile({'*.dcm;*.jpg;*.jpeg',...
'Image Files (*.dcm,*.jpg,*.jpeg)';
'*.dcm', 'DICOM (*.dcm)'; ...
'*.jpg;*.jpeg','JPEG image (*.jpg;*.jpeg)'; ...
'*.*', 'All Files (*.*)'}, ...
'Choose the T1w image', ...
workDir);
% READ IMAGE FILE
try
[~,~,ext] = fileparts(fileImage);
if(strcmp(ext,'.dcm'))
image = dicomread([pathImage, fileImage]);
else
image = imread([pathImage, fileImage]);
end
% ADD TO THE AXIS. IF IT IS NOT THE FIRST ONE, CONTROL THE SLIDER
% Store in the cell array inside axis1
data = guidata(handles.axes1);
% Get the mask from the cell array inside axis1
mask = data.data.MRIImage{3,1};
% Segment the T1W image using the mask (in a external function)
[imgCropped, maskCrop] = segmentMR_Block(image, 'T1W', mask);
% Save images
data.data.MRIImage{1, valueT1w} = image;
data.data.MRIImage{2, valueT1w} = imgCropped;
data.data.MRIImage{3, valueT1w} = maskCrop;
guidata(handles.axes1, data);
%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% SHOW IN THE AXIS %%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%
% Next, set your image display axes as the current axes:
axes(handles.axes1);
% Get axes1 tag
tag = get(handles.axes1, 'Tag');
% Display image in axes1 using bicubic interpolation
hT1w = imshow(data.data.MRIImage{2,valueT1w},[]);
% resizePos = get(handles.axes1, 'Position');
% scaleRes = resizePos(3)/max(size(data.data.MRIImage{2,valueT1w})); % Axis is squared
% %imageRes = imresize(data.data.MRIImage{2,valueFLAIR}, [resizePos(4) resizePos(3)]);
% imageRes = imresize(data.data.MRIImage{2,valueT1w}, scaleRes, 'bicubic');
% hT1w = imshow(imageRes);
% CHECK IF THERE ARE POINTS ALREADY SELECTED, AND SHOW THEM IF
% NECESSARY
selPointsMRI = data.data.tableCoordinatesMRI;
dataTableMRI = get(handles.uitablePointsMRI, 'Data'); % Gets current data of the table
numPointsMRI = size(dataTableMRI,1);
if(numPointsMRI>0)
for nP=1:size(selPointsMRI,1)
coordinates = [selPointsMRI(nP,2), selPointsMRI(nP,1)]; %(x,y)
% Put a marker on the point
hPoint = impoint(handles.axes1, coordinates);
set(hPoint, 'UserData', nP);
% Construct boundary constraint function
fcn = makeConstrainToRectFcn('impoint', get(gca,'XLim'), get(gca,'YLim'));
% Enforce boundary constraint function using setPositionConstraintFcn
setPositionConstraintFcn(hPoint, fcn);
setColor(hPoint, 'r');
addNewPositionCallback(hPoint, @pointDragMRI);
%setString(hPoint, num2str(k));
hTxt = text(coordinates(1), coordinates(2), sprintf('%d',nP), ...
'Color', 'r', 'FontSize',8, ...
'HorizontalAlignment','left', 'VerticalAlignment','top');
end
end
% Set again the properties lost in axes1
set(handles.axes1, 'Tag', tag);
%set(handles.axes1, 'ButtonDownFcn', @axes1_ButtonDownFcn);
set(hT1w, 'ButtonDownFcn', {@getClicksMRI, handles});
% ACTIVATE THE CORRESPONDING RADIOBUTTON
set(handles.rbT1w, 'Value', 1);
% IF THE IMAGE IS WELL READ, MAKE THIS BUTTON INVISIBLE
%set(handles.buttonT1w, 'Visible', 'off');
catch
if(~isempty(fileImage))
errordlg('Choose a DICOM or a JPEG image', 'Error reading the image');
end
end
end
% *************************************************************************
% --- Executes on button press in buttonT2w.
function buttonT2w_Callback(hObject, eventdata, handles)
% hObject handle to buttonT2w (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
valueT2w = 3;
% GET WORKING DIRECTORY
workDir = get(handles.edit3, 'String');
% Get MRI file name
[fileImage, pathImage] = uigetfile({'*.dcm;*.jpg;*.jpeg',...
'Image Files (*.dcm,*.jpg,*.jpeg)';
'*.dcm', 'DICOM (*.dcm)'; ...
'*.jpg;*.jpeg','JPEG image (*.jpg;*.jpeg)'; ...
'*.*', 'All Files (*.*)'}, ...
'Choose the T2w image', ...
workDir);
% READ IMAGE FILE
try
[~,~,ext] = fileparts(fileImage);
if(strcmp(ext,'.dcm'))
image = dicomread([pathImage, fileImage]);
else
image = imread([pathImage, fileImage]);
end
% ADD TO THE AXIS. IF IT IS NOT THE FIRST ONE, CONTROL THE SLIDER
% Store in the cell array inside axis1
data = guidata(handles.axes1);
% Get the mask from the cell array inside axis1
mask = data.data.MRIImage{3,1};
% Segment the T1W image using the mask (in a external function)
[imgCropped, maskCrop] = segmentMR_Block(image, 'T2W', mask);
% Save images
data.data.MRIImage{1, valueT2w} = image;
data.data.MRIImage{2, valueT2w} = imgCropped;
data.data.MRIImage{3, valueT2w} = maskCrop;
guidata(handles.axes1, data);
%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% SHOW IN THE AXIS %%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%
% Next, set your image display axes as the current axes:
axes(handles.axes1);
% Get axes1 tag
tag = get(handles.axes1, 'Tag');
% Display image in axes1 using bicubic interpolation
hT2w = imshow(data.data.MRIImage{2,valueT2w},[]);
% resizePos = get(handles.axes1, 'Position');
% scaleRes = resizePos(3)/max(size(data.data.MRIImage{2,valueT2w})); % Axis is squared
% %imageRes = imresize(data.data.MRIImage{2,valueFLAIR}, [resizePos(4) resizePos(3)]);
% imageRes = imresize(data.data.MRIImage{2,valueT2w}, scaleRes, 'bicubic');
% hT2w = imshow(imageRes);
% CHECK IF THERE ARE POINTS ALREADY SELECTED, AND SHOW THEM IF
% NECESSARY
selPointsMRI = data.data.tableCoordinatesMRI;
dataTableMRI = get(handles.uitablePointsMRI, 'Data'); % Gets current data of the table
numPointsMRI = size(dataTableMRI,1);
if(numPointsMRI>0)
for nP=1:size(selPointsMRI,1)
coordinates = [selPointsMRI(nP,2), selPointsMRI(nP,1)]; %(x,y)
% Put a marker on the point
hPoint = impoint(handles.axes1, coordinates);
set(hPoint, 'UserData', nP);
% Construct boundary constraint function
fcn = makeConstrainToRectFcn('impoint', get(gca,'XLim'), get(gca,'YLim'));
% Enforce boundary constraint function using setPositionConstraintFcn
setPositionConstraintFcn(hPoint, fcn);
setColor(hPoint, 'r');
addNewPositionCallback(hPoint, @pointDragMRI);
%setString(hPoint, num2str(k));
hTxt = text(coordinates(1), coordinates(2), sprintf('%d',nP), ...
'Color', 'r', 'FontSize',8, ...
'HorizontalAlignment','left', 'VerticalAlignment','top');
end
end
% Set again the properties lost in axes1
set(handles.axes1, 'Tag', tag);
%set(handles.axes1, 'ButtonDownFcn', @axes1_ButtonDownFcn);
set(hT2w, 'ButtonDownFcn', {@getClicksMRI, handles});
% ACTIVATE THE CORRESPONDING RADIOBUTTON
set(handles.rbT2w, 'Value', 1);
% IF THE IMAGE IS WELL READ, MAKE THIS BUTTON INVISIBLE
%set(handles.buttonT2w, 'Visible', 'off');
catch
if(~isempty(fileImage))
errordlg('Choose a DICOM or a JPEG image', 'Error reading the image');
end
end
end
% *************************************************************************
% --- Executes on button press in buttonT2star.
function buttonT2star_Callback(hObject, eventdata, handles)
% hObject handle to buttonT2star (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
valueT2star = 4;
% GET WORKING DIRECTORY
workDir = get(handles.edit3, 'String');
% Get MRI file name
[fileImage, pathImage] = uigetfile({'*.dcm;*.jpg;*.jpeg',...
'Image Files (*.dcm,*.jpg,*.jpeg)';
'*.dcm', 'DICOM (*.dcm)'; ...
'*.jpg;*.jpeg','JPEG image (*.jpg;*.jpeg)'; ...
'*.*', 'All Files (*.*)'}, ...
'Choose the T2star image', ...
workDir);
% READ IMAGE FILE
try
[~,~,ext] = fileparts(fileImage);
if(strcmp(ext,'.dcm'))
image = dicomread([pathImage, fileImage]);
else
image = imread([pathImage, fileImage]);
end
% ADD TO THE AXIS. IF IT IS NOT THE FIRST ONE, CONTROL THE SLIDER
% Store in the cell array inside axis1
data = guidata(handles.axes1);
% Get the mask from the cell array inside axis1
mask = data.data.MRIImage{3,1};
% Segment the T1W image using the mask (in a external function)
[imgCropped, maskCrop] = segmentMR_Block(image, 'T2star', mask);
% Save images
data.data.MRIImage{1, valueT2star} = image;
data.data.MRIImage{2, valueT2star} = imgCropped;
data.data.MRIImage{3, valueT2star} = maskCrop;
guidata(handles.axes1, data);
%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% SHOW IN THE AXIS %%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%
% Next, set your image display axes as the current axes:
axes(handles.axes1);
% Get axes1 tag
tag = get(handles.axes1, 'Tag');
% Display image in axes1
hT2star = imshow(data.data.MRIImage{2,valueT2star},[]);
% resizePos = get(handles.axes1, 'Position');
% scaleRes = resizePos(3)/max(size(data.data.MRIImage{2,valueT2star})); % Axis is squared
% %imageRes = imresize(data.data.MRIImage{2,valueFLAIR}, [resizePos(4) resizePos(3)]);
% imageRes = imresize(data.data.MRIImage{2,valueT2star}, scaleRes, 'bicubic');
% hT2star = imshow(imageRes);
% CHECK IF THERE ARE POINTS ALREADY SELECTED, AND SHOW THEM IF
% NECESSARY
selPointsMRI = data.data.tableCoordinatesMRI;
dataTableMRI = get(handles.uitablePointsMRI, 'Data'); % Gets current data of the table
numPointsMRI = size(dataTableMRI,1);
if(numPointsMRI>0)
for nP=1:size(selPointsMRI,1)
coordinates = [selPointsMRI(nP,2), selPointsMRI(nP,1)]; %(x,y)
% Put a marker on the point
hPoint = impoint(handles.axes1, coordinates);
set(hPoint, 'UserData', nP);
% Construct boundary constraint function
fcn = makeConstrainToRectFcn('impoint', get(gca,'XLim'), get(gca,'YLim'));
% Enforce boundary constraint function using setPositionConstraintFcn
setPositionConstraintFcn(hPoint, fcn);
setColor(hPoint, 'r');
addNewPositionCallback(hPoint, @pointDragMRI);
%setString(hPoint, num2str(k));
hTxt = text(coordinates(1), coordinates(2), sprintf('%d',nP), ...
'Color', 'r', 'FontSize',8, ...
'HorizontalAlignment','left', 'VerticalAlignment','top');
end
end
% Set again the properties lost in axes1
set(handles.axes1, 'Tag', tag);
%set(handles.axes1, 'ButtonDownFcn', @axes1_ButtonDownFcn);
set(hT2star, 'ButtonDownFcn', {@getClicksMRI, handles});
% ACTIVATE THE CORRESPONDING RADIOBUTTON
set(handles.rbT2star, 'Value', 1);
% IF THE IMAGE IS WELL READ, MAKE THIS BUTTON INVISIBLE
%set(handles.buttonT2star, 'Visible', 'off');
catch
if(~isempty(fileImage))
errordlg('Choose a DICOM or a JPEG image', 'Error reading the image');
end
end
end
% *************************************************************************
% --- Executes on button press in pushbuttonHE.
function pushbuttonHE_Callback(hObject, eventdata, handles)
% hObject handle to pushbuttonHE (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
valueHistology = 1;
% GET WORKING DIRECTORY
workDir = get(handles.edit3, 'String');
% Get MRI file name
[fileImage, pathImage] = uigetfile({'*.jpg;*.jpeg;*.png',...
'Image Files (*.jpg,*.jpeg,*.png)';
'*.jpg;*.jpeg','JPEG image (*.jpg;*.jpeg)'; ...
'*.png', 'Portable Network Graphics (*.png)'; ...
'*.*', 'All Files (*.*)'}, ...
'Choose the H&E histology image', ...
workDir);
try
% Load image
imageHistology = imread([pathImage, fileImage]);
% % Resize image (TEST: Set scale to 0.25 (i.e. 1/4 of the original
% % size))
%imageHistologyResize = imresize(imageHistology, 0.25);
% Resize image (TEST: Set scale to 0.5 (i.e. 1/2 of the original
% size))
imageHistologyResize = imresize(imageHistology, 0.5);
sizeHistol = size(imageHistology);
clear imageHistology;
% ADD TO THE AXIS. IF IT IS NOT THE FIRST ONE, CONTROL THE SLIDER
% Store in the cell array inside axis1
data = guidata(handles.axes1);
%data.histologyImage{valueHistology} = imageHistology;
data.data.histologyImage{valueHistology,1} = imageHistologyResize;
% Store also path of the original histology image
data.data.histologyImage{valueHistology, 2} = [pathImage, fileImage];
data.data.histologyImage{valueHistology, 3} = sizeHistol;
guidata(handles.axes1, data);
%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% SHOW IN THE AXIS %%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%
% Next, set your image display axes as the current axes:
axes(handles.axes2);
% Get axes2 tag
tag = get(handles.axes2, 'Tag');
% Display image in axes1, using bicubic interpolation
hHistology = imshow(data.data.histologyImage{valueHistology},[]);
% resizePos = get(handles.axes2, 'Position');
% scaleRes = resizePos(3)/max(size(data.data.histologyImage{valueHistology})); % Axis is squared
% %imageRes = imresize(data.data.MRIImage{2,valueFLAIR}, [resizePos(4) resizePos(3)]);
% imageRes = imresize(data.data.histologyImage{valueHistology}, scaleRes, 'bicubic');
% hHistology = imshow(imageRes);
% Set again the properties lost in axes1
set(handles.axes2, 'Tag', tag);
%set(handles.axes1, 'ButtonDownFcn', @axes1_ButtonDownFcn);
set(hHistology, 'ButtonDownFcn', {@getClicksHistology, handles});
% ACTIVATE THE CORRESPONDING RADIOBUTTON
set(handles.rbHE, 'Value', 1);
% Change text above the histology image panel, indicating the user
% to select a few points (needed to make an affine registration to
% the image)
set(handles.text5, 'String', 'Select 4-5 points in both images');
% IF THE IMAGE IS WELL READ, MAKE THIS BUTTON INVISIBLE
% set(handles.buttonMTIse02, 'Visible', 'off');
set(handles.pushbuttonAffRegistration, 'Enable', 'on');
set(handles.pushbuttonRegistration, 'Enable', 'off');
set(handles.radiobutton18, 'Enable', 'on');
set(handles.radiobutton19, 'Enable', 'on');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% DELETE ALL POINTS FROM TABLES AND AXES, IF ANY %%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 1 - DELETE FROM TABLES IN AXES
% Get the table with the points from handles.axis1
dataHandles = guidata(handles.axes1);
% Copy an empty matrix into dataHandles.data.tableCoordinatesMRI
% and dataHandles.data.tableCoordinatesHistol
dataHandles.data.tableCoordinatesMRI = [];
dataHandles.data.tableCoordinatesHistol = [];
% Save the points into handles.axis1
guidata(handles.axes1, dataHandles);
% 2 - REMOVE FROM GUI TABLES
set(handles.uitablePointsMRI, 'Data', []);
set(handles.uitablePointsHistol, 'Data', []);
% 3 - DELETE FROM IMAGES AXES
% If there are po THERE ARE POINTS IN THE IMAGE, REMOVE THEM AND PAINT THEM AGAIN
% Axes1
axes(handles.axes1);
axes1_chil = get(handles.axes1, 'Children');
class_axes1 = arrayfun(@class, axes1_chil, 'UniformOutput', false);
isImage = strcmpi('matlab.graphics.primitive.Image', class_axes1);
delete(axes1_chil(~isImage));
% Axes2
axes(handles.axes2);
axes2_chil = get(handles.axes2, 'Children');
class_axes2 = arrayfun(@class, axes2_chil, 'UniformOutput', false);
isImage = strcmpi('matlab.graphics.primitive.Image', class_axes2);
delete(axes2_chil(~isImage));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
catch
errordlg('Choose a JPEG image', 'Error reading the image');
end
end
% *************************************************************************
% --- Executes on button press in pushbuttonSavePoints.
function pushbuttonSavePoints_Callback(hObject, eventdata, handles)
% hObject handle to pushbuttonSavePoints (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% GET WORKING DIRECTORY
workDir = get(handles.edit3, 'String');
% Get the table with the points
dataHandles = guidata(handles.axes1);
pointsMRI = dataHandles.data.tableCoordinatesMRI;
pointsHistol = dataHandles.data.tableCoordinatesHistol;
% Select file name to save the points
uisave({'pointsMRI','pointsHistol'}, fullfile(workDir, 'Points.mat'));
end
% *************************************************************************
% --- Executes on button press in pushbuttonLoadPoints.
function pushbuttonLoadPoints_Callback(hObject, eventdata, handles)
% hObject handle to pushbuttonLoadPoints (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% % Disable the cellselectioncallback of the uitables
% cellSelectionCallbackMRIAux = get(handles.uitablePointsMRI, 'CellSelectionCallback');
% cellSelectionCallbackHistAux = get(handles.uitablePointsHistol, 'CellSelectionCallback');
% set(handles.uitablePointsMRI, 'CellSelectionCallback', '');
% set(handles.uitablePointsHistol, 'CellSelectionCallback', '');
% GET WORKING DIRECTORY
workDir = get(handles.edit3, 'String');
% GET THE MAT FILE WITH THE POINTS AND LOAD IT
[filePoints, pathPoints] = uigetfile({'*.mat', 'Mat Files (*.mat)';
fullfile(workDir,'*.*'), 'All Files (*.*)'}, ...
'Select a file to save the points', ...
workDir);
if(filePoints==0)
errordlg('File not found','File Error');
return;
end
load([pathPoints, filePoints]);
% CHECK IF THE VARIABLES 'pointsMRI' AND 'pointsHistol' EXIST IN THE
% WORKSPACE. IF THEY DONT, THE FILE HAS NOT BEEN WELL LOADED
if(~(exist('pointsMRI','var') && exist('pointsHistol','var')))
errordlg('This file does not contain points', 'File Error');
return;
end
% GET THE TABLE WITH THE POINTS
dataHandles = guidata(handles.axes1);
% CHECK IF ANY MRI IMAGE HAS BEEN LOADED
indexesMRI = find(~cellfun(@isempty,dataHandles.data.MRIImage(:)));
if(isempty(indexesMRI) || isempty(dataHandles.data.histologyImage{1}))
errordlg('You must load the MRI and the Histology image before loading the points', 'Error images loaded');
return;
end
% COPY THE POINTS INTO THE VARIABLES dataHandles.data.tableCoordinatesMRI
% AND dataHandles.data.tableCoordinatesHistol
dataHandles.data.tableCoordinatesMRI = pointsMRI;
dataHandles.data.tableCoordinatesHistol = pointsHistol;
% SAVE THE POINTS INTO THE AXIS
guidata(handles.axes1, dataHandles);
% COPY THE POINTS INTO THE GUI TABLES:
newDataTableMRI = num2cell(round(pointsMRI));
newDataTableHistol = num2cell(round(pointsHistol));
set(handles.uitablePointsMRI, 'Data', newDataTableMRI);
set(handles.uitablePointsHistol, 'Data', newDataTableHistol);
% IF THERE ARE POINTS IN THE IMAGE, REMOVE THEM AND PAINT THEM AGAIN
% Axes1
axes(handles.axes1);
axes1_chil = get(handles.axes1, 'Children');
class_axes1 = arrayfun(@class, axes1_chil, 'UniformOutput', false);
isImage = strcmpi('matlab.graphics.primitive.Image', class_axes1);
delete(axes1_chil(~isImage));
for nP_MRI=1:size(pointsMRI,1)
coord_MRI = [pointsMRI(nP_MRI,2), pointsMRI(nP_MRI,1)]; %(x,y)
% Put a marker on the point
hPoint = impoint(handles.axes1, coord_MRI);
set(hPoint, 'UserData', nP_MRI);
% Construct boundary constraint function
fcn = makeConstrainToRectFcn('impoint', get(gca,'XLim'), get(gca,'YLim'));
% Enforce boundary constraint function using setPositionConstraintFcn
setPositionConstraintFcn(hPoint, fcn);
setColor(hPoint, 'r');
addNewPositionCallback(hPoint, @pointDragMRI);
%setString(hPoint, num2str(k));
hTxt = text(coord_MRI(1), coord_MRI(2), sprintf('%d',nP_MRI), ...
'Color', 'r', 'FontSize',8, ...
'HorizontalAlignment','left', 'VerticalAlignment','top');
end
% Axes2
axes(handles.axes2);
axes2_chil = get(handles.axes2, 'Children');
class_axes2 = arrayfun(@class, axes2_chil, 'UniformOutput', false);
isImage = strcmpi('matlab.graphics.primitive.Image', class_axes2);
delete(axes2_chil(~isImage));
for nP_Hist=1:size(pointsHistol,1)
coord_Hist = [pointsHistol(nP_Hist,2), pointsHistol(nP_Hist,1)]; %(x,y)
% Put a marker on the point
hPoint = impoint(handles.axes2, coord_Hist);
set(hPoint, 'UserData', nP_Hist);
% Construct boundary constraint function
fcn = makeConstrainToRectFcn('impoint', get(gca,'XLim'), get(gca,'YLim'));
% Enforce boundary constraint function using setPositionConstraintFcn
setPositionConstraintFcn(hPoint, fcn);
setColor(hPoint, 'b');
addNewPositionCallback(hPoint, @pointDragHistol);
%setString(hPoint, num2str(k));
hTxt = text(coord_Hist(1), coord_Hist(2), sprintf('%d',nP_Hist), ...
'Color', 'b', 'FontSize',8, ...
'HorizontalAlignment','left', 'VerticalAlignment','top');
end
% % Enable again the cellselectioncallback of the uitables
% set(handles.uitablePointsMRI, 'CellSelectionCallback', cellSelectionCallbackMRIAux);
% set(handles.uitablePointsHistol, 'CellSelectionCallback', cellSelectionCallbackHistAux)
end
% *************************************************************************
% --- Executes on selection change in listboxPointsMRI.
function listboxPointsMRI_Callback(hObject, eventdata, handles)
% hObject handle to listboxPointsMRI (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns listboxPointsMRI contents as cell array
% contents{get(hObject,'Value')} returns selected item from listboxPointsMRI
end
% *************************************************************************
% --- Executes on selection change in listboxPointsHistol.
function listboxPointsHistol_Callback(hObject, eventdata, handles)
% hObject handle to listboxPointsHistol (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns listboxPointsHistol contents as cell array
% contents{get(hObject,'Value')} returns selected item from listboxPointsHistol
end
% *************************************************************************
% --- Executes when selected object is changed in uibuttongroup1.
function uibuttongroup1_SelectionChangedFcn(hObject, eventdata, handles)
% hObject handle to the selected object in uibuttongroup1
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%tagNewVal = get(eventdata.NewValue, 'Tag'); % Get Tag of selected object.
switch get(eventdata.NewValue, 'Tag') % Get Tag of selected object.
case 'rbFLAIR'
newValMRI = 1;
newModality = 'FLAIR';
case 'rbT1w'
newValMRI = 2;
newModality = 'T1w';
case 'rbT2w'
newValMRI = 3;
newModality = 'T2w';
case 'rbT2star'
newValMRI = 4;
newModality = 'T2star';
end
% Loaded MRI images
dataHandles = guidata(handles.axes1);
% Look if the modality marked by newVal has already been loaded
%if(exist('data.MRIImage','var'))
indexesMRI = find(~cellfun(@isempty,dataHandles.data.MRIImage(:)));
%else
% indexesMRI = 0;
%end
if(sum(indexesMRI==newValMRI)~=0) % Has been loaded
%Show in the axis
% Next, set your image display axes as the current axes:
axes(handles.axes1);
% Get axes1 tag
tag = get(handles.axes1, 'Tag');
% Finally, display the image using bicubic interpolation
hMRI = imshow(dataHandles.data.MRIImage{2,newValMRI},[]);
% resizePos = get(handles.axes1, 'Position');
% scaleRes = resizePos(3)/max(size(dataHandles.data.MRIImage{2,newValMRI})); % Axis is squared
% %imageRes = imresize(data.data.MRIImage{2,valueFLAIR}, [resizePos(4) resizePos(3)]);
% imageRes = imresize(dataHandles.data.MRIImage{2,newValMRI}, scaleRes, 'bicubic');
% hMRI = imshow(imageRes);
% CHECK IF THERE ARE POINTS ALREADY SELECTED, AND SHOW THEM IF
% NECESSARY
selPointsMRI = dataHandles.data.tableCoordinatesMRI;
dataTableMRI = get(handles.uitablePointsMRI, 'Data'); % Gets current data of the table
numPointsMRI = size(dataTableMRI,1);
if(numPointsMRI>0)
for nP=1:size(selPointsMRI,1)
coordinates = [selPointsMRI(nP,2), selPointsMRI(nP,1)]; %(x,y)
% Put a marker on the point
hPoint = impoint(handles.axes1, coordinates);
set(hPoint, 'UserData', nP);
% Construct boundary constraint function
fcn = makeConstrainToRectFcn('impoint', get(gca,'XLim'), get(gca,'YLim'));
% Enforce boundary constraint function using setPositionConstraintFcn
setPositionConstraintFcn(hPoint, fcn);
setColor(hPoint, 'r');
addNewPositionCallback(hPoint, @pointDragMRI);
%setString(hPoint, num2str(k));
hTxt = text(coordinates(1), coordinates(2), sprintf('%d',nP), ...
'Color', 'r', 'FontSize',8, ...
'HorizontalAlignment','left', 'VerticalAlignment','top');
end
end
% Set again the properties lost in axes1
set(handles.axes1, 'Tag', tag);
%set(handles.axes1, 'ButtonDownFcn', @axes1_ButtonDownFcn);
set(hMRI, 'ButtonDownFcn', {@getClicksMRI, handles});
else % Has not been loaded
switch get(eventdata.OldValue, 'Tag') % Get Tag of previously selected object
case 'rbFLAIR'
set(handles.rbFLAIR, 'Value', 1);
case 'rbT1w'
set(handles.rbT1w, 'Value', 1);
case 'rbT2w'
set(handles.rbT2w, 'Value', 1);
case 'rbT2star'
set(handles.rbT2star, 'Value', 1);
end
errordlg(['The modality ', newModality, ' has not been loaded'], 'Modality not loaded');
end
end
% *************************************************************************
% --- Executes on mouse press over MRI axes background.
%function getClicks(objectHandle, eventData)
function getClicksMRI(objectHandle, ~, hGlobal)
% objectHandle: Handle to the image itself
% The second input argument is eventData, but it is not used in this case
axes1Handles = get(objectHandle, 'Parent');
%uiTableMRIHandles = findobj('Tag', 'uitablePointsMRI');
dataHandles = guidata(axes1Handles);
if(~isempty(dataHandles.data.MRIImage{1,1}))
coordsAux = get(axes1Handles, 'CurrentPoint');
coordinates = coordsAux(1,1:2);
% disp(['You clicked in point of the MRI axes: (', num2str(coordinates(2)), ',', num2str(coordinates(1)),')']);
% Add the coordinates to the uitablePointsMRI
%dataTableMRI = get(uiTableMRIHandles, 'Data'); % Gets current data of the table
dataTableMRI = get(hGlobal.uitablePointsMRI, 'Data'); % Gets current data of the table
k = size(dataTableMRI,1)+1; % Row number of the new row of the data
if(k==1) % It is the first point to insert in the data
newDataTableMRI = num2cell(dataTableMRI);
dataHandles.data.tableCoordinatesMRI(k,:) = [coordinates(2), coordinates(1)];
else
newDataTableMRI = dataTableMRI;
dataHandles.data.tableCoordinatesMRI = [dataHandles.data.tableCoordinatesMRI; [coordinates(2), coordinates(1)]];
end
newDataTableMRI{k,1} = round(coordinates(2)); % Row of the point
newDataTableMRI{k,2} = round(coordinates(1)); % Column of the point
% Set the data into the table
%set(uiTableMRIHandles, 'Data', newDataTableMRI);
set(hGlobal.uitablePointsMRI, 'Data', newDataTableMRI);
guidata(axes1Handles, dataHandles);
% Put a marker on the point
hPoint = impoint(axes1Handles, coordinates);
set(hPoint, 'UserData', k);
% Construct boundary constraint function