%% Find box and audio time

%% 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
x1=find(diff(mean(aimg,2)/max(mean(aimg,2))>.6)==1);
x2=find(diff(mean(aimg,2)/max(mean(aimg,2))>.6)==-1);
y1=find(diff(mean(aimg)/max(mean(aimg))>.6)==1);
y2=find(diff(mean(aimg)/max(mean(aimg))>.6)==-1);

x1=4*fix(x1/4);
x2=4*fix(x2/4)+3;
y1=4*fix(y1/4);
y2=4*fix(y2/4)+3;

box=[x1+bd x2-bd y1+bd y2-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([base '_pre'],'box','aimg','mx','mn','Nff','info','base');

