clear
close all
clc
input_dir = 'C:\Users\HP\Desktop\gait\Mobo\moboBgSub\04022\slowWalk\vr17_7';
filenames = dir(fullfile(input_dir, '*.pbm'));
tot_ims1 = numel(filenames);
for n = 1:tot_ims1
filename = fullfile(input_dir, filenames(n).name);
img1_p1 = imread(filename);
img1_p1 = bwareaopen(img1_p1,50);
se = strel('square',5);
s_im= imdilate(img1_p1, se);
s_im= imclose(s_im, se);
[LabeledImage, Num]=bwlabel(s_im);
STATS = regionprops(s_im, 'Area');
[MaxArea,MaxAreaLabel]=max([STATS(:).Area]);
BinaryRegion=LabeledImage==MaxAreaLabel;
labeledImage = bwconncomp(BinaryRegion,8);
measurements = regionprops(labeledImage,'BoundingBox','Centroid');
bb = measurements.BoundingBox;
bco = measurements.Centroid;
AR(n)= bb(4)/bb(3);
end
fgs=5;
subplot( fgs,1, 1)
plot(AR, '-o')
title('aspect ratio')
subplot( fgs,1, 2)
mean_sub= AR- mean(AR);
std_div= mean_sub/std(mean_sub);
smooth_wave= smooth(std_div, 17, 'moving');
plot( smooth_wave )
title('smoothed')
subplot(fgs,1, 3)
autoc= xcorr(smooth_wave);
plot( autoc )
title('autoc')
subplot(fgs,1, 4)
df= diff(autoc);
plot( autoc )
[PKS, LOCS]= findpeaks(df);
hold on
plot(LOCS, PKS, 'ro')
title('deriv')
Distance_Peaks = (max(diff(LOCS))+ mean(diff(LOCS)))/2;
gait_period= round(Distance_Peaks)