1 / 66

Matlab Tutorial

Matlab Tutorial. This introduction will give. a brief overview, it’s not a MATLAB tutorial ! Some basic ideas Main advantages and drawbacks compared to other languages. What is MATLAB?. Matlab Language Features. A high-level language for: matrix calculations, numerical analysis,

tammysmith
Download Presentation

Matlab Tutorial

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 95 102 94 102 95 98 102 99 103 105 110 94 99 94 101 100 98 100 101 101 107 104 97 86 83 97 96 98 104 96 100 102 102 105 91 85 93 89 98 92 95 98 100 102 106 105 99 90 93 96 84 88 93 89 89 98 94 102 99 81 87 86 84 90 91 88 101 104 87 82 90 84 86 87 86 95 102 99 102 90 74 92 101 87 74 77 83 100 92 95 102 100 92 96 110 93 72 71 83 101 87 103 101 105 88 76 94 93 71 69 105 99 105 104 111 101 84 59 78 102 72

  8. 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 !

  9. 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

  10. 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)

  11. 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.

  12. 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.

  13. Entering Variables • Entering a vector, matrix • V = [10, 4.5, 1]; • M = [3, 4 ; -6, 5]; • 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

  14. Images can be treated as matrices !

  15. Simple operations on Matrices >> A=[1 2 3;3 2 1] A = 1 2 3 3 2 1 >> B=A' B = 1 3 2 2 3 1 >>b=A(1,:) b = 1 2 3

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

  17. 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 = 2 4 6 8 10 12 14 16 18 >> A*A ans = 30 36 42 66 81 96 102 126 150 • To perform an elementwise operation use . (.*, ./, .*, .^ etc) >> A.*A ans = 1 4 9 16 25 36 49 64 81 This is normal matrix multiplication • A = • 2 3 • 5 6 • 7 8 9 This is elementwise matrix multiplication Dot means elementwise operation

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

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

  20. 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 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 1 7 21 35 35 21 7 1 1 8 28 56 70 56 28 8 1 1 9 36 84 126 126 84 36 9 1 (with “a=” before each line). [a 0] 1 2 1 1 2 1 [0 a] 1 3 3 1

  21. Image Processing Toolbox

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

  23. 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)

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

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

  26. 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)

  27. 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)

  28. 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 1,2,2 1,2,1 2,1,2

  29. 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,2 1,2,1

  30. 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

  31. Dealing with color images

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

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

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

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

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

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

  38. 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): 384 512 3 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

  39. 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…

  40. 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

  41. How to Get Help in MATLAB • 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 www.mathworks.com/access/helpdesk/help/helpdesk.shtml

  42. Matlab versus C++

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

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

  45. C++ vs. Matlab (cont.) if i == j 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) { …….; }

  46. 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

  47. Functions

  48. Functions 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;

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

More Related