matlab basic and image

56
MathWorks ® MATLAB ® R2012a (7.14) DIVYANSHU RASAURIA ROOL NO. 1313231814 ID 213507 BRANCH -EC

Upload: divyanshu-rashouariya

Post on 08-Jan-2017

136 views

Category:

Education


3 download

TRANSCRIPT

Page 1: Matlab basic and image

MathWorks®

MATLAB®

R2012a (7.14)DIVYANSHU RASAURIAROOL NO. 1313231814

ID 213507BRANCH -EC

Page 2: Matlab basic and image

MATLAB

Introducing MATLAB – Language of technical computing 2

Is an integrated technical computing environment that combines numeric computation, advanced graphics and visualization, and a high-level programming language.

The MathWorks, Inc. High-level language of technical computing

Development environment for engineers, scientists

Interactive tools for design, problem solving

Mathematical function libraries

Graphics and data visualization tools

Custom GUIs

External Interfaces: C, C++, Fortran, Java, COM, Excel, .NET

Page 3: Matlab basic and image

MATLAB product family

Toolboxes: Collections of specialized Functions

Graphics

2D Graphics3D GraphicsColor & LightingAnimationImage, Audio & Video

ComputationsLinear AlgebraData AnalysisPolynomialsInterpolationsSolutions of ODE

External Interfaces

CC++JAVA.netMicorosoft Excel

Parallel ComputingMath, Statistics, and OptimizationControl System Design and AnalysisSignal Processing and CommunicationsImage Processing and Computer VisionTest and Measurement

Built-in Functions

User-written Functions

MATLAB

Computational FinanceComputational Biology

Code Generation and VerificationApplication Deployment

Database Connectivity and Reporting

MATLAB Programming Language

Parallel Computing ToolboxMATLAB Distributed Computing Server

Parallel Computing Symbolic Math ToolboxPartial Differential Equation ToolboxStatistics ToolboxCurve Fitting Toolbox

Optimization ToolboxGlobal Optimization ToolboxNeural Network ToolboxModel-Based Calibration Toolbox

Math, Statistics, and OptimizationControl System ToolboxSystem Identification ToolboxFuzzy Logic Toolbox

Robust Control ToolboxModel Predictive Control ToolboxAerospace Toolbox

Control System Design and Analysis Signal Processing ToolboxDSP System ToolboxCommunications System ToolboxWavelet Toolbox

Fixed-Point ToolboxRF ToolboxPhased Array System Toolbox

Signal Processing and CommunicationsImage Processing ToolboxComputer Vision System ToolboxImage Acquisition ToolboxMapping Toolbox

Image Processing and Computer Vision

Data Acquisition ToolboxInstrument Control ToolboxImage Acquisition ToolboxOPC ToolboxVehicle Network ToolboxTest and Measurement

Financial ToolboxEconometrics Toolbox

Datafeed ToolboxDatabase Toolbox

Computational FinanceSpreadsheet Link EX (for Microsoft Excel)Fixed-Income Toolbox

Financial Derivatives Toolbox

Bioinformatics ToolboxSimBiology

Computational BiologyMATLAB CoderHDL Coder

HDL VerifierFilter Design HDL Coder

Code Generation and Verification

MATLAB CompilerMATLAB Builder NE (for Microsoft .NET Framework)

MATLAB Builder JA (for Java language)MATLAB Builder EX (for Microsoft Excel)

Spreadsheet Link EX (for Microsoft Excel)

Application DeploymentDatabase ToolboxMATLAB Report Generator

Database Connectivity and Reporting

Introducing MATLAB – Language of technical computing 3

Page 4: Matlab basic and image

Agenda

Introducing MATLAB – Language of technical computingMinimal MATLAB

4

Page 5: Matlab basic and image

Agenda

Introducing MATLAB – Language of technical computingMinimal MATLAB

MATLAB DesktopComputing with MATLAB

5

Array – creation & workingPlotting – creating & visualizingAutomating commands – creating, saving & executing script fileConditional control statementsLoop control statementsFunctions – creating & executing a function fileMATLAB for Image Processing

Page 6: Matlab basic and image

Agenda

Introducing MATLAB – Language of technical computingMinimal MATLAB

MATLAB DesktopComputing with MATLABArray – creation & workingPlotting – creating & visualizingAutomating commands – creating, saving & executing script file

Functions – creating & executing a function file

6

Conditional control statementsLoop control statements

Page 7: Matlab basic and image

The main window characterized by the MATLAB command prompt (>>)Command Window

This is where you write, edit, create, and save your own script or programs in files called m-files.

Editor Window

This is where all your files from the current directory are listed.Current Directory

All commands typed on MATLAB prompt in the command window get recorded, even across multiple session in this window.

Command History

This subwindow lists all variables that you have generated so far along with there type and size.

Workspace

MATLAB - Desktop

Minimal MATLAB 7

Page 8: Matlab basic and image

MATLAB - Desktop

The main window characterized by the MATLAB command prompt (>>)Help Window

Minimal MATLAB 8

Page 9: Matlab basic and image

Agenda

Introducing MATLAB – Language of technical computingMinimal MATLAB

MATLAB DesktopComputing with MATLABArray – creation & workingPlotting – creating & visualizingAutomating commands – creating, saving & executing script file

Functions – creating & executing a function file

9

Conditional control statementsLoop control statements

Page 10: Matlab basic and image

Basic Data and Variables Computing with MATLAB

Class

Variable

SizeValue

Minimal MATLAB 10

Page 11: Matlab basic and image

Basic Data and Variables Computing with MATLAB

Multidimensional Array[full or sparse] Scalar

logical

ab

char

{ }cell struct

functionhandle (@)numeric

int8, uint8,int16, unit16,int32, unit32,int64, unit64

single double

Minimal MATLAB 11

Page 12: Matlab basic and image

Arithmetic Operator & Their Precedence

Computing with MATLAB

Operations Operators Examples

AdditionSubtractionMultiplicationRight DivisionLeft DivisionExponentiation

*/\^

>> >> >> >> >> >> (means )

Precedence Order Operators

1

234

Parentheses ( ). For nested parentheses, the innermostare executed first.Exponentiation, ^Multiplication, *; Division, /,\Addition, +; Subtraction, -

help precedenceMinimal MATLAB 12

Page 13: Matlab basic and image

Computing with MATLABMathematical Functions

Trigonometry

sindcosdtandcotdsecdcscd

Degree

sincostancotseccsc

sinhcoshtanhcothsechcsch

asinacosatanacotasecacsc

Basic Hyperbolic Inverse

Radian

Basic

Exponential

exploglog10log2sqrt

Complex

absconjimagrealangle

Matrix

dettracerankinvexpmsqrtm

Discrete Math

factorialgcdlcmprimes

help <type function name>Minimal MATLAB 13

Page 14: Matlab basic and image

Predefined Variables and Keywords

ans

pieps

infi,jNaN

A variable that has the value of the last expression that was not assigned to aspecific variableThe number π.The smallest difference between two numbers. Equal to 2^(–52), which isapproximately 2.2204e–016.Used for infinity.Defined as , which is: 0 + 1.0000i.Stands for Not-a-Number. Used when MATLAB cannot determine a validnumeric value. Example: 0/0.

Predefined Variables

break case catch classdef continue else elseif end for function global if otherwise parfor persistent return spmd switch try while

Keywords

iskeyword

help <Predefined Variable Name>

Computing with MATLAB

Minimal MATLAB 14

Page 15: Matlab basic and image

Managing Variables

clearclear x y zclear allwho

Clears the workspace, all variables are removed from the memoryClears only variables x, y, and zClears all variables and functions from workspaceLists variables currently in the workspace

Command Outcome

Computing with MATLAB

Minimal MATLAB 15

Page 16: Matlab basic and image

Agenda

Introducing MATLAB – Language of technical computingMinimal MATLAB

MATLAB DesktopComputing with MATLABArray – creation & workingPlotting – creating & visualizingAutomating commands – creating, saving & executing script file

Functions – creating & executing a function file

16

Conditional control statementsLoop control statements

Page 17: Matlab basic and image

Overview Array

5

5745 7 4

5 10 566 4 67 78 86

Scalar of 1 X 1 Row Vector of 1 X 3 Column vector of 3 X 1

One Dimensional Matrix of 3 X 3

5 10 56 85 236 4 6 23 27 78 86 53 6

5 14 12 10 566 10 504 4 6 89 0

Two Dimensional Matrix of 3 X 5

5 10 56 85 236 4 6 23 27 78 86 53 6

5 14 12 10 56 6 0

5 14 12 10 56 89 0

Three Dimensional Matrix of 3 X 5

Minimal MATLAB 17

Page 18: Matlab basic and image

Creation Array

One Dimensional Array

variable_name = [ type vector elements ]

Minimal MATLAB 18

Two Dimensional Array

variable_name = [1st row elements; 2nd row elements; 3rd row elements; ... ; last row elements]

from a known list of numbers with constant spacing by specifying the first term, the spacing, and the

last term

Known lists of numbers

Page 19: Matlab basic and image

Array

Minimal MATLAB 19

Creation

Creating a vector with constant spacing by specifying the first term, the spacing, and the last term:

variable_name = [m:q:n]

or

variable_name = m:q:n

m = first term

q = spacing

n = last term

: (colon operator)Constant spacing

Creating a vector with linear (equal) spacing by specifying the first and last terms, and the number of terms:

variable_name = linspace(xi, xf, n)

xi = first element

xf = Last elementn = number of elements

linspaceConstant spacing

Page 20: Matlab basic and image

Array

Minimal MATLAB 20

Array Operators

+ Addition- Subtraction.* Element-by-element multiplication./ Element-by-element division.\ Element-by-element left division.^ Element-by-element power‘ Transpose

Operations Operation

𝐴=1 34 2

𝐵=2 35 7

𝐶=𝐴 .∗𝐵= 2 920 14

𝐶=1×2 3×34×5 2×7

Page 21: Matlab basic and image

Data Manipulation Array

Minimal MATLAB 21

Addressing - Extraction (Subset)

Addressing an element(s) individually or in subgroups: To redefine only some of the elements, When specific elements are to be used in calculations, When a subgroup of the elements is used to define a new variable.

Page 22: Matlab basic and image

Array

Minimal MATLAB 22

Address vector

>> ve = [35 46 78 23 5 14 81 3 55]

ve =

35 46 78 23 5 14 81 3 551 2 3 4 5 6 7 8 9 position or index of element

Data Manipulation

Page 23: Matlab basic and image

Array

Minimal MATLAB 23

Address matrix )

(1,1) (1,2) (1,3) (1,4)

(2,1) (2,2) (2,3) (2,4)

(3,1) (3,2) (3,3) (3,4)

3 11 6 5

4 7 10 2

13 9 0 8

MAT =

Data Manipulation

Page 24: Matlab basic and image

Array

Minimal MATLAB 24

Addressing vector : (colon operator)

Data Manipulation

Addressing matrix : (colon operator)

A(:,n) Refers to the elements in all the rows of column n of the matrix A.

A(n,:) Refers to the elements in all the columns of row n of the matrix A.

A(:,m:n) Refers to the elements in all the rows between columns m and n of the matrix A.

A(m:n,:) Refers to the elements in all the columns between rows m and n of the matrix A.

A(m:n,p:q) Refers to the elements in rows m through n and columns p through q of the matrix A.

Page 25: Matlab basic and image

Agenda

Introducing MATLAB – Language of technical computingMinimal MATLAB

MATLAB DesktopComputing with MATLABArray – creation & workingPlotting – creating & visualizingAutomating commands – creating, saving & executing script file

Functions – creating & executing a function file

25

Conditional control statementsLoop control statements

Page 26: Matlab basic and image

Creation Plotting

Minimal MATLAB 26

ExampleDamped Oscillation equation: 𝒚=𝒆−𝒂𝒕𝐜𝐨𝐬 (𝒃𝒕)

Page 27: Matlab basic and image

Creation Plotting

Minimal MATLAB 27

ExampleDamped Oscillation equation: 𝒚=𝒆−𝒂𝒕𝐜𝐨𝐬 (𝒃𝒕)

a = 0.1; b = 1;t = 0:0.1:20;y = exp(-a*t).*cos(b*t);plot(t,y)grid on;xlabel('Time in second');ylabel('Response in mm');title('Damped Oscillation');legend('a = 0.1,b = 1)');

1-2-3-4-5-6-7-8-9-

Page 28: Matlab basic and image

Creation Plotting

Minimal MATLAB 28

Overview

ylabel xlabel

title

legend

Page 29: Matlab basic and image

Graphing Plotting

Minimal MATLAB 29

Plot function plot(x,y)where the variables x and y are vector and must be of same size i.e the number of elements should be equal.

x=[1 2 3 5 7 7.5 8 10];y=[2 6.5 7 7 5.5 4 6 8];plot(x,y)

Page 30: Matlab basic and image

Graphing Plotting

Minimal MATLAB 30

Plot function

plot(x,y,‘line specifiers’,‘PropertyName’,PropertyValue)

Line specifiers are optional and can be used to define the style and color of theline and the type of markers (if markers are desired).

Line specifiers

solid (default)dasheddotteddash-dot

Line Style Specifier

---:-.

redgreenbluecyan

Line color Specifier

rgbc

magentayellowblackwhite

Line color Specifier

mykw

Page 31: Matlab basic and image

Graphing Plotting

Minimal MATLAB 31

Plot function

plot(x,y,‘line specifiers’,‘PropertyName’,PropertyValue)

Line specifiers are optional and can be used to define the style and color of theline and the type of markers (if markers are desired).

Line specifiers

plus signcircleasteriskpointcrosstriangle (pointed up)triangle (pointed down)

Marker type Specifier

+o*.

^v

squarediamondfive pointed starsix pointed startriangle (pointed left)triangle (pointed right)

Marker type Specifier

sdph<>

Page 32: Matlab basic and image

Graphing Plotting

Minimal MATLAB 32

Plot function

plot(x,y,‘line specifiers’,‘PropertyName’,PropertyValue)

To specify the thickness of the line, the size of the marker, and the colors of the marker’s edge line and fill

Property Name

Page 33: Matlab basic and image

Agenda

Introducing MATLAB – Language of technical computingMinimal MATLAB

MATLAB DesktopComputing with MATLABArray – creation & workingPlotting – creating & visualizingAutomating commands – creating, saving & executing script file

Functions – creating & executing a function file

33

Conditional control statementsLoop control statements

Page 34: Matlab basic and image

Creation Script

Minimal MATLAB 34

OverviewSequence of MATLAB statements in fileScript

A script file is an external file that contains a sequence of MATLAB statements. Script files have a filename extension of .m.

Page 35: Matlab basic and image

Basics Script

Minimal MATLAB 35

variable_name = input(‘prompt message’)

Accepting input from user

Displaying output to user

disp(name of a variable)

disp(‘text as string’)

Saving script file

Creating script file

Executing script file

Page 36: Matlab basic and image

Agenda

Introducing MATLAB – Language of technical computingMinimal MATLAB

MATLAB DesktopComputing with MATLABArray – creation & workingPlotting – creating & visualizingAutomating commands – creating, saving & executing script file

Functions – creating & executing a function file

36

Conditional control statementsLoop control statements

Page 37: Matlab basic and image

Creation Script

Minimal MATLAB 37

Conditional StatementConditional expression consisting of relational and/or logical operators.

Operations Operators

Less thanGreater thanLess than or equal toGreater than or equal toEqual toNot Equal to

><=>===~=

Relational OperatorsOperations Operators

AndOrNot

&||~

Logical Operators

Page 38: Matlab basic and image

Creation Script

Minimal MATLAB 38

Example

𝑔 (𝑡 )=¿{0 , 𝑡<−2  

−4−2𝑡 ,−2<𝑡<0  −4−3 𝑡 ,0<𝑡<4  16−2𝑡 ,4<𝑡<8  

0 , 𝑡>8  }Plot

g1g2g3

t = linspace(-5,10);g1 = -4 - 2*t;g2 = -4 + 3*t;g3 = 16 - 2*t;g = g1.*(-2<t & t<=0)... + g2.*(0<t & t<=4)... + g3.*(4<t & t<=8);plot(t,g)

1-2-3-4-5-6-7-

g1

g2

g3

Page 39: Matlab basic and image

if/elseif/else Statement Script

Minimal MATLAB 39

ifstatement

StatementGroup 1

False

True

End

StatementGroup 2

if expression statementselseif expression statementselse statementsend

Page 40: Matlab basic and image

Creation Script

Minimal MATLAB 40

Switch Statement

False

True

Flowchart

matchcase, k

caseend

False

case statement, k

True

switch switch_expression case case_expression statements case case_expression statements : otherwise statementsend

Page 41: Matlab basic and image

Agenda

Introducing MATLAB – Language of technical computingMinimal MATLAB

MATLAB DesktopComputing with MATLABArray – creation & workingPlotting – creating & visualizingAutomating commands – creating, saving & executing script file

Functions – creating & executing a function file

41

Conditional control statementsLoop control statements

Page 42: Matlab basic and image

Loop control statements Script

Minimal MATLAB 42

for (Iterative Loop)

while (Conditional Loop)

continue, break

for index = start:increment:endstatements

end

while expression statementsend

Page 43: Matlab basic and image

Agenda

Introducing MATLAB – Language of technical computingMinimal MATLAB

MATLAB DesktopComputing with MATLABArray – creation & workingPlotting – creating & visualizingAutomating commands – creating, saving & executing script file

Functions – creating & executing a function file

43

Conditional control statementsLoop control statements

Page 44: Matlab basic and image

Creation Function

Minimal MATLAB 44

Overview

SYSTEM[Mathematical model of Physical Phenomenon]

(function file)

Set of inputs Set of outputs123

m

123

n

[System]Electrical NetworksCommunication SystemControl SystemThermodynamic SystemComputer system, etc

[Input variables]current, voltage,power, energy,temperature, data

[Output variables]current, voltage,

power, energy,temperature,

Processed data

function [out1, out2, ...] = func_name(in1, in2, ...)

declares the function func_name, and its inputs and outputs

Page 45: Matlab basic and image

Creation Function

Minimal MATLAB 45

Example

𝐻=|𝑦𝑥 |= 1

√1+(𝜔𝑅𝐶)2Black BoxX Y

function H = lp(R,C)w = logspace(-2,6);H = 1./sqrt(1 + (w*R*C).^2);semilogx(w,H)xlabel('Angular frquency, w (rad/s)');ylabel('System Response');title('Low-Pass frequency response');

1-2-3-4-5-6-

Source: MATLAB An Introduction with Applications, Amos Gilat, 4e

Page 46: Matlab basic and image

Creation Function

Minimal MATLAB 46

Example

Page 47: Matlab basic and image

Creation Function

Minimal MATLAB 47

Example

Black Box𝑇 𝑇 𝑑𝑇𝑤𝐵𝑃 𝑅𝐻

𝑅𝐻=100 𝑒𝑒𝑠

𝑇 𝑑=243.5 ln ( 𝑒

6.112 )

17.67− ln ( 𝑒6.112

)

, dew point temperature

, dry-bulb temperature

, wet-bulb temperature

, barometric temperature in millibars

, relative humidity in %

𝑃𝑠=6.112 exp17.67𝑇𝑇 +243.5

𝑃𝑤=6.112 exp17.67𝑇𝑤

𝑇𝑤+243.5

𝑒=𝑒𝑤−𝐵𝑃 (𝑇 −𝑇𝑤)0.00066 (1+0.00115𝑇𝑤)

Source: MATLAB An Introduction with Applications, Amos Gilat, 4e

Page 48: Matlab basic and image

Creation Function

Minimal MATLAB 48

Example

function [Td, RH] = DewptRhum(T,Tw,BP)es = 6.112*exp((17.67*T)/(T + 243.5));ew = 6.112*exp((17.67*Tw)/(Tw + 243.5));e = ew - BP*(T - Tw)*0.00066*(1 + 0.00115*Tw);RH = 100*(e/es);Td = (243.5*log(e/6.112))/(17.67 - log(e/6.112));

1-2-3-4-5-

𝑇=25 °𝐶 ,𝑇𝑤=19°𝐶 ,𝐵𝑃=985𝑚𝑖𝑙𝑙𝑖𝑏𝑎𝑟Calculate

𝑇=36 °𝐶 ,𝑇𝑤=31°𝐶 ,𝐵𝑃=1020𝑚𝑖𝑙𝑙𝑖𝑏𝑎𝑟

Page 49: Matlab basic and image

Creation Function

Minimal MATLAB 48

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

Page 50: Matlab basic and image

• 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

• Typically switch images to double to perform any processing and convert back to unsigned integer

• 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

Page 51: Matlab basic and image

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

Images in MATLAB

Page 52: Matlab basic and image

Image Import and Export

• Read and write images in Matlabimg = imread('apple.jpg');dim = size(img);figure;imshow(img);imwrite(img, 'output.bmp', 'bmp');

• Alternatives to imshow imagesc(I) imtool(I) image(I)

Page 53: Matlab basic and image

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

Page 54: Matlab basic and image

Images and Matrices

Binary Image:

row = 256;col = 256;img = rand(row, col);img = round(img);figure;imshow(img);size(im)

Page 55: Matlab basic and image

Image Display

• image - create and display image object• imagesc - scale and display as image• imshow - display image

Page 56: Matlab basic and image

THANKS

DIVYANSHU [email protected]