0% found this document useful (0 votes)
2 views23 pages

CH04 - Introduction to Image Processing Using Matlab

This document serves as an introduction to MATLAB and its application in image processing, covering basic calculations, matrix operations, and image manipulation. It explains how MATLAB treats images as matrices and provides commands for importing, exporting, and performing arithmetic operations on images. Additionally, the document outlines flow control structures and the use of M-files for scripting and function creation in MATLAB.

Uploaded by

xuseendahir18
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views23 pages

CH04 - Introduction to Image Processing Using Matlab

This document serves as an introduction to MATLAB and its application in image processing, covering basic calculations, matrix operations, and image manipulation. It explains how MATLAB treats images as matrices and provides commands for importing, exporting, and performing arithmetic operations on images. Additionally, the document outlines flow control structures and the use of M-files for scripting and function creation in MATLAB.

Uploaded by

xuseendahir18
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 23

Introduction to MATLAB and

image processing
Chapter Outline
• Introduction to matlab.
• Basic calculations in MATLAB.
• Vectors and arrays.
• Multi-dimensional arrays.
• Element by element operations. Polynomial
operations using arrays.
• X-Y Plotting functions.
• Subplots, 3-D Plots and Contour plots
Introduction Matlab
• MATLAB is a programming language and numeric
computing environment that's used for scientific and
engineering applications. It's designed to work with
matrices and arrays, rather than individual numbers.
MATLAB is an abbreviation for "MATrix
LABoratory
• Data analysis, Signal and image processing,
Control systems, Wireless communications,
Robotics, App building, Systems
engineering, Code generation, and
Application deployment.
Introduction to Matlab
• Who uses MATLAB? MATLAB is used by
millions of engineers and scientists. It's
often used in textbooks as an instructional
tool for college-level mathematics,
science, and engineering.
• How does MATLAB work?MATLAB combines a
desktop environment with a programming
language that expresses matrix and array
mathematics directly. It also includes interactive
apps, specialized libraries, and tools for
automatically generating embedded code.
• >> 8/10
ans =
0.8000
• MATLAB indents the numerical result. MATLAB uses high precision
for its computations, but by default it usually displays its results
using four decimal places except when the result is an integer.
• MATLAB assigns the most recent answer to a variable called ans,
which is an abbreviation for answer. A variable in MATLAB is a
symbol used to contain a value. You can use the variable ans for
further calculations; for example, using the MATLAB symbol for
multiplication (*), we obtain
• >> 5*ans
• ans =
• 4
• Note that the variable ans now has the
value 4.
• You can use variables to write
mathematical expressions. Instead of
using the default variable ans, you can
assign the result to a variable of your own
choosing, say, r, as follows:
• >> r=8/10
• r =
• 0.8000
Order of Precedence
Command for managing the work
session
Basic Math Calculations
• >>3*4^2 + 5
• >>8 + 3*5 • ans =
• ans = • 53
• 23 • >>(3*4)^2 + 5
• >>(8 + 3)*5 • ans =
• ans = • 149
• 55 • >>27^(1/3) + 32^(0.2)
• >>4^2 - 12 - 8/4*2 • ans =
• ans = • 5
• 0 • >>27^(1/3) + 32^0.2
• ans =
• >>4^2 - 12 - 8/(4*2)
• 5
• ans =
• >>27^1/3 + 32^0.2
• 3
• ans =
• 11
Test your Understanding
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 • Data types in MATLAB
several image formats – Double (64-bit double-precision
– BMP (Microsoft Windows Bitmap) floating point)
– GIF (Graphics Interchange Files) – Single (32-bit single-precision
– floating point)
HDF (Hierarchical Data Format)
– Int32 (32-bit signed integer)
– JPEG (Joint Photographic Experts
Group) – Int16 (16-bit signed integer)
– PCX (Paintbrush) – Int8 (8-bit signed integer)
– PNG (Portable Network Graphics) – Uint32 (32-bit unsigned integer)
– TIFF (Tagged Image File Format) – Uint16 (16-bit unsigned integer)
– XWD (X Window Dump) – Uint8 (8-bit unsigned integer)
– MATLAB can also load raw-data or
other types of image data
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) X
>> A(2,1)
ans = 4
• : can be used to extract a whole column or
row Y
>> A(:,2)
ans =
2
5
8
• or a part of a column or row
>> A(1:2,2) A=
ans = 1 2 3
2 4 5 6
5 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
A=
ans = 1 4 9
1 2 3
16 25 36
4 5 6
49 64 81 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
A=
col = 1
1 2 3
>> [row col]=find(A>7)
4 5 6
row = 3
7 8 9
3
col = 2
3
>> Indx=find(A<5)
Indx = 1
2
4
7
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
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=
A(row, col)=2;
else 1 2 0
2 1 2
A(row, col)=0;
0 2 1
end
end
end
Flow Control
• while, expression, statements, end A=
1 2 3
4 5 6
Indx=1; 7 8 9
while A(Indx)<6
A(Indx)=0;
Indx=Indx+1;
end

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

You might also like