Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Topics..
2
APPLICATIONS: Plotting functions .. Image Processing Basics .. Robotics Applications .. GUI Design and Programming
Topics..
3
APPLICATIONS: Plotting functions .. Image Processing Basics .. Robotics Applications .. GUI Design and Programming
MATLAB
4
computation. It was originally designed for solving linear algebra type problems using matrices. Its name is derived from MATrix LABoratory.
MATLAB has since been expanded and now has built-in functions for solving problems requiring data analysis, signal processing, optimization, and several other types of scientific computations. It also contains functions for 2-D and 3-D graphics and animation.
MATLAB
5
MATLAB
6
somewhat like UNIX. A prompt appears on the screen and a MATLAB statement can be entered. When the <ENTER> key is pressed, the statement is executed, and another prompt appears.
If a statement is terminated with a semicolon ( ; ), no
results will be displayed. Otherwise results will appear before the next prompt.
MATLAB
8
To get started, type one of these commands: helpwin, helpdesk, or demo a=5; b=a/2 b= 2.5000
Variable names ARE case sensitive Variable names can contain up to 63 characters (as of
Default variable name for results Value of Smallest incremental number Infinity Not a number e.g. 0/0 i = j = square root of -1 The smallest usable positive real number The largest usable positive real number
Topics..
11
APPLICATIONS: Plotting functions .. Image Processing Basics .. Robotics Applications .. GUI Design and Programming
or or or or
a+b a-b
a=b (assign b to a)
>> ... , % ;
prompt continue statement on next line separate statements and data start comment which ends at end of line (1) suppress output (2) used as a row separator in a matrix specify range
Less Than < Less Than or Equal Greater Than Greater Than or Equal Equal To Not Equal To
not and or
~ & |
MATLAB Matrices
16
MATLAB Matrices
17
a_value=23
a_value = 23
MATLAB Matrices
18
vector can be created in MATLAB as follows (note the commas): rowvec = [12 , 14 , 63] rowvec = 12 14 63
MATLAB Matrices
19
MATLAB Matrices
20
Extracting a Sub-Matrix
21
smaller matrix by specifying the names of both matrices and the rows and columns to extract. The syntax is: sub_matrix = matrix ( r1 : r2 , c1 : c2 ) ; where r1 and r2 specify the beginning and ending rows and c1 and c2 specify the beginning and ending columns to be extracted to make the new matrix.
MATLAB Matrices
22
A column vector can be extracted from a matrix. As an example we create a matrix below:
matrix=[1,2,3;4,5,6;7,8,9] matrix = 1 2 3 4 5 6 7 8 9
col_two=matrix( : , 2) col_two = 2 5 8
MATLAB Matrices
23
matrix=[1,2,3;4,5,6;7,8,9]
matrix =
the matrix and make a row vector. Note that the 2:2 specifies the second row and the 1:3 specifies which columns of the row.
1 4 7
2 5 8
3 6 9
rowvec=matrix(2 : 2 , 1 : 3) rowvec = 4 5 6
Topics..
24
APPLICATIONS: Plotting functions .. Image Processing Basics .. Robotics Applications .. GUI Design and Programming
Use of M-File
25
which do not accept input arguments or return output arguments. They operate on data in the workspace. which can accept input arguments and return output arguments. Internal variables are local to the function.
Functions,
If you include ; at the end of each statement, result will not be shown immediately
>> load mydata.dat; % loads file into matrix. % The matrix may be a scalar, a vector, or a % matrix with multiple rows and columns. The % matrix will be named mydata. >> size (mydata) % size will return the number % of rows and number of % columns in the matrix >> length (myvector) % length will return the total % no. of elements in myvector
Topics..
28
APPLICATIONS: Plotting functions .. Image Processing Basics .. Robotics Applications .. GUI Design and Programming
clear x y
clc
List known variables List known variables plus their size >> help sqrt Help on using sqrt >> lookfor sqrt Search for keyword sqrt in m-files >> what a: List MATLAB files in a: Clear all variables from work space Clear variables x and y from work space Clear the command window
which test
List all m-files in current directory List all files in current directory Same as dir Display test.m in command window Delete test.m Change directory to a: Same as cd Show current directory Display directory path to closest
test.m
xor (exclusive or) Ex: xor (a, b) Where a and b are logical expressions. The xor operator evaluates to true if and only if one expression is true and the other is false. True is returned as 1, false as 0. any (x) returns 1 if any element of x is nonzero all (x) returns 1 if all elements of x are nonzero isnan (x) returns 1 at each NaN in x isinf (x) returns 1 at each infinity in x finite (x) returns 1 at each finite value in x
if
expression1 % is true % execute these commands elseif expression2 % is true % execute these commands else % the default % execute these commands end
A for loop in MATLAB for x = array for ind = 1:100 b(ind)=sin(ind/10) end while loop in MATLAB while expression while x <= 10 % execute these commands end
x=0.1:0.1:10; b=sin(x); - Most of the loops can be avoided!!!
a=3; b=[1, 2, 3; 4, 5, 6] b= 1 2 3 4 5 6 c = b / a % Divide each element of b by a c= 0.3333 0.6667 1.0000 1.3333 1.6667 2.0000
Given A:
MATLAB Toolboxes
39
Signal Processing Image Processing Communications System Identification Wavelet Filter Design Control System Fuzzy Logic Robust Control -Analysis and Synthesis LMI Control Model Predictive Control
MATLAB Demo
40
Topics..
42
APPLICATIONS: Plotting functions .. Image Processing Basics .. Robotics Applications .. GUI Design and Programming
Plot
PLOT Linear plot.
PLOT(X,Y) plots vector Y
versus vector X
SEMILOGY, TITLE, XLABEL, YLABEL, AXIS, AXES, HOLD, COLORDEF, LEGEND, SUBPLOT...
43
Plot Properties
Example
44
Hold
Example
plot and all axis properties so that subsequent graphing commands add to the existing graph.
HOLD OFF returns to the
default mode
HOLD, by itself, toggles the
hold state.
45
Subplot
SUBPLOT Create axes in tiled positions.
SUBPLOT(m,n,p), or
SUBPLOT(mnp), breaks the Figure window into an m-by-n matrix of small axes
Example
x = [-3 -2 -1 0 1 2 3]; y1 = (x.^2) -1; % Plot y1 on the top subplot(2,1,1); plot(x, y1,'bo-.'); xlabel('x values'); ylabel('y values'); % Plot y2 on the bottom subplot(2,1,2); y2 = x + 2; plot(x, y2, 'g+:');
46
Figure
FIGURE Create figure window.
FIGURE, by itself, creates a
Surface Plot
x = 0:0.1:2; y = 0:0.1:2; [xx, yy] = meshgrid(x,y); zz=sin(xx.^2+yy.^2); surf(xx,yy,zz) xlabel('X axes') ylabel('Y axes')
48
3 D Surface Plot
contourf-colorbar-plot3-waterfall-contour3-mesh-surf
49
Convolution
50
The behavior of a linear, continuous-time, time-invariant system with input signal x(t) and output signal y(t) is described by the convolution integral - h(t), assumed known, the response of the system to a unit impulse input
For example,
x = [1 1 1 1 1]; [1 1 1 1 1] h = [0 1 2 3]; [3 2 1 0] conv(x,h) yields y = [0 1 3 6 6 6 5 3] stem(y);
ylabel(Conv'); xlabel(sample number);
Topics..
51
APPLICATIONS: Plotting functions .. Image Processing Basics .. Robotics Applications .. GUI Design and Programming
that extend the capability of the MATLAB numeric computing environment. The toolbox supports a wide range of image processing operations, including:
Geometric operations Neighborhood and block operations Linear filtering and filter design Transforms Image analysis and enhancement Binary image operations Region of interest operations
Indexed Images
[x,map] = imread('trees.tif'); imshow(x,map);
54
Intensity Images
image = ind2gray(x,map); imshow(image);
55
Binary Images
imshow(edge(image));
56
RGB Images
57
Image Display
58
image
- create and display image object imagesc - scale and display as image imshow - display image colorbar - display colorbar getimage- get image data from axes truesize - adjust display size of image zoom - zoom in and zoom out of 2D plot
Image Conversion
59
Gray2ind
im2bw
Im2double Im2uint8
Im2uint16
Ind2gray mat2gray rgb2gray rgb2ind
- intensity image to index image - image to binary - image to double precision - image to 8-bit unsigned integers - image to 16-bit unsigned integers - indexed image to intensity image - matrix to intensity image - RGB image to grayscale - RGB image to indexed image
GEOMETRIC OPERATIONS
60
imcrop crops an image to a specified rectangle. imcrop displays the input image and waits for you to specify the crop rectangle with the mouse.
IMAGE ENHANCEMENT
61
Adjust intensity
imadjust histeq
TRANSFORMS
62
Fourier Transform
-fft2, fftshift, ifft2
Radon Transform
-radon, iradon, phantom
Topics..
63
APPLICATIONS: Plotting functions .. Image Processing Basics .. Robotics Applications .. GUI Design and Programming
Robotics Application
64
Robotics Application
65
Concept take image filter using medfilt2 take the subarray of ball region, find cluster of max area ,find its centroid take the subarray of robot region , find cluster of max area, find its centroid interpolate the ball using the ball's current and previous coordinate , give output
Robotics Application
66
MATLAB Code
parport=digitalio('parallel','LPT1'); addline(parport,0:7,'out'); ball_x_prev = 1; ball_y_prev = 1; while (1) % code for acquiring image filtered_image=medfilt2(bw,[3 3]); % filter the image region_ball = filtered_image[20:460,10:575]; % ball region region_bot = filtered_image[20:460,575:630]; % robot region label = bwlabel(region_ball,4); %label the clusters in the region data = regionprops(label,'basic'); % data ontains properties of clusters in the region object = find([data.Area]==max([data.Area])) % object conatains the label of the cluster with max area
Robotics Application
67
ball_x = data(object).Centroid(1); ball_y = data(object).Centroid(2); %x coordinates of the ball
label = bwlabel(region_bot,4); %label the clusters in the region data = regionprops(label,'basic'); % data ontains properties of clusters in the region object =find([data.Area]==max([data.Area])) % object conatains the label of the cluster with max area robot_x = data(object).Centroid(1); robot_y = data(object).Centroid(2); %x coordinates of the bot
% Algorithm for movement of robot if (ball_x > ball_x_prev) % the ball is returning to the bot % Do something if (robot_y > y_proj) % Do something else Etc.
Topics..
68
APPLICATIONS: Plotting functions .. Image Processing Basics .. Robotics Applications .. GUI Design and Programming
What is GUI: A graphical user interface (GUI) is a user interface built with graphical objects such as Buttons Text fields Sliders Menus If the GUI is designed well-designed, it should be intuitively obvious to the user how its components function.
Axes
static text
70
Frames
Checkbox
Slider
Edit text
Radio Buttons
Push Buttons
Guide Editor
Result Figure
Property Inspector
Thanks
73
Questions ??