%%function thresh=autothresh(data,sigma,binsize=0.1,thresholds=1);
%%By Joakim Lindblad 2000-01-25
%% 
%% Automatically thresholds an image
%%
%% data is image data
%% sigma is smoothing applied
%% binsize is resolution of histograms, default value is 0.2 (increase for speed)
%% set thresholds >1 if you want multiple thresholds, default value is 1

function thresh=autothresh(data,sigma,binsize,thresholds);

if (nargin<3)
  binsize=0.2;
end;

if (nargin<4)
  thresholds=1;
end;

data=double(data);

subplot(2,1,1);
[thresh,n,x]=chistplotone(data(:),binsize,sigma);
title(sprintf('Sigma=%.2f',sigma));

thresh=x(local_max(n,thresholds))

subplot(2,1,2);
image(uint8(imthresh(data,thresh)));

subplot;





