You are on page 1of 6

Software

section

Robotic Vision sani the


o

Using Matlab
 Arindam Chakraborty, from top to bot-
Anandamoyee Sengupta, Sunanda tom and left to
Chatterjee, Nilasish Acharya right. That is, the
first component

B
uilding a robot is not only Fig. 1: Block diagram of the robotic vision and control system ‘r’ (the row) in-
a passion but also a dream creases down-
for most budding engineers. ward, while the second component ‘c’
Competitions and R&D in robotics are (the column) increases to the right (see
already being encouraged in various Fig. 2). For example, the data for the
institutions in our country. This article pixel in the third row, second column is
describes a visual sensor system used stored in the matrix element (3, 2). When
in the field of robotics for identifica- the syntax for a function uses ‘r’ and ‘c,’
tion and tracing of the object. The it refers to the pixel coordinate system.
program is designed to capture a red You can use Matlab matrix subscripting
ball through a PC-based webcam using to access the values of individual pixels.
Matlab software. Spatial coordinates. Consider a
The article does not aim at design- pixel as a square patch. From this per-
ing of electronic circuits and mechani- spective, a location such as (3.3, 2.2) is
cal parts of the robot. It describes image a spatial coordinate. Here, locations in
capturing and processing techniques, Fig. 2: Pixel coordinates an image on a plane are described in
followed by an introduction to actual terms of ‘x’ and ‘y.’ When the syntax
robotic application to trace the red ball uses ‘x’ and ‘y,’ it refers to the spatial
using the serial COM port of the PC. coordinate system. Fig. 3 shows the
coordinate convention of the spatial
Robotic vision and control coordinate system. Notice that ‘y’ in-
The whole system of making a robot creases downward.
to follow a red ball can be divided An image may be defined as a two-
into four blocks: image acquisition, dimensional f(x, y) function, where
processing of image, decision-making ‘x’ and ‘y’ are spatial coordinates, and
and motion control. the amplitude of ‘f’ at any pair of co-
Image acquisition can be achieved ordinates (x, y) is called the intensity
by using a PC-based webcam or a or gray level of the image at the point.
digital video camera. This device will The finite discrete values of the coordi-
capture the image and send it to the nates (x, y) and amplitude of ‘f’ are the
camera processor for further process- Fig. 3: Spatial coordinates digital images.
ing in the computer. Its main function An intensity image is a data matrix
is to convert the light energy received monitoring by the operator from the whose values have been scaled to rep-
into electrical signals. keyboard. resent intensities. When the elements
Image processing involves conver- Before going to the software pro- of an intensity image are of class ‘uint8’
sion of RGB colour images into gray- gram in detail, let us see the coordinate or class ‘uint16,’ they have integer val-
scale images, setting of threshold levels, systems to understand the image ac- ues in the range [0, 255] and [0, 65535],
saturation of the features into binary quisition process. respectively. The class of the elements
images and setting of cut-off values to Pixel coordinates. Generally, the used in this program is ‘uint8.’
remove noise in the binary image. most convenient method for express-
Decision-making is done through ing locations in an image is to use pixel Software program
the software program and motion con- coordinates. Here, the image is treated After installing the Matlab software,
trol through either software or constant as a grid of discrete elements, ordered you will see its icon on your compu-

w w w. e f y m ag . co m e l e c t ro n i c s f o r yo u • M ay 2 0 0 9 • 8 5
software
section
dev_info =
Table I
Instructions for Image Acquisition DefaultFormat: ‘RGB24_640x480’
DeviceFileSupported: 0
Instruction Descriptions DeviceName: ‘WebCam Vista #2’
preview(obj) Immediately activates a live image preview window for the video DeviceID: 1
input object ‘obj’. ObjectConstructor:
closepreview(obj) Closes the image preview window associated with image ‘videoinput(‘winvideo’, 1)’
acquisition object ‘obj’. SupportedFormats: {1x11 cell}
celldisp(c) or celldisp Again, the ‘obj = videoinput(‘a
(dev_info. SupportedFormats) Recursively displays the contents of a cell array
daptorname’, deviceID,’ format’)’
start(obj) The control starts capturing the frames. The frames captured
are stored in the memory. The control also obtains exclusive instruction constructs a video input
use of the image acquisition device associated with the video object where the ‘adaptorname’
input object ‘obj’ and locks the device configuration. Starting an string specifies the name of the
object is a necessary first step to acquire image data. device adaptor that the object ‘obj’
triggerconfig(obj, ‘manual’) Sets the trigger configuration. Here, ‘manual’ gets the image is associated with. ‘deviceID’ is the
only when the video is triggered.
identifier of the device in numerical
set(obj, ‘FramesPerTrigger’, 1) Sets the number of frames captured per trigger. Here only one
frame is captured per trigger. number. If ‘deviceID’ is not speci-
set(obj, ‘TriggerRepeat’, Inf) Sets the number of times for which the trigger can be repeated.
fied, the first available device ID is
used. The ‘format’ string specifies
flushdata(obj) Removes all data from the data acquisition engine and resets
the SamplesAvailable property to zero. the video format for the object. If the
stop(obj) Stops video capturing. format is not specified, the device
delete(obj) Deletes the object ‘obj’ default format is used. For example,
clear obj Clears the memory buffer the output of the ‘obj=videoinput
(‘winvideo’, 1, ’RGB24_640×480')’
Note: Inf- indicates that there can be any number of triggers we want. If we specify any number then we will have
to start the video capture again after we have finished triggering the specified number of times. instruction will be:
Summary of Video Input Object Using
ter’s desktop. Run the software and the instruction and press ‘Enter’ key. ‘WebCam Vista’.
screen shows the main Matlab applica- This instruction returns information Acquisition Source(s): input1 is
tion window containing the following about all the adaptors available in the available.
sub-windows: system: Acquisition Parameters: ‘input1’ is
1. Command window. In this win- ans = the current selected source.
dow, the user types Matlab commands InstalledAdaptors: {‘winvideo’} 10 frames per trigger using the
and expressions at ‘>>’ prompt and MATLABVersion: ‘7.0.1 (R14SP1)’ selected source.
the outputs of these commands are ToolboxName: ‘Image Acquisition ‘RGB24_640×480’ video data to be
displayed. Toolbox’ logged upon START.
2. Workspace browser. This win- ToolboxVersion: ‘1.7 (R14SP1)’ Grabbing first of every 1 frame(s).
dow shows variables defined in the The ‘info=imaqhwinfo’ instruction Log data to ‘memory’ on trigger.
current session and also some informa- returns information about a specific Trigger Parameters: 1 ‘immediate’
tion about them. device accessible through a particular trigger(s) on START.
3. Current directory. This window adaptor. Status: Waiting for START.
displays contents of the current direc- The output of the ‘info = 0 frames acquired since starting.
tory and its path. imaqhwinfo(‘winvideo’)’ instruction 0 frames available for GETDATA.
4. Command history window. is: Binary images (B&W). A binary
This window contains a record of info = image is a logical array of 0’s and
the commands (including both cur- AdaptorDllName:G:\MATLAB701\toolbox\ 1’s. Numeric-array images consist-
rent and previous sessions) that the imaq\imaqadaptors\win32\ ing of 0’s and 1’s are converted into
user has entered in ‘Command’ win- mwwinvideoimaq.dll’ binary, with ‘1’ indicating white
dow. AdaptorDllVersion: ‘1.7 (R14SP1)’ colour (maximum intensity) and ‘0’
5. Others. There are one or more AdaptorName: ‘winvideo’ indicating black colour (minimum
windows which are shown only when DeviceIDs: {1x0 cell} intensity):
the user activates graphics. DeviceInfo: [1x0 struct] >>imBw=[1 0 1;0 0 1;1 1 0]
Matlab has built-in adaptors to The output of the ‘dev_info = RGB images. A red, green and blue
access the camera. At the Matlab com- imaqhwinfo(‘winvideo’, 1)’ instruc- (RGB) image is an MxNx3 array of
mand prompt, write the ‘imaqhwinfo’ tion is: colour pixels, where each colour

8 6 • M ay 2 0 0 9 • e l e c t ro n i c s f o r yo u w w w. e f y m ag . co m
Software
section
pixel is a triplet corresponding to Table II
the red, green and blue components
Instructions for Image Processing
of an RGB image at a specific spatial
location. Instruction Description
An RGB image may be viewed im = imread(‘redball.jpg’) Reads the JPEG image into an image array ‘im’
as a stack of three gray-scale images size(im) Returns the row and column dimensions of an image; for example,
that, when fed into the RGB colour [m, n] = size (im);
monitors, produce a colour image. imshow(im) Displays the image on the Matlab desktop.
Eight bits are used to represent the figure, imshow (im2) Displays image ‘im2’ in another window; used when needed to display
two images in two different windows
pixel values of each component of
the image. Thus an RGB image cor- imview(im) Displays the image on the Matlab Desktop. It also shows the pixel value
at the corresponding mouse pointer
responds to 24 bits.
imwrite(A,filename,fmt) Writes the image ‘A’ to the file specified by filename in the format
specified by ‘fmt’
Detection of the red ball
tf = islogical(A) Returns logic 1 (true) if ‘A’ is a logical array and logic ‘0’ (false)
1. Capture a frame and store it in a otherwise
variable, say, ‘rgb_image’ I = rgb2gray(RGB) Converts the true-colour RGB image into the grayscale intensity image ‘I’
2. Extract the red, green and blue BW = im2bw(I) Converts the intensity image ‘I’ into B&W
components of the images and store
Note: 1. Syntax of the functions specified can be obtained from the Matlab Help.
them in variables fR, fG and fB: 2. If the image file is not in the current directory, the whole path of the file should be specified.
fR= rgb_image ( : , : , 1);%extracts 3. After every statement there should be a semicolon. Semicolon is given to suppress the output at the
command window.
the red component.
fG= rgb_image ( : , : , 2);%extracts
the green component. age, you will find that apart from the Here ‘n’ is the total number of con-
fB= rgb_image (: , : , 3);%extracts region of your red ball there are also nected components and ‘L’ is the label
the blue component. some unwanted white regions in the matrix. (Each connected component is
Here, fR, fG and fB are image ma- image. These unwanted white regions given a unique number.)
trices. In Matlab, comments are written are called ‘noise.’ The following statement:
after ‘%’ sign. Before you plot the centre of the [r, c]= find (L= = K) % K= 1, 2 …n
3. Next, find the red object in image, filter the noisy parts of the im- returns the row and column indices for
the image. (R_THRESHOLD=)140, age as follows: all pixels belonging to the Kth object:
(G_THRESHOLD=)105 and (B_ Se = strel (‘disk’, 20); % creates a rbar= mean (n);
THRESHOLD=)100 are specific flat, disk-shaped structuring element cbar= mean(c);
numbers called ‘threshold.’ The with radius 20 Variables ‘rbar’ and ‘cbar’ are the
technique for finding these numbers B= imopen (I, se);%morphological coordinates of the centre of mass.
is described later on. opening As you have already filtered the
The following statement creates a Final= imclose (B, se);%morphological image, the final image contains only
B&W image array ‘I’: closing one white region. But in case there is
I= ((fR≥140) & (fG≤105) & (fB≤100)); Morphological opening removes a computational fault due to exces-
That is, the result of logically ‘AN- those regions of an object which can- sive noise, you might have two con-
Ded’ image matrices fR, fG and fB is not contain the structuring element, nected components. So form a loop
stored in ‘I.’ smoothes object contours, breaks from ‘1’ to ‘n’ using ‘for’ statement,
If the following three conditions are thin connections and removes thin thus calculating the centre of mass
satisfied, the pixel value of the image is protrusions. Morphological closing for all objects. The syntax is:
set to ‘1’: also tends to smooth the contours for K=1:n
(i) fR≥140 if the value of the red of objects besides joining narrow If there are no components in a
component of the pixel is greater than breaks and filling long, thin gulfs frame, the control doesn’t enter the
140. and holes smaller than the structur- loop and ‘rbar’ and ‘cbar’ remain
(ii) fG≤105 if the value of the green ing element: initialised to zero.
component of the pixel is less than 5. Once you obtain the desired part, For checking the output on the
105 find the centre of the ball. computer, use the following instruc-
(iii) fB≤100 if the value of the blue The following statement computes tions:
component of the pixel is less than all the connected components in a bi- imshow (rgb_image);
100 nary image: hold on
4. Once you make the B&W im- [L, n]= bwlabel(Final), plot (cbar, rbar, ‘marker’, ‘ * ’,

w w w. e f y m ag . co m e l e c t ro n i c s f o r yo u • M ay 2 0 0 9 • 8 7
software
section
convert the information into a number
Table III
for easy computation. This can be done
Decision Table by the following code:
cbar ≥ x1 cbar ≤ x2 rbar ≥ y1 rbar ≤ y2 Binary Decimal(e=) Move e=((cbar>= x1)*2*2*2 % bit number 3
0 0 0 0 0 0 X +(cbar<= x2)*2*2 % bit number 2
0 0 0 1 01 1 X +(rbar>= y1)*2 % bit number 1
0 0 1 0 10 2 X +(rbar<= y2)) % bit number 0
0 0 1 1 11 3 X Thereafter, you can simply gener-
0 1 0 0 100 4 X ate a simple switch-case code for out-
0 1 0 1 101 5 Left putting the appropriate data through
0 1 1 0 110 6 Left serial port, to control the external
0 1 1 1 111 7 Left device such as a robot.
1 0 0 0 1000 8 X The decision table derived from the
1 0 0 1 1001 9 Right above conditions is shown in Table III.
1 0 1 0 1010 10 Right ‘X’ denotes a “don’t care” situation.
1 0 1 1 1011 11 Right Some of the cases are imaginary in the
1 1 0 0 1100 12 X
practical world. The robot will stop in
such situations.
1 1 0 1 1101 13 Forward
For calculating the dimensions of
1 1 1 0 1110 14 Backward
the frame, use the sizeof( ) function.
1 1 1 1 1111 15 Stop
This function returns the size of the
‘MarkerEdgeColor’, B ); 3. Various frame.
These statements pop-up a conditions for
window where a ‘blue’ mark is plot- detecting the po- Setting the RGB threshold
ted on the detected centre of mass sition of the ball values
of the red ball. They have been com- are: The red ball is of particular interest.
mented out in the main program for (i) If the ball The steps for setting the RGB threshold
testing. is in region 5, it values of the red ball follow:
Fig. 4: Captured frame is at the centre 1. Take at least ten snaps of the red
Plotting the position of the frame. The ball at various angles (refer Fig. 5)
of the object robot should stop moving. 2. Read each image
The position of the red ball is plotted (ii) If the ball is in region 3, it is at 3. Display the image by using ‘im-
as described below: the left of the frame. The robot should view’ function
1. Divide the frame captured by move left. 4. Note down the pixel values of
the camera (refer Fig. 4) into five (iii) If the ball is in region 4, it is at the red ball by placing the mouse
parts by means of points ‘x1’ and ‘x2’ the right of the frame. The robot should cursor at various portions of the
on X-axis and points ‘y1’ and ‘y2’ on move right. red ball. The threshold for red
Y-axis. (iv) If the ball is in region 1, it is at component (R_THRESHOLD)
2. Calculate x1, x2, y1 and y2 by the the upper part of the frame. The robot should be the least value of the red
following method: should move forward. component found in the red ball.
x1= x/2-numx; x2=x/2+numx (v) If the ball is in region 2, it is at The threshold for green compo-
y1=y/2-numy; y2=y/2+numy the lower part of the frame. The robot nent (G_THRESHOLD) should
‘numx’ and ‘numy’ are arbitrary should move backward. b e t h e max imum val ue of t h e
numbers which you have to find out. Define the above conditions in green component found in the
These depend on the size of your the code for decision making as given red ball. The threshold for blue
ball. We have taken numx=120 and below: component (B_THRESHOLD) should
numy=30 in our program. (i) cbar≥x1 (output either ‘0’ or ‘1’) be the maximum value of the blue
Calculate the coordinates of the (ii) cbar≤x2 (output either ‘0’ component found in the red ball.
centre of the frame, which is nothing or ‘1’) In the screenshot shown in Fig. 5,
but (x/2, y/2). ‘x’ is the maximum (iii) rbar≤y2 (output either ‘0’ or ‘1’) the least value of the red component
dimension of X-axis (in the program (iv) rbar≥y1 (output either ‘0’ is ‘144.’ So R_THRESHOLD can be
it is 640) and ‘y’ is the maximum di- or ‘1’) taken as ‘144.’ The maximum value
mension of Y-axis (in the program it 4. If you consider these four con- of the green component is ‘115.’ So
is 480). ditions to be four bits, you can easily G_THRESHOLD can be taken as ‘115.’

8 8 • M ay 2 0 0 9 • e l e c t ro n i c s f o r yo u w w w. e f y m ag . co m
Software
section
The maximum value of the blue com-
ponent is ‘100.’ So B_THRESHOLD can
be taken as ‘100.’
Syntax of the functions specified
can be had from the Matlab Help. If
the image file is not in the current di-
rectory, specify the whole path of the
file. A semicolon is given after every
statement to suppress the output at the
command window.

Motion control of the robot


The computer processes the image of
the red ball and sends out five different
data through its serial COM port. The
data depends on the location of the red
ball, viz, upper, lower, left, right and
centre of the frame, as captured by the
camera.
Place the PC-based (USB-based)
camera in front of the robot, so that it
acts as a visual sensor.
You have to design a robot that Fig. 5: Screenshot of the red ball
connects to the serial port of your PC.
Program the serial port as follows: >>fclose(ser); EFY-CD
1. obj = serial(‘port’); creates a Here the robot will be controlled 7. Click ‘run’ button in ‘Debug’
serial-port object associated with the by seding the following codes to the option
serial port specified by the port. If the serial port: 8. Hold the red ball in your hand
port does not exist or if it is in use, 1. Key ‘F’ for forward movement near the camera lens
you will not be able to connect the 2. Key ‘L’ for left movement 9. Move the ball left, right, for-
serial-port object to the device. The 3. Key ‘R’ for right movement ward and backward. This information
syntax is: 4. Key ‘B’ for backward movement will be displayed on your screen. For
>>ser=serial(‘COM1’); Any other code will make the robot example, if you move the ball left,
2. obj = serial(‘port’,’Property stop. The code for implementing mo- you can see ‘move left’ command on
Name’,PropertyValue,...); creates a tion control of the robot is given at the the screen
serial-port object with the specified end of this article. 10. Now, place your camera on top
property names and property values. If of your robot. The robot will trace the
an invalid property name or property Testing steps red ball by itself
value is specified, an error is returned 1. Install Matlab (minimum version: To check the serial-port data
and the serial-port object is not created. Matlab 7.0.1(R14)) communication, you can use a se-
The syntax is: 2. Install the camera along with rial-port monitor, which can be freely
>> ser= serial(‘COM1’,’BaudRate’, related software downloaded from ‘www.download.
9600,’DataBits’,8); 3. Restart your computer com/Free-Serial-Port-Monitor/3000-
3. fopen(obj); before you can 4. Run your camera software and 2212_4-70394.html’
perform a read or write operation, check the image captured by it Caution. If you want to end execu-
‘obj’ must be connected to the de- 5. Take snaps of the red ball from tion of your program, go to the Matlab
vice with the fopen function. The various angles and set the threshold command prompt and press ‘ctrl+c’
syntax is: values key. Thereafter, write ‘fclose(ser)’
>>fopen(ser); 6. When all is right, open Matlab against the prompt or else you will
4. fprintf(ser,’data’); sends data to software, go to ‘m.file’ from ‘file’ not be able to access your serial port
the serial port. The syntax is: menu bar, type the code ‘Robotvision. further.
>>fprintf(ser,’F’); m’ (given at the end of the article) EFY note. The source code of
5. fclose(obj); disconnects ‘obj’ from and save it. Alternatively, copy the this article has been included in this
the device. The syntax is: Robotvision.m file from this month’s month’s EFY-CD.

w w w. e f y m ag . co m e l e c t ro n i c s f o r yo u • M ay 2 0 0 9 • 8 9
software
section
robotvision.m
%************************************* Communication,opening serial port (rbar<=y2)) % Converting to decimal
% Red Ball Tracker – Robot Vision while(1) number
% Software used Matlab 7.0.1(R14) rgb_image = getsnapshot(vid); % end
% Algorithim used RGB Colour Detection storing image in an array variable % Decision Making Conditions
%************************************* flushdata(vid); %Flushing the buffer switch (e)
% SOME LINES HERE ARE COMMENTED OUT FOR rbar=0; case 5
FASTER PROCESSING.THE READER SHOULD UN- cbar=0; disp(‘Move
COMMENT THEM WHEN NEEDED e=0; left’),fprintf(ser,’L’);
fR=rgb_image(:,:,1);fG=rgb_ case 6
clear; image(:,:,2);fB=rgb_image(:,:,3);% Stor- disp(‘Move
clc % Clearing Matlab desktop ing RGB components of the image in left’),fprintf(ser,’L’);
vid=videoinput(‘winvideo’,1,’RGB24_ seperate arrays case 7
640X480’); % Defining the video input I=((fR>=140) & (fG<=105) & disp(‘Move
object (fB<=100)); % Converting the RGB Image left’),fprintf(ser,’L’);
set(vid,’FramesPerTrigger’,1); % Setting into binary image///Detecting only the case 9
frames per trigger red component disp(‘Move right’),fprintf
% preview(vid); %////// Showing the % Following are the steps For De- (ser,’R’);
video of the moving Ball(TO BE USED % tecting the red ball case 10
% WHILE TESTING) se=strel(‘disk’,20); disp(‘Move right’),fprintf
B=imopen(I,se); (ser,’R’);
pause(10);% Waiting for a certain time final=imclose(B,se); case 11
for the system to get initialised [L,n]=bwlabel(final); disp(‘Move right’),fprintf
rgb_image = getsnapshot(vid); % Storing %imshow(rgb_image); %////THIS IS TO (ser,’R’);
Image in an array variable BE USED ONLY WHILE TESTING case 13
[a b c]= size(rgb_image); % Determining %hold on % ////THIS IS TO BE USED disp(‘Move forward’),fprin
the size of the captured frame. ONLY WHILE TESTING tf(ser,’F’);
y=a; for k=1:n case 14
x=b; [r,c]=find(L==k); disp(‘Move
% Defining Boundaries rbar=mean(r); back’),fprintf(ser,’B’);
x1=x/2-120; cbar=mean(c); otherwise
x2=x/2+120; %plot(cbar,rbar,’Marker’,’*’,’ disp(‘Stop Moving’),fprint
y1=y/2-30; MarkerEdgeColor’,’B’,’MarkerSize’,20) f(ser,’S’);
y2=y/2+30; %////THIS IS TO BE USED ONLY WHILE TEST- end
ser=serial(‘COM1’); % Defining the speci- ING end
fied COM Port to be used e=(((cbar>=x1)*2*2*2) + fclose(ser); % closing serial port
fopen(ser); % starting serial ((cbar<=x2)*2*2) + ((rbar>=y1)*2) + 

9 0 • M ay 2 0 0 9 • e l e c t ro n i c s f o r yo u w w w. e f y m ag . co m

You might also like