25
What Matlab can do for me? Matlab stands for MATrix LABoratory Matlab is a software package for high- performance numerical computation and visualization. It provides an interactive environment with hundreds of built-in functions. User can write his own functions also in Matlab. Matlab also provides an interface (external) to run the C and Fortran codes from within Matlab. There are several optional toolboxes in Matlab. (eg) Image Processing, Neural Networks, Distributed Computing, Data Acquisition, Video processing, etc.

What Matlab can do for me? Matlab stands for MATrix LABoratory Matlab is a software package for high-performance numerical computation and visualization

Embed Size (px)

Citation preview

Page 1: What Matlab can do for me? Matlab stands for MATrix LABoratory Matlab is a software package for high-performance numerical computation and visualization

What Matlab can do for me?

• Matlab stands for MATrix LABoratory• Matlab is a software package for high-performance

numerical computation and visualization. • It provides an interactive environment with hundreds of

built-in functions. • User can write his own functions also in Matlab. • Matlab also provides an interface (external) to run the C

and Fortran codes from within Matlab. • There are several optional toolboxes in Matlab. (eg)

Image Processing, Neural Networks, Distributed Computing, Data Acquisition, Video processing, etc.

Page 2: What Matlab can do for me? Matlab stands for MATrix LABoratory Matlab is a software package for high-performance numerical computation and visualization

Features of Matlab

• The basic building block of Matlab is matrix. • The fundamental data-type is array. • Vectors, scalars, real and complex matrices are

handled automatically. • We need not declare the dimensions of a matrix. • In Matlab, built-in functions are optimized for

vector operations and hence they can run faster. • Matlab supports many platforms such as

Windows, AIX, HP UX, IRIX, Linux and Solaris operating systems.

Page 3: What Matlab can do for me? Matlab stands for MATrix LABoratory Matlab is a software package for high-performance numerical computation and visualization

Matlab Desktop

• The Matlab desktop is the main MATLAB application window.

• It contains 5 sub windows:• The command window• The Workspace Browser• The current directory window• Command history window• One or more Figure windows (shown only user

displays a graphics)

Page 4: What Matlab can do for me? Matlab stands for MATrix LABoratory Matlab is a software package for high-performance numerical computation and visualization

Command Window & Workspace Browser

• Command window is the place where the user types Matlab commands and expressions at the prompt (>>).

• It is the place where the outputs of those commands are displayed.

• Workspace browser is a place where the set of variables (created by the user) and the information about them are displayed.

• We can do various things such as plotting, by clicking on a variable and then using the right button on the mouse to select our option.

Page 5: What Matlab can do for me? Matlab stands for MATrix LABoratory Matlab is a software package for high-performance numerical computation and visualization

Current Directory Window and Command History Window

• Current Directory window is a place where all our files from current directory are listed.

• By right clicking on a file here, we can run m-files, rename, delete, etc.

• Command history window is a place where all commands typed on Matlab prompt in command window are recorded even across multiple sessions.

• We can select commands from this window with the mouse and execute it in command window by double clicking on it.

• We can also select a set of commands from this window and create a new m-file by right clicking the mouse.

Page 6: What Matlab can do for me? Matlab stands for MATrix LABoratory Matlab is a software package for high-performance numerical computation and visualization

Graphics and Edit Window

• Graphics window: The output of all graphics commands typed in command window are flushed to the graphics or Figure window.

• It is a separate gray window with default white background color.

• User can create many figure windows (memory should allow).

• Edit window: This is the window where we write, edit, create and save our own programs in files called ‘m-files’. (select ‘Desktop’ menu and click ‘Editor’ sub option). We can also type ‘edit’ at the command prompt.

Page 7: What Matlab can do for me? Matlab stands for MATrix LABoratory Matlab is a software package for high-performance numerical computation and visualization

Help in Matlab• We can also use our own editor. • In Matlab, at the prompt, one can type ‘!’ followed by

local operating system commands.• On-line documentation: Matlab provides on-line help for

all its built-in functions and programming language constructs.

• The commands lookfor, help, helpwin and helpdesk give on-line help.

• >> lookfor im2bw (gives only brief details)• >> help im2bw (gives detailed information)• >> helpwin im2bw (creates a separate window and

displays detailed information)• Demo: Typing demo at the Matlab prompts to invoke the

demonstration program.

Page 8: What Matlab can do for me? Matlab stands for MATrix LABoratory Matlab is a software package for high-performance numerical computation and visualization

Output Display & Command History

• Matlab is case-sensitive. Many Matlab commands and built-in functions are typed in lowercase letters.

• Output Display: A semicolon at the end of a command supresses the screen output, except for graphics and on-line help commands.

• To instruct Matlab to show one screen of output at a time, we have to type more on at the Matlab prompt.

• >> more on• >> help im2bw• Command history: Matlab saves previously typed

commands in a buffer. • These commands can be recalled with the up-arrow

key. We can also recall a previous command by typing the first few characters and then pressing the ↑ key.

Page 9: What Matlab can do for me? Matlab stands for MATrix LABoratory Matlab is a software package for high-performance numerical computation and visualization

File types• Matlab has 3 types of files for storing. They are M-files, Mat-files

and Mex-files.• M-files: These are standard ASCII text files, with a .m extension to

the filename. There are 2 types of .m files. They are script files and function files. All built-in functions in Matlab are M-files.

• Mat-files: These are binary data-files with a .mat extension to the filename. These are created by Matlab when we save data with the save command. The data is written in a special format that only Matlab can read.

• savefile = 'test.mat';• p = rand(1, 10);• q = ones(10);• save(savefile, 'p', 'q')• They are loaded into Matlab with the load command.• >> load('test.mat')• Mex-files: These are Matlab-callable Fortran and C programs, with

a .mex extension to the filename.

Page 10: What Matlab can do for me? Matlab stands for MATrix LABoratory Matlab is a software package for high-performance numerical computation and visualization

Important General Commands

• Who - lists variables currently in the workspace

• Whos- lists variables currently in the workspace with their size

• What -lists m-, mat- and mex-files on the disk• Clear -clears the workspace, all variables are

removed• Clear all -clears all variables and functions

from workspace• Clear x y z-clears only variables x, y and z

Page 11: What Matlab can do for me? Matlab stands for MATrix LABoratory Matlab is a software package for high-performance numerical computation and visualization

Important General Commands

• clc, home -clears command window

• clf -clears figure window

• ~c (control-c) -local abort, kills the current command execution

• quit, exit -quits matlab

Page 12: What Matlab can do for me? Matlab stands for MATrix LABoratory Matlab is a software package for high-performance numerical computation and visualization

Simple Matlab Commands• The result of an unassigned expression is saved in the default variable ‘ans’• >> 7 + 5• ans = 12• We can assign the value of an expression to a variable.• >> x = 7 + 5• x = 12• A semicolon at the end suppresses screen output. We can call the value of

x by typing x.• >> theta = acos(-1);• theta = 3.1416• The floating point output display is controlled by format• >> format long• >> theta • theta = 3.14159265358979• Matlab recognizes the letters i and j as imaginary number. A complex

number is represented as 2 + 5i or 2 + 5*j

Page 13: What Matlab can do for me? Matlab stands for MATrix LABoratory Matlab is a software package for high-performance numerical computation and visualization

Matlab for image processing

• Matlab has set of functions for processing multidimensional arrays of which images are special case.

• Image Processing Toolbox (IPT) is a collection of functions that extend the capability of MATLAB.

• In many image processing books, the image origin is defined to be at (x,y) = (0,0).

• But toolbox uses the notation (r,c) to indicate rows and columns.

• Here the origin of the coordinate system is at (r,c) = (1,1).

• The variables in Matlab must begin with a letter and contain only letters, numerals and underscores.

Page 14: What Matlab can do for me? Matlab stands for MATrix LABoratory Matlab is a software package for high-performance numerical computation and visualization

Reading an Image

• We use imread function to read an image in Matlab. • Its syntax is: imread(‘filename’)• The supported file formats here are:• Format Description

Extension• --------- -------------- ------------• TIFF Tagged Image File Format .tif, .tiff• JPEG Joint Photographic Experts Group.jpg, .peg• GIF Graphics Interchange Format .gif• BMP Windows Bitmap .bmp• PNG Portable Network Group .png• XWD X Window Dump .xwd

Page 15: What Matlab can do for me? Matlab stands for MATrix LABoratory Matlab is a software package for high-performance numerical computation and visualization

Reading an Image

• >> f = imread(‘cameraman.tif’);• Here we use single quotes (‘) to delimit the string

filename. • The semicolon suppresses output. • If it is not used, MATLAB displays the result of the

operation at that line. • Here imread reads file from the current directory. • To read an image from a specified directory, we

need to given full or relative path of the directory in filename.

• >> f = imread(‘d:\dip\cameraman.tif’);

Page 16: What Matlab can do for me? Matlab stands for MATrix LABoratory Matlab is a software package for high-performance numerical computation and visualization

Size of an Image• The function size gives the row and column dimensions of an

image:• >> f = imread('cameraman.tif');• >> size(f)• ans = 256 256• >> [M N] = size(f);• This command stores the number of rows in M and columns in N.• The whos function displays some more information about an array.• >>whos f• Gives• Name Size Bytes Class• ------- ----- ------ -------• f 512 X 512 25588 unit8 array

Page 17: What Matlab can do for me? Matlab stands for MATrix LABoratory Matlab is a software package for high-performance numerical computation and visualization

Displaying Images • The function imshow is used to display images in MATLAB.

The syntax is:• imshow(f, G)• where f is an image and G is the number of intensity levels

used to display it. If G is omitted, it defaults to 256 levels. The syntax:

• imshow(f, [low high]) displays as black values less than or equal to low and as white all values greater than or equal to high.

• >> f = imread('cameraman.tif');• >> imshow(f, [50 100]) The syntax: imshow(f, [ ])• sets variable low to the minimum value of array f and high to its

maximum value. This form of imshow is used when we have low dynamic ranges or we have positive and negative values.

Page 18: What Matlab can do for me? Matlab stands for MATrix LABoratory Matlab is a software package for high-performance numerical computation and visualization

Displaying Images• The function pixval is used to display the intensity values of

individual pixels interactively. • This function displays a cursor overlaid on an image. • As the cursor is moved, the coordinates of the cursor position and

its intensity values are shown on a display that appears below the figure window.

• If the left button on the mouse is clicked and then held pressed, pixval displays the Euclidean distance between the initial and current cursor locations.

• Clicking the X button on the cursor window turns it off.• To keep the first image and output a second image, we use function

figure as follows:• >>imshow(f), figure, imshow(g)• This command displays both images.

Page 19: What Matlab can do for me? Matlab stands for MATrix LABoratory Matlab is a software package for high-performance numerical computation and visualization

Writing Images

• Images are stored in a hard disk using the function imwrite. Its syntax is:

• imwrite(f, ‘filename’)• The format of the file can be specified as a

third input argument here.• >>imwrite(f, ‘newcameraman’,’tif’) (or)• >>imwrite(f, ‘newcameraman.tif’)• When there is no path information, the file

is stored in the current directory.

Page 20: What Matlab can do for me? Matlab stands for MATrix LABoratory Matlab is a software package for high-performance numerical computation and visualization

More on imwrite

• The imwrite syntax applicable only to JPEG images is :• imwrite(f, ‘filename.jpg’, ‘quality’, q)• Here q is an integer between 0 and 100 (the lower the

number the higher the degradation due to JPEG compression).

• >> imwrite(f, ‘camearaman.jpg’, ‘quality’, 25).• To get an idea of the compression and to get other

image details, we use:• >> imfinfo cameraman.jpg• This command gives info about date of modification,

filesize, format, width, height, bitdepth, colortype, etc.• >> K = imfinfor(‘cameraman.jpg’);

Page 21: What Matlab can do for me? Matlab stands for MATrix LABoratory Matlab is a software package for high-performance numerical computation and visualization

Computing the compression ratio

• Here we store the output of imfinfo into a structure variable called K for future usage.

• Using the dot operator one can access the fields of a structure variable. (eg) the image image height is available in the field K.height

• >> K = imfinfo(‘cameraman.gif’);• >> image_bytes = K.Width*K.Height*K.BitDepth/8;• >> compressed_bytes = K.FileSize;• >> compression_ratio = image_bytes/compressed_bytes• Contents of a figure window are exported to disk: • We use the File pull-down menu of the figure window

and choose File menu and Export Setup submenu and click Export button. Now user can select a location, file name and format.

Page 22: What Matlab can do for me? Matlab stands for MATrix LABoratory Matlab is a software package for high-performance numerical computation and visualization

Image Types

• The toolbox supports 4 types of images:• Intensity images, binary images, indexed images and

RGB images. • Of these, the first 2 types are very frequently used.• Intensity Images• These images are also called as ‘Gray scale images’.

Here the intensity values are represented by 8 bits. Hence we have 2^8 = 256 possible shades of black and white colors. The lowest one namely black has a value of 0 and the highest one namely white has a value of 255.

• If the image is of class double, the values are floating point numbers.

• Values of scaled, class double intensity images are in the range [0,1] by convention.

Page 23: What Matlab can do for me? Matlab stands for MATrix LABoratory Matlab is a software package for high-performance numerical computation and visualization

Binary Images• It is one of the very frequently used image types.

Here the intensity value can either be 0 (black) or 1 (white). Hence we need only one bit to represent it. A binary image is a logical array of 0s and 1s.

• A binary image is a logical array of 0s and 1s. • A numeric array is converted to binary using

function logical. • B = logical(A)• To test if an array is logical, we use the function:• islogical(C)• If C is logical, this function returns a 1, otherwise

it returns a 0.

Page 24: What Matlab can do for me? Matlab stands for MATrix LABoratory Matlab is a software package for high-performance numerical computation and visualization

Indexed Images

Here all possible colors that are present in a given image are identified and indexed in a table.

• Each of these indexes will have the amount of red, green and blue compositions required to generate that color.

• Then in the image matrix we store only the index values (which may require only lesser number of bits).

• Hence this approach saves space for storage.

Page 25: What Matlab can do for me? Matlab stands for MATrix LABoratory Matlab is a software package for high-performance numerical computation and visualization

RGB Images

• This is one of the widely used color image types.

• Here the intensity of each image pixel is represented as a combination of Red, Green and Blue components.

• Each of these components will have 256 shades (represented by 8 bits).

• Hence each pixel requires 3*8 = 24 bits. • The total number of possible colors can

generate here is 2^24 = 16.6 million colors.