Matlab Tutorial.

Slides:



Advertisements
Similar presentations
Tutorial on Matlab and OpenCV Rui Ma TA of CMPT 414 May 14, 2013 Office hours: Fridays 11:00-12:00, CSIL TA Office 1 (ASB 9838)
Advertisements

Introduction to MATLAB The language of Technical Computing.
Introduction to Matlab
Introduction to Matlab Workshop Matthew Johnson, Economics October 17, /13/20151.
CS231A Matlab Tutorial Philip Lee Winter Overview  Goals › Introduction to Matlab › Matlab Snippets › Basic image manipulations › Helpful Matlab.
Introduction to MATLAB and image processing. MATLAB and images The help in MATLAB is very good, use it! An image in MATLAB is treated as a matrix Every.
MATLAB for Image Processing April 10 th, Outline Introduction to MATLAB –Basics & Examples Image Processing with MATLAB –Basics & Examples.
Matlab The language of technical computing. Outline Introduction C++ vs. Matlab Functions Graphing Matrix Image processing toolbox Neural network toolbox.
Introduction to Matlab Jianguo Wang CSSCR September 2009.
Computational Tools for Image Processing Lecture 1, Jan 22nd, 2007 Part 2 (8:10-9:20pm) by Lexing Xie EE4830 Digital Image Processing
MATLAB for Image Processing CS638-1 TA: Tuo Wang Feb 12 th, 2010.
Martin Ellison University of Warwick and CEPR Bank of England, December 2005 Introduction to MATLAB.
Introduction to MatLab: Image Processing
CIS 601 MATLAB First Impressions. MATLAB This introduction will give Some basic ideas Main advantages and drawbacks compared to other languages.
Introduction to MATLAB adapted from Dr. Rolf Lakaemper.
Introduction to MATLAB Session 1 Prepared By: Dina El Kholy Ahmed Dalal Statistics Course – Biomedical Department -year 3.
You can make this in matlab!. Matlab Introduction and Matrices.
Eng Advanced Marine Vehicles Todays agenda: Lab tomorrow at 2pm (structures lab) ‏ Advanced Marine Party Introduction to Matlab.
Introduction to MATLAB
A Brief Introduction to Matlab Laila Guessous Dept. of Mechanical Engineering Oakland University.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
Eng Ship Structures 1 Introduction to Matlab.
INTRODUCTION TO MATLAB LAB# 01
Gulsah Tumuklu Ozyer MATLAB IMAGE PROCESSING TOOLBOX.
1 Computer Programming (ECGD2102 ) Using MATLAB Instructor: Eng. Eman Al.Swaity Lecture (1): Introduction.
Matlab 14.html Cost: $100 Available in labs on Windows and Unix machines.
Getting Started with MATLAB CS534 TA: Matt McDaniel Sep 17 th, 2012 Slides by Chunhui Zhu – Fall 2011 Thanks to the help from Tuo.
1 © 2010 Cengage Learning Engineering. All Rights Reserved. 1 Introduction to Digital Image Processing with MATLAB ® Asia Edition McAndrew ‧ Wang ‧ Tseng.
Introduction to MATLAB adapted from Dr. Rolf Lakaemper.
CIS 595 MATLAB First Impressions. MATLAB This introduction will give Some basic ideas Main advantages and drawbacks compared to other languages.
Outline Introduction to MATLAB Image Processing with MATLAB
CIS 601 Fall 2003 Introduction to MATLAB Longin Jan Latecki Based on the lectures of Rolf Lakaemper and David Young.
Introduction to MATLAB 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting.
Lecture 27: Image Processing
Introduction to MATLAB Ehsan Adeli M. Iran University of Science and Technology, E-Learing Center, Fall 2008 (1387)
CIS 595 MATLAB First Impressions. MATLAB This introduction will give Some basic ideas Main advantages and drawbacks compared to other languages.
การใช้งานโปรแกรม MATLAB ดร. อำนาจ ขาวเน. BASIC ELEMENTS OF MATLAB MATLAB Desktop MATLAB Editor Help System MATLAB (MATrix LABoratory)
Matlab Tutorial (material available at 1 Dr. Jim Martin Associate Professor School of Computing.
Intro To MATLAB CS Fall 2013 Zach Welch. Overview ●Basics ●MATLAB data structures ●Operations ●Useful functions ●Image Processing and other useful.
Computer Application in Engineering Design
Getting Started with MATLAB
Matlab.
Introduction to MATLAB
Introduction to Matlab
Introduction to Mat lab
Lecture 25.
MatLab Programming By Kishan Kathiriya.
Ch3 Graphics Overview of Plotting Editing Plots
Introduction to Matlab ( Google Search )
Other Kinds of Arrays Chapter 11
Matrices and Arrays.
Outline Matlab tutorial How to start and exit Matlab Matlab basics.
INTRODUCTION TO BASIC MATLAB
MATLAB DENC 2533 ECADD LAB 9.
(Mohammed Sami) Ashhab
Matlab Workshop 9/22/2018.
Introduction to MatLab: Image Processing
Introduction to MATLAB
StatLab Matlab Workshop
Simulation And Modeling
Digital Image Processing using MATLAB
Part I – Matlab Basics.
StatLab Workshop: Intro to Matlab for Data Analysis and Statistical Modeling 11/29/2018.
a = imread(‘picture.jpg’); imshow(a);
Communication and Coding Theory Lab(CS491)
CSE 307 Basics of Image Processing
Matlab Basic Dr. Imtiaz Hussain
Simulation And Modeling
MATLAB stands for MATrix LABoratory.
Introduction to Image Processing with MATLAB
Presentation transcript:

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, 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)

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

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.

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

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 !

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

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)

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.

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.

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

Images can be treated as matrices !

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

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 = 2 3 5 6 7 8 9

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

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

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

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 [0 a] 1 2 1 1 3 3 1

Image Processing Toolbox

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

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)

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

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

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)

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)

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

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

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

Dealing with color images

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

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

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

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

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

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

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): 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

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…

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

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 www.mathworks.com/access/helpdesk/help/helpdesk.shtml

Matlab versus C++

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

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

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) { …….;

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

Functions

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;

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

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

Graphing

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’) ;

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)

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);

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);

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))’)

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

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

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);

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);

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

Typical project for this class using MATLAB

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.

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.

Sources Rolf Lakaemper Fred Annexstein Jason Rife Hyunki Lee Amin Allalou www.cis.udel.edu/~cer/arv