Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

[Jans Hendry / EE&IT UGM Indonesia] August 23, 2011

REAL TIME EDGE DETECTOR of OBJECTS


USING MATLAB

Alright, at this time we’ll talk about how to detect edge of objects in real time video using matlab. In this
article, i’m gonna show you that there is nothing difficult algorithm we have to use. But there is
prerequisites, all articles i wrote related to video processing using matlab (article 1st to 3rd) must have been
read and understood. Because most of syntax i use here, taken from those articles.

Lets take a look at below code:

%% taking camera information


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

vid = videoinput(mycam, 1, resolution);


%%

Piece of program above shows how to acquire webcam information installed in your computer. This
adaptor will be used as object that matlab recognized as peripheral to acquire image frames to process.

Meanwhile, piece below shows you how to set attributs for our webcam.

%% setting camera properties


vid.FramesPerTrigger=Inf;
set(vid, 'ReturnedColorspace', 'grayscale')
vid.FrameGrabInterval=3;
framesneeded=300;

We set grayscale because this kind of format will be used as input for processing, rather than rgb. Because
if we set rgb format, we will need 1 more step which is grayscaling images. Also, you have to notice that
framegrabinterval used in this program is the best smallest value. If you have plan to change it smaller, it
makes program will not give best result. In case you dont agree with me, you have right to make any
changes.

Allright, now we are moving to data acquiring and processing.

%% begin acquiring data and processing


start(vid)
while(vid.FramesAcquired<=framesneeded)
data = getsnapshot(vid); % get one picture every frame
level=graythresh(data);
bw=im2bw(data,level);
% bw=medfilt2(bw,[3 3]);

1
[Jans Hendry / EE&IT UGM Indonesia] August 23, 2011

You must notice that we use single frame or image in processing. This grayscale image is converted to
binary image because we will threshold its intensity to separate objects from its background. Many
thresholding method we can use instead of one used above. Please make some experiment yourself.

bw=bwareaopen(bw, 45);
bw=edge(bw,'roberts'); % please choose other edge filter
se=strel('disk',5); % to solid line edge
bw=imclose(bw,se);
bw=~bw;

I will explain from the upper line:


- Bwareaopen means we are filtering thresholded image by removing objects less than 45 pixels.
- Edge detection using robert method. You can use other method like Susan, Prewitt, Sobel, Canny
or else. Complex method will end up in longer time consuming.
- We make edge line become solid using imclose. This imclose use structure element that defined
by strel command.
- In the last step i change intensity value between edge lines and background. From white color to
black for edge lines.

imshow(bw)
title('Object Edge Detection Real Time','fontsize',14);
xlabel('Jans Hendry - EE&IT Gadjah Mada University, Indonesia');
flushdata(vid); % to clear memory
end
stop(vid); % stop process

Ok, above piece is easy to understand. We use imshow because the data is an image in sequences. After
processing data, we must erase it from memory for each. Its important not to burden your memory. For
this purpose, we use flushdata.

%% clearing
delete(vid);
imaqreset;
%%

% originale by jans hendry


% Gadjah Mada University, Indonesia
%%

Please dont forget to delete object and reset all variabel regarding object by using imaqreset.

2
[Jans Hendry / EE&IT UGM Indonesia] August 23, 2011

Here is the result:

After reading this article and probably simulating the codes, i wish you will be able to modify it and make
some powerfull program.

Thats all dude, we’ll continue to discuss about object detection based on its color in next session... CU.

Lets share your knowledge for Indonesia... ☺

~~~ TERIMA KASIH ~~~

You might also like