CSE/Math 485 Matlab Tutorial and Demo

Similar documents
CSE/Math 456 and CSE/Math 550 Matlab Tutorial and Demo

1 Introduction to Matlab

A very brief Matlab introduction

STAT/MATH 395 A - PROBABILITY II UW Winter Quarter Matlab Tutorial

Matlab Introduction. Scalar Variables and Arithmetic Operators

Matlab Tutorial and Exercises for COMP61021

Constraint-based Metabolic Reconstructions & Analysis H. Scott Hinton. Matlab Tutorial. Lesson: Matlab Tutorial

Introduction to Matlab

A Brief Introduction to MATLAB

Inlichtingenblad, matlab- en simulink handleiding en practicumopgaven IWS

Introduction to Matlab

Appendix A. Introduction to MATLAB. A.1 What Is MATLAB?

Dr Richard Greenaway

A Guide to Using Some Basic MATLAB Functions

MATLAB Programming for Numerical Computation Dr. Niket Kaisare Department Of Chemical Engineering Indian Institute of Technology, Madras

Matlab Tutorial for COMP24111 (includes exercise 1)

MATLAB Lesson I. Chiara Lelli. October 2, Politecnico di Milano

Introduction to Matlab

Programming 1. Script files. help cd Example:

A QUICK INTRODUCTION TO MATLAB

Variable Definition and Statement Suppression You can create your own variables, and assign them values using = >> a = a = 3.

What is MATLAB and howtostart it up?

Stokes Modelling Workshop

Digital Image Processing

MATLAB Tutorial. Digital Signal Processing. Course Details. Topics. MATLAB Environment. Introduction. Digital Signal Processing (DSP)

AMATH 352: MATLAB Tutorial written by Peter Blossey Department of Applied Mathematics University of Washington Seattle, WA

A QUICK INTRODUCTION TO MATLAB. Intro to matlab getting started

MAT 275 Laboratory 1 Introduction to MATLAB

University of Alberta

Creates a 1 X 1 matrix (scalar) with a value of 1 in the column 1, row 1 position and prints the matrix aaa in the command window.

Introduction to MATLAB Practical 1

Chapter 2 Fundamentals. Chapter 2 Fundamentals The images used here are provided by the authors.

Introduction to Matlab. By: Dr. Maher O. EL-Ghossain

Experiment 1: Introduction to MATLAB I. Introduction. 1.1 Objectives and Expectations: 1.2 What is MATLAB?

Lecturer: Keyvan Dehmamy

CSE/NEUBEH 528 Homework 0: Introduction to Matlab

ARRAY VARIABLES (ROW VECTORS)

MATLAB for Image Processing. April 2018 Rod Dockter

Introduction to Matlab. Summer School CEA-EDF-INRIA 2011 of Numerical Analysis

Getting To Know Matlab

What is MATLAB? What is MATLAB? Programming Environment MATLAB PROGRAMMING. Stands for MATrix LABoratory. A programming environment

Why use MATLAB? Mathematcal computations. Used a lot for problem solving. Statistical Analysis (e.g., mean, min) Visualisation (1D-3D)

Introduction to MATLAB

Introduction to Octave/Matlab. Deployment of Telecommunication Infrastructures

Math 2250 MATLAB TUTORIAL Fall 2005

Introduction to Matlab

LAB 1 General MATLAB Information 1

CSCI 6906: Fundamentals of Computational Neuroimaging. Thomas P. Trappenberg Dalhousie University

A GUIDE FOR USING MATLAB IN COMPUTER SCIENCE AND COMPUTER ENGINEERING TABLE OF CONTENTS

How to learn MATLAB? Some predefined variables

ECON 502 INTRODUCTION TO MATLAB Nov 9, 2007 TA: Murat Koyuncu

Teaching Manual Math 2131

MATLAB BASICS. < Any system: Enter quit at Matlab prompt < PC/Windows: Close command window < To interrupt execution: Enter Ctrl-c.

The value of f(t) at t = 0 is the first element of the vector and is obtained by

EE 301 Signals & Systems I MATLAB Tutorial with Questions

Programming in Mathematics. Mili I. Shah

Starting MATLAB To logon onto a Temple workstation at the Tech Center, follow the directions below.

ELEC4042 Signal Processing 2 MATLAB Review (prepared by A/Prof Ambikairajah)

Matlab Tutorial: Basics

Introduction to MATLAB. Simon O Keefe Non-Standard Computation Group

Math Scientific Computing - Matlab Intro and Exercises: Spring 2003

Grace days can not be used for this assignment

MAT 275 Laboratory 2 Matrix Computations and Programming in MATLAB

Introduction to Matlab

Introduzione a MatLab. Prof. Sebastiano Battiato

MATLAB Tutorial EE351M DSP. Created: Thursday Jan 25, 2007 Rayyan Jaber. Modified by: Kitaek Bae. Outline

Laboratory 1 Octave Tutorial

McTutorial: A MATLAB Tutorial

What is Matlab? A software environment for interactive numerical computations

Getting Started. Chapter 1. How to Get Matlab. 1.1 Before We Begin Matlab to Accompany Lay s Linear Algebra Text

MATLAB = MATrix LABoratory. Interactive system. Basic data element is an array that does not require dimensioning.

PART 1 PROGRAMMING WITH MATHLAB

Getting Started with MATLAB

SF1901 Probability Theory and Statistics: Autumn 2016 Lab 0 for TCOMK

matlab_intro.html Page 1 of 5 Date: Tuesday, September 6, 2005

MATLAB Introduction to MATLAB Programming

Outline. CSE 1570 Interacting with MATLAB. Outline. Starting MATLAB. MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An.

Arithmetic expressions can be typed into Maple using the regular operators:

MATLAB for Image Processing

Introduction and MATLAB Basics

ELEMENTARY MATLAB PROGRAMMING

Summer 2009 REU: Introduction to Matlab

Introduction to Matlab

12 whereas if I terminate the expression with a semicolon, the printed output is suppressed.

int16 map is often stored with an indexed image and is automatically loaded with image when using imread

Digital Image Analysis and Processing CPE

UNIVERSITI TEKNIKAL MALAYSIA MELAKA FAKULTI KEJURUTERAAN ELEKTRONIK DAN KEJURUTERAAN KOMPUTER

How to Use MATLAB. What is MATLAB. Getting Started. Online Help. General Purpose Commands

Matlab for FMRI Module 1: the basics Instructor: Luis Hernandez-Garcia

MATLAB Tutorial Matrices & Vectors MATRICES AND VECTORS

MATLAB Introductory Course Computer Exercise Session

Introduction to MatLab. Introduction to MatLab K. Craig 1

Excel R Tips. is used for multiplication. + is used for addition. is used for subtraction. / is used for division

MATH (CRN 13695) Lab 1: Basics for Linear Algebra and Matlab

1 Overview of the standard Matlab syntax

EE168 Handout #6 Winter Useful MATLAB Tips

Matlab and Psychophysics Toolbox Seminar Part 1. Introduction to Matlab

A/D Converter. Sampling. Figure 1.1: Block Diagram of a DSP System

Introduction to MATLAB

An Introductory Guide to MATLAB

Transcription:

CSE/Math 485 Matlab Tutorial and Demo Some Tutorial Information on MATLAB Matrices are the main data element. They can be introduced in the following four ways. 1. As an explicit list of elements. 2. Generated by built in statements or functions. 3. Created by m files. 4. Loaded from external data files. For instance consider the MATLAB command A = [ 1 2 3 ; 4 5 6 ; 7 8 9] It immediately outputs A = 1 2 3 4 5 6 7 8 9 (no parentheses or brackets). Or if you create a file called gena.m and type A = [ 1 2 3 ; 4 5 6 ; 7 8 9] in that file. Then type gena it will also produce A exactly as above. It is important to remember the distinction between row and column vectors in MATLAB. If you type x = [-1.3 sqrt(3) (1+2+3)*4/5] you get Row vectors are default. On the other hand, if you type x = [-1.3; sqrt(3); (1+2+3)*4/5] you get x = 1.3 1.7321 4.8000 1

x = 1.3 1.7321 4.8000. The most important MATLAB command is help. help This lists all help topics help imread Gives the help information for the function imread for reading in an image. help help Explains the help command. MATLAB m files contain two types of items 1. scripts 2. user defined functions Both have the extension.m and are called m-files. Scripts just files full of command. If I have the file with just the one line. If I just the line A= imread( city.jpg ) into a file called gena.m, and then type gena it is the same as typing the line at the command line. Of course, I can put any number of command lines into a script file. A script file just lines of text consisting of MATLAB commands. Invoking the script (done when typing gena or the name of the script file without the.m ) is equivalent to typing the commands directly into MATLAB. Functions have function statements as their first line. Two examples are function A=fun1(x,y,z) function [A,B,C]=fun2(x,y,z) These should be placed in files called fun1.m and fun2.m respectively. Here x,y,z are input arguments, and A,B,C are output arguments. Any of these can be simple variables, vectors, or matrices. MATLAB supports the usual programming structures if, for, while and switch. The syntax is a bit different from C syntax, but the use is much 2

the same. The function bisect2.m contains if and while statements. The following for statement sums the first n integers. sum=0; for k=1:n sum=sum+k; end; The use of for and while should be avoided if the same can be accomplished with matrix operations! Or better, look to see if there is a MATLAB intrinsic function that does the job. Two useful MATLAB command are save and load. MATLAB creates a workspace for all of your variables. When you are done with a MATLAB session, you type quit and you are out of MATLAB. However, your workspace and all of the variables that you are using vanish, they will not be there when you log on to MATLAB again. If, before you quit, you type save mysession the workspace will be saved in the file mysession.mat. If you type save mysession X Y Z then only the variables X,Y, and Z will be saved. save is a completelydestructive operation, mysession.mat is completely overwritten. If you want to retreive your workspace from the previous session type load mysession To know what variables you have type who Most matrix operations in MATLAB are one line. The matrix transpose operation is B = A ; Thus if A has the contents A = 1 2 3 4 5 6 7 8 0 3

then B will have the contents B = 1 4 7 2 5 8 3 6 0 Here are some other functions C = A+B just adds A and B. C = A*B just multplies A and B as matrices. In image processing, it will be more common to multiply them componentwise. We would prefer C = A.*B The dot is significant. Likewise C=A./B is componentwise division. Without the dot, it performs the matrix operation C = AB 1. If the dimensions are wrong in any of these operations, you will get an error message. There are two important functions associated with matrices, size and length. The statement [m,n]=size(a) returns the dimensions of the array A. If m = 1, A is just a row vector, if n = 1 then A is column vector, and if m = n = 1, then A is scalar. With images, this will be important. The statement p=length(a) returns the larger of the two outputs of size. There are a number of ways to generate special vectors and matrices. Here are some examples x = 1:5 yields y = 0:pi/4:pi x = 1 2 3 4 5. 4

yields y = 0 π/4 π/2 3π/4 π. z = 6:-1:1 yields z = 6 5 4 3 2 1. The statement y =exp(z) yields y = e 6 e 5 e 4 e 3 e 2 e 1. Actually, it produces the appropriate floating point numbers. Of course, exp is exponential function. MATLAB supports lots of math functions, such as exp, log, log10, sin, cos, tan, asin, atan, and some you have probably never heard of. Functions can also be defined inline. An example is f= inline( 1/(x+1) ) They can be passed as arguments in other functions in two ways. The first is through the use of function handles. Let s say we want to compute diff(f, x, h) = [f(x + h/2) f(x h/2)]/h the centered difference approximation to the first derivative. If we have the function fun1 defined either in an m-file or inline, we call diff by yprime=diff(fun1,x,h) where fun1 is called a function handle. The m-file for diff could look like this. function yprime=diff(f,x,h) yprime=(feval(f,x+h/2)-feval(f,x-h/2))/h; MATLAB has toolboxes for a variety of purposes. In this course, we will have cause to use the spline toolbox. There are also toolboxes for symbolic processing (based on MAPLE), control, optimization, and many others. They have standard routines for linear equations, roots of nonlinear equations, numerical integration, and ordinary differential equations. We are interested in the image processing toolbox. It supports four types of images 5

Intensity Images Binary Images Indexed Images RGB Images Intensity images are the standard gray level images that we have been talking about. Binary images are images of 0 s and 1 s. There are lots of conversion functions. Some interesting image operations include imadd, imsubstract, immultiply, imdivide For example I = imread( image1.tif ); J=imread( image2.tif ); K= imadd(i,j, unit16 ); Adds two images and represents the result as a 16-bit image. Or add a constant. K = imadd(i,50); The relational operations (<, <=, >, >=, ==, =) are supported. As are the logical operations AND (&), OR ( ), NOT(-), XOR (as a function), all (returns 1 if all elements of an array are nonzero), any (returns 0 if any elements of an array are 1). The Demo First, log on to your account in 222 IST. If you are unable to do that, please get hold of me. Try to bring up a terminal window. Before doing MATLAB, you need to create a.forward file. Thus use emacs or vi or your favorite UNIX, open a file in the main directory and type one line, an email address, e.g. alan.turing@mymachine.edu and then save the file. An email I send you for this class will go directly to that address. On your next UNIX prompt type % mkdir matlab (or whatever you wish to name this directory) % cd matlab Then type 6

% matlab You should get a brief graphic display and then the prompt and a big GUI. If you do not want the GUI type % matlab -nojvm Any image function is represented as f(0, 0) f(0, 1) f(0, N 1) f(1, 0) f(1, 1) f(1, N 1) f(d) =..... f(m 1, 0) f(m 1, 1).. f(m 1, N 1) In MATLAB, this would be represented by a matrix f. Many if not most of MATLAB s operations are on arrays, and the Image Processing Toolbox is array based. So first, we will look at matrix manipulations in MATLAB, then we will look at some particulars of the toolbox. Type the command A = randn(5,5) This generates a random 5 5 matrix according to the normal distribution. You will use this second terminal window to download and use m-files. So start So right click on the screen scroll to Tools then to Terminal and open another terminal. Then type % mozilla & It should bring up Mozilla. You can also use Netscape. Go to the web page http://www.cse.psu.edu/~barlow/cseee485/classnotes.html. You should links to the two files pollen.jpg and rose.jpg. Download them into your new directory, we will play with them a little later. In the matlab window type B=ones(5,5) A=zeros(5,5) C=magic(6) It is pretty obvious where the first two functions do. The third one is the magic square matrix of size 6. All of its rows, columns, and diagonalssum to 111. Type help sum 7

Now use the sum function to verify that the rows and columns all sum to 111. Type ii=[1 4 5] jj=[2 3 5] A=C(ii,jj) What did you just do? Do you now see how MATLAB indexes arrays? What is C(:, 1: 3), C(2: 4, : ) and C(2: 4, 1: 3)? Notice MATLAB automatically prints the results of any computation on the screen. Under some circumstances, particularly with a 1024 1024 image matrix,that is not a good idea. If we type ii=[1 4 5]; jj=[2 3 5]; A=C(ii,jj); then MATLAB does its work silently. Type A But everything is still there. to display the answer. Now a brief introduction to plotting. Type inc=pi/25 x=0:inc:2*pi y=sin(x) plot(x,y) To label the graph type title( Plot of sin x for one period ) xlabel( Domain From zero to 2*pi ) ylabel( Range From -1 to 1 ) You can print the plot clicking on File and then on Print and then click on Print. DO NOT DO THIS NOW. For now, click Cancel. Print it at your convenience some other time. If you click on Save As.. in the file menu, you can save the graph as.ps (PostScript) file which can be viewed with ghostview. 8

Now do f=imread( rose.jpg ) Notice that it prints out a long list of gray values (integers from 0 to 256). Now type [M,N]=size(f) and you see that this is a 1024 1024 image. This size an attribute that is kept in the array f. To see more such attributes type whos f The attributes here are size, number of bytes, and class (uint8 is 8-bit integers). A few more atrributes can be obtained from K=imfinfo( rose.jpg ) For jpg images this can be used to determine the degree of compression. The command for displaying an image is imshow. Its general syntax is imshow(f,[low,high]) For a monochome image (the kind we are talking about for now), low and anything below is displayed as black, high and anything above it is displayed as white. If we say imshow(f) it displays the image as is. If we do imshow(f,[]) low is set to the minimum pixel value, high is set to the maximum pixel value. Try the following g=imread( pollen.jpg ); imshow(g) figure, imshow(g,[]) Notice that the latter figure has better contrast and does not look as washed. You can write this image in tiff format as follows g2=imwrite(g, pollen2.tif, tif ) For jpeg images this can be used for compression, more on this later. MATLAB also supports the C input/output routines fscan and fprintf. They work like the C versions except that they have the ability to input and output matrices. Again, we will do more on input and output later. We can plot a piece of an image. That is Try plot(f(512,:)) plot(g(238,:)) 9

We have just looked a little bit at the commands for processing images. After you are done, type demo and choose the Image Processing Toolbox demo and just browse through it a bit. If you have a lot of curiousity, you can look at the other demos,too. When you are finished type quit or exit 10