-
Notifications
You must be signed in to change notification settings - Fork 1
/
read_hdf5.m
264 lines (204 loc) · 8.83 KB
/
read_hdf5.m
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
clear
clc
%%
movies_file = '/Volumes/behavgenom$/GeckoVideo/Invidual_videos/20150512/Capture_Ch1_12052015_194303/';
%kezhi_file = '/Volumes/behavgenom$/GeckoVideo/kezhi_format/20150512/Capture_Ch1_12052015_194303/';
%kezhi_file = '/Users/ajaver/Desktop/Gecko_compressed/20150511/kezhi_format/Capture_Ch1_11052015_195105_kezhi.hdf5';
%kezhi_file = 'N:\GeckoVideo\kezhi_format\20150512\Capture_Ch1_12052015_194303';
%kezhi_file = 'C:\Kezhi\MyCode!!!\Tracking\PF_Video_EN_Worm_Kezhi\PF_Video_EN\Tracking_Hypo_20\Sample_Video\hdf5\worm_1251.hdf5';
kezhi_file = 'C:\Kezhi\MyCode!!!\Tracking\PF_Video_EN_Worm_Kezhi\PF_Video_EN\Tracking_Hypo_20\Sample_Video\hdf5\worm_7818.hdf5';
masks = h5read(kezhi_file, '/masks');
frames = h5read(kezhi_file,'/frames');
CMs = h5read(kezhi_file, '/CMs');
%plot movement of the center of mass
figure
plot(CMs(1,:), CMs(2,:))
%select a frame
frame = 25;
%imshow(masks(:,:,10),[])
%%
% % Create Frenet Frame
% addpath(genpath('C:\Kezhi\MyCode!!!\.'));
% addpath(genpath('C:\Kezhi\Software\SegWorm-master\SegWorm-master\.'));
% please add the folder name here
addpath(genpath('C:\Kezhi\MyCode!!!\Tracking\PF_Video_EN_Worm_Kezhi\PF_Video_EN\Tracking_Hypo_20\.'));
% The sample step of points on skeleton. Change it accordingly in the Main
% function of video processing.
samp_step = 8;
% Read frames from the video
Y_1 = masks(:,:,1);
Y_2 = masks(:,:,2);
Y_3 = masks(:,:,3);
% threshold
thre = 0.9; % 0.96
% Initialize the Frenet data (including points on skeleton, T vector, N vector, etc.)
Frenet_Pt{1}.xy = {};
Frenet_Pt{1}.xy_flp = {};
Frenet_Pt{1}.T = {};
Frenet_Pt{1}.N = {};
I = Y_1;
% I = rgb2gray(Y_1);
%I = rgb;
[mm,nn]= size(I);
%figure, imshow(I), title('Original')
% II = 255 - I;
%
% se = strel('disk', 1);
% II = imclose(II, se);
% %figure
% %imshow(II), title('Opening-closing (Ioc II)')
%
% III = imclose(II, se);
% % figure
% % imshow(III), title('Opening-closing (Ioc III)')
%figure, hist(double(I),256);
level = graythresh(I)*thre; % 0.96
BW = im2bw(I,level);
% figure, imshow(BW)
reBW = BW;
% reBW = -im2bw(I,level)+1;
% figure, imshow(reBW)
log_reBW = logical(reBW);
% fill the holds inside the worm body
BWdfill = imfill(log_reBW, 'holes');
% figure, imshow(BWdfill);
% title('binary image with filled holes');
BW2 = bwareaopen(BWdfill, 50);
% figure, imshow(BW2);
% title('remove small areas');
% draw the contour of the worm
BWoutline = bwperim(BW2);
figure, imshow(BWoutline);
%[NN, curv, Verticles, Lines] = curvature(BWoutline);
[NN, curv, Vertices, Lines, Vertices_old] = curvature_areainput(BW2);
% find the points with maximum curvature
[curv_norm, curv_point1, curv_point2] = Curv_Pt(Vertices, curv);
% draw the magnitudes of curvatures
figure, plot(curv_norm)
% The matrix will be shown
Segout = gray2rgb(I);
Segout(BWoutline) = 1;
% The indexes of head the tail in the matrix shown
V_x=round([Vertices(curv_point1,1),Vertices(curv_point2,1)]);
V_y=mm-round([Vertices(curv_point1,2),Vertices(curv_point2,2)]);
% Draw the area of 5*5 near points of head and tail with green colour
for ii = V_y(1)-2:V_y(1)+2;
for jj = V_x(1)-2:V_x(1)+2;
Segout(ii,jj,:) = [0.2,0.2,1];
%Segout(ii+2,jj+2,:) = [0.2,0.2,1];
end
end
for ii = V_y(2)-2:V_y(2)+2;
for jj = V_x(2)-2:V_x(2)+2;
Segout(ii,jj,:) = [0.2,0.2,1];
%Segout(ii+2,jj-5,:) = [0.2,0.2,1];
end
end
% debug use
%hold on, plot([Verticles(curv_point1,1),Verticles(curv_point2,1)],[mm-Verticles(curv_point1,2),mm-Verticles(curv_point2,2)],'bo','MarkerSize', 10);
% The worm is roughly divided into 24 segments of musculature (i.e., hinges
% that represent degrees of freedom) on each side. Therefore, 48 segments
% around a 2-D contour.
sWormSegs = 24;
cWormSegs = 2 * sWormSegs;
% Clean up the worm's contour.
%contour = cleanWorm(Vertices, size(Vertices, 1) / cWormSegs);
contour = Vertices;
% Compute the contour's local high/low-frequency curvature.
% Note: worm body muscles are arranged and innervated as staggered pairs.
% Therefore, 2 segments have one theoretical degree of freedom (i.e. one
% approximation of a hinge). In the head, muscles are innervated
% individually. Therefore, we sample the worm head's curvature at twice the
% frequency of its body.
% Note 2: we ignore Nyquist sampling theorem (sampling at twice the
% frequency) since the worm's cuticle constrains its mobility and practical
% degrees of freedom.
cCCLengths = circComputeChainCodeLengths(contour);
wormSegLength = (cCCLengths(1) + cCCLengths(end)) / cWormSegs;
hfAngleEdgeLength = wormSegLength;
hfCAngles = circCurvature(contour, hfAngleEdgeLength, cCCLengths);
lfAngleEdgeLength = 2 * hfAngleEdgeLength;
lfCAngles = circCurvature(contour, lfAngleEdgeLength, cCCLengths);
% Compute the contour's local high/low-frequency curvature maxima.
%[mhfCMaxP mhfCMaxI] = maxPeaksCircDist(mhfCAngles, hfAngleEdgeLength, ...
% cCCLengths);
[lfCMaxP lfCMaxI] = maxPeaksCircDist(lfCAngles, lfAngleEdgeLength, ...
cCCLengths);
% Compute the contour's local low-frequency curvature minima.
[lfCMinP lfCMinI] = minPeaksCircDist(lfCAngles, lfAngleEdgeLength, ...
cCCLengths);
headI = curv_point1;
tailI = curv_point2;
% Compute the worm's skeleton.
[skeleton cWidths] = linearSkeleton(headI, tailI, lfCMinP, lfCMinI, ...
lfCMaxP, lfCMaxI, contour, wormSegLength, cCCLengths);
% Measure the skeleton's chain code length.
sCCLengths = computeChainCodeLengths(skeleton);
sLength = sCCLengths(end);
% Measure the skeleton angles (curvature).
lfAngleEdgeLength = sCCLengths(end) * 2 / sWormSegs;
sAngles = curvature(skeleton, lfAngleEdgeLength, sCCLengths);
% reverse skeleton to skeleton_sw to make it good for plot
skeleton_sw = [mm-skeleton(:,2),skeleton(:,1)];
% find the central point of the skeleton
hf_ske_sw = skeleton_sw(round(length(skeleton)/2),:);
% To show the skeleton
Img_ske = zeros(mm,nn);
skeleton_sw_1d = sub2ind(size(Img_ske), skeleton_sw(:,1), skeleton_sw(:,2));
% as the red colour
Img_ske(skeleton_sw_1d) = 1;
Segout(logical(Img_ske)) = 0.8;
% % draw the central point of skeleton as a green point
% Segout(hf_ske_sw(1)-1:hf_ske_sw(1)+1,hf_ske_sw(2)-1:hf_ske_sw(2)+1,2) = 1;
% Segout(hf_ske_sw(1)-1:hf_ske_sw(1)+1,hf_ske_sw(2)-1:hf_ske_sw(2)+1,1) = 0.2;
% Segout(hf_ske_sw(1)-1:hf_ske_sw(1)+1,hf_ske_sw(2)-1:hf_ske_sw(2)+1,3) = 0.2;
k=1;
% Seg skeleton from tail to head
Frenet_Pt{k}.xy = skeleton([end:-samp_step:1,1],:);
% Seg skeleton from head to tail
Frenet_Pt{k}.xy_flp = skeleton([1:samp_step:end,end],:);
%% Frenet Transform
% adjust the axis to make it convenient to plot
Frenet_Pt{k}.xy = [Frenet_Pt{k}.xy(:,1), mm-Frenet_Pt{k}.xy(:,2)];
Frenet_Pt{k}.xy_flp = [Frenet_Pt{k}.xy_flp(:,1), mm-Frenet_Pt{k}.xy_flp(:,2)];
% if the detected skeleton change oritation, change it back
if k>=3
est_tail = 2*Frenet_Pt{k-1}.xy(1,:)-Frenet_Pt{k-2}.xy(1,:);
if abs(abs(Frenet_Pt{k-1}.xy(end,:)-est_tail))<abs(abs(Frenet_Pt{k-1}.xy(1,:)-est_tail))
temp_pt = Frenet_Pt{k}.xy;
Frenet_Pt{k}.xy = Frenet_Pt{k}.xy_flp;
Frenet_Pt{k}.xy_flp = temp_pt;
end
end
% Frenet Transform
[TT,NN,B,k_fre,t_fre,Frenet_Pt{k}.T,Frenet_Pt{k}.N] = frenet(Frenet_Pt{k}.xy(:,1),Frenet_Pt{k}.xy(:,2));
% reverse skeleton to skeleton_sw to make it good for plot
skeleton_subsamp_sw = [Frenet_Pt{k}.xy(:,1),Frenet_Pt{k}.xy(:,2)];
% find the central point of the skeleton
hf_ske_index = round(length(skeleton_subsamp_sw)/2);
hf_ske_subsamp_sw = skeleton_subsamp_sw(hf_ske_index,:);
% draw the central point of skeleton as a green point
hf_ske_sw = [Frenet_Pt{k}.xy(hf_ske_index,2), Frenet_Pt{k}.xy(hf_ske_index,1)];
Segout(hf_ske_sw(1)-1:hf_ske_sw(1)+1,hf_ske_sw(2)-1:hf_ske_sw(2)+1,2) = 1;
Segout(hf_ske_sw(1)-1:hf_ske_sw(1)+1,hf_ske_sw(2)-1:hf_ske_sw(2)+1,1) = 0.2;
Segout(hf_ske_sw(1)-1:hf_ske_sw(1)+1,hf_ske_sw(2)-1:hf_ske_sw(2)+1,3) = 0.2;
%% Show head, tail and the tangent and perpendicular vector at the central worm
fold = 10;
figure;
imshow(Segout);
line(Frenet_Pt{k}.xy(:,1),Frenet_Pt{k}.xy(:,2)), hold on
quiver(Frenet_Pt{k}.xy(hf_ske_index,1),Frenet_Pt{k}.xy(hf_ske_index,2),Frenet_Pt{k}.T(hf_ske_index,1),Frenet_Pt{k}.T(hf_ske_index,2),0.3*fold,'color','y')
quiver(Frenet_Pt{k}.xy(hf_ske_index,1),Frenet_Pt{k}.xy(hf_ske_index,2),Frenet_Pt{k}.N(hf_ske_index,1),Frenet_Pt{k}.N(hf_ske_index,2),20*fold,'color','g')
%% Save the Frenet for future use
Frenet_Pt{2}=Frenet_Pt{1};
% Save data !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
frenet_name = ['.\Data_source\Frenet_',date,'.mat' ];
save frenet_name Frenet_Pt CMs
%save .\Data_source\Frenet_0306.mat Frenet_Pt
%save .\Data_source\Frenet_2304(2).mat Frenet_Pt
%save Frenet_0904.mat Frenet_Pt
%fname = ['test_',date,'.avi' ]; %%%% Please change them accordingly!!!!
% Create AVI !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
fname = ['test_7818.avi'];
workingDir = 'C:\Kezhi\MyCode!!!\Tracking\PF_Video_EN_Worm_Kezhi\PF_Video_EN\Tracking_Hypo_20\Sample_Video\hdf5';
Create_Mov(masks, fname, frame, workingDir);