Image Processing Using Matlab
Image Processing Using Matlab
Images in Matlab
• Matlab is optimised for operating on
matrices
• Images are matrices!
• Many useful built-in functions in the
Matlab Image Processing Toolbox
• Very easy to write your own image
processing functions
Workspace
History Command
Matrices & Vectors
• All (almost) entities in MATLAB are matrices
• Easy to define: >> A = [16 3; 5 10]
A = 16 3
5 10
• Vectors -
special case
–n=1 column vector
–m=1 row vector
Creating Vectors and Matrices
• Define >> B = [3 4 5
>> A = [16 3; 5 10] 6 7 8]
A = 16 3 B = 3 4 5
5 10 6 7 8
• Transpose
Vector : Matrix:
1 ans =
2 1 3
3 2 4
Creating Vectors
Create vector with equally spaced intervals
>> x=0:0.5:pi
x =
0 0.5000 1.0000 1.5000 2.0000 2.5000 3.0000
>> A*B 5 12
19 22 21 32
43 50
Some Built-in functions
• mean(A) :mean value of a vector
• max(A), min (A) : maximum and minimum.
• sum(A) : summation.
• sort(A) : sorted vector
• median(A) : median value
• std(A) : standard deviation.
• det(A) :determinant of a square matrix
• dot(a,b) : dot product of two vectors
• Cross(a,b) : cross product of two vectors
• Inv(A) : Inverse of a matrix A
Adding Elements to a Vector or a Matrix
>> C=[1 2; 3 4]
>> A=1:3
C=
A=
1 2
1 2 3
3 4
>> A(4:6)=5:2:9
>> C(3,:)=[5 6];
A=
C=
1 2 3 5 7 9
1 2
>> B=1:2
3 4
B=
5 6
1 2
>> D=linspace(4,12,3);
>> B(5)=7;
>> E=[C D’]
B=
1 2 0 0 7 E=
1 2 4
3 4 8
5 6 12
Loading and displaying images
>> I=imread('mandrill.bmp','bmp'); % load image
image format
Matrix with image filename as a string
image data as a string
>> image(I) % display image
>> whos I
Grand total is 786432 elements using 786432 bytes Matlab can only perform
Display the left
arithmetic operations on half of the
Dimensions of I (red, green
data with class double! mandrill image
and blue intensity information)
ans(:,:,3) =
33 Blue
A(1,2:3)=[0.6068 0.4231]
Indexed images
• An indexed image is where the pixel values are indices to elements in a colour map or
colour lookup table.
• The colour map will contain entries corresponding to red, green and blue intensities for
each index in the image.
>> jet(20) % Generate a jet colourmap for 20 indices 3 4 7 3 6 19 8 9 1 2
ans =
5 6 14 4 2 5 6 1 4 5
0 0 0.6000
0 0 0.8000
RGB Entry for index value 3
2 8 9 4 2 13 7 8 4 5
0 0 1.0000
0 0.2000 1.0000 5 1 11 5 6 4 1 7 4 4
0 0.4000 1.0000
1 9 5 6 5 5 14 4 6 5
0 0.6000 1.0000
0 0.8000 1.0000 5 9 2 1 11 1 3 6 1 9
0 1.0000 1.0000
0.2000 1.0000 0.8000 7 6 8 18 1 8 1 9 13 3
0.4000 1.0000 0.6000 9 2 3 7 2 9 8 16 6 4
0.6000 1.0000 0.4000
0.8000 1.0000 0.2000 7 8 6 7 4 15 8 2 1 3
1.0000 1.0000 0 Values can range
1.0000 0.8000 0
7 5 10 8 4 10 4 3 6 4
from 0.0 to 1.0
1.0000 0.6000 0
1.0000 0.4000 0
1.0000 0.2000 0
Red, green and blue intensities of
1.0000 0 0 the nearest index in the colourmap
0.8000 0 0
0.6000 0 0
are used to display the image.
Image Processing using Matlab Sumitha Balasuriya 16
Displaying indexed images
>> I2=I(:,:,2); % green values of I
>> image(I2) Matlab considers I2 as an indexed image as it doesn’t
contain entries for red, green and blue entries
Index
Associated
color
Colour
Lookup
Table
Red =1.0,
Green = 1.0,
Blue =1.0,
corresponds to
• change colourmap index 64
>> colormap(gray)
Red =0.0,
Green = 0.0,
Type >>help graph3d to get a list of built-in Blue = 0.0,
colourmaps. Experiment with different corresponds to
built-in colourmaps. index 1
height width
H ( x, y ) I (i, j ) M ( x i, y j)
j 1 i 1
• 2D convolution
>>conv2(double(I(:,:,2)),fspecial('gaussian‘,[kernel_height kernel_width] ,sigma),'valid')
-0.2 -0.2
>> title('Cosine')
-0.4 -0.4
>> f2=subplot(1,2,2);
-0.6 -0.6
>> plot(x,sin(x),'d');
-0.8 -0.8
>> grid on;
-1 -1
>> title('Sine'); 0 5 10 0 5 10
Save plots
• Use saveas(h,'filename.ext') to save a
figure to a file.
Useful extension types:
>> f=figure; bmp: Windows bitmap
>> x=-5:0.1:5; emf: Enhanced metafile
>> h=plot(x,cos(2*x+pi/3));
eps: EPS Level 1
>> title('Figure 1');
fig: MATLAB figure
>> xlabel('x');
jpg: JPEG image
>> saveas(h,'figure1.fig')
m: MATLAB M-file
>> saveas(h,'figure1.eps')
tif: TIFF image, compressed
Workspace
• Matlab remembers old commands
• And variables as well
• Each Function maintains its own scope
• The keyword clear removes all variables
from workspace
• The keyword who lists the variables
File I/O
• Matlab has a native file format to save and
load workspaces. Use keywords load and
save.
• In addition MATLAB knows a large
number of popular formats. Type “help
fileformats” for a listing.
• In addition MATLAB supports ‘C’ style
low level file I/O. Type “help fprintf” for
more information.