Presentation is loading. Please wait.

Presentation is loading. Please wait.

Matlab Tutorial.

Similar presentations


Presentation on theme: "Matlab Tutorial."— Presentation transcript:

1 Matlab Tutorial

2 This introduction will give
a brief overview, it’s not a MATLAB tutorial ! Some basic ideas Main advantages and drawbacks compared to other languages

3 What is MATLAB?

4 Matlab Language Features
A high-level language for: matrix calculations, numerical analysis, scientific computing Signal Processing GUI high-performance language for technical computing computation, visualization, and programming in an easy-to-use environment Available at ECE PSU. Mathematica: More concerned with symbolic math, but increasing overlap between the two Some language features of MATLAB No variable declarations Automatic memory management (but preallocation helps) Variable argument lists control function behavior Vectorized: Can use for loops, but largely unnecessary (and less efficient)

5 Typical uses of MATLAB Math and computation Algorithm development
Modelling Simulation Prototyping Data analysis, exploration, and visualization Scientific and engineering graphics Application development, including Graphical User Interface building

6 Introduction to MatLab: Image Processing
Most of the programming operations have as input or output a matrix or a vector. Images are often represented as matrices. - MatLab is a powerful tool to manipulate graphics and images.

7 Matlab Matrices

8

9 Why MATLAB? A good choice for vision program development because:
Easy to do very rapid prototyping Quick to learn, and good documentation A good library of image processing functions Excellent display capabilities Widely used for teaching and research in universities and industry Another language to impress your boss with !

10 We will write the final software using C++ and OpenCv
Why not MATLAB? Has some drawbacks: Slow for some kinds of processes Not geared to the web Not designed for large-scale system development We will write the final software using C++ and OpenCv

11 MATLAB Components MATLAB consists of: The MATLAB language
a high-level matrix/array language with control flow statements, functions, data structures, input/output, and object-oriented programming features. The MATLAB working environment the set of tools and facilities that you work with as the MATLAB user or programmer, including tools for developing, managing, debugging, and profiling Handle Graphics the MATLAB graphics system. It includes high-level commands for two-dimensional and three-dimensional data visualization, image processing, animation, and presentation graphics. …(cont’d)

12 MATLAB Components … The MATLAB function library.
a vast collection of computational algorithms ranging from elementary functions like sum, sine, cosine, and complex arithmetic, to more sophisticated functions like matrix inverse, matrix eigenvalues, Bessel functions, and fast Fourier transforms as well as special image processing related functions The MATLAB Application Program Interface (API) a library that allows you to write C and Fortran programs that interact with MATLAB. It include facilities for calling routines from MATLAB (dynamic linking), calling MATLAB as a computational engine, and for reading and writing MAT-files.

13 Some facts for a first impression
Of MATLAB Everything in MATLAB is a matrix ! MATLAB is an interpreted language, no compilation needed (but possible) MATLAB does not need any variable declarations, no dimension statements, has no packaging, no storage allocation, no pointers Programs can be run step by step, with full access to all variables, functions etc.

14 Entering Variables Entering a vector, matrix V = [10, 4.5, 1];
Without semi-colon, input is echoed (this is bad when you’re loading images!) Comma to separate statements on same line size: Number of rows, columns

15 Images can be treated as matrices !

16 Simple operations on Matrices
>> B=A' B = >>b=A(1,:) b =

17 Images and Matrices X Y A = 2 3 5 6 7 8 9
Accesing image elements (row, column) >> A(2,1) ans = 4 : can be used to extract a whole column or row >> A(:,2) ans = 2 5 8 or a part of a column or row >> A(1:2,2) A =

18 Image Arithmetic: matrix versus elementwise
Arithmetic operations such as addition, subtraction, multiplication and division can be applied to images in MATLAB +, -, *, / performs matrix operations >> A+A ans = >> A*A ans = To perform an elementwise operation use . (.*, ./, .*, .^ etc) >> A.*A ans = This is normal matrix multiplication A = This is elementwise matrix multiplication Dot means elementwise operation

19 A simple example of Plotting
From zero Step of loop plot t = 0:pi/100:2*pi; y = sin(t); plot(t,y) To two-pi 2*pi sinus

20 What does Matlab code look like?
Another simple example: t = 0:pi/100:2*pi; y = sin(t); plot(t,y) Remember: EVERYTHING IN MATLAB IS A MATRIX ! creates 1 x 200 Matrix Argument and result: 1 x 200 Matrix

21 What does Matlab code look like?
A simple example: a = 1 while length(a) < 10 a = [0 a] + [a 0] end which prints out Pascal’s triangle: 1 1 1 1 2 1 (with “a=” before each line). [a 0] 1 2 1 [0 a] 1 2 1

22 Image Processing Toolbox

23 Lots of images for testing your algorithms jpg
bmp These images can be found in some tools or on internet

24 Formats of Images in MATLAB
MATLAB can import/export several image formats BMP (Microsoft Windows Bitmap) GIF (Graphics Interchange Files) HDF (Hierarchical Data Format) JPEG (Joint Photographic Experts Group) PCX (Paintbrush) PNG (Portable Network Graphics) TIFF (Tagged Image File Format) XWD (X Window Dump) MATLAB can also load raw-data or other types of image data Data types in MATLAB Double (64-bit double-precision floating point) Single (32-bit single-precision floating point) Int32 (32-bit signed integer) Int16 (16-bit signed integer) Int8 (8-bit signed integer) Uint32 (32-bit unsigned integer) Uint16 (16-bit unsigned integer) Uint8 (8-bit unsigned integer)

25 Image Processing Toolbox
Read an image I=imread(filename) Display an image ---- imshow(I) Write an image imwrite(I, filename,format)

26 Image import and export
Read and write images in Matlab >> I=imread('cells.jpg'); >> imshow(I) >> size(I) ans = (RGB image) >> Igrey=rgb2gray(I); >> imshow(Igrey) >> imwrite(lgrey, 'cell_gray.tif', 'tiff') Converts from RGB to gray-level imread imshow

27 Image Processing Toolbox
Loading, displaying images: I=imread(‘im1.jpg’), imshow(I) Saving images: imwrite(I, ‘newim.jpg’) Image representation Grayscale: Matrix of uint8 Color: Stack of 3 matrices for R, G, and B Conversion: I2 = double(I1)

28 Types of Images in MATLAB
• Binary images : {0,1} • Intensity images : [0,1] or uint8, double etc. • RGB images : m-by-n-by-3 • Indexed images : m-by-3 color map • Multidimensional images m-by-n-by-p (p is the number of layers)

29 Command SUBPLOT subplot divides the current figure into rectangular panes that are numbered rowwise. Each pane contains an axes object. Subsequent plots are output to the current pane. h = subplot(m,n,p), or subplot(mnp) breaks the Figure window into an m-by-n matrix of small axes, selects the pth axes object for the current plot, and returns the axis handle. The axes are counted along the top row of the Figure window, then the second row, etc. 2,1,1 2,1,2 1,2,1 1,2,2

30 Image Processing Toolbox(cont.)
ImageRWD.m function ImageTest Itif=imread('image1.tif'); imwrite(Itif,'image1.bmp','bmp'); Ibmp=imread('image1.bmp'); subplot(1,2,1); imshow(Itif); subplot(1,2,2); imshow(Ibmp); Means 1 row 2 columns 1,2,1 1,2,2

31 2,1,1 EdgeTest.m 2,1,2 function EdgeTest Itif=imread('image1.tif');
For example, subplot(2,1,1), plot(income) subplot(2,1,2), plot(outgo) plots income on the top half of the window and outgo on the bottom half. If the CurrentAxes is nested in a uipanel, the panel is used as the parent for the subplot instead of the current figure. The new axes object becomes the current axes. 2,1,1 EdgeTest.m function EdgeTest Itif=imread('image1.tif'); B=edge(Itif,'canny'); subplot (2,1,1); imshow(Itif); subplot(2,1,2); imshow(B); 2,1,2

32 Dealing with color images

33 Loading a color image and displaying it in color
a = imread(‘picture.jpg’); imshow(a);

34 Showing size of a color image as a three dimensional matrix
Images Image (=matrix) size: size(a): R G B 384 512

35 The concept of a matrix with three planes
Images Color image: 3D Matrix of RGB planes G R B

36 Show RED plane: a(:,:,2:3) = 0; imshow(a); Removing G and B planes
Images Show RED plane: a(:,:,2:3) = 0; imshow(a); Removing G and B planes

37 Images Show GREEN plane: a(:,:,[1 3]) = 0; imshow(a);

38 Images Show BLUE plane: a(:,:,1:2) = 0; imshow(a);

39 Shuffling columns of the color image
Sort list rn Create a random list of numbers in the range from 1 to 512 rn = rand(1,512); [rn1,i] = sort(rn); b = a(:,i,:); imshow(b); Image (=matrix) size: size(a): Create the new image b Display new image Do not change the plane Take from matrix a Put sorted column Do not change the row value in column Shuffling columns of the color image

40 Image Processing Toolbox(cont.)
There are many programs ready for you to use Pixel values and statistics --corr2,imhist… Image enhancement – histeq, medfilt2… Linear filter conv2, filter2… Image transform dct2, fft… Binary image Op dilate, erode…

41 HELP subsystem in MATLAB
» Help eig EIG Eigenvalues and eigenvectors. E = EIG(X) is a vector containing the eigenvalues of a square matrix X. [V,D] = EIG(X) produces a diagonal matrix D of eigenvalues and a full matrix V whose columns are the corresponding eigenvectors so that X*V = V*D. » lookfor differentiation NUDERST Selects the step size for numerical differentiation DIFF Alternative entry to the symbolic differentiation function. In unix: !ls or !mkdir

42 How to Get Help in MATLAB
Type “help” to get a listing of topics “help <topic>” gets help for that topic. The information is at the level of a Unix man page On the web “Matlab links” on course web page has pointers Especially MathWorks help desk: There’s always Google…but be careful

43 Matlab versus C++

44 C++ vs. Matlab for i = 1:2:N for J = 1:N A(I,J) =(I+J-1); end int j; .
for (j=1;j<23;j=j+2) { A[4][j]=3*j; } for i = 1:2:N for J = 1:N A(I,J) =(I+J-1); end Not the same code, just examples of use

45 C++ vs. Matlab (cont.) int j; while (j<28) { …….; } while N> 0,
E = E + F; F = A*F/N; N = N + 1; end

46 C++ vs. Matlab (cont.) if i == j If (i==j) A(i,j) = 2; { A[i][j]=2;
elseif abs(i-j) ~= 1 A(i,j) = -1; else A(i,j) = 0; end If (i==j) { A[i][j]=2; } else if (abs(i-j)!=1) { …….;

47 C++ vs. Matlab (cont.) Index to an array can be zero.
double, float , int… “;” is very important Index into matrix can’t be negative or zero. Don’t need to worry about the data type “;” not so important

48 Functions

49 Functions n = length(x); mean = avg(x,n); stdev =…
double* stat(double *x) { …….; return stdev; } function [mean,stdev] = stat(x) n = length(x); mean = avg(x,n); stdev =… function mean = avg(x,n) mean = sum(x)/n;

50 Functions(cont.) void Matrix2Vector( )
{ ……; } function Matrix2Vector Av=A(1,:); for i=2:x Av=[Av A(i,:)]; end Av=Av';

51 Functions(cont.) void AddF(int i); int main() { ……
} void AddF(int i) { i=i+1; File name testFunR.m function testFun i=2; AddF(i); i function AddF(i) i=i+1; A function declaration cannot appear within a script M-file

52 Graphing

53 Graphing To make a graph of y = sin(t) on the interval t = 0 to t = 10
In file PlotTest.m t = 0:.3:10; y = sin(t); plot(t,y,’r’) ;

54 Graphing(cont.) [x,y] = meshgrid (-2:.2:2, -2:.2:2);
graphing the function z(x,y) = x exp( - x^2 - y^2) In file MeshTest.m Loop for x Loop for y [x,y] = meshgrid (-2:.2:2, -2:.2:2); z = x .* exp(-x.^2 - y.^2); mesh(z)

55 Graphing(cont.) Multiple windows in one figure SubplotTest.m
y = sin(t); z= cos(t); subplot(2,1,1); plot(t,y) ; subplot(2,1,2); plot(t,z);

56 Graphing(cont.) Multiple windows in one figure SubplotTest.m
y = sin(t); z= cos(t); subplot(1,2,1); plot(t,y) ; subplot(1,2,2); plot(t,z);

57 Sinc = Example of graphing
[x,y] = meshgrid(a); R = sqrt(x.^2+y.^2)+eps; Z = sin(R)./R; mesh(a,a,Z) mesh(x,y,Z) title(‘Sinc(sqrt(x^2+y^2))’)

58 Visualization and Graphics
plot(x,y), plot(x,sin(x)) %plot 1-D function figure , figure(k) %open a new figure hold on, hold off mesh(x_ax,y_ax,z_mat) %view surface contour(z_mat) %view z as top. map subplot(3,1,2) %several plots in figure, 3 rows axis([xmin xmax ymin ymax]) title(‘figure title’) %add title to figure

59 Neural Network Feed-forward backpropagatoin Hopfield Networks
Self-organizing maps Radial Basis networks ………………

60 Neural Networks(cont.)
Create feed-forward NN Net=newff([-1 2;0 5],[31],{‘tansig’,’purelin’},’traingd’); Neural Model – tansig, logsig, purelin. Training method –traingd, traingdm. Training [net,tr]=train(net,p,t) net.trainParam.????; “show”, “lr”, “epochs”, “goal” Simulation A=sim(net,q);

61 Menu function MenuDemo cell_list = {}; fig_number = 1;
title_figure = 'Menu Demo'; cell_list{1,1} = {'Plot','PlotTest;'}; cell_list{1,2} = {'Mesh','MeshTest;'}; cell_list{1,3} = {'Image','ImageRWD;'}; cell_list{1,4} = {'Image Edge','EdgeTest;'}; cell_list{2,1} = {'????','PlotTest;'}; cell_list{2,2} = {'????','PlotTest;'}; cell_list{2,3} = {'????','PlotTest;'}; cell_list{2,4} = {'Exit',['disp(''Bye. To run again, type "menu".''); close(' num2str(fig_number) ');']}; show_window(cell_list,fig_number,title_figure,120,20,3);

62 Menu (cont.) cell_list{1,1} = {‘name',‘function;'};
show_window(cell_list,fig_number,title_figure,x,y,z);

63 Typical project for this class using MATLAB

64 Robot Vision Development in Practice: Robot in a Maze
Build your robot (you are done) Install a camera on it. Learn software that comes with camera, understand formats. JPG Install Matlab on your laptop – student version, much software for free on WWW – Octave Be able to display image from camera in one window so that you will see it and Matlab code or processing in other windows.

65 Robot Vision Development in Practice: Robot in a Maze (cont)
5. Guide your robot through stage or labyrinth and see with your own eyes what robot sees. 6. Think what is a good processing method for the image that you see. 7. Protytype this in Matlab using existing functions. 8. If necessary, use parallel Matlab on CUDA 9. If necessary, rewrite to C++ or Python or Java.

66 Sources Rolf Lakaemper Fred Annexstein Jason Rife Hyunki Lee
Amin Allalou


Download ppt "Matlab Tutorial."

Similar presentations


Ads by Google