0001 function eidors_saveimg( img, fname, format, pp )
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 if ischar(img) && strcmp(img,'UNIT_TEST'); do_unit_test; return; end
0028
0029 switch nargin
0030 case 2
0031 fmt = detect_format(fname);
0032 if isempty( fmt )
0033 error('file format unspecified, can`t autodetect');
0034 end
0035 case {3,4}
0036 fmt1 = detect_format(fname);
0037 fmt = lower(format);
0038 if isempty(fmt1);
0039 fname = [fname '.' fmt];
0040 else
0041 if ~strcmp(fmt1, fmt)
0042 warning(['The extension specified (',fmt1, ...
0043 ') in file name (',fname, ...
0044 ') doesn''t match the file format:',fmt]);
0045 end
0046 end
0047 otherwise
0048 error('Usage: eidors_saveimg( img , fname, format,pp )');
0049 end
0050
0051
0052 switch fmt
0053 case 'igt'
0054 mceit_saveimg( img, fname );
0055 case 'e3d'
0056 native_saveimg( img, fname);
0057 case 'zri.mat'
0058 zriDmat_saveimg( img, fname,pp);
0059 otherwise
0060 error('eidors_readdata: file "%s" format unknown', fmt);
0061 end
0062
0063
0064
0065
0066
0067 function fmt = detect_format( fname )
0068
0069 dotpos = find(fname == '.');
0070 if isempty( dotpos )
0071 fmt = [];
0072 else
0073 dotpos= dotpos(1);
0074 format= fname( dotpos+1:end );
0075 fmt= lower(format);
0076 end
0077
0078
0079
0080
0081 function fid = open_file( fname );
0082
0083 if exist(fname,'file')
0084 disp('File already exists.');
0085 reply = input('Overwrite? Y/N [Y]: ', 's');
0086 if isempty(reply), reply = 'Y'; end
0087 reply = lower(reply);
0088
0089 if ~strcmp(reply,'y');
0090 fid = -1;
0091 return;
0092 end
0093 end
0094 fid = fopen( fname ,'w');
0095
0096
0097
0098
0099
0100
0101
0102 function mceit_saveimg( img, fname );
0103
0104
0105 fid = open_file( fname );
0106 if fid < 0
0107 error('Cannot open file.');
0108 end
0109
0110 n = size(img.elem_data,1);
0111 if n == 912
0112
0113 fwrite(fid,img.elem_data','4*float');
0114 else
0115 data = img2igt(img);
0116 fwrite(fid, data , '4*float');
0117 end
0118
0119 fclose(fid);
0120
0121 function zriDmat_saveimg( img, fname,pp);
0122 imgs = calc_slices(img);
0123 imgs(isnan(imgs))= 0;
0124 data = fill_in_params_zriDmat(pp, imgs);
0125 if exist('OCTAVE_VERSION')==5
0126
0127 save(fname, 'data','-V6');
0128 else
0129 save(fname, 'data');
0130 end
0131
0132 function po = fill_in_params_zriDmat(pi, imgs);
0133 sz_imgs = size(imgs);
0134 po.patient.ROI.RightLung =zeros(sz_imgs([1,2]));
0135 po.patient.ROI.LeftLung = zeros(sz_imgs([1,2]));
0136 po.patient.ROI.Heart = zeros(sz_imgs([1,2]));
0137
0138
0139 po.patient.halfChest = 'NaN';
0140 po.patient.height = 'NaN';
0141 po.patient.weight = 'NaN';
0142 po.patient.gender = 'NaN';
0143
0144 po.measurement.Position.transversal = zeros(1, sz_imgs(3));
0145 po.measurement.Position.longitudinal= zeros(1, sz_imgs(3));
0146 po.measurement.ImageQuality = 100*ones(1,sz_imgs(3));
0147 po.measurement.ElectrodeQuality = zeros(sz_imgs(3),32);
0148 po.measurement.ZeroRef = imgs;
0149
0150 po.injctionPattern= 'NaN';
0151 po.SensorBelt.NumEl= 'NaN';
0152
0153 po.measurement.CompositValue=squeeze(sum(sum(imgs,2),1));
0154
0155 po = replacefields(po, pi, 1 );
0156
0157 function po = replacefields(po, pi, d )
0158
0159 if ~isstruct(pi)
0160 po = pi;
0161
0162 return
0163 end
0164 for ff= fieldnames(pi)'; fn= ff{1};
0165
0166 if isfield(po,fn);
0167 po.(fn) = replacefields(po.(fn), pi.(fn), d+1 );
0168 else
0169 po.(fn) = pi.(fn);
0170 end
0171 end
0172
0173
0174 function native_saveimg( img, fname )
0175
0176
0177
0178
0179
0180 if ~exist('OCTAVE_VERSION') && str2double(version('-release')) < 14
0181 save('e3d.temp', 'img');
0182 else
0183 save('e3d.temp', 'img', '-v6');
0184 end
0185 zip('temp.zip','e3d.temp');
0186 movefile('temp.zip',fname);
0187 delete e3d.temp
0188
0189 function do_unit_test
0190 load montreal_data_1995;
0191 imdl = mk_common_model('c2t3',16);
0192 imdl.hyperparameter.value = 0.1;
0193 imgr = inv_solve(imdl,zc_resp(:,1),zc_resp);
0194 imgr.calc_colours.npoints = 64;
0195 p.imageRate = 4.7;
0196 p.patient.ROI.Inside = 100*ones(64);
0197 show_slices(imgr);
0198 eidors_saveimg(imgr,'mtldata.zri.mat','zri.mat',p);
0199
0200 load 'mtldata.zri.mat'
0201 unit_test_cmp('zri.mat',eidors_var_id(data.patient), ...
0202 'id_D6A2DB33EB61F4756BFE55BB696BBC2412D9BB3F');
0203 unit_test_cmp('zri.mat',eidors_var_id(data.SensorBelt), ...
0204 'id_E57BF6A94F6263AAA469AD2B3A70631FD0FCE2A6');
0205 if 0
0206 unit_test_cmp('zri.mat',eidors_var_id(data), ...
0207 'id_A4D719762BF416C86AF2C27A3A9B4A2758CCAB6D');
0208 end