eidors_saveimg

PURPOSE ^

EIDORS saveimg - save reconstructed image files in formats

SYNOPSIS ^

function eidors_saveimg( img, fname, format, pp )

DESCRIPTION ^

 EIDORS saveimg - save reconstructed image files in formats
    of various EIT equipment manufacturers
 eidors_saveimg( img, fname, format, params )

 Currently the list of supported file formats is:
    - MCEIT (Goettingen / Viasys) "igt" file format 
        format = "IGT"
    - SenTec / iBex - the "mat" format for SenTec's ibeX software
        format = "ZRI.MAT"

 Usage
 eidors_saveimg( img,fname,format )
     img   = eidors image structure
     fname = file name

 For the "zri.mat" format, use (minumum)
  p.imageRate = FR;
  p.patient.ROI.Inside = thorax_ROI*100;
  eidors_saveimg( img, 'filename.zri.mat','zri.mat', p)

  If format is unspecified, we attempt to autodetect.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function eidors_saveimg( img, fname, format, pp )
0002 % EIDORS saveimg - save reconstructed image files in formats
0003 %    of various EIT equipment manufacturers
0004 % eidors_saveimg( img, fname, format, params )
0005 %
0006 % Currently the list of supported file formats is:
0007 %    - MCEIT (Goettingen / Viasys) "igt" file format
0008 %        format = "IGT"
0009 %    - SenTec / iBex - the "mat" format for SenTec's ibeX software
0010 %        format = "ZRI.MAT"
0011 %
0012 % Usage
0013 % eidors_saveimg( img,fname,format )
0014 %     img   = eidors image structure
0015 %     fname = file name
0016 %
0017 % For the "zri.mat" format, use (minumum)
0018 %  p.imageRate = FR;
0019 %  p.patient.ROI.Inside = thorax_ROI*100;
0020 %  eidors_saveimg( img, 'filename.zri.mat','zri.mat', p)
0021 %
0022 %  If format is unspecified, we attempt to autodetect.
0023 
0024 % (C) 2009 by Bartlomiej Grychtol. Licensed under GPL v2 or v3
0025 % $Id: eidors_saveimg.m 6015 2019-07-02 06:53:14Z aadler $
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' % Will be deprecated
0056         native_saveimg( img, fname);
0057     case 'zri.mat' % Will be deprecated
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 % mceit_readimg - saves IGT files.
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    %already the right format
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    % FIXME ... currently (4.4.1) octave gives errors for -V7
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    % put to dummy because they are missing
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 % fprintf('%d:',d);
0159   if ~isstruct(pi)
0160     po = pi;
0161 %   disp('=');
0162     return
0163   end
0164   for ff= fieldnames(pi)'; fn= ff{1};
0165 %   disp(fn);
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 % The 'native' image format is deprecated - May 2019 - aa
0174 function native_saveimg( img, fname )
0175    % native_saveimg - saves E3D file.
0176    % E3D file is a zipped matlab v6 compatible .mat file called "e3d.temp"
0177    % containing one eidors image struct variable named "img".
0178 
0179    % save temporary mat file
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 %% Remove test ... varies too much between versions
0206    unit_test_cmp('zri.mat',eidors_var_id(data), ...
0207          'id_A4D719762BF416C86AF2C27A3A9B4A2758CCAB6D');
0208 end

Generated on Tue 31-Dec-2019 17:03:26 by m2html © 2005