Professional Documents
Culture Documents
Matlab Pra Biologos
Matlab Pra Biologos
Lecture 5
Péter Horvá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),
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
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,
% 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);
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
4
values depending on the pixel intensities are smaller or bigger than
a given number.
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:
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
help for more information.
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:
originalImage = imread(’eight.tif’);
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);
6
Figure 3: The Gaussian filter.
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:
originalImage = imread(’eight.tif’);
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