Home > phiwave > @phiw_wvimg > phiw_wvimg.m

phiw_wvimg

PURPOSE ^

constructor for phiw_wvimg object - a container for a wt'ed image

SYNOPSIS ^

function wvimg = phiw_wvimg(inpimg,input_options,waveobj,scales)

DESCRIPTION ^

 constructor for phiw_wvimg object - a container for a wt'ed image
 FORMAT wvimg = phiw_wvimg(inpimg,input_options,waveobj,scales)

 Function call takes four forms
 wvimg = phiw_wvimg;   no args, returns default object
 wvimg = phiw_wvimg(wv_img); object passed, object returned
 wvimg = phiw_wvimg(inpimg,input_options,wavelet,scales)
         (Four imput arguments)
         Creates wavelet structure from _untransformed_ data
         in inpimg (either SPM vol struct, or data matrix)
         and (possibly queues) transform with wavelet, scales
 wvimg = phiw_wvimg(inpimg, [input_options], [twvimg])
         (between one and three input arguments, where first argument is
         not a wvimg object already)
         Puts previously _transformed_ data from inpimg into wvimg object
         This can have three forms:
         1) If no third arg - try to derive info from inpimg .mat file
         (return empty if not successful)
         2) If third arg is phiw_wvimg object, use this as template
         3) If third arg is vol struct, try and get info from that .mat
         file (return empty if not successful)

 object fields are:
         ovol    = SPM vol struct describing not wt'ed data
                   with fields: dim, mat, fname, pinfo
         wvol    = wavelet transformed SPM vol struct representing dimensions,
                   .mat data, filename for image data, datatype;
         options = struct with options, with fields below. Options fall
                   into two categories; permanent, and object creation
                   options (noproc, remap); the latter are reset to 0 by
                   this routine  
         img     = contains matrix data, or SPM vol struct representing
                   data
         wtf     = set if data has been wavelet transformed 
         changef = set if data has been changed since passed
         wavelet = phiw_wavelet object giving wavelet for transform;
         scales  = number of coarsest scale for wt;
         descrip = extended text description field;

 passed options can be empty, or one or more fields from this list
 defaults in [], OCO = object creation option
 
         datatype  - datatype for transformed data ['float']
         wtprefix  - default prefix for transformed file ['wt_']
         verbose   - flag to output feedback in matlab window [1]
         descrip   - description (appended to fulldescrip obj field)
                     ['']  OCO
         noproc    - if not zero defer vol struct load and any wavelet
                     transform [0] OCO 
         remap     - forces remap of passed vol struct [0] OCO
         find_similar - when trying to create a wavelet image, first
                     look for a compatible image that already exists [0]
                     OCO  

 wvimg returned is empty if inputs are incorrect
 
 Routine does little filling of fields, and relies on methods filling
 the fields that they need, or complaining if appropriate

 The constructor can also be called to give class functions, where the
 name of the class function is a character string which is one of:
    'orig_vol'  returns original vol struct from wt'ed vol
    'is_wted'   returns 1 is this vol appears to be be WT'ed
    'wtinfo'    returns wtinfo, if available, empty otherwise

 This class relies on lots of SPM routines
 (spm_read_vols, spm_write_vol, spm_type, spm_vol, etc)

 Matthew Brett 21/5/01 (C/NZD)

 $Id: phiw_wvimg.m,v 1.11 2005/06/21 04:01:04 matthewbrett Exp $

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function wvimg = phiw_wvimg(inpimg,input_options,waveobj,scales)
0002 % constructor for phiw_wvimg object - a container for a wt'ed image
0003 % FORMAT wvimg = phiw_wvimg(inpimg,input_options,waveobj,scales)
0004 %
0005 % Function call takes four forms
0006 % wvimg = phiw_wvimg;   no args, returns default object
0007 % wvimg = phiw_wvimg(wv_img); object passed, object returned
0008 % wvimg = phiw_wvimg(inpimg,input_options,wavelet,scales)
0009 %         (Four imput arguments)
0010 %         Creates wavelet structure from _untransformed_ data
0011 %         in inpimg (either SPM vol struct, or data matrix)
0012 %         and (possibly queues) transform with wavelet, scales
0013 % wvimg = phiw_wvimg(inpimg, [input_options], [twvimg])
0014 %         (between one and three input arguments, where first argument is
0015 %         not a wvimg object already)
0016 %         Puts previously _transformed_ data from inpimg into wvimg object
0017 %         This can have three forms:
0018 %         1) If no third arg - try to derive info from inpimg .mat file
0019 %         (return empty if not successful)
0020 %         2) If third arg is phiw_wvimg object, use this as template
0021 %         3) If third arg is vol struct, try and get info from that .mat
0022 %         file (return empty if not successful)
0023 %
0024 % object fields are:
0025 %         ovol    = SPM vol struct describing not wt'ed data
0026 %                   with fields: dim, mat, fname, pinfo
0027 %         wvol    = wavelet transformed SPM vol struct representing dimensions,
0028 %                   .mat data, filename for image data, datatype;
0029 %         options = struct with options, with fields below. Options fall
0030 %                   into two categories; permanent, and object creation
0031 %                   options (noproc, remap); the latter are reset to 0 by
0032 %                   this routine
0033 %         img     = contains matrix data, or SPM vol struct representing
0034 %                   data
0035 %         wtf     = set if data has been wavelet transformed
0036 %         changef = set if data has been changed since passed
0037 %         wavelet = phiw_wavelet object giving wavelet for transform;
0038 %         scales  = number of coarsest scale for wt;
0039 %         descrip = extended text description field;
0040 %
0041 % passed options can be empty, or one or more fields from this list
0042 % defaults in [], OCO = object creation option
0043 %
0044 %         datatype  - datatype for transformed data ['float']
0045 %         wtprefix  - default prefix for transformed file ['wt_']
0046 %         verbose   - flag to output feedback in matlab window [1]
0047 %         descrip   - description (appended to fulldescrip obj field)
0048 %                     ['']  OCO
0049 %         noproc    - if not zero defer vol struct load and any wavelet
0050 %                     transform [0] OCO
0051 %         remap     - forces remap of passed vol struct [0] OCO
0052 %         find_similar - when trying to create a wavelet image, first
0053 %                     look for a compatible image that already exists [0]
0054 %                     OCO
0055 %
0056 % wvimg returned is empty if inputs are incorrect
0057 %
0058 % Routine does little filling of fields, and relies on methods filling
0059 % the fields that they need, or complaining if appropriate
0060 %
0061 % The constructor can also be called to give class functions, where the
0062 % name of the class function is a character string which is one of:
0063 %    'orig_vol'  returns original vol struct from wt'ed vol
0064 %    'is_wted'   returns 1 is this vol appears to be be WT'ed
0065 %    'wtinfo'    returns wtinfo, if available, empty otherwise
0066 %
0067 % This class relies on lots of SPM routines
0068 % (spm_read_vols, spm_write_vol, spm_type, spm_vol, etc)
0069 %
0070 % Matthew Brett 21/5/01 (C/NZD)
0071 %
0072 % $Id: phiw_wvimg.m,v 1.11 2005/06/21 04:01:04 matthewbrett Exp $
0073 
0074 myclass = 'phiw_wvimg';
0075 
0076 % default object struct; all fields are empty to allow fill below
0077 % it has to be this way to make sure all fields are in the same order
0078 defstruct = struct(...
0079     'ovol', [], ...
0080     'wvol', [], ...
0081     'options', [], ...
0082     'img', [], ...
0083     'wavelet', [], ...
0084     'scales', [], ...
0085     'oimgi', [], ...
0086     'descrip', '', ...
0087     'wtf', [], ...
0088     'changef', []);
0089 
0090 % no args, return default object
0091 if nargin < 1
0092   wvimg = class(defstruct,myclass);
0093   return
0094 end
0095 
0096 % wvimg object passed as first arg, return it unaltered
0097 if isa(inpimg, myclass)
0098   wvimg = inpimg;
0099   return
0100 end
0101 
0102 % set unpassed inputs to empty
0103 if nargin < 2, input_options = [];end
0104 if nargin < 3, waveobj = [];end
0105 if nargin < 4, scales = [];end
0106 
0107 % overall default options
0108 defopts = struct('datatype','float', ...
0109          'wtprefix','wt_',...
0110          'verbose',1,...
0111          'descrip','', ...
0112          'noproc',0, ...
0113          'remap',0, ...
0114          'find_similar',0);
0115 
0116 % parse out string action calls (class functions)
0117 if ischar(inpimg)
0118   s_def = struct('noproc',1);
0119   switch inpimg
0120    case 'orig_vol'
0121     % Second argument is (array of) vol structs
0122     VY = input_options;
0123     if nargin < 2, error('Need vol struct'); end
0124     for v = 1:prod(size(VY))
0125       wvobj = phiw_wvimg(VY(v), s_def);
0126       if isempty(wvobj), oVY(v) = VY(v); else oVY(v) = wvobj(v).ovol; end
0127     end
0128     wvimg = reshape(oVY, size(VY));
0129     return
0130    case 'is_wted'
0131     % Second argument is (array of) vol structs
0132     VY = input_options;
0133     is_wtf = zeros(size(VY));
0134     for v = 1:prod(size(VY))
0135       is_wtf(v) = ~isempty(pr_getwave(VY(v)));
0136     end
0137     wvimg = is_wtf;
0138     return
0139    case 'wtinfo'
0140     % Second argument is a single vol struct
0141     VY = input_options;
0142     if prod(size(VY)) > 1, error('Can only handle one vol'); end
0143     wvimg = pr_getwave(VY);
0144     if ~ismempty(wvimg)
0145       wvimg = wtinfo(wvimg);
0146     end
0147     return
0148   end
0149 end
0150 
0151 % process passed options
0152 if isfield(input_options,'datatype')
0153   if ~ischar(input_options.datatype)
0154     input_options.datatype = spm_type(input_options.datatype);
0155   end
0156 end
0157 
0158 % get passed options from input
0159 filled_opts = mars_struct('ffillsplit', defopts, input_options);
0160 
0161 % process inpimg argument
0162 % ----------------------------------------------
0163 % remap volume if specified and is vol struct
0164 if isstruct(inpimg) & filled_opts.remap
0165   inpimg = inpimg.fname;
0166 end
0167 % convert string inpimg input to SPM vol struct
0168 if ischar(inpimg)
0169   inpimg = spm_vol(inpimg);
0170 end
0171 
0172 % process different types of function call
0173 if nargin > 3  % must be untransformed data with wavelet and scales
0174   
0175   % insert necessary data into wvimg structure to start
0176   [wvimg others] = mars_struct('ffillsplit', defstruct, ...
0177                    struct(...
0178                    'img', inpimg, ...
0179                    'options', filled_opts, ...
0180                    'wtf', 0, ...
0181                    'descrip', filled_opts.descrip));
0182   
0183   if ~isa(waveobj,'phiw_wavelet') | isempty(scales)
0184     error('Need wavelet and scales for untransformed data');
0185   end
0186 
0187   % set ovol and ouput filename
0188   if isstruct(inpimg) % got a vol struct
0189     % set output file name by adding wt prefix
0190     [p f e] = fileparts(inpimg.fname);
0191     out_fname = fullfile(p, [wvimg.options.wtprefix f e]);
0192     
0193     % look for a similar image that exists already
0194     if filled_opts.find_similar
0195       if exist(out_fname, 'file')
0196     Vw = spm_vol(out_fname);
0197     if phiw_wvimg('is_wted', Vw)
0198       Wo = phiw_wvimg(Vw, struct('noproc', 1));
0199       if same_wtinfo(Wo, struct(...
0200           'wavelet', waveobj, ...
0201           'scales', scales))
0202         wvimg = Wo;
0203         if ~filled_opts.noproc
0204           wvimg = doproc(wvimg);
0205         end
0206         return
0207       end
0208     end    
0209       end
0210     end      
0211       
0212     % No found similar image - make afresh
0213     wvimg.ovol = inpimg;
0214     if ~isempty(wvimg.descrip)
0215       wvimg.descrip = [wvimg.descrip '; ' wvimg.ovol.descrip];
0216     end
0217     wvimg.wvol.fname = out_fname;
0218   elseif ~isempty(inpimg) & isnumeric(inpimg)
0219     % maybe data passed was a matrix, we'll do our best
0220     sz = ones(1,3);
0221     sz(1:length(size(inpimg))) = size(inpimg);
0222     offs = -(sz+1)/2;
0223     wvimg.ovol = struct('dim', [sz spm_type(wvimg.options.datatype)],...
0224         'pinfo',[1 0 0]',...
0225         'fname',[],...
0226         'mat', [1 0 0 offs(1); 0 1 0 offs(2); 0 0 1 offs(3); ...
0227             0 0 0 1]);
0228   end
0229   
0230   % last two args are wavelet and scales
0231   wvimg.wavelet = waveobj;
0232   wvimg.scales = scales;
0233   
0234   % set bits for output image struct
0235   wvimg.wvol = mars_struct('fillafromb', wvimg.wvol, wvimg.ovol);
0236   wvimg.wvol.dim(1:3) = outdim(wvimg.wavelet, wvimg.ovol.dim(1:3));
0237 
0238 else % i.e nargin <= 3
0239   
0240   % this must be a wt'ed vol struct for putting into object
0241   wvimg.wtf = 1;
0242 
0243   % We need phiw_wvimg template information.  We can get it from the input
0244   % vol struct, or from the third argument.  Third argument can be empty
0245   % (missing), vol struct, or phiw_wvimg object...
0246     
0247   % if no third argument, try and get template from .mat file
0248   if isempty(waveobj), waveobj = inpimg; end
0249   
0250   % look for wavelet information in vol struct
0251   if isstruct(waveobj)  
0252     waveobj = pr_getwave(waveobj);
0253   end
0254   
0255   % no template obtainable -> give up and return empty
0256   if isempty(waveobj), wvimg = []; return, end
0257   
0258   % Fill any missing input stuff from the template object
0259   wvimg = mars_struct('fillafromb', ...
0260               struct(...
0261               'img', inpimg, ...
0262               'options', input_options), ...
0263               struct(waveobj));
0264   
0265   % fill any missing / empty object fields from defaults
0266   wvimg.options = mars_struct('ffillsplit', defopts, wvimg.options);
0267   [wvimg others] = mars_struct('ffillsplit', defstruct, wvimg);
0268   
0269   % input vol struct overrides template vol struct
0270   if isstruct(inpimg)
0271     wvimg.wvol = inpimg;
0272   else % input image dimensions override template
0273     wvimg.wvol.dim(1:3) = size(wvimg.img);
0274   end;
0275 
0276 end
0277 
0278 % set any options passed, overriding previous
0279 if mars_struct('isthere', input_options, 'datatype')
0280   wvimg.wvol.dim(4) = spm_type(input_options.datatype);
0281 end
0282 
0283 % fill the vol structs with empty fields
0284 evol = struct('fname','','mat',eye(4),'dim',[1 1 1], ...
0285           'pinfo',ones(3,1),'descrip','');
0286 wvimg.ovol = mars_struct('fillafromb', wvimg.ovol,evol);
0287 wvimg.wvol = mars_struct('fillafromb', wvimg.wvol,evol);
0288 
0289 % set datatypes if missing
0290 if length(wvimg.ovol.dim) < 4
0291   wvimg.ovol.dim(4) = spm_type(wvimg.options.datatype);
0292 end
0293 if length(wvimg.wvol.dim) < 4
0294   wvimg.wvol.dim(4) = spm_type(wvimg.options.datatype);
0295 end
0296 
0297 % bless (as we say in perl)
0298 wvimg = class(wvimg,myclass);
0299 
0300 % do processing as necessary
0301 if ~wvimg.options.noproc
0302   wvimg = doproc(wvimg);
0303 end
0304 
0305 % unset first pass options
0306 wvimg.options.noproc = 0;
0307 wvimg.options.remap = 0;
0308 wvimg.options.find_similar = 0;
0309 wvimg.options.descrip = '';
0310 
0311 return
0312 
0313

Generated on Wed 06-Jul-2005 18:07:21 by m2html © 2003