-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalciumSignalSegmentation.m
More file actions
230 lines (192 loc) · 6.03 KB
/
Copy pathCalciumSignalSegmentation.m
File metadata and controls
230 lines (192 loc) · 6.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
%Calcium signal analysis
%function [binaryImageLabelledResized, binaryImageLabelled, cellLocations] = CalciumSignalSegmentation()
%% read image
close all; clear all;
%Initialize
Plot = 0;
detector = 'SURF';
shiftCentroid = 1;
distThresh = 10;
cellSize = 50;
con = 4;
%I = double(imread(image));
%ITimeseries = double(imread(imageTimeseries));
%% Extract mean image and time series image
I = double(imread('AT61_A-001.tif'));
imgInfo = imfinfo('AT61_A_T-series-001.tif');
numLayers = length(imgInfo);
for time = 1:numLayers
ITimeseries(:,:,time) = double(imread('AT61_A_T-series-001.tif', time));
end
if Plot == 1
figure, imagesc(I); colormap(gray); title('original image');
axis off;
end
sizeImage = size(I,1);
sizeITs = size(ITimeseries,1);
%% Laplacian of Gaussians convolution
[LoGImage, padSize] = LoGConv(I, Plot); %convolve image with LoG mask
LoGNorm = LoGImage / max((LoGImage(:)));
if strcmp(detector, 'SURF')
%SURF keypoints
SURFPoints = detectSURFFeatures(LoGNorm);
SURFLocations = SURFPoints.Location;
SURFLocations = SURFLocations - padSize(1);
detectorLocations.SURF = SURFLocations;
interestPoints = SURFLocations;
elseif strcmp(detector, 'harris')
%Harris keypoints
harrisPoints = detectHarrisFeatures(LoGNorm);
harrisLocations = harrisPoints.Location;
harrisLocations = harrisLocations - padSize(1);
detectorLocations.harris = harrisLocations;
interestPoints = harrisLocations;
elseif strcmp(detector, 'SIFT')
%SIFT keypoints
[SIFTPoints, SIFTFeatures] = vl_sift(single(LoGNorm));
[~, indexFeatures] = sort(sum(SIFTFeatures,1), 'ascend');
SIFTLocations = SIFTPoints(1:2,indexFeatures(1:100))';
SIFTLocations = SIFTLocations - padSize(1);
detectorLocations.SIFT = SIFTLocations;
interestPoints = SIFTLocations;
end
%check all points are positive
[row,~] = find(interestPoints < 1);
interestPoints(row,:) = [];
[row,~] = find(interestPoints > sizeImage);
interestPoints(row,:) = [];
cellLocations = interestPoints;
%% Plot interest points
if Plot==1
figure; imagesc(I); colormap(gray); hold on;
plot(interestPoints(:,1),interestPoints(:,2),'r+')
title('Cell interest points');
axis off;
end
%% Remove locations with low intensity
%
% meanIntensity = mean(I(:));
% STDIntensity = std(double(I(:)));
%
% for i = 1:length(interestPoints)
% intensities(i) = I(round(interestPoints(i,1)),round(interestPoints(i,2)));
% end
%
% [~,col] = find(intensities < (meanIntensity - STDIntensity));
% interestPoints(col,:) = [];
%
% % Plot interest points
% if Plot==1
% figure; imagesc(I); colormap(gray); hold on;
% plot(interestPoints(:,1),interestPoints(:,2),'b+')
% title('Cell interest points');
% axis off;
% end
%% Shift centroids to local maximum
%shift interest points to their local max
%[cellLocations] = shiftCentroidsToLocalMax(cellLocations, I, shiftCentroid);
%% Discard close boutons
%remove boutons closer than distThresh. Bouton with the lower pixel
%intensity is removed
[cellLocations] = removeAllCloseBoutons(cellLocations, I, distThresh);
% Plot interest points
if Plot==1
figure; imagesc(I); colormap(gray); hold on;
plot(cellLocations(:,1),cellLocations(:,2),'g+')
title('Cell interest points');
axis off;
end
%% Extract bouton patches
%Extract the bouton patches using the bouton locations extracted
for n = 1:length(cellLocations)
x1(n) = round(cellLocations(n,1) - round(cellSize/2));
x2(n) = round(cellLocations(n,1) + round(cellSize/2));
y1(n) = round(cellLocations(n,2) - round(cellSize/2));
y2(n) = round(cellLocations(n,2) + round(cellSize/2));
%Ensure indeces are within the image margins
if x1(n) <= 0
x1(n) = 1;
end
if y1(n) <= 0
y1(n) = 1;
end
if x2(n) > sizeImage
x2(n) = round(sizeImage);
end
if y2(n) > sizeImage
y2(n) = round(sizeImage);
end
end
[cellPatch] = extractBoutonPatch(cellLocations, cellSize, sizeImage, I, 0);
%create a binary image
binaryImage = zeros(sizeImage);
%% Segmentation
for n = 1:length(cellPatch)
patch = cellPatch{n};
maxPixelVal = max(patch(:));
meanPixelVal = mean(patch(:));
stdPixel = std(patch(:));
thresh = meanPixelVal + stdPixel;
BW = patch > thresh;
%remove small regions
BW2 = bwareaopen(BW,100,con);
%dilate
BW2 = bwmorph(BW2,'close',inf);
%remove all but 1 cells
[BWLabelled, num] = bwlabel(BW2, con);
if num > 1
area = [];
tempPatch = zeros([size(patch),num]);
for j = 1:num
tempPatch(:,:,j) = BWLabelled == j;
area(j) = bwarea(tempPatch(:,:,j));
end
[~,index] = max(area);
BW2 = tempPatch(:,:,index);
end
%cc = bwconncomp(BW); %not needed
binaryImage(y1(n):y2(n),x1(n):x2(n)) = BW2;
if Plot == 1
figure, imagesc(patch); colormap(gray);
title('Orignal patch');
movegui('west');
axis off;
figure, imagesc(BW2); colormap(gray);
title('Segmented Image');
movegui('east');
axis off;
pause(1);
end
end
binaryImage = bwmorph(binaryImage,'close',1);
%[binaryImageLabelled, NumCells] = bwlabel(binaryImage, con);
%% Plot
I2 = imresize(I, [sizeITs sizeITs]);
binaryImage2 = imresize(binaryImage, [sizeITs sizeITs]);
[binaryImageLabelledResized, NumCells] = bwlabel(binaryImage2, con);
figure, imagesc(I2); colormap(gray);
title('Orignal image with segmented cells');
axis off;
hold on;
[B,L] = bwboundaries(binaryImage2,'noholes');
%imshow(label2rgb(L, @jet, [.5 .5 .5]))
for k = 1:length(B)
boundary = B{k};
plot(boundary(:,2), boundary(:,1), 'w', 'LineWidth', 2)
end
%% use binary mask to extract time series
figure;
for c = 1:NumCells
mask = binaryImageLabelledResized == c;
%mask3D = repmat(mask,[1,1,numLayers]);
%maskOnly = mask3D .* ITimeseries;
for time = 1:numLayers
temp = ITimeseries(:,:,time);
trace(c,time) = mean(mean(temp(mask)));
end
end
subplot(2,1,1);
plot(1:1:800,trace(1,:));
subplot(2,1,2);
plot(1:1:800,trace(2,:));
%end