0001 function [ROIs, imgR, imdl] = define_ROIs(imdl, 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 if ischar(imdl) && strcmp(imdl,'UNIT_TEST'); do_unit_test; return; end
0031
0032 if isstruct(varargin{end})
0033 opts = varargin{end};
0034 last = 1;
0035 else
0036 opts = struct([]);
0037 last = 0;
0038 end
0039
0040
0041
0042
0043 opts = mergestructs(struct( ...
0044 'reorder',[],'normalize',false,'usec2f',false), opts);
0045
0046 recc2f = 1;
0047 switch imdl.type
0048 case 'fwd_model';
0049 fmdl = imdl;
0050 case 'inv_model';
0051 if isfield(imdl,'rec_model');
0052 fmdl = imdl.rec_model;
0053 recc2f = fmdl.coarse2fine;
0054 else
0055 fmdl = imdl.fwd_model;
0056 end
0057
0058 otherwise;
0059 error('define_ROIs: require fwd_ or inv_model');
0060 end
0061 if opts.usec2f
0062 recc2f = fmdl.coarse2fine;
0063 end
0064
0065
0066 rmdl = mk_grid_model([],varargin{1:end-last});
0067
0068 ROIa = mk_approx_c2f(fmdl,rmdl);
0069
0070
0071
0072 ROIs = ROIa.' * bsxfun(@rdivide, recc2f, sum(recc2f,1) );
0073 if ~isempty(opts.reorder)
0074 ROIs = ROIs(opts.reorder(:),:);
0075 end
0076 if opts.normalize
0077 ROIs = ROIs ./ sum(ROIs,2);
0078 end
0079 imgR= mk_image(fmdl,ROIs.');
0080 imgR.calc_colours.ref_level = 0;
0081
0082
0083 if strcmp(imdl.type,'inv_model') && ...
0084 isfield(imdl,'rec_model') && ...
0085 isfield(imdl,'solve_use_matrix')
0086
0087 imdl.rec_model.coarse2fine = ROIa;
0088 imdl.solve_use_matrix.RM = ROIs*imdl.solve_use_matrix.RM;
0089 end
0090
0091 function do_unit_test
0092 do_GREIT_test
0093
0094
0095
0096 function do_GREIT_test
0097 imdl = mk_common_model('n3r2',[16,2]);
0098 [ROIs,imgR] = define_ROIs(imdl,-1:0.5:1,-1:0.5:1);
0099 unit_test_cmp('n3r2: 2D 1', size(ROIs), [16,828]);
0100 unit_test_cmp('n3r2: 2D 2', ROIs(:,1:9), ...
0101 [0;0;0;0;0;0;0;0;0;0;0;1;0;0;0;0]*ones(1,9));
0102 unit_test_cmp('n3r2: 2D 2', ROIs(:,10:11), ...
0103 [0,0;0,0;0,0;0,0;0,0;0,0;0,0;0,0;
0104 0,0;0,0;0,0;95,80;0,0;0,0;0,0;5,20]/100,1e-14);
0105 [ROIs,imgR] = define_ROIs(imdl,-1:1,-1:1,0:2);
0106 unit_test_cmp('n3r2: 3D 1', size(ROIs), [8,828]);
0107 unit_test_cmp('n3r2: 3D 2', ROIs(:,1:9), ...
0108 [0;0;0;1;0;0;0;0]*ones(1,9),1e-14);
0109 unit_test_cmp('n3r2: 3D 3', ROIs(:,61:69), ...
0110 [0;0;1;0;0;0;0;0]*ones(1,9),1e-14);
0111
0112 fmdl = mk_library_model('neonate_32el_lungs');
0113 [stim,msel] = mk_stim_patterns(32,1,[0,5],[0,5],{'no_meas_current_next2'},1);
0114 fmdl.stimulation = stim;
0115 fmdl.meas_select = msel;
0116 imdl= select_imdl(fmdl,'GREIT:NF=0.500 32x32');
0117 [ROIs,imgR] = define_ROIs(imdl,-1:0.5:1,-1:0.5:1);
0118 subplot(221); show_slices(imgR)
0119 imdl= select_imdl(fmdl,'GREIT:NF=0.500 16x16');
0120 [ROIs,imgR] = define_ROIs(imdl,-1:0.5:1,-1:0.5:1);
0121 subplot(222); show_slices(imgR)
0122 opts.reorder = reshape(1:16,4,4)';
0123 [ROIs,imgR] = define_ROIs(imdl,-1:0.5:1,-1:0.5:1,opts);
0124 subplot(223); show_slices(imgR)
0125 [ROIs,imgR] = define_ROIs(imdl,-1:0.5:1,1:-0.5:-1,opts);
0126 subplot(224); show_slices(imgR)