

%load data set
load('C:\Data\BioPhys480\11-10-19\Anonymous-2011-10-19-18-58.mat')

gain=25;
offset=118;
boxsize=10;
minphotons=4;
PSFsigma=1.5;

%correct gain and offset
sequence=(sequence-offset)/gain;

%clip negative values
sequence(sequence<0)=0;

%show sequence
%use 'n' and 'p' for 'next' and 'previous' to move through the z-direction
dipshow(sequence)

%grab a single frame:
f=sequence(:,:,17);
f=squeeze(f)

%smooth using a gaussian kernel
f_smooth=gaussf(f)

%find maximum regions
m=(maxf(f_smooth,boxsize)==f_smooth)&(f_smooth>minphotons)
cntrs=findcoord(m)

%cut out a box
cntr=cntrs(1,:)
st=cntr-boxsize/2;
en=st+boxsize-1;
roi=f(st(1):en(1),st(2):en(2))

%setup for 'fminsearch'

%the first input to 'BlobMLE_Fast' must be a 1D vector, matlab variable type
minfunction=@BlobMLE_Fast;
options = optimset('Display','iter');

data=roi;
%setting an initial guess
X0(1)=boxsize/2;
X0(2)=boxsize/2;
X0(3)=max(data)*(2*pi*PSFsigma^2);
X0(4)=min(data);

%fminsearch iterates to minimize 'fval'
xmatrix=xx(data);
ymatrix=yy(data);
[params,fval]=fminsearch(minfunction,X0,options,data,PSFsigma,xmatrix,ymatrix); 

%show data and model in color image
[fval model]=BlobMLE_Fast(params,data,PSFsigma,xmatrix,ymatrix);
joinchannels('RGB',model,data)




