clear;
% ****************** %
% motion detection %
% ****************** %
frame_info_1 = aviinfo('dmo_9.avi');
frame_info = aviinfo('dmo_9_bw');
frame_number = frame_info.NumFrames;
frame_number = frame_number - 1;
for frame_th = 2:frame_number
avi_data = aviread('dmo_9',frame_th);
image_cdata(:,:,frame_th) = avi_data.cdata;
I_frame_class = aviread('dmo_9_bw',frame_th);
I_gray(:,:,frame_th) = I_frame_class.cdata;
I_gray_double(:,:,frame_th) = double(I_gray(:,:,frame_th));
end
%back_frame = 175*ones(288,384); % unusful,just write here
%ave_frame(1:288,1:384) = back_frame; % unusful,just write here
%figure(1);
%imshow(I_gray_double(:,:,2));
%pixval
%break;
%************ INITIANT PARAMEYER ******************
N = 50; % particle number
root_x_init = 87;
root_y_init = 120;
bodylength = 70;
bodywide = 28;
upperleg_top = 10;
upperleg_bottom = 7;
upperleg_length = 40;
theta_upper = atan((upperleg_top - upperleg_bottom)/upperleg_length);
lowerleg_top = 7;
lowerleg_bottom = 6;
lowerleg_length = 39;
theta_lower = atan((lowerleg_top - lowerleg_bottom)/lowerleg_length);
angle_lu_init = -pi/180*24; % left upperleg
angle_ll_init = -pi/180*23; % left lowerleg
angle_ru_init = pi/180*29; % right upperleg
angle_rl_init = pi/180*30; % right lowerleg
frame_begin = 2;
frame_end = 59;
frame_length = frame_end - frame_begin + 1;
% *********** BOYDY TRACKING ****************
fig=figure(1);
aviobj = avifile('bodytracking7.avi')
for frame_th = 1:frame_length
%==================== firs level,body tracking ====================
if frame_th==1
root_x_temp = round(root_x_init + 10*randn(1,N));
root_y_temp = round(root_y_init + 1*randn(1,N));
else
root_x_temp = (round(mean(x_sample) + 5 + 10*randn(size(x_sample))))';
root_y_temp = (round(mean(y_sample) + 0*randn(size(y_sample))))';
end
body_area = zeros(N,1);
for particle_th = 1:N
for m = root_y_temp(1,particle_th) - bodylength : root_y_temp(1,particle_th)
if m<=0
m=1;
elseif m>288
m=288
end
for n = root_x_temp(1,particle_th) -17 : root_x_temp(1,particle_th) - 17+ bodywide
if n<=0
n=1;
elseif n>384
n=384;
end
body_area(particle_th,1) = body_area(particle_th,1) + I_gray_double(m,n,frame_begin+frame_th-1);
end
end
end
body_q = body_area./ sum(body_area(:,1));
%---------------body root resampling -------------------
u = rand(N+1,1);
t = -log(u);
x_temp = root_x_temp';
y_temp = root_y_temp';
x_sample = 160.*ones(size(x_temp));
y_sample = 120.*ones(size(y_temp));
T = cumsum(t);
Q = cumsum(body_q);
i = 1;
j = 1;
while j <= N,
if (Q(j,1)*T(N,1)) > T(i,1)
x_sample(i,1) = x_temp(j,1);
y_sample(i,1) = y_temp(j,1);
i = i+1;
else
j = j+1;
end;
end;
%------------ body root estimating -------------
map_index_body = min(find(body_q == (max(body_q))));
root_x_vec = root_x_temp(map_index_body);
root_y_vec = root_y_temp(map_index_body);
root_x = root_x_vec(1);
root_y = root_y_vec(1);
%================== second level,upper leg tracking =================
if frame_th==1
angle_lu_temp = angle_lu_init + 5*pi/180*randn(1,N);
angle_ru_temp = angle_ru_init + 5*pi/180*randn(1,N);
elseif frame_th==2
%else
angle_lu_temp = (map_angle_lu_sample(frame_th-1) + 5*pi/180*randn(size(angle_lu_sample)))';
angle_ru_temp = (map_angle_ru_sample(frame_th-1) + 5*pi/180*randn(size(angle_ru_sample)))';
else
if abs(map_angle_lu_sample(frame_th - 1))<=5*pi/180 & (map_angle_lu_sample(frame_th - 1)-map_angle_lu_sample(frame_th - 2))>0
angle_lu_temp = (map_angle_lu_sample(frame_th - 1) + 5*pi/180 + 0.5*pi/180*randn(size(angle_lu_sample)))';
else
angle_lu_temp = (2*map_angle_lu_sample(frame_th - 1) - 1*map_angle_lu_sample(frame_th - 2)...
+ 1.1*pi/180*randn(size(angle_lu_sample)))';
end
if abs(map_angle_ru_sample(frame_th - 1))<=5*pi/180 & (map_angle_ru_sample(frame_th - 1)-map_angle_ru_sample(frame_th - 2))>0
angle_ru_temp = (map_angle_ru_sample(frame_th - 1) + 5*pi/180 + 0.5*pi/180*randn(size(angle_lu_sample)))';
else
angle_ru_temp = (2*map_angle_ru_sample(frame_th - 1) - 1*map_angle_ru_sample(frame_th - 2)...
+ 1.1*pi/180*randn(size(angle_ru_sample)))';
end
end
%------------constrains---------------------
%-------------------------------------------
lu_area = zeros(N,1);
ru_area = zeros(N,1);
T_upperleg = [1 0 0;
0 1 0;
root_x-2 root_y-2 1];
for particle_th = 1:N
R_lu = [ cos(angle_lu_temp(1,particle_th)) sin(angle_lu_temp(1,particle_th)) 0;
-sin(angle_lu_temp(1,particle_th)) cos(angle_lu_temp(1,particle_th)) 0;
0 0 1];
R_ru = [ cos(angle_ru_temp(1,particle_th)) sin(angle_ru_temp(1,particle_th)) 0;
-sin(angle_ru_temp(1,particle_th)) cos(angle_ru_temp(1,particle_th)) 0;
0 0 1];
for m = 1:upperleg_length
dot_upperleg = upperleg_top - m*tan(theta_upper);
for n = -round(dot_upperleg):round(dot_upperleg)
lu_area_pixel = round([n m 1]*R_lu*T_upperleg);
ru_area_pixel = round([n m 1]*R_ru*T_upperleg);
if lu_area_pixel(1,1) <= 0
lu_area_pixel(1,1) = 1;
elseif lu_area_pixel(1,1) > 384
lu_area_pixel(1,1) = 384;
end
if lu_area_pixel(1,2) <= 0
lu_area_pixel(1,2) = 1;
elseif lu_area_pixel(1,2) > 288
lu_area_pixel(1,2) = 288;
end
if ru_area_pixel(1,1)<=0
ru_area_pixel(1,1) = 1;
elseif ru_area_pixel(1,1) > 384
ru_area_pixel(1,1) = 384;
end
if ru_area_pixel(1,2)<=0
ru_area_pixel(1,2) = 1;
elseif ru_area_pixel(1,2) > 288
ru_area_pixel(1,2) = 288;
end
lu_area(particle_th,1) = lu_area(particle_th,1) + ...
I_gray_double(lu_area_pixel(1,2),lu_area_pixel(1,1),frame_begin+frame_th-1);
ru_area(particle_th,1) = ru_area(particle_th,1) + ...
I_gray_double(ru_area_pixel(1,2),ru_area_pixel(1,1),frame_begin+frame_th-1);
end
end
end
lu_q = lu_area./ sum(lu_area(:,1));
ru_q = ru_area./ sum(ru_area(:,1));
%---------------upper leg resampling -------------------
u = rand(N+1,1);
t = -log(u);
Alu_temp = angle_lu_temp';
Aru_temp = angle_ru_temp';
angle_lu_sample = pi/180.*ones(size(x_temp));
angle_ru_sample = pi/180.*ones(size(y_temp));
T = cumsum(t);
Q_lu = cumsum(lu_q);
Q_ru = cumsum(ru_q);
i = 1;
j = 1;
while j <= N,
if (Q_lu(j,1)*T(N,1)) > T(i,1)
angle_lu_sample(i,1) = Alu_temp(j,1);
i = i+1;
else
j = j+1;
end;
end;
i = 1;
j = 1;
while j <= N,
if (Q_