0001 function [image_file_out, m_filetype] = vb_bias_correction_by_spm(image_file)
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
0028
0029
0030
0031
0032 if ~exist('image_file', 'var')
0033 [this] = file_dialog;
0034
0035 this = set(this, 'file_extensions', {'.nii', '.hdr'});
0036
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
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
0060
0061 h = msgbox('Now doing bias correction...');
0062
0063
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
0083 bias_corrected_hdr = [prefix, '.hdr'];
0084 bias_corrected_file = [prefix, '.img'];
0085 org_file = [p_, filesep, f_, '.img'];
0086 case 1
0087 bias_corrected_hdr = [prefix, '.hdr'];
0088 bias_corrected_file = [prefix, '.img'];
0089 org_file = [p_, filesep, f_, '.img'];
0090 case 2
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
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
0120
0121
0122 [m_hdr, m_filetype] = load_nii_hdr_cbi(bias_corrected_hdr);
0123
0124 if (o_filetype == 0) && (m_filetype == 0)
0125
0126 avw = avw_hdr_read(image_file);
0127 o_hdr = avw.hdr;
0128
0129
0130
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
0137
0138
0139
0140
0141
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
0156
0157
0158 elseif (o_filetype == 2) && (m_filetype == 2)
0159
0160 prefix = [prefix, '.nii'];
0161 elseif (o_filetype == 1) && (m_filetype == 0)
0162
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
0168 if exist(bias_info_file, 'file') == 2
0169 delete(bias_info_file);
0170 end
0171
0172
0173 if exist(bias_corrected_mat, 'file') == 2
0174 delete(bias_corrected_mat);
0175 end
0176
0177
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