mouse control using webcam based on colour detected object
TRANSCRIPT
Jans Hendry / EE&IT UGM, Indonesia August 28, 2011
1
MOUSE CONTROL USING WEBCAM
BASED ON COLOUR DETECTED OBJECT
This project is combination between 2 articles i wrote before, they are:
1. Controlling mouse with java object
2. Detecting object based on its colour
So, i will only give you explanation for short. Steps you can follow to make this program are:
a. Clear all workspace and running object
b. Detect your webcam and get information needed
c. Make object from this webcam
d. Set some important attributes
e. Begin acquire data
f. Do some flipping image (frame)
g. Do process to take certain value of pixel based on its colour
h. Put some filter process to eliminate noise that could present somehow
i. Take centroid or center of mass of an object
j. Import java library for mouse controll
k. Take only 1 centroid value then use this value as new coordinat for your mouse
l. Put all this activity in just one figure
m. Flush your memory
n. Close all object and stop taking data
o. Clear all workspace
Ok, i consider all points above have explained what i did. Codes below will demonstrate all
points above. Take a look:
clear all; close all; clc;
caminf = imaqhwinfo; mycam = char(caminf.InstalledAdaptors(end)); mycaminfo = imaqhwinfo(mycam); resolution = char(mycaminfo.DeviceInfo.SupportedFormats(end));
vid = videoinput(mycam, 1, resolution); set(vid, 'FramesPerTrigger', Inf); set(vid, 'ReturnedColorspace', 'rgb'); vid.FrameGrabInterval = 3;
screenSize = get(0, 'screensize'); width=screenSize(3); height=screenSize(4);
Jans Hendry / EE&IT UGM, Indonesia August 28, 2011
2
start(vid)
while(vid.FramesAcquired<=300)
dataa = getsnapshot(vid);
% flipping each frame data1=dataa(:,:,1); data1=fliplr(data1); data2=dataa(:,:,2); data2=fliplr(data2); data3=dataa(:,:,3); data3=fliplr(data3); data=cat(3,data1,data2,data3);
% processing diff_im = imsubtract(data(:,:,1), rgb2gray(data)); % originale diff_im = medfilt2(diff_im, [3 3]); diff_im = im2bw(diff_im,0.2); diff_im = bwareaopen(diff_im,80); bw=bwconncomp(diff_im,8); stats = regionprops(bw, 'Centroid'); imshow(data)
hold on
% placing centroid if length(stats)>1 cent = stats(1).Centroid; plot(cent(1),cent(2), '-go','MarkerFaceCOlor','g','MarkerSize',10);
% moving mouse and callibration with window size import java.awt.Robot; import java.awt.event.*; mouse=Robot; mouse.mouseMove(round((width/640)*cent(1)),... round((height/480)*cent(2))); end hold off flushdata(vid); % to clear memory end stop(vid); delete(vid); imaqreset;
% originale by Jans Hendry % UGM, Indonesia
Jans Hendry / EE&IT UGM, Indonesia August 28, 2011
3
Executing codes above results:
~~~ THANKS ~~~