1  function DataOut=im_levels(DataIn,Calib)


2  %


3  %parameters


4  np=30


5  %


6  DataOut=DataIn;%default


7 


8  B=double(DataIn.A(:,:,1));


9  windowsize=round(min(size(B,1),size(B,2))/20);


10  windowsize=floor(windowsize/2)*2+1;


11  ix=[1/2windowsize/2:1/2+windowsize/2];%


12  %del=np/3;


13  %fct=exp((ix/del).^2);


14  fct2=cos(ix/((np1)/2)*pi/2);


15  %Mfiltre=(ones(5,5)/5^2);


16  %Mfiltre=fct2';


17  Mfiltre=fct2'*fct2;


18  Mfiltre=Mfiltre/(sum(sum(Mfiltre)));


19 


20  C=filter2(Mfiltre,B);


21  C(:,1:windowsize)=C(:,windowsize)*ones(1,windowsize);


22  C(:,endwindowsize+1:end)=C(:,endwindowsize+1)*ones(1,windowsize);


23  C(1:windowsize,:)=ones(windowsize,1)*C(windowsize,:);


24  C(endwindowsize+1:end,:)=ones(windowsize,1)*C(endwindowsize,:);


25  C=tanh(B./(2*C));


26  [n,c]=hist(reshape(C,1,[]),100);


27  % figure;plot(c,n);


28 


29  [m,i]=max(n);


30  c_max=c(i);


31  [dummy,index]=sort(abs(cc(i)));


32  n=n(index);


33  c=c(index);


34  i_select = find(cumsum(n)<0.95*sum(n));


35  if isempty(i_select)


36  i_select = 1:length(c);


37  end


38  c_select=c(i_select);


39  n_select=n(i_select);


40  cmin=min(c_select);


41  cmax=max(c_select);


42  C=(Ccmin)/(cmaxcmin)*256;


43  DataOut.AA=uint8(C);


44 


45 


46 


47  DataOut.A=uint8(C);

