You are on page 1of 3

Jans Hendry / EE&IT UGM, Indonesia

August 28, 2011

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. b. c. d. e. f. g. h. i. j. k. l. m. n. o. Clear all workspace and running object Detect your webcam and get information needed Make object from this webcam Set some important attributes Begin acquire data Do some flipping image (frame) Do process to take certain value of pixel based on its colour Put some filter process to eliminate noise that could present somehow Take centroid or center of mass of an object Import java library for mouse controll Take only 1 centroid value then use this value as new coordinat for your mouse Put all this activity in just one figure Flush your memory Close all object and stop taking data 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


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);

August 28, 2011

% 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


Executing codes above results:

August 28, 2011

~~~ THANKS ~~~