SlideShare a Scribd company logo
Introduction to MATLAB and
image processing
Amin Allalou
amin@cb.uu.se
Centre for Image Analysis
Uppsala University
Computer Assisted Image Analysis
April 4 2008
MATLAB and images
• The help in MATLAB is very good, use it!
• An image in MATLAB is treated as a matrix
• Every pixel is a matrix element
• All the operators in MATLAB defined on
matrices can be used on images: +, -, *, /, ^, sqrt, sin, cos etc.
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)
– MATLAB can also load raw-data or
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-by-n-by-3
• Indexed images : m-by-3 color map
• Multidimensional images m-by-n-by-p (p is the number of layers)
Image import and export
• Read and write images in Matlab
>> I=imread('cells.jpg');
>> imshow(I)
>> size(I)
ans = 479 600 3 (RGB image)
>> Igrey=rgb2gray(I);
>> imshow(Igrey)
>> imwrite(lgrey, 'cell_gray.tif', 'tiff')
Alternatives to imshow
>>imagesc(I)
>>imtool(I)
>>image(I)
Images and Matrices
• How to build a matrix (or image)?
>> A = [ 1 2 3; 4 5 6; 7 8 9 ];
A = 1 2 3
4 5 6
7 8 9
>> B = zeros(3,3)
B = 0 0 0
0 0 0
0 0 0
>> C = ones(3,3)
C = 1 1 1
1 1 1
1 1 1
>>imshow(A) (imshow(A,[]) to get automatic pixel range)
Images and Matrices
• Accesing image elements (row, column)
>> A(2,1)
ans = 4
• : can be used to extract a whole column or
row
>> A(:,2)
ans =
2
5
8
• or a part of a column or row
>> A(1:2,2)
ans =
2
5
X
Y
A =
12 3
45 6
7 8 9
Image Arithmetic
• Arithmetic operations such as addition, subtraction, multiplication and
division can be applied to images in MATLAB
– +, -, *, / performs matrix operations
>> A+A
ans = 2 4 6
8 10 12
14 16 18
>> A*A
ans = 30 36 42
66 81 96
102 126 150
• To perform an elementwise operation use . (.*, ./, .*, .^ etc)
>> A.*A
ans = 1 4 9
16 25 36
49 64 81
A =
12 3
45 6
7 8 9
Logical Conditions
• equal (==) , less than and greater than (< and >), not equal (~=) and not (~)
• find(‘condition’) - Returns indexes of A’s elements that satisfies the
condition.
>> [row col]=find(A==7)
row = 3
col = 1
>> [row col]=find(A>7)
row = 3
3
col = 2
3
>> Indx=find(A<5)
Indx = 1
2
4
7
A =
12 3
45 6
7 8 9
Flow Control
• Flow control in MATLAB
- if, else and elseif statements
(row=1,2,3 col=1,2,3)
if row==col
A(row, col)=1;
elseif abs(row-col)==1
A(row, col)=2;
else
A(row, col)=0;
end
A =
1 2 0
2 1 2
0 2 1
Flow Control
• Flow control in MATLAB
- for loops
for row=1:3
for col=1:3
if row==col
A(row, col)=1;
elseif abs(row-col)==1
A(row, col)=2;
else
A(row, col)=0;
end
end
end
A =
1 2 0
2 1 2
0 2 1
Flow Control
• while, expression, statements, end
Indx=1;
while A(Indx)<6
A(Indx)=0;
Indx=Indx+1;
end
A =
12 3
45 6
7 8 9
A =
0 2 3
0 5 6
7 8 9
Working with M-Files
• M-files can be scripts that simply execute a series of MATLAB statements,
or they can be functions that also accept input arguments and produce
output.
• MATLAB functions:
– Are useful for extending the MATLAB language for your application.
– Can accept input arguments and return output arguments.
– Store variables in a workspace internal to the function.
Working with M-Files
• Create a new empty m-file
function B=test(I)
[row col]=size(I)
for r=1:row
for c=1:col
if r==c
A(r, c)=1;
elseif abs(r-c)==1
A(r, c)=2;
else
A(r, c)=0;
end
end
end
B=A;

More Related Content

What's hot (20)

PPTX
MATRIX HASHING WITH TWO LEVEL OF COLLISION RESOLUTION
Nvk Chaitanya
 
PPTX
Matlab matrices and arrays
Ameen San
 
PDF
Aaa ped-Data-8- manipulation: Plotting and Visualization
AminaRepo
 
PDF
R programmingmilano
Ismail Seyrik
 
PDF
Computer Graphics Concepts
SHAKOOR AB
 
PPT
Matlab Nn Intro
Imthias Ahamed
 
PPT
Matlab on basic mathematics
monauweralam1
 
PPTX
Dip 5 mathematical preliminaries
Manas Mantri
 
PDF
Aaa ped-4- Data manipulation: Numpy
AminaRepo
 
PPTX
Tomato Classification using Computer Vision
Raman Pandey
 
PPTX
Chapter 7.3
sotlsoc
 
PPTX
Data Analysis in Python-NumPy
Devashish Kumar
 
PDF
2-D array
Swarup Kumar Boro
 
PPT
DATASTRUCTURES UNIT-1
Malikireddy Bramhananda Reddy
 
PPTX
Dip digital image 3
Shajun Nisha
 
PPTX
Arrays in Data
Afzal Badshah
 
PDF
C Graphics Functions
SHAKOOR AB
 
PPTX
Algorithms 101 for Data Scientists
Christopher Conlan
 
MATRIX HASHING WITH TWO LEVEL OF COLLISION RESOLUTION
Nvk Chaitanya
 
Matlab matrices and arrays
Ameen San
 
Aaa ped-Data-8- manipulation: Plotting and Visualization
AminaRepo
 
R programmingmilano
Ismail Seyrik
 
Computer Graphics Concepts
SHAKOOR AB
 
Matlab Nn Intro
Imthias Ahamed
 
Matlab on basic mathematics
monauweralam1
 
Dip 5 mathematical preliminaries
Manas Mantri
 
Aaa ped-4- Data manipulation: Numpy
AminaRepo
 
Tomato Classification using Computer Vision
Raman Pandey
 
Chapter 7.3
sotlsoc
 
Data Analysis in Python-NumPy
Devashish Kumar
 
DATASTRUCTURES UNIT-1
Malikireddy Bramhananda Reddy
 
Dip digital image 3
Shajun Nisha
 
Arrays in Data
Afzal Badshah
 
C Graphics Functions
SHAKOOR AB
 
Algorithms 101 for Data Scientists
Christopher Conlan
 

Similar to Intro matlab (20)

PPT
MATLAB-tutorial for Image Processing with Lecture 3.ppt
ssuser5fb79d
 
PDF
Lecture1_computer vision-2023.pdf
ssuserff72e4
 
PPTX
MATLAB & Image Processing
Techbuddy Consulting Pvt. Ltd.
 
PDF
Image processing using matlab
dedik dafiyanto
 
PDF
Basics of Image Processing using MATLAB
vkn13
 
PDF
Matlab dip
Jeevan Reddy
 
PPTX
Image Processing Using MATLAB
Amarjeetsingh Thakur
 
PPTX
Image processing in MATLAB
Amarjeetsingh Thakur
 
PDF
Image processing
Pooya Sagharchiha
 
PDF
Matlab intro
fvijayami
 
PDF
Introduction to MATLAB
Dun Automation Academy
 
PPT
INTRODUCTION TO MATLAB for PG students.ppt
Karthik537368
 
PDF
MATLAB - PRESENTATION for PG studentspdf
Karthik537368
 
PPT
mathematics laboratory lecture 1_matlab.ppt
DBalraj1
 
PDF
MATLAB
svati sharma
 
PPT
Intro matlab and convolution islam
Islam Alabbasy
 
PPT
lec1_matlab.ppt basic all operations matlab operations
samraj sundarraj
 
PPT
MATLAB_CIS601-03.ppt
aboma2hawi
 
PPT
Matlab Tutorial.ppt
RaviMuthamala1
 
PDF
tutorial1.pdf
ShwetaPandey248972
 
MATLAB-tutorial for Image Processing with Lecture 3.ppt
ssuser5fb79d
 
Lecture1_computer vision-2023.pdf
ssuserff72e4
 
MATLAB & Image Processing
Techbuddy Consulting Pvt. Ltd.
 
Image processing using matlab
dedik dafiyanto
 
Basics of Image Processing using MATLAB
vkn13
 
Matlab dip
Jeevan Reddy
 
Image Processing Using MATLAB
Amarjeetsingh Thakur
 
Image processing in MATLAB
Amarjeetsingh Thakur
 
Image processing
Pooya Sagharchiha
 
Matlab intro
fvijayami
 
Introduction to MATLAB
Dun Automation Academy
 
INTRODUCTION TO MATLAB for PG students.ppt
Karthik537368
 
MATLAB - PRESENTATION for PG studentspdf
Karthik537368
 
mathematics laboratory lecture 1_matlab.ppt
DBalraj1
 
MATLAB
svati sharma
 
Intro matlab and convolution islam
Islam Alabbasy
 
lec1_matlab.ppt basic all operations matlab operations
samraj sundarraj
 
MATLAB_CIS601-03.ppt
aboma2hawi
 
Matlab Tutorial.ppt
RaviMuthamala1
 
tutorial1.pdf
ShwetaPandey248972
 
Ad

Recently uploaded (20)

PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PDF
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PDF
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PDF
July Patch Tuesday
Ivanti
 
PDF
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
PDF
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PDF
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PDF
Python basic programing language for automation
DanialHabibi2
 
PDF
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
PPTX
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
July Patch Tuesday
Ivanti
 
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
Python basic programing language for automation
DanialHabibi2
 
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
Ad

Intro matlab

  • 1. Introduction to MATLAB and image processing Amin Allalou [email protected] Centre for Image Analysis Uppsala University Computer Assisted Image Analysis April 4 2008
  • 2. MATLAB and images • The help in MATLAB is very good, use it! • An image in MATLAB is treated as a matrix • Every pixel is a matrix element • All the operators in MATLAB defined on matrices can be used on images: +, -, *, /, ^, sqrt, sin, cos etc.
  • 3. 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) – MATLAB can also load raw-data or 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)
  • 4. Images in MATLAB • Binary images : {0,1} • Intensity images : [0,1] or uint8, double etc. • RGB images : m-by-n-by-3 • Indexed images : m-by-3 color map • Multidimensional images m-by-n-by-p (p is the number of layers)
  • 5. Image import and export • Read and write images in Matlab >> I=imread('cells.jpg'); >> imshow(I) >> size(I) ans = 479 600 3 (RGB image) >> Igrey=rgb2gray(I); >> imshow(Igrey) >> imwrite(lgrey, 'cell_gray.tif', 'tiff') Alternatives to imshow >>imagesc(I) >>imtool(I) >>image(I)
  • 6. Images and Matrices • How to build a matrix (or image)? >> A = [ 1 2 3; 4 5 6; 7 8 9 ]; A = 1 2 3 4 5 6 7 8 9 >> B = zeros(3,3) B = 0 0 0 0 0 0 0 0 0 >> C = ones(3,3) C = 1 1 1 1 1 1 1 1 1 >>imshow(A) (imshow(A,[]) to get automatic pixel range)
  • 7. Images and Matrices • Accesing image elements (row, column) >> A(2,1) ans = 4 • : can be used to extract a whole column or row >> A(:,2) ans = 2 5 8 • or a part of a column or row >> A(1:2,2) ans = 2 5 X Y A = 12 3 45 6 7 8 9
  • 8. Image Arithmetic • Arithmetic operations such as addition, subtraction, multiplication and division can be applied to images in MATLAB – +, -, *, / performs matrix operations >> A+A ans = 2 4 6 8 10 12 14 16 18 >> A*A ans = 30 36 42 66 81 96 102 126 150 • To perform an elementwise operation use . (.*, ./, .*, .^ etc) >> A.*A ans = 1 4 9 16 25 36 49 64 81 A = 12 3 45 6 7 8 9
  • 9. Logical Conditions • equal (==) , less than and greater than (< and >), not equal (~=) and not (~) • find(‘condition’) - Returns indexes of A’s elements that satisfies the condition. >> [row col]=find(A==7) row = 3 col = 1 >> [row col]=find(A>7) row = 3 3 col = 2 3 >> Indx=find(A<5) Indx = 1 2 4 7 A = 12 3 45 6 7 8 9
  • 10. Flow Control • Flow control in MATLAB - if, else and elseif statements (row=1,2,3 col=1,2,3) if row==col A(row, col)=1; elseif abs(row-col)==1 A(row, col)=2; else A(row, col)=0; end A = 1 2 0 2 1 2 0 2 1
  • 11. Flow Control • Flow control in MATLAB - for loops for row=1:3 for col=1:3 if row==col A(row, col)=1; elseif abs(row-col)==1 A(row, col)=2; else A(row, col)=0; end end end A = 1 2 0 2 1 2 0 2 1
  • 12. Flow Control • while, expression, statements, end Indx=1; while A(Indx)<6 A(Indx)=0; Indx=Indx+1; end A = 12 3 45 6 7 8 9 A = 0 2 3 0 5 6 7 8 9
  • 13. Working with M-Files • M-files can be scripts that simply execute a series of MATLAB statements, or they can be functions that also accept input arguments and produce output. • MATLAB functions: – Are useful for extending the MATLAB language for your application. – Can accept input arguments and return output arguments. – Store variables in a workspace internal to the function.
  • 14. Working with M-Files • Create a new empty m-file function B=test(I) [row col]=size(I) for r=1:row for c=1:col if r==c A(r, c)=1; elseif abs(r-c)==1 A(r, c)=2; else A(r, c)=0; end end end B=A;