Home > functions > tool_box > spmtools > vb_bias_correction_by_spm.m

vb_bias_correction_by_spm

PURPOSE ^

Apply bias correction to image_file by spm(2/5-) function.

SYNOPSIS ^

function [image_file_out] = vb_bias_correction_by_spm(image_file)

DESCRIPTION ^

 Apply bias correction to image_file by spm(2/5-) function.
 [USAGE]
    image_file_out = bias_correction_using_spm(<<image_file>>);

 [IN]
        image_file : Analyze or NIfTI file (.hdr/.img)

 [OUT]
    image_file_out : bias corrected filename
                     The format is same as input file.
                     'm' + image_file(ex. foo.hdr/img->mfoo.hdr/img)
 [NOTE]
   Before using this function, please set SPM path on your MATLAB.
   SPM2 cannot treat NIfTI format.
   If you want to do bias correction to NIfTI image,
   use SPM5 or greater version.
   In that case, when you use SPM2, process will be aborted.

 [SEE]
   spm_bias_estimate.m, spm_bias_apply.m
 
 2010/05/12 rhayashi Initial version

 Copyright (C) 2011, ATR All Rights Reserved.
 License : New BSD License(see VBMEG_LICENSE.txt)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [image_file_out] = vb_bias_correction_by_spm(image_file)
0002 % Apply bias correction to image_file by spm(2/5-) function.
0003 % [USAGE]
0004 %    image_file_out = bias_correction_using_spm(<<image_file>>);
0005 %
0006 % [IN]
0007 %        image_file : Analyze or NIfTI file (.hdr/.img)
0008 %
0009 % [OUT]
0010 %    image_file_out : bias corrected filename
0011 %                     The format is same as input file.
0012 %                     'm' + image_file(ex. foo.hdr/img->mfoo.hdr/img)
0013 % [NOTE]
0014 %   Before using this function, please set SPM path on your MATLAB.
0015 %   SPM2 cannot treat NIfTI format.
0016 %   If you want to do bias correction to NIfTI image,
0017 %   use SPM5 or greater version.
0018 %   In that case, when you use SPM2, process will be aborted.
0019 %
0020 % [SEE]
0021 %   spm_bias_estimate.m, spm_bias_apply.m
0022 %
0023 % 2010/05/12 rhayashi Initial version
0024 %
0025 % Copyright (C) 2011, ATR All Rights Reserved.
0026 % License : New BSD License(see VBMEG_LICENSE.txt)
0027 
0028 %
0029 % --- Check Input file name
0030 %
0031 if ~exist('image_file', 'var')
0032     [this] = file_dialog;
0033     this.dialog_title = 'Select Image(Analyze/NIfTI) file(*.hdr)';
0034     [image_dir, image_file] = visible(this);
0035     if length(image_file) == 0, return; end
0036     image_file = [image_dir filesep image_file{1}];
0037 elseif exist(image_file, 'file') ~= 2
0038     error('Input file is not found.');
0039 end
0040 
0041 %
0042 % --- Check SPM Version
0043 %
0044 ver_func = which('spm.m');
0045 if isempty(ver_func)
0046     error('Please check path of SPM.');
0047 end
0048 func_h = @spm;
0049 spm_ver = feval(func_h, 'Ver');
0050 if strcmp(spm_ver, 'SPM99')
0051     error('SPM2 or greater version is required.');
0052 end
0053 
0054 %
0055 % --- Check Original image type
0056 %
0057 h = msgbox('Now doing bias correction...');
0058 
0059 % hide pushbutton
0060 h_children = get(h, 'children');
0061 for k=1:length(h_children)
0062 try
0063     style = get(h_children(k), 'style');
0064     if strcmp(style, 'pushbutton')
0065         set(h_children(k), 'Visible', 'off');
0066     end
0067 catch
0068 end
0069 end
0070 drawnow; pause(0.1);
0071 
0072 [o_ni_hdr, o_filetype] = load_nii_hdr(image_file);
0073 avw = avw_hdr_read(image_file);
0074 o_hdr = avw.hdr;
0075 
0076 switch(o_filetype)
0077     case 0 % Analyze
0078     case 1 % NIfTI(.hdr, .img)
0079         if strcmp(spm_ver, 'SPM2')
0080             if ishandle(h), delete(h); end
0081             error('SPM2 cannot treat NIfTI format. please use SPM5 or greater version.');
0082         end
0083     otherwise
0084         if ishandle(h), delete(h); end
0085         error('The image file type is not supported.');
0086 end
0087 
0088 %
0089 % --- Bias correction
0090 %
0091 [p_, f_, e_] = vb_get_file_parts(image_file);
0092 prefix = [p_, filesep, 'm', f_];
0093 bias_corrected_hdr    = [prefix, '.hdr'];
0094 bias_corrected_file   = [prefix, '.img'];
0095 bias_corrected_mat    = [prefix, '.mat'];
0096 bias_info_file        = fullfile(p_, ['bias_' f_ '.mat']);
0097 
0098 org_file = [p_, filesep, f_, '.img'];
0099 try
0100     spm_bias_estimate(org_file);
0101 catch
0102     if exist(bias_info_file, 'file') ~= 2
0103         if ishandle(h), delete(h); end
0104         e = lasterror;
0105         error(e.message);
0106     end
0107 end
0108 
0109 try
0110 spm_bias_apply(org_file, bias_info_file);
0111 
0112 %
0113 % --- Check bias corrected file type
0114 %
0115 
0116 [m_hdr, m_filetype] = load_nii_hdr(bias_corrected_hdr);
0117 
0118 if (o_filetype == 0) && (m_filetype == 0)
0119     % --- Analyze to Analyze case
0120 
0121     % Change originator to original one.
0122     % (SPM changes originator from original to the center of the image)
0123     avw = avw_hdr_read(bias_corrected_file);
0124     avw.hdr.hist.originator = o_hdr.hist.originator;
0125     avw.hdr.hist.orient     = o_hdr.hist.orient;
0126     avw_hdr_write(avw);
0127 elseif (o_filetype == 0) && (m_filetype == 1)
0128     % --- Analyze to NIfTI case
0129     %  SPM8(or greater) changes analyze to NIfTI format automatically.
0130     %  So this program changes header from NIfTI image to Analyze.
0131 
0132     % Change originator to original one.
0133     % (SPM changes originator from original to the center of the image)
0134     avw = avw_hdr_read(bias_corrected_file);
0135     avw.hdr.hist.originator = o_hdr.hist.originator;
0136     avw.hdr.hist.orient     = o_hdr.hist.orient;
0137     if isempty(o_hdr.hist.smin)
0138         o_hdr.hist.smin = 0;
0139     end
0140     avw.hdr.hist.smin       = o_hdr.hist.smin;
0141     avw_hdr_write(avw);
0142 
0143 elseif (o_filetype == 1) && (m_filetype == 1)
0144     % --- NIfTI to NIfTI case
0145 
0146     % There is nothing to do.
0147 elseif (o_filetype == 1) && (m_filetype == 0)
0148     % --- NIfTI to Analyze case
0149     if ishandle(h), delete(h); end
0150     error('NIfTI file is processed by SPM2. This might occur by program bug.');
0151 end
0152 
0153 % Remove bias correction info file
0154 if exist(bias_info_file, 'file') == 2
0155     delete(bias_info_file);
0156 end
0157 
0158 % Remove SPM specific file
0159 if exist(bias_corrected_mat, 'file') == 2
0160     delete(bias_corrected_mat);
0161 end
0162 
0163 % Output filename
0164 image_file_out = prefix;
0165 
0166 disp('Bias correction is finished.');
0167 if ishandle(h), delete(h); end
0168 
0169 catch
0170 if ishandle(h), delete(h); end
0171 e = lasterror;
0172 error(e.message);
0173 end

Generated on Tue 27-Aug-2013 11:46:04 by m2html © 2005