0001 function [image_file_out] = 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 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
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
0056
0057 h = msgbox('Now doing bias correction...');
0058
0059
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
0078 case 1
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
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
0114
0115
0116 [m_hdr, m_filetype] = load_nii_hdr(bias_corrected_hdr);
0117
0118 if (o_filetype == 0) && (m_filetype == 0)
0119
0120
0121
0122
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
0129
0130
0131
0132
0133
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
0145
0146
0147 elseif (o_filetype == 1) && (m_filetype == 0)
0148
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
0154 if exist(bias_info_file, 'file') == 2
0155 delete(bias_info_file);
0156 end
0157
0158
0159 if exist(bias_corrected_mat, 'file') == 2
0160 delete(bias_corrected_mat);
0161 end
0162
0163
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