0001 function varargout = matRad_exportGUI(varargin)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041 gui_Singleton = 1;
0042 gui_State = struct('gui_Name', mfilename, ...
0043 'gui_Singleton', gui_Singleton, ...
0044 'gui_OpeningFcn', @matRad_exportGUI_OpeningFcn, ...
0045 'gui_OutputFcn', @matRad_exportGUI_OutputFcn, ...
0046 'gui_LayoutFcn', [] , ...
0047 'gui_Callback', []);
0048 if nargin && ischar(varargin{1})
0049 gui_State.gui_Callback = str2func(varargin{1});
0050 end
0051
0052 if nargout
0053 [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
0054 else
0055 gui_mainfcn(gui_State, varargin{:});
0056 end
0057
0058
0059
0060 function matRad_exportGUI_OpeningFcn(hObject, eventdata, handles, varargin)
0061
0062
0063
0064
0065
0066
0067
0068 handles.output = hObject;
0069
0070
0071 if evalin('base','exist(''cst'',''var'')') == 1
0072 cst = evalin( 'base', 'cst' );
0073 tableData = cell(numel(cst(:,2)),2);
0074 tableData(:,2) = cst(:,2);
0075 tableData(:,1) = {true};
0076 else
0077 tableData = cell(0);
0078 set(handles.checkbox_CT,'Enable','off');
0079 end
0080 set(handles.uitable_vois,'data',tableData);
0081
0082
0083 if evalin('base','exist(''resultGUI'',''var'')')
0084 result = evalin( 'base', 'resultGUI' );
0085 cubeNames = fieldnames(result);
0086 cubeIx = 1;
0087 for f = 1:numel(cubeNames)
0088 if ndims(result.(cubeNames{f})) < 3
0089 continue;
0090 end
0091 cubes{cubeIx} = cubeNames{f};
0092 cubeIx = cubeIx + 1;
0093 end
0094 numCubes = cubeIx - 1;
0095 tableData = cell(numCubes,2);
0096 tableData(:,2) = cubes;
0097 tableData(:,1) = {true};
0098 else
0099 tableData = cell(0);
0100 set(handles.checkbox_dose,'Enable','off');
0101 end
0102 set(handles.uitable_doseCubes,'data',tableData);
0103
0104
0105
0106 guidata(hObject, handles);
0107
0108 initialize_gui(hObject, handles, false);
0109
0110
0111
0112
0113
0114
0115 function varargout = matRad_exportGUI_OutputFcn(hObject, eventdata, handles)
0116
0117
0118
0119
0120
0121
0122 varargout{1} = handles.output;
0123
0124
0125 function initialize_gui(fig_handle, handles, isreset)
0126
0127
0128
0129 if isfield(handles, 'metricdata') && ~isreset
0130 return;
0131 end
0132
0133
0134 handles.metricdata.density = 0;
0135 handles.metricdata.volume = 0;
0136
0137 set(handles.density, 'String', handles.metricdata.density);
0138 set(handles.volume, 'String', handles.metricdata.volume);
0139 set(handles.mass, 'String', 0);
0140
0141 set(handles.unitgroup, 'SelectedObject', handles.english);
0142
0143 set(handles.text4, 'String', 'lb/cu.in');
0144 set(handles.text5, 'String', 'cu.in');
0145 set(handles.text6, 'String', 'lb');
0146
0147
0148 guidata(handles.figure1, handles);
0149
0150
0151
0152 function checkbox_CT_Callback(hObject, eventdata, handles)
0153
0154
0155
0156
0157 saveCT = get(hObject,'Value');
0158
0159
0160 if (saveCT)
0161 set(handles.uitable_vois,'Visible', 'on', 'Enable','on');
0162 else
0163 set(handles.uitable_vois,'Visible', 'off', 'Enable','off');
0164 end
0165
0166
0167
0168 function uitable_vois_Callback(hObject, eventdata, handles)
0169
0170
0171
0172
0173
0174
0175
0176
0177
0178 function uitable_vois_CreateFcn(hObject, eventdata, handles)
0179
0180
0181
0182
0183
0184
0185 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
0186 set(hObject,'BackgroundColor','white');
0187 end
0188
0189
0190
0191 function checkbox_dose_Callback(hObject, eventdata, handles)
0192
0193
0194
0195
0196
0197
0198 saveDose = get(hObject,'Value');
0199 if (saveDose)
0200 set(handles.uitable_doseCubes,'Visible', 'on', 'Enable','on');
0201
0202 else
0203 set(handles.uitable_doseCubes,'Visible', 'off', 'Enable','off');
0204
0205 end
0206
0207
0208
0209 function uitable_doseCubes_Callback(hObject, eventdata, handles)
0210
0211
0212
0213
0214
0215
0216
0217
0218
0219 function listbox_dose_CreateFcn(hObject, eventdata, handles)
0220
0221
0222
0223
0224
0225
0226 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
0227 set(hObject,'BackgroundColor','white');
0228 end
0229
0230
0231
0232 function exportDir = pushbutton_dir_export_browse_Callback(hObject, eventdata, handles)
0233
0234
0235
0236
0237 exportDir = uigetdir('', 'Choose the export directory...');
0238 if exportDir ~= 0
0239 exportDir = [exportDir filesep];
0240 set(handles.edit_dir_export,'String',exportDir);
0241
0242 guidata(hObject, handles);
0243 end
0244
0245
0246 function exportDir = edit_dir_export_Callback(hObject, eventdata, handles)
0247
0248
0249
0250
0251
0252
0253
0254 exportDir = get(handles.edit_dir_export,'String');
0255
0256
0257 if exportDir(end) ~= filesep;
0258 exportDir = [exportDir filesep];
0259 end
0260
0261
0262 if ~exist(exportDir,'dir')
0263 warndlg(['Folder ' exportDir ' does not exist!']);
0264 exportDir = '';
0265 end
0266 set(handles.edit_dir_export,'String',exportDir);
0267 guidata(hObject, handles);
0268
0269
0270 function edit_dir_export_CreateFcn(hObject, eventdata, handles)
0271
0272
0273
0274
0275
0276
0277 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
0278 set(hObject,'BackgroundColor','white');
0279
0280
0281
0282 end
0283
0284
0285
0286 function popupmenu_extension_Callback(hObject, eventdata, handles)
0287
0288
0289
0290
0291
0292
0293
0294
0295
0296 function popupmenu_extension_CreateFcn(hObject, eventdata, handles)
0297
0298
0299
0300
0301
0302 extensions{1} = '*.nrrd';
0303 extensions{2} = '*.vtk';
0304 extensions{3} = '*.mha';
0305 set(hObject,'String',extensions);
0306
0307
0308
0309 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
0310 set(hObject,'BackgroundColor','white');
0311 end
0312
0313
0314
0315 function btn_export_Callback(hObject, eventdata, handles)
0316
0317
0318
0319
0320
0321 exportDir = get(handles.edit_dir_export,'String');
0322
0323
0324 if numel(exportDir) == 0
0325 errordlg('No Export folder selected!');
0326 return;
0327 elseif ~exist(exportDir,'dir')
0328 errordlg(['Folder ' exportDir ' does not exist!']);
0329 return;
0330 else
0331
0332 if exportDir(end) ~= filesep;
0333 exportDir = [exportDir filesep];
0334 end
0335 end
0336
0337
0338 extensionIndex = get(handles.popupmenu_extension,'Value');
0339 extensions = get(handles.popupmenu_extension,'String');
0340 extension = extensions{extensionIndex};
0341 extension = extension(2:end);
0342
0343 saveCT = get(handles.checkbox_CT,'Value');
0344 saveResults = get(handles.checkbox_dose,'Value');
0345
0346
0347
0348 if (saveCT)
0349 voiDir = [exportDir '/vois/'];
0350 if ~exist(voiDir,'dir')
0351 if ~mkdir(voiDir)
0352 warndlg('Could not create subfolder for VOI masks. Masks will be stored in base folder.');
0353 voiDir = exportDir;
0354 end
0355 end
0356 end
0357
0358 if (saveResults)
0359 resultDir = [exportDir '/results/'];
0360 if ~exist(resultDir,'dir')
0361 if ~mkdir(resultDir)
0362 warndlg('Could not create subfolder for resulting dose cubes. Cubes will be stored in base folder.');
0363 resultDir = exportDir;
0364 end
0365 end
0366 end
0367
0368
0369 ct = evalin('base','ct');
0370
0371 metadata.resolution = [ct.resolution.x ct.resolution.y ct.resolution.z];
0372 metadata.compress = get(handles.checkbox_compress,'Value');
0373
0374
0375 if isfield(ct,'dicomInfo')
0376 if isfield(ct.dicomInfo,'ImagePositionPatient')
0377 metadata.imageOrigin = ct.dicomInfo.ImagePositionPatient;
0378 if ~isrow(metadata.imageOrigin)
0379 metadata.imageOrigin = transpose(metadata.imageOrigin);
0380 end
0381 end
0382 end
0383
0384
0385 numExportCubes = 0;
0386 if (saveCT)
0387 if isfield(ct,'cubeHU')
0388 numExportCubes = numExportCubes + 1;
0389 end
0390
0391 if isfield(ct,'cube')
0392 numExportCubes = numExportCubes + 1;
0393 end
0394 voiNames = get(handles.uitable_vois,'Data');
0395 voiIndices = find([voiNames{:,1}] == true);
0396 numExportCubes = numExportCubes + numel(voiIndices);
0397
0398 else
0399 numExportCubes = 0;
0400 end
0401
0402 if saveResults
0403 cubeNames = get(handles.uitable_doseCubes,'data');
0404 cubeIndices = find([cubeNames{:,1}] == true);
0405 numExportCubes = numExportCubes + numel(cubeIndices);
0406 end
0407
0408
0409 if numExportCubes == 0
0410 errordlg('No data was selected for export!');
0411 return;
0412 end
0413
0414 currentCube = 0;
0415
0416 hWaitbar = waitbar(0,'Exporting...','WindowStyle', 'modal');
0417 cleanUp = onCleanup(@() close(hWaitbar));
0418
0419
0420 if saveCT
0421
0422 if isfield(ct,'cube')
0423
0424 currentCube = currentCube + 1;
0425 waitbar(currentCube/numExportCubes,hWaitbar,['Exporting CT Intensity values (' num2str(currentCube) '/' num2str(numExportCubes) ') ...']);
0426 matRad_writeCube(fullfile(exportDir,['CT_ED' extension]),ct.cube{1},'double',metadata);
0427 end
0428
0429 if isfield(ct,'cubeHU')
0430 currentCube = currentCube + 1;
0431 waitbar(currentCube/numExportCubes,hWaitbar,['Exporting CT in HU (' num2str(currentCube) '/' num2str(numExportCubes) ') ...']);
0432 matRad_writeCube(fullfile(exportDir,['CT_HU' extension]),ct.cubeHU{1},'double',metadata);
0433 end
0434
0435
0436 cst = evalin('base','cst');
0437
0438 for voiIx = voiIndices
0439
0440 currentCube = currentCube + 1;
0441 waitbar(currentCube/numExportCubes,hWaitbar,['Exporting Segmentation Mask (' num2str(currentCube) '/' num2str(numExportCubes) ') ...']);
0442
0443
0444 voiRow = find(strcmp(voiNames{voiIx,2},cst(:,2)));
0445 voiIndexList = cst{voiRow,4}{1};
0446
0447 voiMask = zeros(ct.cubeDim);
0448 voiMask(voiIndexList) = 1;
0449
0450 matRad_writeCube(fullfile(voiDir,[voiNames{voiIx,2} extension]),voiMask,'uint8',metadata);
0451 end
0452
0453 end
0454
0455
0456 if saveResults
0457 results = evalin('base','resultGUI');
0458 cubeNames = get(handles.uitable_doseCubes,'data');
0459
0460 for cubeIx = cubeIndices
0461
0462 currentCube = currentCube + 1;
0463 waitbar(currentCube/numExportCubes,hWaitbar,['Exporting Results (' num2str(currentCube) '/' num2str(numExportCubes) ') ...']);
0464 matRad_writeCube(fullfile(resultDir,[cubeNames{cubeIx,2} extension]),results.(cubeNames{cubeIx,2}),'double',metadata);
0465 end
0466 end
0467
0468 close(handles.figure1);
0469
0470
0471
0472 function btn_cancel_Callback(hObject, eventdata, handles)
0473
0474
0475
0476 close(handles.figure1);
0477
0478
0479 function btn_cancel_CreateFcn(hObject, eventdata, handles)
0480
0481
0482
0483
0484
0485 function checkbox_compress_Callback(hObject, eventdata, handles)
0486
0487
0488
0489
0490