mouse pointer control by light source

by

 

using color sgregation, mouse pointer is controlled by tracking a red led... Tuning is required

gray.m
jRobot=java.awt.Robot;
vid = videoinput('winvideo',2,'RGB24_640x480');
set(vid,'TriggerRepeat',Inf);
vid.FrameGrabInterval = 1;
start(vid);
while(vid.FramesAcquired<=500) % Stop after 100 frames
    data = getdata(vid,2);
    j1=0.2989*data(:,:,1,1) + 0.587 *data(:,:,2,1) + 0.114 * data(:,:,3,1);
    j2=0.2989*data(:,:,1,2) + 0.587 *data(:,:,2,2) + 0.114 * data(:,:,3,2);
    d1=j1>240;
    d2=j2>240;
    d=d2-d1;
    [label, num] = bwlabel(d,4);
    stats = regionprops (label, 'basic');
    mp = find([stats.Area]<300 & [stats.Area]>120 );
    imshow(d);
    if(size(mp))
    x=stats(mp).Centroid;
    x=x.*2;
    jRobot.mouseMove(x(1),x(2));
    end
end
stop(vid);

Contact us