matRad_exportGUI

Purpose ^

MATRAD_EXPORTGUI MATLAB code for matRad_exportGUI.fig

Synopsis ^

function varargout = matRad_exportGUI(varargin)

Description ^

 MATRAD_EXPORTGUI MATLAB code for matRad_exportGUI.fig
      MATRAD_EXPORTGUI, by itself, creates a new MATRAD_EXPORTGUI or raises the existing
      singleton*.

      H = MATRAD_EXPORTGUI returns the handle to a new MATRAD_EXPORTGUI or the handle to
      the existing singleton*.

      MATRAD_EXPORTGUI('CALLBACK',hObject,eventData,handles,...) calls the local
      function named CALLBACK in MATRAD_EXPORTGUI.M with the given input arguments.

      MATRAD_EXPORTGUI('Property','Value',...) creates a new MATRAD_EXPORTGUI or raises
      the existing singleton*.  Starting from the left, property value pairs are
      applied to the GUI before matRad_exportGUI_OpeningFcn gets called.  An
      unrecognized property name or invalid value makes property application
      stop.  All inputs are passed to matRad_exportGUI_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

Cross-reference information ^

This function calls: This function is called by:

Subfunctions ^

Source code ^

0001 function varargout = matRad_exportGUI(varargin)
0002 % MATRAD_EXPORTGUI MATLAB code for matRad_exportGUI.fig
0003 %      MATRAD_EXPORTGUI, by itself, creates a new MATRAD_EXPORTGUI or raises the existing
0004 %      singleton*.
0005 %
0006 %      H = MATRAD_EXPORTGUI returns the handle to a new MATRAD_EXPORTGUI or the handle to
0007 %      the existing singleton*.
0008 %
0009 %      MATRAD_EXPORTGUI('CALLBACK',hObject,eventData,handles,...) calls the local
0010 %      function named CALLBACK in MATRAD_EXPORTGUI.M with the given input arguments.
0011 %
0012 %      MATRAD_EXPORTGUI('Property','Value',...) creates a new MATRAD_EXPORTGUI or raises
0013 %      the existing singleton*.  Starting from the left, property value pairs are
0014 %      applied to the GUI before matRad_exportGUI_OpeningFcn gets called.  An
0015 %      unrecognized property name or invalid value makes property application
0016 %      stop.  All inputs are passed to matRad_exportGUI_OpeningFcn via varargin.
0017 %
0018 %      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
0019 %      instance to run (singleton)".
0020 %
0021 % See also: GUIDE, GUIDATA, GUIHANDLES
0022 
0023 % Edit the above text to modify the response to help matRad_exportGUI
0024 
0025 % Last Modified by GUIDE v2.5 07-Jul-2016 14:50:05
0026 %
0027 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0028 %
0029 % Copyright 2015 the matRad development team.
0030 %
0031 % This file is part of the matRad project. It is subject to the license
0032 % terms in the LICENSE file found in the top-level directory of this
0033 % distribution and at https://github.com/e0404/matRad/LICENSES.txt. No part
0034 % of the matRad project, including this file, may be copied, modified,
0035 % propagated, or distributed except according to the terms contained in the
0036 % LICENSE file.
0037 %
0038 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0039 
0040 % Begin initialization code - DO NOT EDIT
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 % End initialization code - DO NOT EDIT
0058 
0059 % --- Executes just before matRad_exportGUI is made visible.
0060 function matRad_exportGUI_OpeningFcn(hObject, eventdata, handles, varargin)
0061 % This function has no output args, see OutputFcn.
0062 % hObject    handle to figure
0063 % eventdata  reserved - to be defined in a future version of MATLAB
0064 % handles    structure with handles and user data (see GUIDATA)
0065 % varargin   command line arguments to matRad_exportGUI (see VARARGIN)
0066 
0067 % Choose default command line output for matRad_exportGUI
0068 handles.output = hObject;
0069 
0070 %Fills structure export table
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 %Fills result cubes export table
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 % Update handles structure
0106 guidata(hObject, handles);
0107 
0108 initialize_gui(hObject, handles, false);
0109 
0110 % UIWAIT makes matRad_exportGUI wait for user response (see UIRESUME)
0111 % uiwait(handles.figure1);
0112 
0113 
0114 % --- Outputs from this function are returned to the command line.
0115 function varargout = matRad_exportGUI_OutputFcn(hObject, eventdata, handles)
0116 % varargout  cell array for returning output args (see VARARGOUT);
0117 % hObject    handle to figure
0118 % eventdata  reserved - to be defined in a future version of MATLAB
0119 % handles    structure with handles and user data (see GUIDATA)
0120 
0121 % Get default command line output from handles structure
0122 varargout{1} = handles.output;
0123 
0124 % --------------------------------------------------------------------
0125 function initialize_gui(fig_handle, handles, isreset)
0126 % If the metricdata field is present and the btn_cancel flag is false, it means
0127 % we are we are just re-initializing a GUI by calling it from the cmd line
0128 % while it is up. So, bail out as we dont want to btn_cancel the data.
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 % Update handles structure
0148 guidata(handles.figure1, handles);
0149 %}
0150 
0151 % --- Executes on button press in checkbox_CT.
0152 function checkbox_CT_Callback(hObject, eventdata, handles)
0153 % hObject    handle to checkbox_CT (see GCBO)
0154 % eventdata  reserved - to be defined in a future version of MATLAB
0155 % handles    structure with handles and user data (see GUIDATA)
0156 
0157 saveCT = get(hObject,'Value');
0158 
0159 %Show the VOI-table only if we want to save a CT
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 % --- Executes on selection change in listbox_vois.
0168 function uitable_vois_Callback(hObject, eventdata, handles)
0169 % hObject    handle to listbox_vois (see GCBO)
0170 % eventdata  reserved - to be defined in a future version of MATLAB
0171 % handles    structure with handles and user data (see GUIDATA)
0172 
0173 % Hints: contents = cellstr(get(hObject,'String')) returns listbox_vois contents as cell array
0174 %        contents{get(hObject,'Value')} returns selected item from listbox_vois
0175 
0176 
0177 % --- Executes during object creation, after setting all properties.
0178 function uitable_vois_CreateFcn(hObject, eventdata, handles)
0179 % hObject    handle to listbox_vois (see GCBO)
0180 % eventdata  reserved - to be defined in a future version of MATLAB
0181 % handles    empty - handles not created until after all CreateFcns called
0182 
0183 % Hint: listbox controls usually have a white background on Windows.
0184 %       See ISPC and COMPUTER.
0185 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
0186     set(hObject,'BackgroundColor','white');
0187 end
0188 
0189 
0190 % --- Executes on button press in checkbox_dose.
0191 function checkbox_dose_Callback(hObject, eventdata, handles)
0192 % hObject    handle to checkbox_dose (see GCBO)
0193 % eventdata  reserved - to be defined in a future version of MATLAB
0194 % handles    structure with handles and user data (see GUIDATA)
0195 
0196 
0197 %Show the Result-table only if we want to save dose cubes
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     %set(handles.uitable_vois,'data',cell(0));
0205 end    
0206 
0207 
0208 % --- Executes on selection change in listbox_dose.
0209 function uitable_doseCubes_Callback(hObject, eventdata, handles)
0210 % hObject    handle to listbox_dose (see GCBO)
0211 % eventdata  reserved - to be defined in a future version of MATLAB
0212 % handles    structure with handles and user data (see GUIDATA)
0213 
0214 % Hints: contents = cellstr(get(hObject,'String')) returns listbox_dose contents as cell array
0215 %        contents{get(hObject,'Value')} returns selected item from listbox_dose
0216 
0217 
0218 % --- Executes during object creation, after setting all properties.
0219 function listbox_dose_CreateFcn(hObject, eventdata, handles)
0220 % hObject    handle to listbox_dose (see GCBO)
0221 % eventdata  reserved - to be defined in a future version of MATLAB
0222 % handles    empty - handles not created until after all CreateFcns called
0223 
0224 % Hint: listbox controls usually have a white background on Windows.
0225 %       See ISPC and COMPUTER.
0226 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
0227     set(hObject,'BackgroundColor','white');
0228 end
0229 
0230 
0231 % --- Executes on button press in pushbutton_dir_export_browse.
0232 function exportDir = pushbutton_dir_export_browse_Callback(hObject, eventdata, handles)
0233 % hObject    handle to pushbutton_dir_export_browse (see GCBO)
0234 % eventdata  reserved - to be defined in a future version of MATLAB
0235 % handles    structure with handles and user data (see GUIDATA)
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     % Update handles structure
0242     guidata(hObject, handles);
0243 end
0244 
0245 
0246 function exportDir = edit_dir_export_Callback(hObject, eventdata, handles)
0247 % hObject    handle to edit_dir_export (see GCBO)
0248 % eventdata  reserved - to be defined in a future version of MATLAB
0249 % handles    structure with handles and user data (see GUIDATA)
0250 
0251 % Hints: get(hObject,'String') returns contents of edit_dir_export as text
0252 %        str2double(get(hObject,'String')) returns contents of edit_dir_export as a double
0253 
0254 exportDir = get(handles.edit_dir_export,'String');
0255 
0256 %Add filesperator
0257 if exportDir(end) ~= filesep;
0258     exportDir = [exportDir filesep];
0259 end
0260 
0261 %Check if the user specified an existing directory
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 % --- Executes during object creation, after setting all properties.
0270 function edit_dir_export_CreateFcn(hObject, eventdata, handles)
0271 % hObject    handle to edit_dir_export (see GCBO)
0272 % eventdata  reserved - to be defined in a future version of MATLAB
0273 % handles    empty - handles not created until after all CreateFcns called
0274 
0275 % Hint: edit controls usually have a white background on Windows.
0276 %       See ISPC and COMPUTER.
0277 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
0278     set(hObject,'BackgroundColor','white');
0279     
0280 
0281     
0282 end
0283 
0284 
0285 % --- Executes on selection change in popupmenu_extension.
0286 function popupmenu_extension_Callback(hObject, eventdata, handles)
0287 % hObject    handle to popupmenu_extension (see GCBO)
0288 % eventdata  reserved - to be defined in a future version of MATLAB
0289 % handles    structure with handles and user data (see GUIDATA)
0290 
0291 % Hints: contents = cellstr(get(hObject,'String')) returns popupmenu_extension contents as cell array
0292 %        contents{get(hObject,'Value')} returns selected item from popupmenu_extension
0293 
0294 
0295 % --- Executes during object creation, after setting all properties.
0296 function popupmenu_extension_CreateFcn(hObject, eventdata, handles)
0297 % hObject    handle to popupmenu_extension (see GCBO)
0298 % eventdata  reserved - to be defined in a future version of MATLAB
0299 % handles    empty - handles not created until after all CreateFcns called
0300 
0301 %These sets up the available extensions
0302 extensions{1} = '*.nrrd';
0303 extensions{2} = '*.vtk';
0304 extensions{3} = '*.mha';
0305 set(hObject,'String',extensions);
0306 
0307 % Hint: popupmenu controls usually have a white background on Windows.
0308 %       See ISPC and COMPUTER.
0309 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
0310     set(hObject,'BackgroundColor','white');
0311 end
0312 
0313 
0314 % --- Executes on button press in btn_export.
0315 function btn_export_Callback(hObject, eventdata, handles)
0316 % hObject    handle to btn_export (see GCBO)
0317 % eventdata  reserved - to be defined in a future version of MATLAB
0318 % handles    structure with handles and user data (see GUIDATA)
0319 
0320 %Get the export dir
0321 exportDir = get(handles.edit_dir_export,'String');
0322 
0323 %Sanity check
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     %Add file separator if necessary
0332     if exportDir(end) ~= filesep;
0333         exportDir = [exportDir filesep];
0334     end 
0335 end
0336 
0337 %Get the file extension
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 %%Prepare for export
0347 %If we export CT, try to create a subdirectory for VOIs
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 %If we export results, try to create a subdirectory for VOIs
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 %prepare metadata
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 %Check if we have position information
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 %This is only for the waitbar to get the number of cubes you wanna save
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 %Give an error if nothing was selected
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 %CT and Mask export
0420 if saveCT   
0421     
0422     if isfield(ct,'cube')
0423         %Export the CT (ED suffix to clarify it is not in HU)
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     %Export VOI masks
0436     cst = evalin('base','cst');
0437     
0438     for voiIx = voiIndices
0439         %Waitbar
0440         currentCube = currentCube + 1;
0441         waitbar(currentCube/numExportCubes,hWaitbar,['Exporting Segmentation Mask (' num2str(currentCube) '/' num2str(numExportCubes) ') ...']);
0442         
0443         %Get the index list
0444         voiRow = find(strcmp(voiNames{voiIx,2},cst(:,2)));
0445         voiIndexList = cst{voiRow,4}{1};
0446         %Set up the full mask
0447         voiMask = zeros(ct.cubeDim);
0448         voiMask(voiIndexList) = 1;
0449         %Export...
0450         matRad_writeCube(fullfile(voiDir,[voiNames{voiIx,2} extension]),voiMask,'uint8',metadata);
0451     end
0452     
0453 end
0454 
0455 %Results Export
0456 if saveResults
0457     results = evalin('base','resultGUI');    
0458     cubeNames = get(handles.uitable_doseCubes,'data');
0459        
0460     for cubeIx = cubeIndices
0461         %Export
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 % --- Executes on button press in btn_cancel.
0472 function btn_cancel_Callback(hObject, eventdata, handles)
0473 % hObject    handle to btn_cancel (see GCBO)
0474 % eventdata  reserved - to be defined in a future version of MATLAB
0475 % handles    structure with handles and user data (see GUIDATA)
0476 close(handles.figure1);
0477 
0478 % --- Executes during object creation, after setting all properties.
0479 function btn_cancel_CreateFcn(hObject, eventdata, handles)
0480 % hObject    handle to btn_cancel (see GCBO)
0481 % eventdata  reserved - to be defined in a future version of MATLAB
0482 % handles    empty - handles not created until after all CreateFcns called
0483 
0484 % --- Executes on button press in checkbox_compress.
0485 function checkbox_compress_Callback(hObject, eventdata, handles)
0486 % hObject    handle to checkbox_compress (see GCBO)
0487 % eventdata  reserved - to be defined in a future version of MATLAB
0488 % handles    structure with handles and user data (see GUIDATA)
0489 
0490 % Hint: get(hObject,'Value') returns toggle state of checkbox_compress

| Generated by m2html © 2005