Skip to content

Commit eb46dc0

Browse files
authored
Add files via upload
1 parent ea2923b commit eb46dc0

13 files changed

+790
-0
lines changed

Diff for: Ada_boost1_mdl2.mat

184 KB
Binary file not shown.

Diff for: Kalman_state_reorder.m

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
2+
function [kalman_data,blob_data,blob_state] = Kalman_state_reorder...
3+
(nw_order,kalman_data,blob_data,blob_state)
4+
%variable initialization
5+
tmp_kalman_data = kalman_data;
6+
tmp_blob_data = blob_data;
7+
max_cnt = length(blob_state.Area_new);
8+
pst_cnt = length(blob_state.Area_pst);
9+
A_new = blob_state.Area_new;
10+
C_new = blob_state.Centroid_new;
11+
BB_new = blob_state.BB_Box_new;
12+
%Rearranging the filter orders and initializing new states
13+
for i = 1:max_cnt
14+
kalman_data(i) = tmp_kalman_data(nw_order(i));
15+
blob_data(i) = tmp_blob_data(nw_order(i));
16+
if(blob_data(i).detect_cnt == 1)%Initializing the states
17+
kalman_data(i).X = blob_state.BB_Box_new(i,:)';
18+
end
19+
end
20+
21+
%Deleting the lost filters
22+
m = 0;
23+
for i = 1:max_cnt
24+
if(blob_data(i).lost_cnt > blob_state.lost_th)%Deleting the states
25+
nw_cnt = length(A_new);
26+
pos = nw_cnt - (max_cnt-i);
27+
if(pos < nw_cnt)
28+
A_new = [A_new(1:pos-1),A_new(pos+1:nw_cnt)];
29+
C_new = [C_new(1:pos-1,:);C_new(pos+1:nw_cnt,:)];
30+
BB_new = [BB_new(1:pos-1,:);BB_new(pos+1:nw_cnt,:)];
31+
else
32+
A_new = A_new(1:pos-1);
33+
C_new = C_new(1:pos-1,:);
34+
BB_new = BB_new(1:pos-1,:);
35+
end
36+
skip=1;
37+
else
38+
m = m+1;
39+
skip = 0;
40+
end
41+
if(~skip)
42+
kalman_data(m) = kalman_data(i);
43+
blob_data(m) = blob_data(i);
44+
end
45+
end
46+
% Resetting the deleted states to zeros
47+
if (m < max_cnt)
48+
resest_cnt = m+1;
49+
for i = resest_cnt:max_cnt
50+
kalman_data(i).X = zeros(4,1);
51+
kalman_data(i).P = eye(4);
52+
blob_data(i).lost_cnt = 0;
53+
blob_data(i).detect_cnt = 0;
54+
end
55+
end
56+
% assigning them back to the state
57+
blob_state.Area_new = A_new;
58+
blob_state.Centroid_new = C_new;
59+
blob_state.BB_Box_new = BB_new;

Diff for: Litereture_review_movingobj.pdf

292 KB
Binary file not shown.

Diff for: Object_Detection.pptx

2.66 MB
Binary file not shown.

Diff for: ada_boost_learning.m

+266
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,266 @@
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

Comments
 (0)