1
+ %% training a adaboost classifier
2
+ close all ; clear ; clc ;
3
+ % Reading annotations file
4
+
5
+ fileID = fopen(' MVI_20011.xml' ,' r' );
6
+ A = fscanf(fileID ,' %c ' );
7
+ idcs = strfind(A ,[' num=' ]);
8
+ blue = [255 ,20 ,147 ];
9
+ n_indx = 1 ;
10
+
11
+ % for loop over each video frame
12
+ for l_indx = 1 : length(idcs )
13
+ % searching for annotations in each frame
14
+ if (l_indx < 10 )
15
+ str1 = ' 0000' ;
16
+ elseif (l_indx < 100 )
17
+ str1 = ' 000' ;
18
+ else
19
+ str1 = ' 00' ;
20
+ end
21
+ if (l_indx ~= 664 )
22
+ b_str = A(idcs(l_indx ): idcs(l_indx + 1 ));
23
+ else
24
+ b_str = A(idcs(l_indx ): end );
25
+ end
26
+ idx_targets = strfind(b_str ,' <target id=' );
27
+ num_obj = length(idx_targets );
28
+
29
+ BB_box_num = zeros(num_obj ,4 );
30
+ for box_i = 1 : num_obj
31
+ if (box_i ~= num_obj )
32
+ tmp_str = b_str(idx_targets(box_i ): idx_targets(box_i + 1 ));
33
+ else
34
+ tmp_str = b_str(idx_targets(box_i ): end );
35
+ end
36
+ b_indx1 = strfind(tmp_str ,' <box left="' );
37
+ b_indx2 = strfind(tmp_str ,' top="' );
38
+ b_indx3 = strfind(tmp_str ,' width=' );
39
+ b_indx4 = strfind(tmp_str ,' height=' );
40
+ b_indx5 = strfind(tmp_str ,' <attribute' );
41
+ BB_box_num(box_i ,1 ) = str2num(tmp_str(b_indx1 + length(' <box left="' ): b_indx2 - 3 ));
42
+ BB_box_num(box_i ,2 ) = str2num(tmp_str(b_indx2 + length(' top="' ): b_indx3 - 2 ));
43
+ BB_box_num(box_i ,3 ) = str2num(tmp_str(b_indx3 + length(' width="' ): b_indx4 - 2 ));
44
+ BB_box_num(box_i ,4 ) = str2num(tmp_str(b_indx4 + length(' height="' ): b_indx5 - 18 ));
45
+ end
46
+ % reading image
47
+ img_file_name = [' img' ,str1 ,num2str(l_indx ),' .jpg' ];
48
+ folder = ' MVI_20011' ;
49
+ fullname = fullfile(folder ,img_file_name );
50
+ Currentframe = imread(fullname );
51
+
52
+ for box_i = 1 : 1 : num_obj
53
+ crop_img = imcrop(Currentframe ,BB_box_num(box_i ,: ));
54
+ % imshow(crop_img);
55
+
56
+ cell_num1 = (length(crop_img(: ,1 ,1 )));
57
+ cell_num2 = (length(crop_img(1 ,: ,1 )));
58
+ cell_num1 = floor(cell_num1 / 2 );
59
+ cell_num2 = floor(cell_num2 / 2 );
60
+ tmp = [extractHOGFeatures(crop_img ,' numbins' ,8 ,' cellsize' ,[cell_num1 ,cell_num2 ])];
61
+ HOG_feature(box_i , : ) = tmp ;
62
+ HOG_feature(box_i , : ) = (HOG_feature(box_i , : ) );
63
+ colrhist_feature(box_i ,: ) = [imhist(crop_img(: ,: ,1 ),8 );...
64
+ imhist(crop_img(: ,: ,2 ), 8 );imhist(crop_img(: ,: ,2 ), 8 )]' ;
65
+ hus_feature(box_i ,: ) = hus_invariance(crop_img );
66
+ end
67
+
68
+ % Displaying the output
69
+ % shapeInserter = vision.ShapeInserter('BorderColor','Custom',...
70
+ % 'CustomBorderColor',blue);
71
+ % out1 = step(shapeInserter, Currentframe, int32(BB_box_num));
72
+ % imshow(out1)
73
+ if (l_indx > 1 )
74
+ NUM_OF_PAST_BLOBS = length(past_colrhist_feature(: ,1 )) ;
75
+
76
+ for indx1 = 1 : num_obj
77
+ for indx2 = 1 : NUM_OF_PAST_BLOBS
78
+ sim_values(indx1 ,indx2 ,1 ) = pdist2(past_HOG_features(indx2 ,: ),HOG_feature(indx1 ,: ));
79
+ sim_values(indx1 ,indx2 ,2 ) = pdist2(past_colrhist_feature(indx2 ,: ),colrhist_feature(indx1 ,: ));
80
+ sim_values(indx1 ,indx2 ,3 ) = pdist2(past_hus_feature(indx2 ,: ),hus_feature(indx1 ,: ));
81
+ end
82
+ end
83
+ sim_values(: ,: ,1 ) = (sim_values(: ,: ,1 ))./max(sim_values(: ,: ,1 ));
84
+ sim_values(: ,: ,2 ) = (sim_values(: ,: ,2 ))./max(sim_values(: ,: ,2 ));
85
+ sim_values(: ,: ,3 ) = (sim_values(: ,: ,3 ))./max(sim_values(: ,: ,3 ));
86
+ % sim_values(:,:,4) = zscore(sim_values(:,:,4));
87
+ if (mod(l_indx ,6 ) == 0 )
88
+ X(n_indx ,: ) = sim_values(6 ,6 ,: );
89
+ Y(n_indx ) = 1 ;
90
+ elseif (mod(l_indx ,6 ) == 1 )
91
+ X(n_indx ,: ) = sim_values(3 ,3 ,: );
92
+ Y(n_indx ) = 1 ;
93
+ elseif (mod(l_indx ,6 ) == 2 )
94
+ X(n_indx ,: ) = sim_values(1 ,1 ,: );
95
+ Y(n_indx ) = 1 ;
96
+ elseif (mod(l_indx ,6 ) == 4 )
97
+ X(n_indx ,: ) = sim_values(1 ,6 ,: );
98
+ Y(n_indx ) = 0 ;
99
+ elseif (mod(l_indx ,6 ) == 5 )
100
+ X(n_indx ,: ) = sim_values(3 ,4 ,: );
101
+ Y(n_indx ) = 0 ;
102
+ elseif (mod(l_indx ,6 ) == 6 )
103
+ X(n_indx ,: ) = sim_values(6 ,1 ,: );
104
+ Y(n_indx ) = 0 ;
105
+ end
106
+
107
+ clear past_HOG_features ;
108
+ clear past_hus_feature
109
+ clear past_colrhist_feature ;
110
+ clear sim_values ;
111
+ end
112
+
113
+ clear BB_box_num
114
+
115
+ if (l_indx > 1 )
116
+ past_HOG_features = HOG_feature ;
117
+ past_colrhist_feature = colrhist_feature ;
118
+ past_hus_feature = hus_feature ;
119
+ % past_centroid_feat = centroid_feat;
120
+ clear HOG_feature ;
121
+ clear colrhist_feature ;
122
+ clear hus_feature
123
+
124
+ else
125
+ past_HOG_features = zeros(1 ,32 );
126
+ past_colrhist_feature = zeros(1 ,24 );
127
+ past_centroid_feat = zeros(1 ,2 );
128
+ past_hus_feature = zeros(1 ,8 );
129
+ end
130
+ n_indx = n_indx + 1 ;
131
+ end
132
+
133
+
134
+ %% Training part
135
+ % Mdl_ada = fitcensemble(X,Y,'OptimizeHyperparameters','auto',...
136
+ % 'HyperparameterOptimizationOptions',struct('AcquisitionFunctionName',...
137
+ % 'expected-improvement-plus'));
138
+ Mdl_ada = fitcensemble(X ,Y ,' Method' ,' AdaBoostM1' );
139
+
140
+ %% Testing part
141
+ fileID = fopen(' MVI_20012.xml' ,' r' );
142
+ A = fscanf(fileID ,' %c ' );
143
+ idcs = strfind(A ,[' num=' ]);
144
+ n_indx = 1 ;
145
+ for l_indx = 1 : 500
146
+ % searching for annotations in each frame
147
+ if (l_indx < 10 )
148
+ str1 = ' 0000' ;
149
+ elseif (l_indx < 100 )
150
+ str1 = ' 000' ;
151
+ else
152
+ str1 = ' 00' ;
153
+ end
154
+ if (l_indx ~= length(idcs ))
155
+ b_str = A(idcs(l_indx ): idcs(l_indx + 1 ));
156
+ else
157
+ b_str = A(idcs(l_indx ): end );
158
+ end
159
+ idx_targets = strfind(b_str ,' <target id=' );
160
+ num_obj = length(idx_targets );
161
+
162
+ BB_box_num = zeros(num_obj ,4 );
163
+ for box_i = 1 : num_obj
164
+ if (box_i ~= num_obj )
165
+ tmp_str = b_str(idx_targets(box_i ): idx_targets(box_i + 1 ));
166
+ else
167
+ tmp_str = b_str(idx_targets(box_i ): end );
168
+ end
169
+ b_indx1 = strfind(tmp_str ,' <box left="' );
170
+ b_indx2 = strfind(tmp_str ,' top="' );
171
+ b_indx3 = strfind(tmp_str ,' width=' );
172
+ b_indx4 = strfind(tmp_str ,' height=' );
173
+ b_indx5 = strfind(tmp_str ,' <attribute' );
174
+ BB_box_num(box_i ,1 ) = str2num(tmp_str(b_indx1 + length(' <box left="' ): b_indx2 - 3 ));
175
+ BB_box_num(box_i ,2 ) = str2num(tmp_str(b_indx2 + length(' top="' ): b_indx3 - 2 ));
176
+ BB_box_num(box_i ,3 ) = str2num(tmp_str(b_indx3 + length(' width="' ): b_indx4 - 2 ));
177
+ BB_box_num(box_i ,4 ) = str2num(tmp_str(b_indx4 + length(' height="' ): b_indx5 - 18 ));
178
+ end
179
+ % reading image
180
+ img_file_name = [' img' ,str1 ,num2str(l_indx ),' .jpg' ];
181
+ folder = ' MVI_20012' ;
182
+ fullname = fullfile(folder ,img_file_name );
183
+ Currentframe = imread(fullname );
184
+ for box_i = 1 : 1 : num_obj
185
+ crop_img = imcrop(Currentframe ,BB_box_num(box_i ,: ));
186
+ % imshow(crop_img);
187
+
188
+ cell_num1 = (length(crop_img(: ,1 ,1 )));
189
+ cell_num2 = (length(crop_img(1 ,: ,1 )));
190
+ cell_num1 = floor(cell_num1 / 2 );
191
+ cell_num2 = floor(cell_num2 / 2 );
192
+ tmp = [extractHOGFeatures(crop_img ,' numbins' ,8 ,' cellsize' ,[cell_num1 ,cell_num2 ])];
193
+ HOG_feature(box_i , : ) = tmp ;
194
+ HOG_feature(box_i , : ) = (HOG_feature(box_i , : ) );
195
+ colrhist_feature(box_i ,: ) = [imhist(crop_img(: ,: ,1 ),8 );...
196
+ imhist(crop_img(: ,: ,2 ), 8 );imhist(crop_img(: ,: ,2 ), 8 )]' ;
197
+ colrhist_feature(box_i ,: ) = (colrhist_feature(box_i , : ) );
198
+ hus_feature(box_i ,: ) = hus_invariance(crop_img );
199
+ end
200
+
201
+
202
+ % Displaying the output
203
+ shapeInserter = vision .ShapeInserter(' BorderColor' ,' Custom' ,...
204
+ ' CustomBorderColor' ,blue );
205
+ out1 = step(shapeInserter , Currentframe , int32(BB_box_num ));
206
+
207
+
208
+ if (l_indx > 1 )
209
+ NUM_OF_PAST_BLOBS = length(past_colrhist_feature(: ,1 )) ;
210
+
211
+ for indx1 = 1 : num_obj
212
+ for indx2 = 1 : NUM_OF_PAST_BLOBS
213
+ sim_values(indx1 ,indx2 ,1 ) = pdist2(past_HOG_features(indx2 ,: ),HOG_feature(indx1 ,: ));
214
+ sim_values(indx1 ,indx2 ,2 ) = pdist2(past_colrhist_feature(indx2 ,: ),colrhist_feature(indx1 ,: ));
215
+ sim_values(indx1 ,indx2 ,3 ) = pdist2(past_hus_feature(indx2 ,: ),hus_feature(indx1 ,: ));
216
+ end
217
+ end
218
+ sim_values(: ,: ,1 ) = (sim_values(: ,: ,1 ))./max((sim_values(: ,: ,1 )));
219
+ sim_values(: ,: ,2 ) = (sim_values(: ,: ,2 ))./max((sim_values(: ,: ,2 )));
220
+ sim_values(: ,: ,3 ) = (sim_values(: ,: ,3 ))./max((sim_values(: ,: ,3 )));
221
+
222
+ if (l_indx < 10 )
223
+ X_match(l_indx ,: )= [sim_values(1 ,1 ,: )];
224
+ else
225
+ X_match(l_indx ,: )= [sim_values(2 ,2 ,: )];
226
+ end
227
+ X_nomatch(l_indx ,: )= [sim_values(1 ,end ,: )];
228
+ % for indx = 1:length(sim_values(:,1,1))
229
+ % X_ip = [sim_values(indx,:,1);sim_values(indx,:,2);sim_values(indx,:,3)];
230
+
231
+ % tracks_i = find(labels);
232
+ % new_track = min_indx2(tracks_i);
233
+
234
+
235
+ % end
236
+ % track_i = find(labels);
237
+ clear past_HOG_features ;
238
+ clear past_colrhist_feature ;
239
+ clear sim_values ;
240
+ clear score ;
241
+ clear labels ;
242
+ end
243
+ clear BB_box_num
244
+
245
+ if (l_indx > 1 )
246
+ past_HOG_features = HOG_feature ;
247
+ past_colrhist_feature = colrhist_feature ;
248
+ % past_centroid_feat = centroid_feat;
249
+ clear HOG_feature ;
250
+ clear colrhist_feature ;
251
+ past_hus_feature = hus_feature ;
252
+ else
253
+ past_HOG_features = zeros(1 ,32 );
254
+ past_colrhist_feature = zeros(1 ,24 );
255
+ past_centroid_feat = zeros(1 ,2 );
256
+ past_hus_feature = zeros(1 ,8 );
257
+ end
258
+ n_indx = n_indx + 1 ;
259
+ end
260
+ [labels ,score ] = predict(Mdl_ada ,[X_match ' ,X_nomatch ' ]' );
261
+
262
+ Y = [ones(1 ,500 ),zeros(1 ,500 )];
263
+ % Loss_1 =loss(Mdl_ada,X',Y);
264
+ % Loss_2 =loss(Mdl_ada,X',Y);
265
+ save([' Ada_boost1_mdl2' ],' Mdl_ada' );
266
+ Confusion_matrix = confusionmat(Y ,labels )./500
0 commit comments