0001 function [th, step, y, pdf, err] = vb_back_threshold(B,pmax,Nwid,Imax)
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
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045 if ~exist('pmax','var'), pmax = 0.998; end;
0046 if ~exist('Imax','var'), Imax = 256; end;
0047 if ~exist('Nwid','var'), Nwid = 5; end;
0048
0049
0050 rmax = sqrt(2*abs(log(1-pmax)));
0051
0052 rgas = erfinv(pmax) * sqrt(2);
0053
0054
0055 rexp = log(1-pmax);
0056
0057
0058 err_max = 0.1;
0059
0060
0061 [y,x,step] = vb_mri_histgram(B(:), Imax);
0062 Bmax = max(B(:));
0063
0064
0065
0066 [ix_min, ix_max, z] = vb_histmin_estimate(y,Nwid);
0067 x0 = x(ix_min);
0068
0069
0070 [ymax ,imax]= max(y);
0071 if imax == 1, imax = 3; end;
0072
0073
0074 kmax = max(ceil(imax*rmax), ix_min);
0075 ix = 1:kmax;
0076
0077
0078 [s1,pdf1,err1] = vb_rayleigh_estimate(y,x,kmax);
0079
0080 x1 = s1*rmax;
0081
0082
0083 [x2,s2,C,err2] = vb_gaussian_estimate(y(imax:kmax),x(imax:kmax));
0084
0085
0086 pdf2 = C * exp(-0.5*((x(ix) - x2)/s2).^2);
0087
0088
0089 x2 = x2 + s2*rgas;
0090
0091
0092
0093 [a, b, c, err3] = vb_exponential_estimate(y(imax:kmax), x(imax:kmax));
0094
0095
0096 pdf3 = a + b * exp( c * x(ix) );
0097
0098 x3 = x(imax) + abs(rexp/c);
0099
0100 if err1 < err_max
0101 th = x1;
0102 pdf = pdf1;
0103 err = err1;
0104 str = 'Rayleigh';
0105 else
0106
0107 [tmp,jmode] = min([err2, err3]);
0108
0109 switch jmode
0110 case 1
0111 th = x2;
0112 pdf = pdf2;
0113 err = err2;
0114 str = 'Gaussian';
0115 case 2
0116 th = x3;
0117 pdf = pdf3;
0118 err = err3;
0119 str = 'Exponential';
0120 end
0121 end
0122
0123 fprintf('Max intensity = %g\n',Bmax)
0124 fprintf('Histgram step = %d\n',step)
0125
0126 fprintf('Rayleigh = %g (err=%g)\n',x1,err1)
0127 fprintf('Gaussian = %g (err=%g)\n',x2,err2)
0128 fprintf('Exponential = %g (err=%g)\n\n',x3,err3)
0129
0130 fprintf('%s distribution estimation\n',str)
0131 fprintf('Threshold = %g (err=%g)\n',th,err)
0132
0133 if nargout < 5, return; end;
0134
0135
0136 zmax = max(z);
0137 zmid = z(ix_max);
0138
0139
0140 ix_th = round(th/step);
0141
0142
0143 subplot(2,2,1)
0144 plot(y)
0145 hold on
0146 plot(z,'-r')
0147
0148 plot(ix, pdf1,'-c')
0149 plot(ix, pdf2,'-m')
0150 plot(ix, pdf3,'-y')
0151
0152 plot([ix_th ix_th], [0 0.5*ymax],'-r')
0153
0154 ylim([0 ymax]);
0155 xlim([0 ix_min])
0156 title('Low intensity histogram')
0157
0158
0159 subplot(2,2,2)
0160 plot(y)
0161 hold on
0162 plot(z,'-r')
0163
0164 plot(ix, pdf1,'-c')
0165
0166 plot([ix_th ix_th], [0 8*zmax],'-r')
0167
0168 ylim([0 zmax]);
0169 xlim([0 ix_min])
0170 title('Low intensity histogram')
0171
0172
0173 subplot(2,2,3)
0174 plot(y)
0175 hold on
0176 plot(z,'-r')
0177
0178 plot(ix, pdf2,'-m')
0179 plot(ix, pdf3,'-y')
0180
0181 plot([ix_th ix_th], [0 8*zmax],'-r')
0182
0183 ylim([0 zmax]);
0184 xlim([0 ix_min])
0185 title('Low intensity histogram')
0186
0187
0188
0189 subplot(2,2,4)
0190 plot(y)
0191 hold on
0192 plot(z,'-r')
0193
0194 plot(ix, pdf1,'-c')
0195 plot(ix, pdf2,'-m')
0196 plot(ix, pdf3,'-y')
0197
0198 plot([ix_th ix_th], [0 zmax],'-r')
0199
0200 ylim([0 2*zmid]);
0201 xlim([0 1.5*ix_max])
0202 title('Intensity histogram')
0203
0204 return
0205
0206 xx = x(1:kmax);
0207
0208
0209 subplot(2,2,1)
0210 plot(x,y)
0211 hold on
0212 plot(x,z,'-r')
0213
0214 plot(xx, pdf1,'-c')
0215 plot(xx, pdf2,'-y')
0216 plot(xx, pdf3,'-m')
0217
0218 plot([th th], [0 0.5*ymax],'-r')
0219
0220 ylim([0 max(y)]);
0221 xlim([0 x0])
0222 title('Low intensity histogram')
0223
0224
0225 subplot(2,2,2)
0226 plot(x,y)
0227 hold on
0228 plot(x,z,'-r')
0229
0230 plot(xx, pdf1,'-c')
0231
0232 plot([th th], [0 8*zmax],'-r')
0233
0234 ylim([0 max(z)]);
0235 xlim([0 x0])
0236 title('Low intensity histogram')
0237
0238
0239 subplot(2,2,3)
0240 plot(x,y)
0241 hold on
0242 plot(x,z,'-r')
0243
0244 plot(xx, pdf2,'-c')
0245 plot(xx, pdf3,'-y')
0246
0247 plot([th th], [0 8*zmax],'-r')
0248
0249 ylim([0 max(z)]);
0250 xlim([0 x0])
0251 title('Low intensity histogram')
0252
0253
0254
0255 subplot(2,2,4)
0256 plot(x,y)
0257 hold on
0258 plot(x,z,'-r')
0259
0260 plot(xx, pdf,'-y')
0261 plot([th th], [0 zmax],'-r')
0262
0263 ylim([0 2*zmax]);
0264 xlim([0 1.5*x(ix_max)])
0265 title('Low intensity histogram')
0266 title('Intensity histogram')
0267
0268 return
0269
0270
0271 Bhigh = max(Nhist(j0+1:end));
0272
0273 subplot(2,1,1)
0274
0275 plot(level,Nhist);
0276 hold on
0277
0278 plot([th th], [0 0.5*max(Nhist)], 'r-');
0279 plot(x0,rpdf,'-r')
0280 xlim([0 2*th]);
0281
0282 title('Low intensity histogram of MRI (Rayleigh dist)')
0283
0284 subplot(2,1,2)
0285 plot(level,Nhist);
0286 hold on
0287 plot([th th], [0 Nhist(j0)], 'r-');
0288 plot(x0,rpdf,'-r')
0289 xlim([th*0.8 Bmax]);
0290 ylim([0 Bhigh]);
0291
0292 title('Intensity histogram of MRI (Rayleigh dist)')
0293
0294 return
0295
0296
0297 [s2,pdf2,err2] = vb_rayleigh_estimate(z,x,kmax);
0298
0299 x2 = s2*rmax;
0300
0301
0302 jx = find( x >= x1);
0303 j0 = jx(1);
0304 dh = y(j0+1:end) - y(j0:end-1);
0305 jj = find( dh > 0 );
0306 j0 = j0 + jj(1) - 1;
0307 xm = x(j0);