% webcamdemo4
%
% small demo opening live image stream from webcam. Grabs initial
% image as reference image and plots pixels with larger deviation
% than a given constant. Additional functionality is possible: Alarm

% Bernard Haasdonk 9.1.2006

%wavsample = wavread('BATTLOW');
b = vcapg2;
bd = double(b);
f = figure;set(f,'doublebuffer','on'); 
for i = 1:1000; 
  a = vcapg2; 
  ad = double(a);
  cdo = bd-ad;
  cdo = sum(cdo.^2,3)>400; 
  cdo = cdo*255;
  c = uint8(cdo);
  c = imerode(c,ones(5,5));
  imshow(c); 
  A = sum(sum(c))/255;
  disp(['Fläche des Segments: ',num2str(A)]);
  if (A>5000)
    text(50+50*rand(1),50+50*rand(1),'Alarm','color','red',...
	 'fontsize', 20+50*rand(1));
    drawnow; 
%    wavplay(wavsample(1:20000,:),44100);
  end;
  drawnow; 
end
