Tuesday, February 15, 2011

Image Processing on matlab


Many people might face some trouble getting started with machine vision on Matlab . They might be well aware of the finer nuances of microcontrollers,their programming..interfacing them with other devices....and what not. But Matlab is an entirely different field to conquer. The following code preforms some basic image processing and tracks a red object placed in front of the camera. It worked for me. Go through the code. I hope it doesn't work for you. At least that would get some of you to post queries and learn rather than performing a naive Copy >>Paste operation . I hope to reply to the posts as quickly as possible.

OBJECT TRACKING IN MATLAB

imaqhwinfo                                                     % to view all the cameras connected to the pc

a=imaqhwinfo('winvideo',2);                            % to get a detailed note on the winvideo adapter with index '2'

a.SupportedFormats                                       %gives the list of all supported formats of winvideo

obj1=videoinput('winvideo',2,'RGB24_320x240');         % set the format based on the output you get from previous statement

triggerconfig(obj1,'manual');                                           %to set the triggering mode to manual
set(obj1,'FramesPerTrigger',1);                                     % to set the frames per trigger propert of obj1 to '1'
set(obj1,'TriggerRepeat',Inf);                                        % to set number of such triggers to infinity
preview(obj1);                                                             %command to preview the output from the video input object obj1;

start(obj1);                                                                  %starting the image acquisition

sr=strel('disk',8);                                                         %defining a structuring element

pause(5);
%%

while 1                                                                       % infinite loop

trigger(obj1);                                                             %triggering the obj1 vid input object
                                                                                  %(this is like clicking camera button to snap a photo)
dat=getdata(obj1);                                                     % capturing frames from the video stream after                                             triggering and saving them in 'dat' variable

i=dat(:,:,:,1);                                                                %getting the first frame the resulatant stream of clips

%%% THE ACTUAL IMAGE PROCESSING

bw=(i(:,:,1)>200)&(i(:,:,2)<150)&(i(:,:,3)<150); %extracting the regions where the red is more than 200 but not blue and green

imshow(bw);                                                       % it is a black and white image
                                                                           % cleaning up the image and removing unwanted noise
bw=imclose(bw,sr);                                            % performing a morphological close operation

bw=imfill(bw,'holes');                                           %removing small white spots here and there

bw=bwareaopen(bw,900);                                  % filtering out all white blobs less than 900 pixels in area

L=bwlabel(bw); %labelling the blobs



S=regionprops(L,'Centroid','Area');                 % extracting the information from the labelled items

cent=S(find(([S.Area] == max([S.Area])))).Centroid;% finding the centroid of the blob with the maximum

area

text(cent(1),cent(2), 'centroid');         % placing a text at the point(the centroid) on the image where the red
object is present

% code to send the data based on the centroid co-ordinates
% values to be sent via a serial port to micro


end                                      % end of the while loop

% use ctrl + break to stop the infinite loop



CAPTURED IMAGE
FILTERED IMAGE MASK

Centroid of the image overlaid on live cam feed

No comments:

Post a Comment