You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
Image processing<br />
in <strong>Matlab</strong><br />
TBMI02<br />
Medical Image Analysis<br />
Division of Medical Informatics<br />
Department of Biomedical Engineering<br />
Linköping University
Agenda<br />
<strong>Matlab</strong> basics<br />
How are images represented in <strong>Matlab</strong>?<br />
How do we read images into <strong>Matlab</strong>?<br />
How do we write images from <strong>Matlab</strong>?<br />
How do we look at images?<br />
How do we perform operations on images?<br />
How do we perform convolution?<br />
Information, then laboration with exercises
<strong>Matlab</strong> basics<br />
<strong>Matlab</strong> = matrix laboratory<br />
All variables are matrices, a vector is a N x 1<br />
or a 1 x N matrix, a scalar is a 1 x 1 matrix<br />
Interpreting language, no compiler<br />
For-loops are slow<br />
Vectorized code is fast<br />
Type help and the function name to know<br />
how the function works<br />
Code is either a function or a script<br />
Every line that is not terminated with a<br />
semicolon prints the result
<strong>Matlab</strong> basics<br />
Create a matrix<br />
a = ones(10,20), all the values are 1<br />
a = zeros(40,42), all values are 0<br />
a = randn(100,100), random values from a normal<br />
distribution with mean 0 and variance 1<br />
a = rand(100,100), random values from a uniform<br />
distribution on the interval (0,1)<br />
Warning, if you don’t create a matrix before your<br />
operations, your program can be slow due to that <strong>Matlab</strong><br />
has to allocate memory and copy data each time you<br />
change the matrix
<strong>Matlab</strong> basics<br />
my_vector = ones(100,1)<br />
my_vector(10) gives the 10th value<br />
my_image = ones(100,100)<br />
my_image(20,30) gives the pixel value at<br />
x = 30, y = 20<br />
my_volumes = ones(100,100,100,100)<br />
my_volumes(5,2,3,4) gives the voxel value at<br />
x = 2, y = 5, z = 3, t = 4
<strong>Matlab</strong> basics<br />
Indirect indexing<br />
my_image(my_image > 10) = 100<br />
my_image(isnan(my_image)) = 0
<strong>Matlab</strong> basics<br />
Pointwise operations use a dot before the<br />
operator, for example .* ./ , otherwise the<br />
operator is interpreted as a matrix operation<br />
Addition, subtraction<br />
C = A + B, C = A – B<br />
Pointwise multiplication and matrix<br />
multiplication<br />
C = A .* B, C = A * B<br />
Pointwise division and matrix division<br />
C = A ./ B, C = A / B<br />
A = B^2, matrix square, A = B.^2, pointwise<br />
square
<strong>Matlab</strong> basics<br />
Transpose a matrix with ’<br />
Row vector, a = ones(1,10)<br />
Column vector, b = ones(10,1)<br />
Scalar product, c = b’*b<br />
Matrix product, c = b*b’
for-loops<br />
for a = 1:5<br />
a<br />
end<br />
for a = 1:2:100<br />
a<br />
end<br />
for a = 100:-1:1<br />
a<br />
end
if-statements<br />
if (a == b)<br />
c = d;<br />
elseif (a == f)<br />
c = t;<br />
else<br />
c = e;<br />
end
Functions and scripts<br />
A function takes a number of parameters and<br />
returns a number of parameters<br />
Can’t access variables inside the function,<br />
without setting them as out parameters<br />
A script simply runs the code in a m-file<br />
Use scripts for the laborations in this course<br />
Scripts and functions can be launched from<br />
the <strong>Matlab</strong> terminal, i.e. if the filename is<br />
mylab1.m, simply type mylab1 and enter<br />
Scripts can also be launched with the runbutton<br />
in the editor
Image representation<br />
An image in <strong>Matlab</strong> is stored as a matrix<br />
The size of the matrix is height * width<br />
my_image = ones(height,width)<br />
The data is stored as row major, i.e. y first<br />
(column major is normally used in other<br />
languages)<br />
Pixel value at (x,y) = my_image(y,x)<br />
Origo is at the top left corner of the image<br />
y is positive downwards, x is positive to the<br />
right<br />
[height width] = size(my_image)
Casting between types<br />
<strong>Matlab</strong> variables are normally doubles, i.e.<br />
floating point numbers with 64 bit precision<br />
When an image is read into <strong>Matlab</strong>, it is<br />
normally NOT a double, necessary to convert<br />
to a double<br />
my_image = double(my_image);<br />
Other types are single (32 bit float),<br />
uint16 (16 bit unsigned integers) etc<br />
my_image = single(my_image);
Reading and writing images<br />
Read an image into a <strong>Matlab</strong> matrix<br />
my_image =<br />
double(imread(’my_image.jpg’,’jpg’));<br />
Write a matrix to file<br />
imwrite(uint8(image1),’image1.jpg’,’jpg’);<br />
For medical images<br />
dicomread, dicomwrite
Looking at images<br />
imagesc(my_image), autoscaled<br />
(WARNING, autoscaling can fool you)<br />
image(my_image), not autoscaled<br />
colormap gray to look at grayscale images<br />
colorbar to see the scale<br />
gopimage, to look at complex valued images<br />
(in this course only, not <strong>Matlab</strong> standard)<br />
figure, create a new figure<br />
figure(number), create a figure with a number<br />
surf(my_image), simple surface rendering
The colon operator<br />
Colour images have 3 channels<br />
(for example RGB or YCbCr), i.e.<br />
my_image = zeros(sy,sx,3)<br />
To get one channel, use the colon operator<br />
my_red_image = my_image(:,:,1);<br />
To get one vertical line from an image,<br />
my_line = my_image(:,5)<br />
To get one horizontal line from an image,<br />
my_line = my_image(5,:)<br />
Downsample an image a factor 2<br />
my_image = my_image(1:2:end,1:2:end);
The colon operator<br />
Useful for max, min, sum etc<br />
my_image(:) returns a column vector with all<br />
the pixel values<br />
max(my_image) gives the maximum for each<br />
column<br />
max(my_image(:)) gives the maximum value<br />
of the image, equivalent to<br />
max(max(my_image))<br />
The same principle for min, sum
Convolution<br />
conv2 performs 2D convolution<br />
’same’, the filter response has the same size as the<br />
image<br />
filter_response = conv2(my_image,my_filter,’same’)<br />
’valid’, the filter response is smaller than the image<br />
filter_response = conv2(my_image,my_filter,’valid’)<br />
’full’ (default), the filter response is bigger than the<br />
image<br />
filter_response = conv2(my_image,my_filter)<br />
For higher dimensions, convn (slow)
Fast Fourier Transform<br />
MY_IMAGE = fft2(my_image);<br />
my_image = ifft2(MY_IMAGE);<br />
fftshift, ifftshift to move the DC component<br />
Can be used for convolution<br />
For higher dimensions, fftn, ifftn
Interpolation<br />
interp2 can be used for 2D interpolation,<br />
supports nearest, linear, cubic and sinc<br />
interpolation<br />
For higher dimensions, interp3, interpn<br />
(both are slow)
Help functions<br />
clc, removes all text in the terminal<br />
close all, close all the figures<br />
clear all, removes all the variables<br />
save, save <strong>Matlab</strong> data to a *.mat file<br />
load, load data from a *.mat file
Mex-files<br />
To get faster programs (for example forloops),<br />
it is possible to combine C<br />
programming with <strong>Matlab</strong> through mex-files<br />
A mex-file is a C-file with some extra code for<br />
communication with <strong>Matlab</strong><br />
The mex-file is compiled through <strong>Matlab</strong><br />
The compiled mex-file acts as a normal<br />
<strong>Matlab</strong> function<br />
Not used in this course but can be good to<br />
know that it exists
Exercises<br />
Read an image into <strong>Matlab</strong><br />
Look at the image<br />
Create a simple filter, for example with fspecial<br />
(use randn to see fun results)<br />
Look at the filter with surf<br />
Perform 2D convolution with ’same’, ’valid’ and ’full’<br />
and compare the results at the edge<br />
Try different filter sizes<br />
Perform a 2D FFT and look at the logarithm of the<br />
magnitude, with and without fftshift<br />
Compare pointwise multiplication and matrix<br />
multiplication when multiplying two small matrices
Exercises<br />
Loop through all the pixels in an image and<br />
multiply the pixel value by 2 if the pixel value<br />
is bigger than a threshold that you set<br />
Do the same operation as above, but without<br />
for-loops<br />
Take an image and interpolate it to double the<br />
size, using different interpolation techniques,<br />
and compare the results<br />
Add random noise to an image, with different<br />
variance of the noise, look at the results<br />
Flip every second line in an image from left to<br />
right, using fliplr