%progam for seting of camera with calibrating image with two dots
%Author: Branislav Micusik
%Date:8 Sep 2001

global stop;
stop = 1;

prah = 120;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%
platform ='sva9x';    %%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%

str_init=[platform '(''init'',1);'];
str_acq=['im=' platform '(''acq'',1);'];
str_end=[platform '(''end'',1);'];

eval(str_init);

tf  = figure(3);
axis off
tt(1) = text(0.3,0.1,'');
tt(2) = text(0.3,0.4,'');
tt(3) = text(0.3,0.7,'');
set(tt(1),'fontsize',50,'color','r');
set(tt(2),'fontsize',50);
set(tt(3),'fontsize',50);
fig = figure(1); 

eval(str_acq);   
imshow(im,[]); 
s = uimenu('Label','STOP','Callback','stop=0;');

set(fig,'BackingStore','off');
set(fig,'DoubleBuffer','on')
set(fig,'Position',[100 100 size(im,2)*2 size(im,1)*2]);

while(stop)
   eval(str_acq);   
%   set(get(gca,'children'),'cdata',im);
   set(get(get(fig,'CurrentAxes'),'children'),'cdata',im);
   drawnow;
   
   %points detection
   fig2=figure(2);
   im2 = im(42:152,180:195)<prah;
   [im3 num] = bwlabel(im2,8);
   stats = imfeature(im3,'Area','Centroid','Orientation');
   
   circle = [];
   for i=1:num
     if(stats(i).Area > 6) circle = [circle;stats(i).Centroid];
     end
   end
  

   if(size(circle,1) == 4)
       [xxx,ix]=sort(circle(:,2));
       circle = circle(ix,:);

       size1=sqrt((circle(1,1)-circle(2,1))^2+(circle(1,2)-circle(2,2))^2);
       size2=sqrt((circle(3,1)-circle(4,1))^2+(circle(3,2)-circle(4,2))^2);
       ratio=size1/size2;
       set(tt(1),'string',sprintf('%.02f',ratio));     
       set(tt(2),'string',sprintf('%.02f',size2));     
       set(tt(3),'string',sprintf('%.02f',size1));     
   else
	    set(tt(1),'string',sprintf(''));     
   end


   imshow(im2,[]);
    
   drawnow;
end;

eval(str_end);