Uploaded by Worajedt Sitthidumrong

matlab lect 3

advertisement
MatLab for biologists
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),
• 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
(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
Exercise
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
command!
0
0
0
1
1
0
0
0
0
0
3
10
152
158
100
66
0
0
0
31
106
38
0
0
0
0
59
70
83
82
74
69
0
0
0
19
111
45
5
52
51
66
88
93
84
90
102
112
0
0
0
10
79
108
167
200
173
194
187
155
162
173
170
176
0
0
2
80
115
130
167
153
206
191
178
184
167
157
189
208
0
1
97
166
116
172
186
152
215
195
191
215
180
94
154
193
0
54
204
191
166
181
144
170
203
196
198
219
170
83
83
167
1
138
162
127
117
137
94
188
168
178
176
202
145
77
74
92
1
137
122
139
129
148
135
173
145
148
146
166
109
72
74
74
1
113
123
124
100
158
161
149
153
131
147
133
92
74
82
74
0
49
126
103
119
174
158
146
159
150
134
103
89
84
81
65
0
2
63
97
110
138
128
133
152
120
100
97
92
86
69
56
0
0
2
72
90
90
91
98
107
104
98
93
88
69
55
56
1
22
78
92
5
18
36
46
90
88
87
81
67
56
55
59
1
43
139
42
0
0
0
0
27
58
53
53
42
49
56
65
0;
0;
4;
0;
0;
0;
0;
0;
6;
33;
41;
31;
49;
80;
69;
70
Do you recognize? How would you create a black and white image from
this?
2
Exercise
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.
2
4
24
11
3
1
41
10
5
28
7
17
6
9
39
32
62
66
56
52
35
70
19
40
34
25
15
47
54
20
22
26
37
92
87
96
63
76
58
51
98
85
65
57
46
30
82
72
36
14
89
74
45
27
93
94
61
55
13
79 81
83
68
53
33
42
97
59
49
21
18
91
77
69
31
8
86
78
90
44
23
12
67
43
84
75
60
50
29
16
48
38
73
64
71 80
95
88
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:
• Open the image (download from the course webpage),
• threshold,
• extract the edges,
• count the BLOBs1 and label them.
In MatLab we can write a script to do that e.g. :
% Computational steps
nucleiImage = imread(’cells.bmp’);
threshImage = nucleiImage > 18;
edgeImage = edge(uint8(threshImage), ’sobel’);
labeledImage = bwlabel(threshImage);
1
Binary Large OBject
3
1
2
3
4
% Visualization steps
figure(1);
subplot(2, 2, 1); imagesc(nucleiImage);
subplot(2, 2, 2); imagesc(threshImage);
subplot(2, 2, 3); imagesc(edgeImage);
subplot(2, 2, 4); imagesc(labeledImage);
5
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
50
100
100
150
150
200
200
250
250
300
300
350
350
400
400
450
450
500
50
100
150
200
250
300
350
400
450
500
50
100
150
200
250
300
350
400
450
500
500
50
100
150
200
250
300
350
400
450
500
Figure 2: The result of the first script.
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.
2. To find regions correspond to background and to cells we threshold the
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 subregions 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
help for more information.
Try to find a method to determine the best threshold value for the given
problem. Use the hist command.
5
Exercise
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
to one another. Try to develop an algorithm to split them. See imerode,
imdilate, strel, watershed.
Exercise
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);
filt = fspecial(’gaussian’, [5 5],2);
denoisedImage = imfilter(noisyImage,filt);
1
2
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 (Gaussian) noise, with µ = 0 and σ = 0.005 parameters. Other possibilities
are salt and pepper, Poisson, speckle noise. Try what happens if you
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
Exercise
Figure 3: The Gaussian filter.
command, we create a 5 × 5 gaussian filter. Try with different σ and
sizes!
3. The imfilter command applies a given filter on a given image.
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:
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.
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
Exercise
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.
How to smooth the image?
3
Used material
• An Introduction to MatLab – David F. Griffiths – University of Dundee
• Using MatLab – The MathWorks, Inc.
• A MatLab c ‘primer’ – Ernesto Di Iorio – ETH Zurich
8
Exercise
Download