Vous êtes sur la page 1sur 8

MatLab for biologists

Lecture 5

Péter Horváth
Light Microscopy Centre ETH Zurich
peter.horvath@lmc.biol.ethz.ch
May 5, 2008

1
1 Reading and writing tables with MatLab
(.xls, .csv, ASCII delimited)
MatLab has functions to read and write matrices form and to different file
types. These are:
• Ms Excel c (xlsread, xlswrite),

• comma separated value (csvread, csvwrite),

• ASCII-delimited files (dlmread, dlmwrite) , the user can define the
delimiter between the elements.
Create a 50 × 50 random matrix in MatLab and compute its determinant Exercise
(det). Save in different formats. Open the file in Excel, transpose the matrix
and save it. Read the file with MatLab and compute its determinant again.
Is it the same?

2 Image processing with MatLab

First we talk about grayscale images. A digital (graysscale) image is an n×m
matrix of numbers, where these numbers represent the light intensities at a
given position of the image. Usually the intensity values are real numbers
in [0, 1] or integer numbers represented with 8, 12, or 16 bits. This means
that 28 = 256, 212 = 4096, or 216 = 65536 different intensity values can be
represented.
Create the following matrix (grayscale image) and display with the imshow Exercise
command!
0 0 0 0 0 0 0 1 1 1 0 0 0 1 1 0;
0 0 0 0 0 1 54 138 137 113 49 2 0 22 43 0;
0 0 0 0 2 97 204 162 122 123 126 63 2 78 139 4;
1 31 19 10 80 166 191 127 139 124 103 97 72 92 42 0;
1 106 111 79 115 116 166 117 129 100 119 110 90 5 0 0;
0 38 45 108 130 172 181 137 148 158 174 138 90 18 0 0;
0 0 5 167 167 186 144 94 135 161 158 128 91 36 0 0;
0 0 52 200 153 152 170 188 173 149 146 133 98 46 0 0;
0 0 51 173 206 215 203 168 145 153 159 152 107 90 27 6;
0 0 66 194 191 195 196 178 148 131 150 120 104 88 58 33;
3 59 88 187 178 191 198 176 146 147 134 100 98 87 53 41;
10 70 93 155 184 215 219 202 166 133 103 97 93 81 53 31;
152 83 84 162 167 180 170 145 109 92 89 92 88 67 42 49;
158 82 90 173 157 94 83 77 72 74 84 86 69 56 49 80;
100 74 102 170 189 154 83 74 74 82 81 69 55 55 56 69;
66 69 112 176 208 193 167 92 74 74 65 56 56 59 65 70

Do you recognize? How would you create a black and white image from
this?

2
2.1 Case study I: cell counting
First we will solve a very important problem of bio-image processing and
learn several image processing functions of MatLab . In figure 1 left, we see
an image containing nuclei.
10 41 75 84
5 48
24 60
38 86
11 28 67 78 91
3 50
7 17 29 62 97
43 66 77
39
1 16 32 90
6 44
56 69
2 9 23 35 52 79 81 93
12 70 83
31
4 8 59 94
19 49
40 89
21 68 82
33 53
61 74 92
42 54 87
18
47
34
25 72 96
55 63
15
27 45 76 98
13 36 58
85
65
20 46 57
30 73 95
14
26 64
51 88
22 37
71 80

Figure 1: Original image and the desired result.

Now we will see how to determine the boundary of the nuclei and count them
(see figure 1 right). The following steps can be applied:

• threshold,

In MatLab we can write a script to do that e.g. :

% Computational steps
nucleiImage = imread(’cells.bmp’); 1
threshImage = nucleiImage > 18; 2
edgeImage = edge(uint8(threshImage), ’sobel’); 3
labeledImage = bwlabel(threshImage); 4

1
Binary Large OBject

3
% Visualization steps
figure(1);
subplot(2, 2, 1); imagesc(nucleiImage); 5
subplot(2, 2, 2); imagesc(threshImage);
subplot(2, 2, 3); imagesc(edgeImage);
subplot(2, 2, 4); imagesc(labeledImage);

The result of the above code can be seen in figure 2.

50 50

100 100

150 150

200 200

250 250

300 300

350 350

400 400

450 450

500 500

50 100 150 200 250 300 350 400 450 500 50 100 150 200 250 300 350 400 450 500

50 50

100 100

150 150

200 200

250 250

300 300

350 350

400 400

450 450

500 500

50 100 150 200 250 300 350 400 450 500 50 100 150 200 250 300 350 400 450 500

1. As a first step, we open the image. The imread function of MatLab

can open many of the known image formats including tiff stacks. In
case of grayscale images the result will be an n × m array of 8 or 16 bit
numbers, while color RGB images are read into a n × m × 3 array. To
save images we can use imwrite.

image.

• Threshold: Classify the image into two or more classes according

to the pixel intensities.
• The result of a logical expression is a logical value (true/fasle),
here we applied it for the whole array, results an array of logical

4
values depending on the pixel intensities are smaller or bigger than
a given number.

3. To extract the edges of a greyscale or binary image MatLab offers a

function called edge.

4. Sometimes we would like to label the connected objects or count them,

for this reason we can use the bwlabel function, which labels each
and every separated objects with different numbers from 1 to n and 0
represents the background.

5. During the visualization using subplot command we can define subre-

gions of the images where we would like to plot our graphs.

Now we will see how to determine the centroid of the segmented cells
and label them. In MatLab the regionprops command computes different
properties of the image regions, such as centroid, area, perimeter, bounding
box, etc. Completing our code with the following, we can print the labels of
the cells into their centroid:

stats = regionprops(labeledImage,’Centroid’, ’Area’);

edges = find(edgeImage ∼= 0);
nucleiiImage(edges) = 255;

figure(2);
imshow(nucleiiImage);

for i =1:length(stats)
text(stats(i).Centroid(1), stats(i).Centroid(2),...
num2str(i), ’Color’,[1, 1, 0], ’FontSize’, 14,...
’HorizontalAlignment’, ’center’);
end;

We used the very important find command of MatLab , this gives back
the list of vector (matrix) indexes where the values satisfy an expression. See

Try to find a method to determine the best threshold value for the given Exercise
problem. Use the hist command.

5
Plot the area of the cells and determine the median value. Exercise

Determine the bounding box of the cells and plot it to the image! Exercise

Merged cells had been detected as one (e.g. 56, 72) because they are too close Exercise
to one another. Try to develop an algorithm to split them. See imerode,
imdilate, strel, watershed.

2.2 Filters
Convolution: The basic idea is that a window of some finite size and shape
is scanned across the image. The output pixel value is the weighted sum
of the input pixels within the window where the weights are the values of
the filter assigned to every pixel of the window itself. The window with its
weights is called the convolution kernel.
One of the most important filters is the Gaussian filter, since in the real
life most of the noise can be successfully modeled as normally distributed
noise. In our first example we open an image, add random Gaussian noise
and try to remove it with a Gaussian filter. The following code presents a
way to do it, in figure 3 we can see the resulting image:

noisyImage = imnoise(originalImage, ’gaussian’, 0, 0.005); 1
filt = fspecial(’gaussian’, [5 5],2); 2
denoisedImage = imfilter(noisyImage,filt); 3

figure(1);
subplot(2, 2, 1); imshow(originalImage);
subplot(2, 2, 2); imshow(noisyImage);
subplot(2, 2, 3); imshow(denoisedImage);

1. The imnoise command adds differently distributed and parameterized

noise into the image, in this case we add normally distributed (Gaus-
sian) noise, with µ = 0 and σ = 0.005 parameters. Other possibilities
are salt and pepper, Poisson, speckle noise. Try what happens if you Exercise
use ’localvar’ type of noise!

2. To design a filter we have two opportunities, we can create our own

filter as a matrix or use special predefined filters with the fspecial

6
Figure 3: The Gaussian filter.

sizes!

Try to visualize the Gaussian kernel we used! Exercise

In many cases we have different kind of noise and Gaussian filters are not
the appropriate solution the remove them. Now we will see an other very
typical type of noise called salt and pepper. To remove this kind of noise we
can use median filtering, see figure 4:

noisyImage = imnoise(originalImage, ’salt & pepper’, 0.3);
medianImage = medfilt2(noisyImage,[4 4]);

Visualize the result, compare with the result of gaussian filtering. Exercise
We can create our own filters and apply them. What happens if we use
the following filter:
−1 0 1
−1 0 1
−1 0 1

7
Figure 4: Salt and pepper noise, filtered with Gaussian and median filters.

Develop a filter, which find out only the diagonal edges; all the edges. Exercise
How to smooth the image?

3 Used material
• An Introduction to MatLab – David F. Griffiths – University of Dundee

• Using MatLab – The MathWorks, Inc.

c ‘primer’ – Ernesto Di Iorio – ETH Zurich
• A MatLab