SlideShare une entreprise Scribd logo
1  sur  14
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;

Contenu connexe

Tendances

Chapter 7.3
Chapter 7.3Chapter 7.3
Chapter 7.3
sotlsoc
 

Tendances (20)

MATRIX HASHING WITH TWO LEVEL OF COLLISION RESOLUTION
MATRIX HASHING WITH TWO LEVEL OF COLLISION RESOLUTIONMATRIX HASHING WITH TWO LEVEL OF COLLISION RESOLUTION
MATRIX HASHING WITH TWO LEVEL OF COLLISION RESOLUTION
 
Matlab matrices and arrays
Matlab matrices and arraysMatlab matrices and arrays
Matlab matrices and arrays
 
Aaa ped-Data-8- manipulation: Plotting and Visualization
Aaa ped-Data-8- manipulation: Plotting and VisualizationAaa ped-Data-8- manipulation: Plotting and Visualization
Aaa ped-Data-8- manipulation: Plotting and Visualization
 
Queue
QueueQueue
Queue
 
R programmingmilano
R programmingmilanoR programmingmilano
R programmingmilano
 
Computer Graphics Concepts
Computer Graphics ConceptsComputer Graphics Concepts
Computer Graphics Concepts
 
Matlab Nn Intro
Matlab Nn IntroMatlab Nn Intro
Matlab Nn Intro
 
Matlab on basic mathematics
Matlab on basic mathematicsMatlab on basic mathematics
Matlab on basic mathematics
 
Dip 5 mathematical preliminaries
Dip 5 mathematical preliminariesDip 5 mathematical preliminaries
Dip 5 mathematical preliminaries
 
Aaa ped-4- Data manipulation: Numpy
Aaa ped-4- Data manipulation: Numpy Aaa ped-4- Data manipulation: Numpy
Aaa ped-4- Data manipulation: Numpy
 
Tomato Classification using Computer Vision
Tomato Classification using Computer VisionTomato Classification using Computer Vision
Tomato Classification using Computer Vision
 
Matlab
MatlabMatlab
Matlab
 
Chapter 7.3
Chapter 7.3Chapter 7.3
Chapter 7.3
 
Data Analysis in Python-NumPy
Data Analysis in Python-NumPyData Analysis in Python-NumPy
Data Analysis in Python-NumPy
 
2-D array
2-D array2-D array
2-D array
 
DATASTRUCTURES UNIT-1
DATASTRUCTURES UNIT-1DATASTRUCTURES UNIT-1
DATASTRUCTURES UNIT-1
 
Dip digital image 3
Dip digital image 3Dip digital image 3
Dip digital image 3
 
Arrays in Data
Arrays in DataArrays in Data
Arrays in Data
 
C Graphics Functions
C Graphics FunctionsC Graphics Functions
C Graphics Functions
 
Algorithms 101 for Data Scientists
Algorithms 101 for Data ScientistsAlgorithms 101 for Data Scientists
Algorithms 101 for Data Scientists
 

Similaire à Intro matlab

Intro matlab and convolution islam
Intro matlab and convolution islamIntro matlab and convolution islam
Intro matlab and convolution islam
Islam Alabbasy
 
Lecture1_computer vision-2023.pdf
Lecture1_computer vision-2023.pdfLecture1_computer vision-2023.pdf
Lecture1_computer vision-2023.pdf
ssuserff72e4
 
Basics of Image Processing using MATLAB
Basics of Image Processing using MATLABBasics of Image Processing using MATLAB
Basics of Image Processing using MATLAB
vkn13
 
CIV1900 Matlab - Plotting & Coursework
CIV1900 Matlab - Plotting & CourseworkCIV1900 Matlab - Plotting & Coursework
CIV1900 Matlab - Plotting & Coursework
TUOS-Sam
 
Variables in matlab
Variables in matlabVariables in matlab
Variables in matlab
TUOS-Sam
 
Image Processing using Matlab . Useful for beginners to learn Image Processing
Image Processing using Matlab . Useful for beginners to learn Image ProcessingImage Processing using Matlab . Useful for beginners to learn Image Processing
Image Processing using Matlab . Useful for beginners to learn Image Processing
Ashok Kumar
 
Matlab intro
Matlab introMatlab intro
Matlab intro
fvijayami
 

Similaire à Intro matlab (20)

Intro matlab and convolution islam
Intro matlab and convolution islamIntro matlab and convolution islam
Intro matlab and convolution islam
 
Lecture1_computer vision-2023.pdf
Lecture1_computer vision-2023.pdfLecture1_computer vision-2023.pdf
Lecture1_computer vision-2023.pdf
 
MATLAB & Image Processing
MATLAB & Image ProcessingMATLAB & Image Processing
MATLAB & Image Processing
 
Fundamentals of Image Processing & Computer Vision with MATLAB
Fundamentals of Image Processing & Computer Vision with MATLABFundamentals of Image Processing & Computer Vision with MATLAB
Fundamentals of Image Processing & Computer Vision with MATLAB
 
Matlab-1.pptx
Matlab-1.pptxMatlab-1.pptx
Matlab-1.pptx
 
Summer training matlab
Summer training matlab Summer training matlab
Summer training matlab
 
Basics of Image Processing using MATLAB
Basics of Image Processing using MATLABBasics of Image Processing using MATLAB
Basics of Image Processing using MATLAB
 
Summer training matlab
Summer training matlab Summer training matlab
Summer training matlab
 
4.ArraysInC.pdf
4.ArraysInC.pdf4.ArraysInC.pdf
4.ArraysInC.pdf
 
Introduction to Machine Learning
Introduction to Machine LearningIntroduction to Machine Learning
Introduction to Machine Learning
 
Introduction to MATLAB
Introduction to MATLABIntroduction to MATLAB
Introduction to MATLAB
 
Matlab intro
Matlab introMatlab intro
Matlab intro
 
CIV1900 Matlab - Plotting & Coursework
CIV1900 Matlab - Plotting & CourseworkCIV1900 Matlab - Plotting & Coursework
CIV1900 Matlab - Plotting & Coursework
 
Variables in matlab
Variables in matlabVariables in matlab
Variables in matlab
 
Image Processing using Matlab . Useful for beginners to learn Image Processing
Image Processing using Matlab . Useful for beginners to learn Image ProcessingImage Processing using Matlab . Useful for beginners to learn Image Processing
Image Processing using Matlab . Useful for beginners to learn Image Processing
 
Matlab intro
Matlab introMatlab intro
Matlab intro
 
MATLAB Programming
MATLAB Programming MATLAB Programming
MATLAB Programming
 
Presentation: Plotting Systems in R
Presentation: Plotting Systems in RPresentation: Plotting Systems in R
Presentation: Plotting Systems in R
 
Matlab ppt
Matlab pptMatlab ppt
Matlab ppt
 
Mat lab workshop
Mat lab workshopMat lab workshop
Mat lab workshop
 

Dernier

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Dernier (20)

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 

Intro matlab

  • 1. 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
  • 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;