Image Processing in Matlab: TBMI02 Medical Image Analysis
Image Processing in Matlab: TBMI02 Medical Image Analysis
Agenda
Matlab basics
How are images represented in Matlab? How do we read images into Matlab? How do we write images from Matlab? How do we look at images? How do we perform operations on images? How do we perform convolution?
Matlab basics
Matlab = matrix laboratory
All variables are matrices, a vector is a N x 1 or a 1 x N matrix, a scalar is a 1 x 1 matrix
Type help and the function name to know how the function works
Code is either a function or a script
Every line that is not terminated with a semicolon prints the result
Matlab basics
Create a matrix
a = ones(10,20), all the values are 1 a = zeros(40,42), all values are 0 a = randn(100,100), random values from a normal distribution with mean 0 and variance 1 a = rand(100,100), random values from a uniform distribution on the interval (0,1)
Warning, if you dont create a matrix before your operations, your program can be slow due to that Matlab has to allocate memory and copy data each time you change the matrix
Matlab basics
my_vector = ones(100,1) my_vector(10) gives the 10th value my_image = ones(100,100)
my_image(20,30) gives the pixel value at x = 30, y = 20 my_volumes = ones(100,100,100,100) my_volumes(5,2,3,4) gives the voxel value at x = 2, y = 5, z = 3, t = 4
Matlab basics
Indirect indexing
my_image(my_image > 10) = 100 my_image(isnan(my_image)) = 0
Matlab basics
Pointwise operations use a dot before the operator, for example .* ./ , otherwise the operator is interpreted as a matrix operation Addition, subtraction
C = A + B, C = A B
Matlab basics
Transpose a matrix with (however, use with caution)
Row vector, a = ones(1,10) Column vector, b = ones(10,1) Scalar product, c = b*b
for-loops
for a = 1:5 a end
for a = 1:2:100 a end for a = 100:-1:1 a end
if-statements
if (a == b) c = d; elseif (a == f) c = t; else c = e; end
Scripts and functions can be launched from the Matlab terminal, i.e. if the filename is mylab1.m, simply type mylab1 and enter
Scripts can also be launched with the run-button in the editor
Image representation
An image in Matlab is stored as a matrix
The size of the matrix is height * width my_image = ones(height,width) The data is stored as row major, i.e. y first (column major is normally used in other languages)
Looking at images
imagesc(my_image), autoscaled (WARNING, autoscaling can fool you)
image(my_image), not autoscaled
Convolution
conv2 performs 2D convolution
same, the filter response has the same size as the image filter_response = conv2(my_image,my_filter,same) valid, the filter response is smaller than the image filter_response = conv2(my_image,my_filter,valid) full (default), the filter response is bigger than the image filter_response = conv2(my_image,my_filter)
Interpolation
interp2 can be used for 2D interpolation, supports nearest, linear, cubic and sinc interpolation For higher dimensions, interp3, interpn (both are slow)
Help functions
clc, removes all text in the terminal
close all, close all the figures clear all, removes all the variables save, save Matlab data to a *.mat file load, load data from a *.mat file
Mex-files
To get faster programs (for example for-loops), it is possible to combine C programming with Matlab through mex-files A mex-file is a C-file with some extra code for communication with Matlab
The mex-file is compiled through Matlab
Exercises
Read an image into Matlab and visualiaze the image
Create a simple filter, for example with fspecial (use randn to see fun results) and visualize the filter with surf
Perform 2D convolution with same, valid and full and compare the results at the edge
Try different filter sizes
Perform a 2D FFT and look at the logarithm of the magnitude, with and without fftshift
Compare pointwise multiplication and matrix multiplication when multiplying two small matrices
Exercises
Loop through all the pixels in an image and multiply the pixel value by 2 if the pixel value is bigger than a threshold that you set Do the same operation as above, but without for-loops
Take an image and interpolate it to double the size, using different interpolation techniques, and compare the results Add random noise to an image, with different variance of the noise, look at the results
Flip every second line in an image from left to right, using fliplr