1  %=======================================================================


2  % Copyright 20082016, LEGI UMR 5519 / CNRS UGA GINP, Grenoble, France


3  % http://www.legi.grenobleinp.fr


4  % Joel.Sommeria  Joel.Sommeria (A) legi.cnrs.fr


5  %


6  % This file is part of the toolbox UVMAT.


7  %


8  % UVMAT is free software; you can redistribute it and/or modify


9  % it under the terms of the GNU General Public License as published


10  % by the Free Software Foundation; either version 2 of the license,


11  % or (at your option) any later version.


12  %


13  % UVMAT is distributed in the hope that it will be useful,


14  % but WITHOUT ANY WARRANTY; without even the implied warranty of


15  % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the


16  % GNU General Public License (see LICENSE.txt) for more details.


17  %=======================================================================


18 


19  function DataOut=im_levels(DataIn)


20  %% set GUI config: no action defined


21  DataOut=[]; %default output field


22  if strcmp(DataIn,'*')


23  return


24  end


25  %


26  %parameters


27  np=30


28  %


29  DataOut=DataIn;%default


30 


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


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


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


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


35  %del=np/3;


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


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


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


39  %Mfiltre=fct2';


40  Mfiltre=fct2'*fct2;


41  Mfiltre=Mfiltre/(sum(sum(Mfiltre)));


42 


43  C=filter2(Mfiltre,B);


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


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


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


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


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


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


50  % figure;plot(c,n);


51 


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


53  c_max=c(i);


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


55  n=n(index);


56  c=c(index);


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


58  if isempty(i_select)


59  i_select = 1:length(c);


60  end


61  c_select=c(i_select);


62  n_select=n(i_select);


63  cmin=min(c_select);


64  cmax=max(c_select);


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


66  DataOut.AA=uint8(C);


67 


68 


69 


70  DataOut.A=uint8(C);

