Home > vbmeg > 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, m_filetype] = 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)
        m_filetype : bias corrected file type
                     = 0 : anaylze file(.hdr/.img)
                     = 1 : NIfTI file(.hdr/.img)
                     = 2 : NIfTI file(.nii)
 [NOTE]
   Before using this function, please set SPM5or8 path on your MATLAB.

 [SEE]
   spm_bias_estimate.m, spm_bias_apply.m
 
 2010/05/12 rhayashi Initial version
 2016/07/14 rhayashi supported NIfTI file(.nii)

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

Generated on Mon 22-May-2023 06:53:56 by m2html © 2005