image thumbnail

Virtual Touch Screen

by

 

Webcam based virtual touchscreen/keyboard

virtual_touch.m
close all;
clear all;
clc;
info=imaqhwinfo('winvideo');
x=0;
y=0;
num=imread('number_touch_screen.jpg');
vid = videoinput('winvideo',1, 'YUY2_320x240');
set(vid, 'ReturnedColorspace', 'RGB')% snapshot is rgb not preview

preview(vid)
start(vid)
while(1)
    
    data=getsnapshot(vid);
    pause(0.1);
    imC=rgb2gray(data);
   imC(:,155:165)=0;
   imC(115:125,:)=0; % current right and left corner of image
  % subplot(2,2,1);
   subplot(2,2,1);  imshow(imC);xlabel('2*2 Matrix');
 
  % a=imC(1:115,1:155); imshow(a);subplot(2,2,2);    % current right and left next of image
   %b=imC(1:115,175:315);imshow(b);subplot(2,2,3);    
 
   %c=imC(125:240,1:155);imshow(c);subplot(2,2,4);     % current right and left next of image
   %d=imC(125:240,165:320);imshow(d);	
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 % blob and color analysis
    diff_im = imsubtract(data(:,:,1), rgb2gray(data));
    
    diff_im = medfilt2(diff_im, [3 3]);
    
        % Convert the resulting grayscale image into a binary image.
    diff_im = im2bw(diff_im,0.18);
    subplot(2,2,2);imshow(diff_im);xlabel('Difference Image'); 
    
    % Remove all those pixels less than 300px
    diff_im = bwareaopen(diff_im,300);
    
    % Label all the connected components in the image.
    bw = bwlabel(diff_im, 8);
    
    % Here we do the image blob analysis.
    
    stats = regionprops(bw, 'BoundingBox', 'Centroid');
    
    % Display the image
    subplot(2,2,3);imshow(data);xlabel('RebObject Detection');
    
   hold on
    
    for object = 1:length(stats)
        
        bc = stats(object).Centroid;
        y=round(bc(1))
        x=round(bc(2))
    
        plot(bc(1),bc(2), '-m+')
        a=text(bc(1)+15,bc(2), strcat('X: ', num2str(round(bc(1))), '    Y: ', num2str(round(bc(2)))));
        set(a, 'FontWeight', 'bold', 'FontSize', 12, 'Color', 'yellow');
        
    end
    hold off
   
    
  % a=imC(1:115,1:155); 
  %b=imC(1:115,175:315);
 
   %c=imC(125:240,1:155); 
   %d=imC(125:240,165:320);
   
    %x=row 320 , 160
   %y=column 240, 120
  
   % y has 290
   if x ~= 0 && y ~= 0
       
    if x < 115 && y < 165
         disp('one');
         imC(1:115,1:155 )=0;
        
      elseif x < 115 &&  y >165 %%left down
         disp('two');
         imC(1:115,155:320)=0; 
         
      elseif  x > 115  && y<165
         disp('three');
         imC(115:240,1:155)=0;
         
         
      elseif x > 115 && y>165
         disp('four');
         imC(115:240,155:320)=0;
    end
        
    subplot(2,2,1);  imshow(imC);
       pause(0.1);
    else
        
    end    
     x=0;y=0;
end

Contact us