% mexVTrackPtsGrabim2  Interactively start trackers with mouse. 

if(~exist('xvui_'))xvui;end; % xvuig00 <- Gain 00

figure(2)
clf

plot_transform=[-1 0; 0 1]; % Rearranges scaling

main_id=xvui_.id(1);

if 1 % 0 To rerun using same trackers

y_plot=plot_transform*MexVision('get',main_id);
ax=[y_plot(1) y_plot(1) y_plot(2) y_plot(2)];
for(count=1:10)
 y_plot=plot_transform*MexVision('get',main_id);
 if(y_plot(1)<ax(1))ax(1)=y_plot(1);end;
 if(y_plot(1)>ax(2))ax(2)=y_plot(1);end;
 if(y_plot(2)<ax(3))ax(3)=y_plot(2);end;
 if(y_plot(2)>ax(4))ax(4)=y_plot(2);end;
 count
 delay(1);
end
end

input('initialize the rest of the trackers');

% NrFrames = 16;
 NrFrames = 64;
%  NrFrames = 128;
% NrFrames = 256;
%NrFrames = 512;
%NrFrames = 20;
y = MexVision('get')
NrY = length(y)/2;
clear W
W = zeros(2*NrFrames,NrY);
mexV = MexVision('frame');
% mexVims = uint8(zeros([size(mexV),NrFrames]));
clear mexVims
mexVims(:,:,:,NrFrames) = mexV; % Force whole array to be pre-allocated
figure(2)
clf
axis ij
grid

delay(3)
figure(2)
tic
for count = 1:NrFrames
count
  y = MexVision('get');
  plot(y_plot(1,:),y_plot(2,:),'*k');
  axis(ax);
  hold on;
  y_plot =[plot_transform*MexVision('get',main_id),y_plot];
  plot(y_plot(1,1),y_plot(2,1),'*r');
  hold off;

  if(y_plot(1)<ax(1))ax(1)=y_plot(1);end;
  if(y_plot(1)>ax(2))ax(2)=y_plot(1);end;
  if(y_plot(2)<ax(3))ax(3)=y_plot(2);end;
  if(y_plot(2)>ax(4))ax(4)=y_plot(2);end;
  
  drawnow;
%  images(:,:,:,count) = YUVtoRGB(grabimage);
  mexVims(:,:,:,count) = MexVision('frame'); 
%  disp(char(7))
  pause(0.8)
  W([count, count+NrFrames],:) = [y([1:2:2*NrY]),y([2:2:2*NrY])]';
end

%figure(3); imshow(mexVims(:,:,1,1));hold on
%for(i=1:size(W,1)/2)text(2*W(NrFrames+1,i),2*W(1,i),num2str(i));end;
%for(i=1:size(W,1)/2);plot(2*W(NrFrames+1,i),2*W(1,i),'w+');end; hold off


