You are on page 1of 3

[Jans Hendry / EE&IT UGM, Indonesia]

August 27, 2011

MOVING OBJECT DETECTION BASED ON ITS COLOUR USING MATLAB

Ok, after doing some experiment with matlab about how to detect moving object by its colour, i decided that i have already fix codes for this issue. This code will show you how you can possibly detect object around you by its colour. While developing this code, i found how hard is to define absolute value for some parameters to make good detection. As we know well, when coloured object hit by some light it become bright. Matlab become unreliable for this case. So i tried to experiment some value based on actual condition around me. You should notice this condition, so this value may change these values to match with your condition. Ok, let stop having this chit chat... Below, i will show you my codes and short explanation to each code line. My advice is please make some improving to the code to make you understand better about the theory involved in making this code. Lets clear all workspace and hardware object in our program
clear all; close all; clc; imaqreset;

Dont forget to make codes for taking camera information


caminf = imaqhwinfo; mycam = char(caminf.InstalledAdaptors(end)); mycaminfo = imaqhwinfo(mycam); resolution = char(mycaminfo.DeviceInfo.SupportedFormats(end));

Prepare your camera by setting some properties needed to acquire data and start object to get data
vid = videoinput(mycam, 1, resolution); set(vid, 'FramesPerTrigger', Inf); set(vid, 'ReturnedColorspace', 'rgb'); vid.FrameGrabInterval = 2; framesneed=300; start(vid)

These codes show main process starting by flipping frame acquired, filtering, finding centroid, and last but not least is flushing each taken frame from memory.
while(vid.FramesAcquired<=framesneed)

This code line express how to get single frame of video


RGB = getsnapshot(vid);

[Jans Hendry / EE&IT UGM, Indonesia]

August 27, 2011

This code line express how to flip each frame, i found this problem in my webcam
R=RGB(:,:,1); R=fliplr(R); G=RGB(:,:,2); G=fliplr(G); B=RGB(:,:,3); B=fliplr(B); RGB=cat(3,R,G,B);

This code line express how to estimate certain RED value. You may notice that we are dealing with RED object here. If you want to detect other colour (RGB) please change R to G or B.
% processing R = R - G/2 - B/2; bw = R > 40;

This code line express some filtering technique i used. Median filter is one of most used filter. Also using bwareaopen i tried to eliminate some pixel less than 20 in size.
bw = medfilt2(bw, [3 3]); bw = bwareaopen(bw, 20); bw=bwconncomp(bw,8);

This code line express how to find centroid or center of mass of an object. Then we display our image that catched by camera.
stats = regionprops(bw,{'Centroid'}); imshow(RGB) hold on

You must notice, that there will be cases where matlab detect more than one centroid in one object or same object. Thats why we choose only one centroid that is quite to represent the object.
% placing centroid if length(stats)>1 cent = stats(1).Centroid; plot(cent(1),cent(2), '-go','MarkerFaceCOlor', 'g', 'MarkerSize',10); end hold off flushdata(vid); % to clear memory end

Stopping object doing its job and clearing all workspace.


stop(vid); stoppreview(vid); delete(vid); imaqreset;

[Jans Hendry / EE&IT UGM, Indonesia]

August 27, 2011

You can put bounding box also in your program. There is a lot of amazing thing you can do base on this program, TRY SOME!!!... Actually, you can use this program also to detect object in static image base on its colour. So you can make new project for your research. This is the result when i executed my program...

I will make new project that combine this program with controlling mouse based on the centroid detected by matlab...

~~~ THANKS ~~~

You might also like