Getting Started with MATLAB CS534 TA: Matt McDaniel Sep 17 th, 2012 Slides by Chunhui Zhu – Fall 2011 Thanks to the help from Tuo.

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 Programming using Matlab Session 2 P DuffourJan 2008.
Matlab Intro Simple introduction to some basic Matlab syntax. Declaration of a variable [ ] Matrices or vectors Some special (useful) syntax. Control statements.
Lecture 5.
Matlab Tutorial. Session 1 Basics, Filters, Color Space, Derivatives, Pyramids, Optical Flow Gonzalo Vaca-Castano.
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.
ES 314 Lecture 2 Sep 1 Summary of lecture 1: course overview intro to matlab sections of Chapters 2 and 3.
Programming in Visual Basic
Chapter 8 and 9 Review: Logical Functions and Control Structures Introduction to MATLAB 7 Engineering 161.
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.
CSci 2031: Matlab Tutorial Guoquan (Paul) Huang Jan 24, 2008.
Computer Science in Practice This course is an introduction to problems (and solutions) that arise in applied fields of computer science such as machine.
1 Introduction to MatLab MatLab stands for Matrix Laboratory. As the name suggests most of the programming operations have as input or output a matrix.
Laboratory of Image Processing Pier Luigi Mazzeo November 4 th, 2014.
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
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.
MATLAB and SimulinkLecture 11 To days Outline  Introduction  MATLAB Desktop  Basic Features  Branching Statements  Loops  Script file / Commando.
Martin Ellison University of Warwick and CEPR Bank of England, December 2005 Introduction to MATLAB.
Nonparametric Econometrics1 Intro to Matlab for Data Analysis and Statistical Modeling.
Introduction to MATLAB Session 1 Prepared By: Dina El Kholy Ahmed Dalal Statistics Course – Biomedical Department -year 3.
Introduction to Matlab 1. Outline: What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators Plotting Flow Control Using of M-File Writing.
Introduction to MATLAB
1 Lab of COMP 406 Teaching Assistant: Pei-Yuan Zhou Contact: Lab 1: 12 Sep., 2014 Introduction of Matlab (I)
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 ,
INTRODUCTION TO MATLAB LAB# 01
Gulsah Tumuklu Ozyer MATLAB IMAGE PROCESSING TOOLBOX.
10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.
CS112 Scientific Computation Department of Computer Science Wellesley College Numb3rs Number and image types.
Matlab The language of Technical computing Mr. D. Suresh Assistant Professor, Dept. of CSE, PSNA CET, Dindigul.
Presented By: ROLL No IMTIAZ HUSSAIN048 M.EHSAN ULLAH012 MUHAMMAD IDREES027 HAFIZ ABU BAKKAR096(06)
Introduction MATLAB stands for MATrix LABoratory.  Basics  Matrix Manipulations  MATLAB Programming  Graphics  Image types  Image Processing  Useful.
1 © 2010 Cengage Learning Engineering. All Rights Reserved. 1 Introduction to Digital Image Processing with MATLAB ® Asia Edition McAndrew ‧ Wang ‧ Tseng.
ME6104: CAD. Module 4. ME6104: CAD. Module 4. Systems Realization Laboratory Module 4 Matlab ME 6104 – Fundamentals of Computer-Aided Design.
Introduction to Matlab. Outline:  What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators (Arithmetic, relational, logical ) Display.
Matlab Screen  Command Window  type commands  Current Directory  View folders and m-files  Workspace  View program variables  Double click on a.
Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz SUNY-New Paltz “Lecture 2”
Digital Image Processing Lecture 6: Introduction to M- function Programming.
Ch13-1 Chap 13 Introduction to Matlab 13.1 Introduction MATLAB : The MATrix LABoratory program Not only is the MATLAB programming language exceptionally.
Digital Image Processing Introduction to M-function Programming.
Outline Introduction to MATLAB Image Processing with MATLAB
Digital Image Processing Introduction to MATLAB. Background on MATLAB (Definition) MATLAB is a high-performance language for technical computing. The.
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
NET 222: COMMUNICATIONS AND NETWORKS FUNDAMENTALS ( NET 222: COMMUNICATIONS AND NETWORKS FUNDAMENTALS (PRACTICAL PART) Tutorial 2 : Matlab - Getting Started.
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
Introduction to MATLAB Ehsan Adeli M. Iran University of Science and Technology, E-Learing Center, Fall 2008 (1387)
Graphics and Image Data Representations 1. Q1 How images are represented in a computer system? 2.
CS100A, Fall 1998, Lecture 201 CS100A, Fall 1998 Lecture 20, Tuesday Nov 10 More Matlab Concepts: plotting (cont.) 2-D arrays Control structures: while,
Introduction to MATLAB CS 534 Fall What you'll be learning today ●MATLAB basics (debugging, IDE) ●Operators ●Matrix indexing ●Image I/O ●Image display,
การใช้งานโปรแกรม MATLAB ดร. อำนาจ ขาวเน. BASIC ELEMENTS OF MATLAB MATLAB Desktop MATLAB Editor Help System MATLAB (MATrix LABoratory)
Basics of MATLAB 2- Programming in MATLAB By DR. Wafaa Shabana
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
Tutorial on Matlab Basics
Lecture: MATLAB Chapter 1 Introduction
MATLAB DENC 2533 ECADD LAB 9.
Matlab Workshop 9/22/2018.
StatLab Matlab Workshop
Digital Image Processing using MATLAB
StatLab Workshop: Intro to Matlab for Data Analysis and Statistical Modeling 11/29/2018.
Lecture 2 Introduction to MATLAB
Communication and Coding Theory Lab(CS491)
MATLAB stands for MATrix LABoratory.
Introduction to Image Processing with MATLAB
Presentation transcript:

Getting Started with MATLAB CS534 TA: Matt McDaniel Sep 17 th, 2012 Slides by Chunhui Zhu – Fall 2011 Thanks to the help from Tuo Wang and Prof. Dyer in making these slides.

Outline Introduction to MATLAB –Basics & Examples Image Processing with MATLAB –Basics & Examples

What is MATLAB? MATLAB = Matrix Laboratory “MATLAB is an interactive, matrix-based system for scientific and engineering numeric computation and visualization. You can solve complex numerical problems in a fraction of the time required with a programming Language such as Fortran or C.” ---- Matlab Primer

Task: compute the eigenvectors and eigenvalues for matrix A JAVA/C++: OK, let me first define a matrix struct or class, then look up the eigenvalue definition in linear algebra book, figure out the solution, write the code and debug…Hopefully, get the right answer… MATLAB: One command line. MATLAB: quick implementation of your idea JAVA&C++: for development MATLAB vs. JAVA&C++

The MATLAB Environment

MATLAB Help

MATLAB Help (cont.) Search by keywords Type in command window: help functionname(eg. help sqrt)

Entering Matrices By an explicit list of elements – A=[1,2,3; 4 5 6; 7 8, 9]; By built-in statements and functions: Evenly distributed vector: 0:0.5:2 generates[0,0.5,1.5,2] zeros(n,m), ones(n,m), eye(n,m), rand(), randn(),repmat() Load from external data files or applications

Basic Operations on Matrices All operators in MATLAB are defined on matrices: +, -, *, /, ^, sqrt, sin, cos, etc. Element-wise operators defined with a preceding dot:.*,./,.^ Be sure about whether you are using matrix operators or element-wise operators!

Logical Operators ==,, (not equal) ~=, (not) ~ find(‘condition’) – Returns indexes of A’s elements that satisfy the condition

Logical Operators (cont.) Example: >> A=[7 3 5; 6 2 1], [r,c] = find(A < 4) A= r = 1 c =

Some Built-in Matrix Funcs [eigVec,eigVal]=eig(A), eigenvectors and eigenvalues for matrix A inv(A),inverse of matrix A det(A),determinant of matrix A rank(A),the rank of matrix A size(A,1),number of rows of Matrix A; size(A,2), number of columns …

Variable Name in Matlab Variable naming rules - must be unique in the first 63 characters - must begin with a letter - may not contain blank spaces or other types of punctuation - may contain any combination of letters, digits, and underscores - are case-sensitive - should not use Matlab keywords Pre-defined variable names pi

Scripts and Functions There are two kinds of M-files(.m): -Scripts, which do not accept input arguments or return output arguments. They operate on data in the workspace. Equivalent to typing into the command window. -Functions, which can accept input arguments and return output arguments. Internal variables are local to the function.

Functions in MATLAB (cont.) Example: –A file called stat.m: function [mean, stdev]=stat(x) %STAT Interesting statistics. n=length(x); mean=sum(x)/n; stdev=sqrt(sum((x-mean).^2)/n); –Defines a new function called stat that calculates the mean and standard deviation of a vector. Function name and file name should be the SAME! –CODECODE

Suggested Project Organization A script file + Several self-defined function files Script file acting like a main.cpp in C++, it calls the self-defined functions or built-in system functions. Self-defined function files implement details of algorithms. DEMO

Flow Control MATLAB has five flow control constructs: – if statement – switch statement – for loop – while loop – break statement

if IF statement condition –The general form of the IF statement is IF expression statements ELSEIF expression statements ELSE statements END CODE

switch SWITCH – Switch among several cases based on expression The general form of SWITCH statement is: SWITCH switch_expr CASE case_expr, statement, …, statement CASE {case_expr1, case_expr2, case_expr3, …} statement, …, statement … OTHERWISE statement, …, statement END

switch (cont.) Note: –Only the statements between the matching CASE and the next CASE, OTHERWISE, or END are executed –Unlike C, the SWITCH statement does not fall through (so BREAK s are unnecessary) CODE

for FOR repeats statements a specific number of times The general form of a FOR statement is: FOR variable=expr statements END CODE

while WHILE repeats statements an indefinite number of times The general form of a WHILE statement is: WHILE expression statements END CODE

It seems like I can use these loops as I do in C/C++/Java… Try to AVOID THIS!

Time Cost Comparison Loop vs. No Loop A = rand(1000,1000);B = rand(1000,1000); for i = 1:size(A,1), for j = 1:size(A,2), C(i,j) = A(i,j) + B(i,j); end Using loop: Elapsed time is seconds.

Time Cost Comparison(cont.) Loop vs. no loop C = A + B Elapsed time is seconds.

Visualization and Graphics plot(x,y),plot(x,sin(x)) – plot 1D function figure, figure(k) – open a new figure hold on, hold off – refreshing axis([xmin xmax ymin ymax]) – change axes title(‘figure titile’) – add title to figure mesh(x_ax,y_ax,z_mat) – view surface contour(z_mat) – view z as topo map subplot(3,1,2) – locate several plots in figure - CODE and Debug CODECODE

Saving your Work save mysession % creates mysession.mat with all variables save mysession a b % save only variables a and b clear all % clear all variables clear a b % clear variables a and b load mysession % load session

Debug Techniques Debug is essential. Easy and flexible access during debug. DEMO

Outline Introduction to MATLAB –Basics & Examples Image Processing with MATLAB –Basics & Examples

What is the Image Processing Toolbox? The Image Processing Toolbox is a collection of functions that extend the capabilities of the MATLAB’s 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

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) –raw-data and 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)

Images in MATLAB Binary images : {0,1} Intensity images : [0,1] or uint8, double etc. RGB images : m × n × 3 Multidimensional images: m × n × p (p is the number of layers)

Image Import and Export Read and write images in Matlab img = imread('apple.jpg'); dim = size(img); figure; imshow(img); imwrite(img, 'output.bmp', 'bmp'); Alternatives to imshow imagesc(I) imtool(I) image(I)

Images and Matrices Column 1 to 256 Row 1 to 256 o [0, 0] o [256, 256] How to build a matrix (or image)? Intensity Image: row = 256; col = 256; img = zeros(row, col); img(100:105, :) = 0.5; img(:, 100:105) = 1; figure; imshow(img);

Images and Matrices Binary Image: row = 256; col = 256; img = rand(row, col); img = round(img); figure; imshow(img);

Image Display 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 gray2ind - intensity image to index image im2bw - image to binary im2double - image to double precision im2uint8 - image to 8-bit unsigned integers im2uint16 - image to 16-bit unsigned integers ind2gray - indexed image to intensity image mat2gray - matrix to intensity image rgb2gray - RGB image to grayscale rgb2ind - RGB image to indexed image

Image Operations RGB image to gray image Image resize Image crop Image rotate Image histogram Image histogram equalization Image DCT/IDCT Convolution - CODE

Outline Introduction to MATLAB –Basics & Examples Image Processing with MATLAB –Basics & Examples

Examples working with Images (1/3) Create AVI movie with a series of images & Read specific frame from video file Related funcs: avifile, addframe, mmreader DEMO

Examples working with Images (2/3) Simplified version of image blending: Each pixel in the apple-orange is: w(i, j) * Apple(i, j) + (1-w(i, j)) * Orange(i, j)

Examples working with Images (3/3) Sobel edge detection on image

Examples working with Images (3/3)(cont.) The use of imfilter function: A great assistant to avoid loop in Matlab image processing

Examples working with Images (3/3) (cont.) y = imfilter(x,h) DEMO

Performance Issues The idea: MATLAB is –very fast on vector and matrix operations –Correspondingly slow with loops Try to avoid loops Try to vectorize your code notes/1100/1109.html

THE END Thanks for your attention! Questions?