Sie sind auf Seite 1von 24

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?

Information, then laboration with exercises

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

Interpreting language, no compiler


For-loops are slow Vectorized code is fast

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

Pointwise multiplication and matrix multiplication


C = A .* B, C = A * B

Pointwise division and matrix division


C = A ./ B, C = A / B A = B^2, matrix square, A = B.^2, pointwise square

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

Matrix 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

Functions and scripts


A function takes a number of parameters and returns a number of parameters
Cant access variables inside the function, without setting them as out parameters A script simply runs the code in a m-file Use scripts for the laborations in this course

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)

Pixel value at (x,y) = my_image(y,x)


Origo is at the top left corner of the image y is positive downwards, x is positive to the right

[height width] = size(my_image)

Casting between types


Matlab variables are normally doubles, i.e. floating point numbers with 64 bit precision
When an image is read into Matlab, it is normally NOT a double, necessary to convert to a double my_image = double(my_image); Other types are single (32 bit float), uint16 (16 bit unsigned integers) etc my_image = single(my_image);

Reading and writing images


Read an image into a Matlab matrix
my_image = double(imread(my_image.jpg,jpg)); Write a matrix to file imwrite(uint8(image1),image1.jpg,jpg); For medical images dicomread, dicomwrite

Looking at images
imagesc(my_image), autoscaled (WARNING, autoscaling can fool you)
image(my_image), not autoscaled

colormap gray to look at grayscale images


colorbar to see the scale gopimage, to look at complex valued images (in this course only, not Matlab standard) figure, create a new figure figure(number), create a figure with a number

surf(my_image), simple surface rendering

The colon operator


Colour images have 3 channels (for example RGB or YCbCr), i.e. my_image = zeros(sy,sx,3) To get one channel, use the colon operator my_red_image = my_image(:,:,1);
To get one vertical line from an image, my_line = my_image(:,5)

To get one horizontal line from an image, my_line = my_image(5,:)


Downsample an image a factor 2 my_image = my_image(1:2:end,1:2:end);

The colon operator


Useful for max, min, sum etc
my_image(:) returns a column vector with all the pixel values

max(my_image) gives the maximum for each column


max(my_image(:)) gives the maximum value of the image, equivalent to max(max(my_image)) The same principle for min, sum

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)

For higher dimensions, convn (slow)

Fast Fourier Transform


MY_IMAGE = fft2(my_image);
my_image = ifft2(MY_IMAGE); fftshift, ifftshift to move the DC component Can be used for convolution For higher dimensions, fftn, ifftn

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

The compiled mex-file acts as a normal Matlab function


Not used in this course but can be good to know that it exists

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

Das könnte Ihnen auch gefallen