Matlab Tutorial Handout

Visual Servo Control / Prof. Ricky Lee / TA Christian Ivancsits
Intelligent Control Systems / Prof. Ricky Lee / TA Chang Tzu-Yuan
Version 1.0 – 03/2010
Christian Ivancsits

......................................................................................................................... 2 2.......3 Plot styles ..3 Camera dependent options ...............................2..................................................................................................................................................................................... 5 Matlab Image Acquisition Toolbox............ 1 Matlab Visualization tools ............1 Image I/O and Displaying .................................................................1 Figures and Axes .......1 Images in Matlab ............................................................................................................................................................................................................................ 6 4..................................................................... 5 3................................................. 2 2....................................................................................................................................................................................................................................................1...................2 Image Processing Toolbox Functions ......4 Image Adjustment ................... 2 2......4 Line Styles ...............................................7 Labeling Objects ........................................................................4 Camera trigger options ............................................................................... 4 3..2 Matlab Figure..................... 1 1.................................................................3 4 Examples: Figure........2... 6 4...................................................................................................................................................1........ 6 4......................................................5 Plot Annotations ...........................2.............................................................................. 7 4................and Plot Functions ....................................................................3 Color.......................................................2...................................................... 7 I|Page .................................................2.................2 Display preview of video stream ..................................1 Create and modify video input object..................... 1 1...and Plot Functions ..........................................................................................................................................................................................................1 Figures in Matlab ............................................................ 4 3..................................................................1 Matlab Help / Documentation ..................2.........2..................................2 Matlab Editor Shortcuts .................................. 4 3...................................2 Spatial Transformations ...... 5 3................ 4 3.2 Examples: Image Acquisition ...................................................2.................. 3 2. 2 2....................................... 5 3.............................................. 4 3.....................................................................5 Image capturing ......................................................................................................................... 6 4........................................................6 Morphological Operations .......3 3 Matlab Image Processing Toolbox .............1.....................................................2............................... 4 3..............................1........................... 2 2.. 3 Examples: Image Processing .................................................................. 6 4....................................................................................................................................................................................................................................................................1 Image Acquisition Toolbox Functions ....................................................... 4 3...................Table of Contents 1 2 Matlab in General.......................................................................... 6 4.....................................................................................................................2............................ 2 2...........................................2..............................................2..................................1..................... 2 2..................................................................................................2 Axes Scaling .........and Type-Conversion........................................5 Linear Filters ........................................................................................

.............. 8 5.................1.... 10 Appendix: Example Scripts for Matlab .......................................... 11 II | P a g e .........................................................................3 Obstacle positions ............1...........................1 Matlab IPC Interface Functions .....................................................................................................................................3 Robot pose commands .....................................................................................................................................................................1.................................................1 Connect to the central IPC server .................................................................1.........................................................................................................................1 5...................................................................................................................................6 Motion commands – distance / orientation ..........................1.............................................1 Connect to the robot or mobileSim .....................1................................1................................................................................................2 Sonar sensor commands ......4 Motion parameter setup ................................................................................... 8 5......................................................................................................................................................... 10 6......................... 9 5................... 9 Matlab IPC Interface ......................... 8 Examples: Matlab IPC Interface .........................................2 Robot pose ................................ 10 6............................. 10 6.................... 9 5................................ 10 6.......................................Matlab Tutorial Handout / AC6502701 Visual Servo Control / NTUST 5 Matlab Aria Interface ............................... 8 5..............................5 Motion commands – velocity ...... 10 6..........2 7 Matlab Aria Interface Functions ........................................................................................................................1............1................................................................................................1......................4 Image of the test area (ceiling camera).................................... 8 5.......................2 6 Examples: Matlab Aria Interface ............................................. 10 6....................................... 8 5...

Furthermore. Ricky Lee / TA Christian Ivancsits Intelligent Control Systems / Prof.0 (R2007b). Please refer to the Matlab help.1 Matlab Help / Documentation The Matlab help has vital information which will assist you in finding the solution of a problem. 1 Matlab in General This document is based on Matlab version 7.Matlab Tutorial Handout / AC6502701 Visual Servo Control / NTUST Matlab Tutorial Handout Visual Servo Control / Prof. Matlab -> Help -> Contents -> Image Processing Toolbox -> Getting started 1. 1. since here only the function name and the standard parameters are provided. Ricky Lee / TA Chang Tzu-Yuan Version 1.5. The second part of this handout contains several Matlab scripts which demonstrate the use of most mentioned functions. Matlab is a very popular tool and there is abundant information on the internet. You may get excellent tutorials and introductions to Matlab (at least in English language).g. The listed functions should help to find the adequate command for a given problem.0 – 03/2010 Christian Ivancsits This document is not intended to be an introduction to Matlab.2 Matlab Editor Shortcuts There are some shortcuts that can be used for convenience: • • • • • • • “Ctrl + r” “Ctrl + t” F1 F5 F9 … comment line … un-comment line … place curser on keyword and press F1 to get Matlab help information … execute the current script … executes the high-lighted part of a script (works also with examples in the Matlab help) F12 … set / delete breakpoint in current line “Ctrl + Enter” … Evaluate cell 1|Page . However it is up to the user to refer to the Matlab help to get more detailed information. but is an (incomplete) collection of convenient and most frequently required functions of several Matlab Toolboxes. section “Contents” for detailed information on a specific toolbox: e. as well as very specific solutions for many problems.

Matlab Tutorial Handout / AC6502701 Visual Servo Control / NTUST 2 Matlab Visualization tools 2.2. • • plot(data) plot(xData.yData) … plot the values in data against the value-index … plot the values in xData against the values in yData 2.p and n define the position of the object m … number of rows n … number of columns p … index of the current axes … close the figure corresponding to the handler … close all currently open figures … set the scaling (range) of axes … places the coordinate system of the plot in the upper left corner / lower left corner 2. val) plot(data. ‘.2..2 • • Axes Scaling axis([xmin xmax ymin ymax]) axis ji / axis xy … create figure and pass on handle … set focus to the figure corresponding to the handle … get a list of all figure properties … set a certain figure property … if the current figure is hold. please refer to the Matlab help for detailed information.2. data-plots or images. plot line in the specified color.. param = [‘r’. e.’m’. incomplete collection of frequently used functions for visualizing data with Matlab. ‘property’.and Plot Functions This is a small. • • • • plot(data.’b’.1 Figures in Matlab Objects that can contain elements for data visualization in matlab are denoted figures. ‘-. Each figure can contain one or several axes.’c’] … plot dashed line … plot dash-dotted line 2|Page .-‘) plot(data. where an axes-object contains one of several types of datavisualization.n.1 • • • • • Figures and Axes h = figure figure(h) v = get(h) set(h.’k’.2 Matlab Figure.’) … plot the line with the specified width . the next plot command will draw over the current image … create an axes-object within the current figure.2. 2. ‘linewidth’.3 Plot styles See Matlab help “Basic plots and graphs” for a list of all plot styles.4 Line Styles See Matlab help “Line properties” for detailed information.’g’. where m. Here only the function name and the standard parameters are listed. param) plot(data.g. 2.p) • • close(h) close all 2. value) hold on / hold off • subplot(m.

and Plot Functions Please see Matlab scipt “Matlab_Plotting_Functions_and_Figure_Properties.’|’o’|’x’|’*’ ] plot(data.'g--x'.15) Plot Annotations title(‘sting’) xlabel(‘string) / ylabel(‘string’) legend(‘string1’.'linewidth'. ‘string2’.'markersize'. val) … plot marker with the specified size Line style attributes can also be combined: e.5 • • • • • • plot(data. ‘string’) … add title to current axis-object … add label to the x.g. title(‘sting’. y.3 Examples: Figure. ‘fontsize’.2.g. …) text(x. title(‘Greek letters: \alpha \beta) … plot Greek letters in the title xlabel(‘Values of k_1. marker) … plot markers instead of a line. marker.2. value) … set the font size of the title special characters e. k_2.g. ‘markersize’. marker = [‘. plot(data.or y-axis … insert legend …print the specified string at the given position text properties are applicable to many text-related functions(see Matlab help) e. 3|Page . k_3’) … print index as subscript 2.m”.Matlab Tutorial Handout / AC6502701 Visual Servo Control / NTUST • • • 2.

4 • • • Image Adjustment imhist(I) J = imadjust(I) J = histeq(I) … display histogram of image data … adjusts image intensity values (improves contrast) … enhance contrast using histogram equalization 4|Page .2.2. The data type of images is “uint8”. Here only the function name and the standard parameters are listed. cform) … create color transformation structure … apply color space transformation • • I2 = im2double(I) I2 = im2unit8(I) … convert to data type double … convert to data type uint8 3.2. Pixels of the image are indexed as any other matrix element.2 Image Processing Toolbox Functions This is a small.1 Images in Matlab An image is represented as a matrix. scale) B = imrotate(A.Matlab Tutorial Handout / AC6502701 Visual Servo Control / NTUST 3 Matlab Image Processing Toolbox 3. filename) … write image to file • • • imshow(I) imagesc(C) colormap gray … displays image in figure … scales displayed image in size as well as intensity-range … display image in grayscale (other color-schemes are available) • [x.y] = ginput(n) … select points from the figure using the mouse 3. incomplete collection of frequently used functions for image processing with Matlab. please refer to the Matlab help for detailed information.1 • • Image I/O and Displaying A = imread(filename) … read image from file imwrite(A.3 • • • Color. whereas the standard data type of Matlab is “double”. 3.2.2 • • • Spatial Transformations B = imresize(A. where gray scale images have the dimension “rows x cols” and color images have the dimension “rows x cols x dim” (dim = 3 for RGB mode). angle) J = imcrop(I) 3. Some functions of the image processing toolbox only accept “uint8”. 3.and Type-Conversion I = rgb2gray(RGB) … convert RGB image to grayscale hsv_image = rgb2hsv(M) … convert RGB image to HSV colormap YCBCR = rgb2ycbcr(RGB) … convert RGB image to YCbCr color space … resize image … rotate image … interactive cropping of the image • • cform = makecform(type) B = applyform(A.

g.6 Linear Filters B = imfilter(A. 5|Page .. sobel …) … find edges in grayscale image • • • • Morphological Operations Morphology Fundamentals se = strel(shape. level) level = graythresh(I) … create structuring element for erode/dilate … erodes the grayscale image IM .m”. ‘Area’. properties) … measures a set of properties for each labeled region in the label matrix L (e. n) … label connected components in binary image get corresponding indices with “[r. based on threshold … determines global threshold value Labeling and Measuring Objects L = bwlabel(BW. se) • Distance Transform D = bwdist(bw) • BW2 = bwmorph(BW.c] = find(bwlabel(BW)==2)” RGB = label2rgb(L) … convert label matrix to RGB image STATS = regionprops(L.. dilates the grayscale image IM .2.2.2. se) • IM2 = imtophat(IM. gaussian.3 Examples: Image Processing Please see Matlab scipt “Matlab_Image_Processing_Basics. operation) 3.Matlab Tutorial Handout / AC6502701 Visual Servo Control / NTUST 3. depending on the parameter … convert to binary image. se) IM2 = imopen(IM. se) IM2 = imdilate(IM. ‘Orientation’) 3.5 • • • 3. parameter) IM2 = imerode(IM.. se) • IM2 = imclose(IM..7 • • • • • Labeling Objects BW = im2bw(I. morphological opening of the grayscale image (opening = erosion followed by dilation) … morphological closing of the grayscale image (closing = dilation followed by erosion) … performs morphological top-hat filtering on the input (corrects uneven illumination when background is dark) … distance transform of binary image … applies different morphological operations to the binary image. ‘Centroid’.H) h = fspecial(type) BW = edge(I) … perform filtering of images (correlation or convolution) … create predefined response-matrix for imfilter (average.

’FrameRate’. source) dev_info. ‘property’. set(src.SupportedFormats(“your selection”) … choose image format vid = videoinput(‘winvideo’.SupportedFormats • • videoFormat = dev_info.1. value) • inspect(src) … get handler to camera properties … set the value to for certain property e. 1) … capture one frame per trigger event vid.’TriggerRepeat’.2 • • Display preview of video stream preview(vid) … show a preview of the camera stream closepreview(vid) … close the preview window 4.1 • • Create and modify video input object imaqhwinfo … determine camera adapter name (e.4 • 6|Page . 4. winvideo) info = imaqhwinfo(‘winvideo’) … determine device id of the camera • • • source = info.1.DeviceIDs{“your id”} dev_info = imaqhwinfo(‘winvideo’.1. usb webcam) and it supports many device properties.1.1 Image Acquisition Toolbox Functions This is a small.g.g. videoFormat{1}) … create video input object … get camera device id info … get camera info object … display supported formats 4.FrameGrabInterval = 1 … take every frame from the video stream 4. 4. please refer to the Matlab help for detailed information. incomplete collection of functions for capturing camera images with Matlab. Here only the function name and the standard parameters are listed. Inf) … allows triggering unlimited number of captures (infinit) set(vid.3 • Camera dependent options get(vid) … display all properties of the video input object “vid” • get(getselectedsource(vid)) … display all properties of the current camera • • src = getselectedsource(vid) set(src. ‘manual’) … set trigger to manual (command starts capturing) • • • set(vid. source.g.’FramesPerTrigger’. 30) … starts GUI for adjusting camera properties Camera trigger options config = trigger(vid) … get info about the trigger • triggerconfig(vid.Matlab Tutorial Handout / AC6502701 Visual Servo Control / NTUST 4 Matlab Image Acquisition Toolbox The Matlab image acquisition toolbox allows capturing images from a camera (e.

1.2 Examples: Image Acquisition Please see Matlab scipt “Matlab_Image_Acqisition_Basics.Matlab Tutorial Handout / AC6502701 Visual Servo Control / NTUST 4. 7|Page .5 • • • • Image capturing start(vid) trigger(vid) img = getdata(vid.1) stop(vid) … start the video stream … cause a trigger event …get one image from video object frame buffer … stop the video stream.m”. release capturing device 4.

1.) The P3DX robot has currently the IP 140. Here only the function name and the standard parameters are listed. The cycle time defines the rate at which data packets are sent to the robot by the Aria software.4 • • • • pose = position() Motion parameter setup setTransAccel(val) setTransDecel(val) setRotAccel(val) setRotDecel(val) … set the absolute pose which is stored inside the robot x [mm]. please refer to the function definition (. (Please note that the use of the Matlab IPC Interface is not possible in connection with mobileSim. To open the script in the editor you can place the cursor on the command and press right mouse button --> Open Selection. 5.. “switchSonar”. set the cycle time where cycleTime [ms] shutdown() … shutdown robot and terminate wifi connection 5. “getRobotCycleTime” and “setRobotCycleTime” were integrated by the author of this handout.y. mm. theta] in [mm. deg] … set the translational acceleration where val [mm/sec2] … set the translational deceleration where val [mm/ sec2] … set the rotational acceleration where val[deg/ sec2] … set the rotational deceleration where val[deg/ sec2] 8|Page . incomplete collection of functions for capturing controlling the robots P3DX and Amigo with Matlab.1.1 Connect to the robot or mobileSim The Matlab Aria Interface can connect to the real robot as well as to the mobileSim simulator.1. theta [deg] … read the current position from robot odometry (not from camera!) where pose = [x. • • • • • robotInit() … connect to the MobileSim simulator robotInitWifi( ‘IP_address’ ) … connect to the mobile robot. y. y [mm].3 • Robot pose commands setPosition(x..38. IP_address is <string> getRobotCycleTime() … read the cycle time of the packet transmission to the robot setRobotCycleTime(cycleTime) .118.1] sonarRange = sonarSensorRange() … get readings of the sonar sensors 5. or press “Ctrl+D”.2 • • • • Sonar sensor commands sonarSensorAngles() … returns the angles of sonar sensors sonarSensorPositions() … returns the positions of sonar sensors switchSonar(state) … switch sonar on/off where state [0.108.1 Matlab Aria Interface Functions This is a small.1.Matlab Tutorial Handout / AC6502701 Visual Servo Control / NTUST 5 Matlab Aria Interface The Matlab Aria Interface makes the functionality of the Aria software available within the Matlab environment. 5.theta) • 5. The original version was created by Luis Felipe Posada of the Technical University Dortmund. The purpose of the interface is to control the sensors and motion commands of the P3DX and Amigo robots with Matlab.m file) for detailed information. The additional functions “robotInitWifi”.

1.Matlab Tutorial Handout / AC6502701 Visual Servo Control / NTUST 5.2 Examples: Matlab Aria Interface All the practical assignments in the Visual Servo Control course you can use the computer in the lab T1-102 where all necessary packages are already installed.m” in the folder “Matlab_Aria_Interface\test”.r. 9|Page .pdf” and “Matlab Aria Interface Tutorial. However if you want to install the Matlab Aria Interface and the MobileSim simulation program you can follow the instructions in the documents “Interface Compilation Guide.6 • • • Motion commands – distance / orientation move(dist) … move straight forward/backward for the defined distance in [mm] setHeading(deg) … set heading of robot w. global coordinate system in [deg] setDeltaHeading(deg) … set heading relative to current robot pose in [deg] 5.pdf”. A simple example for the use of robot motion commands can be found in the Matlab script “demoDirectMotionCommands.1.rightVel) … set the velocity for both wheels separately [deg/sec] stop() … stop the robot 5.t.5 • • • • Motion commands – velocity setVel(linVel) … set the linear velocity where linVel [mm/sec] setRotVel(rotVel) … set the rotational velocity where rotVel [deg/sec] setVel2(leftVel.

The interface was created by the author of this handout.1.38. Please be aware that the central server has to be running on the PC that processes the image data from the ceiling camera (IP: 140.1 • • Connect to the central IPC server IPC_Init(‘server_IP’) … connect to the central server. y.91). Each row contains the coordinates of one point [x.4 • Image of the test area (ceiling camera) img = IPC_get_Cam_image() … returns the most recent image from the ceiling camera 6. before you can use the functions of the interface. 6.1.html. The functionality is based on the Inter Process Communication library by Reid Simmons of the Carnegie Mellon University.2 • Robot pose pose = IPC_get_P3DX_pose() … Returns the most recent pose of P3DX with respect to the origin of the global coordinate system (center of the image). The pose is [x.1 Matlab IPC Interface Functions The functions of the Matlab IPC Interface are listed in this section. More detailed information about the IPC library can be found in http://www.cs.cmu.118.Matlab Tutorial Handout / AC6502701 Visual Servo Control / NTUST 6 Matlab IPC Interface The Matlab IPC Interface allows receiving data messages from the server that processes the images from the ceiling camera (pose measurement and obstacle detection).3 • Obstacle positions obstacles = IPC_get_Obstacle_positions() … Returns a matrix of points which belong to an obstacle. rad].y] with respect the origin of the global coordinate system (center of the image). mm. 6.edu/afs/cs/project/TCA/www/ipc/index. 6. Please note: This function returns the angle theta in rad. 6.1. theta] in [mm. where server_ip is <string> IPC_Disconnect() … terminate the connection to the server 6.2 Examples: Matlab IPC Interface Matlab_IPC_Interface_Examples.1.m 10 | P a g e . Be aware that the image processing algorithm of the server can only detect obstacles that have relatively high pixel intensity in the ceiling camera image (“bright objects”).

Matlab Tutorial Handout / AC6502701 Visual Servo Control / NTUST 7 Appendix: Example Scripts for Matlab a) b) c) d) e) Matlab_Plotting_Functions_and_Figure_Properties.m Matlab_Image_Acquisition_Basics.m Matlab_Image_Processing_Basics.m Matlab_Aria_Interface_Motion_Commands.m 11 | P a g e .m Matlab_IPC_Interface_Example.

Sign up to vote on this title
UsefulNot useful