Image Processing Using Matlab
Image Processing Using Matlab
Image Processing Using Matlab
Sumitha Balasuriya
http//www.dcs.gla.ac.uk/~sumitha/
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
2
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)
3
Representation of Images
• Images are just an array of numbers
>> I % ctrl+c to halt output!
ans(:,:,3) =
33 Blue
4
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 5
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
6
Displaying indexed images (continued)
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
7
Useful functions for displaying images
>> axis image % plot fits to data
>> h=axes('position', [0 0 0.5 0.5]);
>> axes(h);
>> imagesc(I2)
8
Histograms
• Frequency of the intensity values of the image
• Quantise frequency into intervals (called bins)
• (Un-normalised) probability density function of
image intensities
9
Computing histograms of images in Matlab
>>hist(reshape(double(Lena(:,:,2)),[512*512 1]),50)
Generate the histograms of the green channel of the Lena image using the
following number of bins : 10, 20, 50, 100, 200, 500, 1000
10
Visualising the intensity surface
>>surf(double(imresize(Lena(:,:,2),[50 50])))
11
Useful functions for manipulating
images
• Convert image to grayscale
>>Igray=rgb2gray(I);
• Resize image
>>Ismall=imresize(I,[100 100], 'bilinear');
• Rotate image
>>I90=imrotate(I,90);
12
Other useful functions
Convert polar coordinates to Check if a variable is null Trigonometric functions
cartesian coordinates >>isempty(I) sin, cos, tan
>>pol2cart(rho,theta)
Convert polar coordinates to Find indices and elements in a Fast Fourier Transform
cartesian coordinates matrix
fft2(I)
>>cart2pol(x,y) >>[X,Y]=find(I>100)
13
Convolution
Bit of theory! Convolution of two functions f(x) and g(x)
h( x ) f ( x ) g ( x )
f (r ) g ( x r )dr
height width
H ( x, y ) I (i, j )M ( x i, y j )
j 1 i 1
15
Convolution example in 1D
Horizontal slice from Mandrill image
Filtered Signal
1D Gaussian filter
=
• 1D convolution
>>conv(signal,filter)
• 2D convolution
>>conv2(double(I(:,:,2)),fspecial('gaussian‘,[kernel_height kernel_width] ,sigma),'valid')