%% Find background and box

%% User input
idir='ImageRepository';
fns='*.JPG'; % filenames to process
bd=50;     % boundary of cell must be even
Nb=200;    % number of frames to get background
froff=100;  % frame offset to get background

%% Setup
dr=dir([idir '/' fns]);
Nf=length(dr);
info=imfinfo([idir '/' dr(1).name]);
base=info.DateTime([3 4 6 7 9 10 12 13 15 16 18 19]);

%% get sample data
[aimg mx mn Nff info]=im2mov([idir '/' fns],froff,froff+Nb);
[Nx Ny]=size(mx);

%% find cell
box=findbox(aimg);

box=box+[bd -bd +bd -bd];
[rNx rNy]=size(mn(box(1):box(2),box(3):box(4)));

simage([mx(box(1):box(2),box(3):box(4)) mn(box(1):box(2),box(3):box(4))]);
drawnow;

%% Save results

save(['bg_' base],'box','aimg','mx','mn','Nff','info','base');

