Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
18 views

Image Processing Using Matlab

Matlab is optimized for operating on matrices. Images can be represented as matrices, with each pixel value corresponding to intensities of red, green, and blue colors. Matlab has many built-in image processing functions and it is easy to write custom functions. Images are loaded and displayed as matrices, with indexing used to access pixel values. Indexed images use a color lookup table to map pixel values to colors for display.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Image Processing Using Matlab

Matlab is optimized for operating on matrices. Images can be represented as matrices, with each pixel value corresponding to intensities of red, green, and blue colors. Matlab has many built-in image processing functions and it is easy to write custom functions. Images are loaded and displayed as matrices, with indexing used to access pixel values. Indexed images use a color lookup table to map pixel values to colors for display.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

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

Image Processing using Matlab Sumitha Balasuriya 2


MATLAB Desktop
Menu and toolbar

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

• Use ‘,’ or ‘ ’ to separate row elements -- use


‘;’ to separate rows
Matrices & Vectors - II
• Order of Matrix - mn
– m=no. of rows, n=no. of columns

• 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:

>> a=[1 2 3]; >> A=[1 2; 3 4];

>> a' >> A'

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

Create vector with n equally spaced intervals


>> x=linspace(0, pi, 7)
x =
0 0.5236 1.0472 1.5708 2.0944 2.6180 3.1416

Note: MATLAB uses pi to represent  , uses i or j to represent


imaginary unit
Creating Matrices
• zeros(m, n): matrix with all zeros
• ones(m, n): matrix with all ones.
• eye(m, n): the identity matrix
• rand(m, n): uniformly distributed random
• randn(m, n): normally distributed random
• magic(m): square matrix whose elements have
the same sum, along the row, column and
diagonal.
Matrix operations
• ^ : exponentiation
• * : multiplication
• / : division
• \ : left division. The operation A\B is effectively
the same as INV(A)*B, although left division
is calculated differently and is much quicker.
• + : addition
• - : subtraction
Array Operations
• Evaluated element by element
.' : array transpose (non-conjugated transpose)
.^ : array power
.* : array multiplication
./ : array division
• Very different from Matrix operations
>> A=[1 2;3 4]; But:

>> B=[5 6;7 8]; >> A.*B

>> 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

Name Size Bytes Class

I 512x512x3 786432 uint8 array

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)

Image Processing using Matlab Sumitha Balasuriya 13


Representation of Images
• Images are just an array of numbers
>> I % ctrl+c to halt output!

• Intensity of each pixel is represented by the pixel element’s


value in the red, green and blue matrices
>> I(1,1,:) % RGB values of element (1,1)
ans(:,:,1) =
Red
135 Images where the pixel value in the image
ans(:,:,2) = represents the intensity of the pixel are called
intensity images.
97 Green

ans(:,:,3) =
33 Blue

Image Processing using Matlab Sumitha Balasuriya 14


Indexing Matrices
Given the matrix: A = n

m 0.9501 0.6068 0.4231


Then: 0.2311 0.4860 0.2774

A(1,2) = 0.6068 Aij ,i  1...m, j  1...n


A(3) = 0.6068
index (i 1)m  j
A(:,1) = [0.9501
1:m  ]
0.2311


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

>> colorbar % display colourmap

Index

Associated
color

Colour
Lookup
Table

Image Processing using Matlab Sumitha Balasuriya 17


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

Define your own colourmap mymap by Red =1.0,


creating a matrix (size m x 3 ) with red, Green = 1.0,
green, blue entries. Display an image using Blue =1.0,
your colourmap. corresponds to
index 255

• scale colourmap Red =0.0,


Green = 0.0,
>> imagesc(I2) Blue = 0.0,
corresponds to
index 0

Image Processing using Matlab Sumitha Balasuriya 18


Useful functions for displaying images
>> axis image % plot fits to data
>> h=axes('position', [0 0 0.5 0.5]);
>> axes(h);
>> imagesc(I2)

Investigate axis and axes


functions using Matlab’s help

Image Processing using Matlab Sumitha Balasuriya 19


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);

Image Processing using Matlab Sumitha Balasuriya 20


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)

Get size of matrix Change the dimensions of a Discrete Cosine Transform


>>size(I) matrix dct(I)
>>reshape(rand(10,10),[100 1])
Add elements of a Matrix Exponentials and Logarithms
(columnwise addition in matrices) exp
>>sum(I) log
log10

Image Processing using Matlab Sumitha Balasuriya 21


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

Image convolution Filter Support region


Output of filter where
filtered image operator (mask/kernel)
g(x-r) is nonzero

Discrete image processing 2D form


height width
H ( x, y )    I (i, j ) M ( x  i, y  j)
j 1 i 1
Compute the convolution where
there are valid indices in the kernel

Image Processing using Matlab Sumitha Balasuriya 22


Convolution example
Image (I)
0 0 0 0 0 0 0 0 0 0
197 199 195 194 189 190 132 90 112 101 Filter (M) 0 196 196 194 192 170 137 105 97 0
194 194 198 201 189 196 150 85 87 97
0 195 196 194 192 167 133 98 92 0
194 194 198 195 186 191 109 90 90 124
0 194 194 193 189 158 124 92 90 0
197 187 195 198 185 186 115 78 81 96 1/9 1/9 1/9 0 193 193 191 186 154 122 92 89 0
194
194
190
194
198
190
193
190
187
179
177
177
88
93
86
99
94
95
69
100  j 1/9 1/9 1/9 = 0
0
194
194
192
192
189
188
184
182
149
146
121
122
91
93
90
95
0
0
201 194 191 186 186 181 74 110 82 76
196 194 195 191 183 164 77 119 84 88 1/9 1/9 1/9 0 195 193 190 183 147 128 100 106 0
0 194 192 189 181 146 125 100 105 0
192 194 199 192 191 174 89 164 103 129
i 0 0 0 0 0 0 0 0 0 0
201 190 187 189 178 168 90 82 88 84

height width
H ( x, y )    I (i, j ) M ( x  i, y  j)
j 1 i 1

Write your own convolution function


myconv.m to perform a convolution.
It should accept two parameters – the
input matrix (image) and convolution
http://www.s2.chalmers.se/undergraduate/courses0203/ess060/PDFdocuments/ForScreen/Notes/Convolution.pdf
kernel, and output the filtered matrix.

Image Processing using Matlab Sumitha Balasuriya 23


Common convolution kernels
0.11 0.11 0.11 -0.17 -0.67 -0.17 -0.17 -0.67 -0.17 0.01 0.08 0.01
0.11 0.11 0.11 -0.67 3.33 -0.67 -0.67 4.33 -0.67 0.08 0.62 0.08
0.11 0.11 0.11 -0.17 -0.67 -0.17 -0.17 -0.67 -0.17 0.01 0.08 0.01
Arithmetic mean Laplacian (enhance edges) Sharpening filter Gaussian filter
filter (smoothing) >>fspecial('laplacian') >>fspecial('unsharp') (smoothing)
>>fspecial('average') >>fspecial('gaussian')

Investigate the listed kernels in Matlab by


1 0 -1 1 2 1 performing convolutions on the Mandrill and
2 0 -2 0 0 0 Lena images. Study the effects of different
kernel sizes (3x3, 9x9, 25x25) on the output.
1 0 -1 -1 -2 -1
Sobel operators (edge detection in x
and y directions) The median filter is used for noise reduction. It works by
>>fspecial('sobel') replacing a pixel value with the median of its neighbourhood
pixel values (vs the mean filter which uses the mean of the
>>fspecial('sobel')’
neighbourhood pixel values). Apply Matlab’s median filter
function medfilt2 on the Mandrill and Lena images. Remember
to use different filter sizes (3x3, 9x9, 16x16).
Image Processing using Matlab Sumitha Balasuriya 24
Useful functions for convolution

• 2D convolution
>>conv2(double(I(:,:,2)),fspecial('gaussian‘,[kernel_height kernel_width] ,sigma),'valid')

image kernel Border padding options

Perform the convolution of an image using Gaussian


kernels with different sizes and standard deviations
and display the output images.

Image Processing using Matlab Sumitha Balasuriya 25


subplots
• Use subplots to divide a plotting window
into several panes. Cosine Sine
1 1

>> x=0:0.1:10; 0.8 0.8

>> f=figure; 0.6 0.6

>> f1=subplot(1,2,1); 0.4 0.4

>> plot(x,cos(x),'r'); 0.2 0.2

>> grid on; 0 0

-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.

You might also like