% webcam_gray
%
% small demo opening live image stream from webcam. Different point
% operations can be selected.
% Klick on image for exit.

% Bernard Haasdonk 27.7.2007

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% general setting that may be changes
pa = 0.1; % pause-length after each plot
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

disp('please klick on the window to exit');

f = figure;
set(f,'doublebuffer','on'); 
set(f,'Name','Tolles Webcam Tool')

a = vcapg;

set(f,'WindowButtonDownFcn','set(gcbf,''Userdata'',1);');
set(f,'WindowButtonUpFcn','set(gcbf,''Userdata'',0);');
set(f,'Userdata',0);

% draw livestream
while 1~=0
  % get webcam image and transform to gray-image
  a = vcapg;
  da = double(a);  
  da = mean(da,3);
  da2 = da;
  
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %% here the image can be modified and point-operation can be selected
  %% by uncommenting a single line of the following:
  
  %da2 = da * 1.0;
  %da2 = da * 0.3;              
  %da2 = 255-da;                
  %da2 = 255/(100-50)*(da-50); da2 = max(min(da2,255),0); 
  %da2 = (da.^2)/255;           
  %da2 = double(da > 50)*255;   
  %da2 = floor(da / 50) * 50;   
  
  %% end of image modification
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  
  subplot(1,2,1),imshow(uint8(da)); title('original');
  subplot(1,2,2),imshow(uint8(da2)); title('processed');
  drawnow; 

  % check if button is pressed for exit or not.
  mousebutton = get(f,'Userdata');
  if mousebutton
    close(f);
    return;
  else
    pause(pa);
  end;

end
