SlideShare a Scribd company logo
MATLAB for Image Processing
April 10th
, 2015
Outline
• Introduction to MATLAB
– Basics & Examples
• Image Processing with MATLAB
– Basics & Examples
What is MATLAB?
• MATLAB = Matrix Laboratory
• “MATLAB is a high-level language and
interactive environment that enables you to
perform computationally intensive tasks faster
than with traditional programming languages
such as C, C++ and Fortran.” (
www.mathworks.com)
• MATLAB is an interactive, interpreted language
that is designed for fast numerical matrix
calculations
The MATLAB Environment
• MATLAB window
components:
Workspace
> Displays all the defined
variables
Command Window
> To execute commands
in the MATLAB
environment
Command History
> Displays record of the
commands used
File Editor Window
> Define your functions
MATLAB Help
• MATLAB Help is an
extremely powerful
assistance to learning
MATLAB
• Help not only contains the
theoretical background,
but also shows demos for
implementation
• MATLAB Help can be
opened by using the HELP
pull-down menu
MATLAB Help (cont.)
• Any command description
can be found by typing the
command in the search field
• As shown above, the
command to take square
root (sqrt) is searched
• We can also utilize
MATLAB Help from the
command window as shown
More about the Workspace
• who, whos – current variables in the
workspace
• save – save workspace variables to *.mat
file
• load – load variables from *.mat file
• clear – clear workspace variables
Matrices in MATLAB
• Matrix is the main MATLAB data type
• How to build a matrix?
– A=[1 2 3; 4 5 6; 7 8 9];
– Creates matrix A of size 3 x 3
• Special matrices:
– zeros(n,m), ones(n,m), eye(n,m),
rand(), randn()
• Numbers are always double (64 bits)
unless you specify a different data
type
Basic Operations on Matrices
• All operators in MATLAB are defined on
matrices: +, -, *, /, ^, sqrt,
sin, cos, etc.
• Element-wise operators defined with a
preceding dot: .*, ./, .^
• size(A) – size vector
• sum(A) – columns sums vector
• sum(sum(A)) – sum of all the elements
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 keyword
• Pre-defined variable names
• pi
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], Idx=find(A<4)
A=
7 3 5
6 2 1
Idx=
3
4
6
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 BREAKs 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
Scripts and Functions
• There are two kinds of M-files:
– Scripts, which do not accept input arguments
or return output arguments. They operate on
data in the workspace
– 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!
– CODE
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
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
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
• YOU CANNOT USE THE IMAGE PROCESSING TOOLBOX FOR
HOMEWORK OR FINAL PROJECT
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
• Typically switch images to double
• 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);
size(im)
Image Display
• image - create and display image object
• imagesc - scale and display as image
• imshow - display image
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
http://www.mathworks.com/support/tech-notes/
1100/1109.html
THE END
• Thank you 
• Questions?
Ad

Recommended

MATLAB & Image Processing
MATLAB & Image Processing
Techbuddy Consulting Pvt. Ltd.
 
Intro matlab
Intro matlab
danie_sileshi
 
Lecture1_computer vision-2023.pdf
Lecture1_computer vision-2023.pdf
ssuserff72e4
 
Image processing
Image processing
Pooya Sagharchiha
 
Introduction to MATrices LABoratory (MATLAB) as Part of Digital Signal Proces...
Introduction to MATrices LABoratory (MATLAB) as Part of Digital Signal Proces...
Ahmed Gad
 
Matlab Introduction
Matlab Introduction
ideas2ignite
 
Matlab Tutorial.ppt
Matlab Tutorial.ppt
RaviMuthamala1
 
Mbd dd
Mbd dd
Aditya Choudhury
 
Matlab for beginners, Introduction, signal processing
Matlab for beginners, Introduction, signal processing
Dr. Manjunatha. P
 
MATLAB Workshop yugjjnhhasfhlhhlllhl.pptx
MATLAB Workshop yugjjnhhasfhlhhlllhl.pptx
prashantkumarchinama
 
Matlab anilkumar
Matlab anilkumar
THEMASTERBLASTERSVID
 
MATLAB Programming
MATLAB Programming
محمدعبد الحى
 
MATLAB INTRODUCTION
MATLAB INTRODUCTION
Dr. Krishna Mohbey
 
Introduction to MATLAB
Introduction to MATLAB
Dun Automation Academy
 
MatlabIntro.ppt
MatlabIntro.ppt
ShwetaPandey248972
 
MatlabIntro.ppt
MatlabIntro.ppt
Rajmohan Madasamy
 
MatlabIntro.ppt
MatlabIntro.ppt
konkatisandeepkumar
 
MatlabIntro.ppt
MatlabIntro.ppt
ssuser772830
 
Matlab intro
Matlab intro
THEMASTERBLASTERSVID
 
INTRODUCTION TO MATLAB for PG students.ppt
INTRODUCTION TO MATLAB for PG students.ppt
Karthik537368
 
An Introduction to MATLAB with Worked Examples
An Introduction to MATLAB with Worked Examples
eAssessment in Practice Symposium
 
MatlabIntro1234.ppt.....................
MatlabIntro1234.ppt.....................
RajeshMadarkar
 
MATLAB.pptx
MATLAB.pptx
SarikaAyyathurai1
 
WIDI FREAK MANUSIA SETENGAH EDIOTDAN LEMBOT
WIDI FREAK MANUSIA SETENGAH EDIOTDAN LEMBOT
IrlanMalik
 
MATLAB_CIS601-03.ppt
MATLAB_CIS601-03.ppt
aboma2hawi
 
Programming Environment in Matlab
Programming Environment in Matlab
DataminingTools Inc
 
Matlab: Programming Environment
Matlab: Programming Environment
matlab Content
 
Introduction to scientific computing with matlab.pptx
Introduction to scientific computing with matlab.pptx
CristianFloresMaldon
 
computer-science_engineering_digital-signal-processing_iir-filter-design_note...
computer-science_engineering_digital-signal-processing_iir-filter-design_note...
ssuser5fb79d
 
VLSI-UNIT-2-sheet Resistance and Electrical Properties
VLSI-UNIT-2-sheet Resistance and Electrical Properties
ssuser5fb79d
 

More Related Content

Similar to MATLAB-tutorial for Image Processing with Lecture 3.ppt (20)

Matlab for beginners, Introduction, signal processing
Matlab for beginners, Introduction, signal processing
Dr. Manjunatha. P
 
MATLAB Workshop yugjjnhhasfhlhhlllhl.pptx
MATLAB Workshop yugjjnhhasfhlhhlllhl.pptx
prashantkumarchinama
 
Matlab anilkumar
Matlab anilkumar
THEMASTERBLASTERSVID
 
MATLAB Programming
MATLAB Programming
محمدعبد الحى
 
MATLAB INTRODUCTION
MATLAB INTRODUCTION
Dr. Krishna Mohbey
 
Introduction to MATLAB
Introduction to MATLAB
Dun Automation Academy
 
MatlabIntro.ppt
MatlabIntro.ppt
ShwetaPandey248972
 
MatlabIntro.ppt
MatlabIntro.ppt
Rajmohan Madasamy
 
MatlabIntro.ppt
MatlabIntro.ppt
konkatisandeepkumar
 
MatlabIntro.ppt
MatlabIntro.ppt
ssuser772830
 
Matlab intro
Matlab intro
THEMASTERBLASTERSVID
 
INTRODUCTION TO MATLAB for PG students.ppt
INTRODUCTION TO MATLAB for PG students.ppt
Karthik537368
 
An Introduction to MATLAB with Worked Examples
An Introduction to MATLAB with Worked Examples
eAssessment in Practice Symposium
 
MatlabIntro1234.ppt.....................
MatlabIntro1234.ppt.....................
RajeshMadarkar
 
MATLAB.pptx
MATLAB.pptx
SarikaAyyathurai1
 
WIDI FREAK MANUSIA SETENGAH EDIOTDAN LEMBOT
WIDI FREAK MANUSIA SETENGAH EDIOTDAN LEMBOT
IrlanMalik
 
MATLAB_CIS601-03.ppt
MATLAB_CIS601-03.ppt
aboma2hawi
 
Programming Environment in Matlab
Programming Environment in Matlab
DataminingTools Inc
 
Matlab: Programming Environment
Matlab: Programming Environment
matlab Content
 
Introduction to scientific computing with matlab.pptx
Introduction to scientific computing with matlab.pptx
CristianFloresMaldon
 
Matlab for beginners, Introduction, signal processing
Matlab for beginners, Introduction, signal processing
Dr. Manjunatha. P
 
MATLAB Workshop yugjjnhhasfhlhhlllhl.pptx
MATLAB Workshop yugjjnhhasfhlhhlllhl.pptx
prashantkumarchinama
 
INTRODUCTION TO MATLAB for PG students.ppt
INTRODUCTION TO MATLAB for PG students.ppt
Karthik537368
 
MatlabIntro1234.ppt.....................
MatlabIntro1234.ppt.....................
RajeshMadarkar
 
WIDI FREAK MANUSIA SETENGAH EDIOTDAN LEMBOT
WIDI FREAK MANUSIA SETENGAH EDIOTDAN LEMBOT
IrlanMalik
 
MATLAB_CIS601-03.ppt
MATLAB_CIS601-03.ppt
aboma2hawi
 
Programming Environment in Matlab
Programming Environment in Matlab
DataminingTools Inc
 
Matlab: Programming Environment
Matlab: Programming Environment
matlab Content
 
Introduction to scientific computing with matlab.pptx
Introduction to scientific computing with matlab.pptx
CristianFloresMaldon
 

More from ssuser5fb79d (8)

computer-science_engineering_digital-signal-processing_iir-filter-design_note...
computer-science_engineering_digital-signal-processing_iir-filter-design_note...
ssuser5fb79d
 
VLSI-UNIT-2-sheet Resistance and Electrical Properties
VLSI-UNIT-2-sheet Resistance and Electrical Properties
ssuser5fb79d
 
Structured Logic Design With Very Higspeed Integrated Circuit Hardware Descri...
Structured Logic Design With Very Higspeed Integrated Circuit Hardware Descri...
ssuser5fb79d
 
presentationofvlsi-180404214525 (2).pptx
presentationofvlsi-180404214525 (2).pptx
ssuser5fb79d
 
VLSID_2023 Unit II BEP of MOS Bi-CMOS Circuits.pdf
VLSID_2023 Unit II BEP of MOS Bi-CMOS Circuits.pdf
ssuser5fb79d
 
DSS2_Explorations.pptx
DSS2_Explorations.pptx
ssuser5fb79d
 
WondersOfGPU.ppt
WondersOfGPU.ppt
ssuser5fb79d
 
Network Analysis
Network Analysis
ssuser5fb79d
 
computer-science_engineering_digital-signal-processing_iir-filter-design_note...
computer-science_engineering_digital-signal-processing_iir-filter-design_note...
ssuser5fb79d
 
VLSI-UNIT-2-sheet Resistance and Electrical Properties
VLSI-UNIT-2-sheet Resistance and Electrical Properties
ssuser5fb79d
 
Structured Logic Design With Very Higspeed Integrated Circuit Hardware Descri...
Structured Logic Design With Very Higspeed Integrated Circuit Hardware Descri...
ssuser5fb79d
 
presentationofvlsi-180404214525 (2).pptx
presentationofvlsi-180404214525 (2).pptx
ssuser5fb79d
 
VLSID_2023 Unit II BEP of MOS Bi-CMOS Circuits.pdf
VLSID_2023 Unit II BEP of MOS Bi-CMOS Circuits.pdf
ssuser5fb79d
 
DSS2_Explorations.pptx
DSS2_Explorations.pptx
ssuser5fb79d
 
Ad

Recently uploaded (20)

60 Years and Beyond eBook 1234567891.pdf
60 Years and Beyond eBook 1234567891.pdf
waseemalazzeh
 
Machine Learning - Classification Algorithms
Machine Learning - Classification Algorithms
resming1
 
تقرير عن التحليل الديناميكي لتدفق الهواء حول جناح.pdf
تقرير عن التحليل الديناميكي لتدفق الهواء حول جناح.pdf
محمد قصص فتوتة
 
Modern multi-proposer consensus implementations
Modern multi-proposer consensus implementations
François Garillot
 
Complete University of Calculus :: 2nd edition
Complete University of Calculus :: 2nd edition
Shabista Imam
 
Generative AI & Scientific Research : Catalyst for Innovation, Ethics & Impact
Generative AI & Scientific Research : Catalyst for Innovation, Ethics & Impact
AlqualsaDIResearchGr
 
Proposal for folders structure division in projects.pdf
Proposal for folders structure division in projects.pdf
Mohamed Ahmed
 
Tally.ERP 9 at a Glance.book - Tally Solutions .pdf
Tally.ERP 9 at a Glance.book - Tally Solutions .pdf
Shabista Imam
 
Industrial internet of things IOT Week-3.pptx
Industrial internet of things IOT Week-3.pptx
KNaveenKumarECE
 
Microwatt: Open Tiny Core, Big Possibilities
Microwatt: Open Tiny Core, Big Possibilities
IBM
 
Rapid Prototyping for XR: Lecture 6 - AI for Prototyping and Research Directi...
Rapid Prototyping for XR: Lecture 6 - AI for Prototyping and Research Directi...
Mark Billinghurst
 
System design handwritten notes guidance
System design handwritten notes guidance
Shabista Imam
 
Rapid Prototyping for XR: Lecture 5 - Cross Platform Development
Rapid Prototyping for XR: Lecture 5 - Cross Platform Development
Mark Billinghurst
 
دراسة حاله لقرية تقع في جنوب غرب السودان
دراسة حاله لقرية تقع في جنوب غرب السودان
محمد قصص فتوتة
 
Abraham Silberschatz-Operating System Concepts (9th,2012.12).pdf
Abraham Silberschatz-Operating System Concepts (9th,2012.12).pdf
Shabista Imam
 
LECTURE 7 COMPUTATIONS OF LEVELING DATA APRIL 2025.pptx
LECTURE 7 COMPUTATIONS OF LEVELING DATA APRIL 2025.pptx
rr22001247
 
Industry 4.o the fourth revolutionWeek-2.pptx
Industry 4.o the fourth revolutionWeek-2.pptx
KNaveenKumarECE
 
Solar thermal – Flat plate and concentrating collectors .pptx
Solar thermal – Flat plate and concentrating collectors .pptx
jdaniabraham1
 
NEW Strengthened Senior High School Gen Math.pptx
NEW Strengthened Senior High School Gen Math.pptx
DaryllWhere
 
Rapid Prototyping for XR: Lecture 1 Introduction to Prototyping
Rapid Prototyping for XR: Lecture 1 Introduction to Prototyping
Mark Billinghurst
 
60 Years and Beyond eBook 1234567891.pdf
60 Years and Beyond eBook 1234567891.pdf
waseemalazzeh
 
Machine Learning - Classification Algorithms
Machine Learning - Classification Algorithms
resming1
 
تقرير عن التحليل الديناميكي لتدفق الهواء حول جناح.pdf
تقرير عن التحليل الديناميكي لتدفق الهواء حول جناح.pdf
محمد قصص فتوتة
 
Modern multi-proposer consensus implementations
Modern multi-proposer consensus implementations
François Garillot
 
Complete University of Calculus :: 2nd edition
Complete University of Calculus :: 2nd edition
Shabista Imam
 
Generative AI & Scientific Research : Catalyst for Innovation, Ethics & Impact
Generative AI & Scientific Research : Catalyst for Innovation, Ethics & Impact
AlqualsaDIResearchGr
 
Proposal for folders structure division in projects.pdf
Proposal for folders structure division in projects.pdf
Mohamed Ahmed
 
Tally.ERP 9 at a Glance.book - Tally Solutions .pdf
Tally.ERP 9 at a Glance.book - Tally Solutions .pdf
Shabista Imam
 
Industrial internet of things IOT Week-3.pptx
Industrial internet of things IOT Week-3.pptx
KNaveenKumarECE
 
Microwatt: Open Tiny Core, Big Possibilities
Microwatt: Open Tiny Core, Big Possibilities
IBM
 
Rapid Prototyping for XR: Lecture 6 - AI for Prototyping and Research Directi...
Rapid Prototyping for XR: Lecture 6 - AI for Prototyping and Research Directi...
Mark Billinghurst
 
System design handwritten notes guidance
System design handwritten notes guidance
Shabista Imam
 
Rapid Prototyping for XR: Lecture 5 - Cross Platform Development
Rapid Prototyping for XR: Lecture 5 - Cross Platform Development
Mark Billinghurst
 
دراسة حاله لقرية تقع في جنوب غرب السودان
دراسة حاله لقرية تقع في جنوب غرب السودان
محمد قصص فتوتة
 
Abraham Silberschatz-Operating System Concepts (9th,2012.12).pdf
Abraham Silberschatz-Operating System Concepts (9th,2012.12).pdf
Shabista Imam
 
LECTURE 7 COMPUTATIONS OF LEVELING DATA APRIL 2025.pptx
LECTURE 7 COMPUTATIONS OF LEVELING DATA APRIL 2025.pptx
rr22001247
 
Industry 4.o the fourth revolutionWeek-2.pptx
Industry 4.o the fourth revolutionWeek-2.pptx
KNaveenKumarECE
 
Solar thermal – Flat plate and concentrating collectors .pptx
Solar thermal – Flat plate and concentrating collectors .pptx
jdaniabraham1
 
NEW Strengthened Senior High School Gen Math.pptx
NEW Strengthened Senior High School Gen Math.pptx
DaryllWhere
 
Rapid Prototyping for XR: Lecture 1 Introduction to Prototyping
Rapid Prototyping for XR: Lecture 1 Introduction to Prototyping
Mark Billinghurst
 
Ad

MATLAB-tutorial for Image Processing with Lecture 3.ppt

  • 1. MATLAB for Image Processing April 10th , 2015
  • 2. Outline • Introduction to MATLAB – Basics & Examples • Image Processing with MATLAB – Basics & Examples
  • 3. What is MATLAB? • MATLAB = Matrix Laboratory • “MATLAB is a high-level language and interactive environment that enables you to perform computationally intensive tasks faster than with traditional programming languages such as C, C++ and Fortran.” ( www.mathworks.com) • MATLAB is an interactive, interpreted language that is designed for fast numerical matrix calculations
  • 4. The MATLAB Environment • MATLAB window components: Workspace > Displays all the defined variables Command Window > To execute commands in the MATLAB environment Command History > Displays record of the commands used File Editor Window > Define your functions
  • 5. MATLAB Help • MATLAB Help is an extremely powerful assistance to learning MATLAB • Help not only contains the theoretical background, but also shows demos for implementation • MATLAB Help can be opened by using the HELP pull-down menu
  • 6. MATLAB Help (cont.) • Any command description can be found by typing the command in the search field • As shown above, the command to take square root (sqrt) is searched • We can also utilize MATLAB Help from the command window as shown
  • 7. More about the Workspace • who, whos – current variables in the workspace • save – save workspace variables to *.mat file • load – load variables from *.mat file • clear – clear workspace variables
  • 8. Matrices in MATLAB • Matrix is the main MATLAB data type • How to build a matrix? – A=[1 2 3; 4 5 6; 7 8 9]; – Creates matrix A of size 3 x 3 • Special matrices: – zeros(n,m), ones(n,m), eye(n,m), rand(), randn() • Numbers are always double (64 bits) unless you specify a different data type
  • 9. Basic Operations on Matrices • All operators in MATLAB are defined on matrices: +, -, *, /, ^, sqrt, sin, cos, etc. • Element-wise operators defined with a preceding dot: .*, ./, .^ • size(A) – size vector • sum(A) – columns sums vector • sum(sum(A)) – sum of all the elements
  • 10. 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 keyword • Pre-defined variable names • pi
  • 11. Logical Operators • ==, <, >, (not equal) ~=, (not) ~ • find(‘condition’) – Returns indexes of A’s elements that satisfy the condition
  • 12. Logical Operators (cont.) • Example: >>A=[7 3 5; 6 2 1], Idx=find(A<4) A= 7 3 5 6 2 1 Idx= 3 4 6
  • 13. Flow Control • MATLAB has five flow control constructs: – if statement – switch statement – for loop – while loop – break statement
  • 14. if • IF statement condition – The general form of the IF statement is IF expression statements ELSEIF expression statements ELSE statements END • CODE
  • 15. 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
  • 16. 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 BREAKs are unnecessary) • CODE
  • 17. for • FOR repeats statements a specific number of times • The general form of a FOR statement is: FOR variable=expr statements END • CODE
  • 18. while • WHILE repeats statements an indefinite number of times • The general form of a WHILE statement is: WHILE expression statements END • CODE
  • 19. Scripts and Functions • There are two kinds of M-files: – Scripts, which do not accept input arguments or return output arguments. They operate on data in the workspace – Functions, which can accept input arguments and return output arguments. Internal variables are local to the function
  • 20. 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! – CODE
  • 21. 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
  • 22. 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
  • 23. Outline • Introduction to MATLAB – Basics & Examples • Image Processing with MATLAB – Basics & Examples
  • 24. 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 • YOU CANNOT USE THE IMAGE PROCESSING TOOLBOX FOR HOMEWORK OR FINAL PROJECT
  • 25. 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 • Typically switch images to double • 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)
  • 26. 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)
  • 27. 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)
  • 28. 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);
  • 29. Images and Matrices Binary Image: row = 256; col = 256; img = rand(row, col); img = round(img); figure; imshow(img); size(im)
  • 30. Image Display • image - create and display image object • imagesc - scale and display as image • imshow - display image
  • 31. 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 http://www.mathworks.com/support/tech-notes/ 1100/1109.html
  • 32. THE END • Thank you  • Questions?

Editor's Notes

  • #2: Firstly, I will talk about some basics of MATLAB, including the development environment, basic operations and syntax of MATLAB language, so that we can have a big picture of MATLAB. Then, I will focus on the image processing issues with MATLAB. At the end, I will give some more examples for demonstration.
  • #3: MATLAB stands for..
  • #8: After looking through the developing environment, let’s have a look at MATLAB language itself
  • #12: Return linear indices
  • #23: Firstly, I will talk about some basics of MATLAB, including the development environment, basic operations and syntax of MATLAB language, so that we can have a big picture of MATLAB. Then, I will focus on the image processing issues with MATLAB. At the end, I will give some more examples for demonstration.
  • #27: The imagesc function scales image data to the full range of the current colormap and displays the image. imtool opens a new Image Tool in an empty state. imtool(I) displays the grayscale image I. image(C) displays matrix C as an image.