-
Notifications
You must be signed in to change notification settings - Fork 63
/
falldetection.m
185 lines (163 loc) · 6.28 KB
/
falldetection.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
function [] = falldetection(vidname)
% finding path where all files in the program are stored
[path,name,ext] = fileparts(mfilename('fullpath'));
% reading the video
vid = vision.VideoFileReader(sprintf('%s\\videos\\%s',path,vidname));
% initializing foreground and blob detectors
detector = vision.ForegroundDetector(...
'NumTrainingFrames',10,'NumGaussians',5,...
'MinimumBackgroundRatio', 0.7,'InitialVariance',0.05,...
'LearningRate',0.0002);
blob = vision.BlobAnalysis(...
'CentroidOutputPort', true, 'AreaOutputPort', true, ...
'BoundingBoxOutputPort', true, ...
'MinimumBlobAreaSource', 'Property', 'MinimumBlobArea', 500);
%duration of mhi (or) max. value of mhi (or) no of frames taken into acc. for mhi
tmhi = 15;
%strel parameters
strelType = 'square';
strelSize = 5;
%tolerance while find coordinate of y at min. x in counding ellipse
%tolerance for yxmin <= Cy+1 in degrees
noiseYxmin = 3;
%resize Parameter (fraction of input frame)
resizeFactor = 0.25;
% Fall Detection Parameters
%threshold for detecting high motion
thresMotion = 1.8;
%threshold speed for concluding fall
thSpeed = 2;
%threshold orientation change for concluding fall
thOrChg = 15;
%threshold area change for concluding fall
thAreaChg = 15;
%no of frames that form a fall sequence
noFrFallSeq = 5;
%object that contains possible fall sequences
%object contains ->speed , ->noFrames, ->avgOrChg, ->avgAreaChg
%noFrames - no. of frames that have been taken into acc. till now
posFalls = struct([]);
prevOr = [];
frameNo = 0;
while ~isDone(vid)
pause(0.0001);
frame = step(vid);
frameNo = frameNo+1;
%resizing original frame
%frame = imresize(frame,resizeFactor);
%assigning initial value to motion history image
if frameNo == 1
mhimage = zeros(size(frame,1),size(frame,2));
end
%detecting foreground mask
fgMask = step(detector,frame);
%modifying mask to close gaps and fill holes
fgClose = modifyMask(fgMask,strelType,strelSize);
%finding largest blob
[area,centroid,box] = step(blob,fgClose);
pos = find(area==max(area));
box = box(pos,:);
speed = [];orientation = [];area = [];
if ~isempty(box)
%fgBBox - the mask after inside bouding box
%removing cordinates outside bounding box
fgBBox = maskInsideBBox(fgClose,box);
[mhimage,speed] = calcSpeed(mhimage,fgBBox,tmhi);
if speed >= thresMotion
posFalls = initializeFallObj(posFalls,size(posFalls,2)+1);
end
filledCannyMask = logical(edge(fgBBox,'Roberts'));
[xcoord,ycoord] = coordInsideMask(filledCannyMask,box);
ellipse = fitellipse(xcoord,ycoord);
if ~isempty(ellipse)
orientation = calcOrientation(ellipse,noiseYxmin);
area = pi*ellipse(3)*ellipse(4);
%output
subplot(1,4,1);
imshow(frame);
title(sprintf('Original Video\nFrame no - %d',frameNo),'FontSize',20);
subplot(1,4,2);
imshow(fgMask);
title(sprintf('Human detection\nFrame no - %d',frameNo),'FontSize',20);
subplot(1,4,3);
imshow(uint8((mhimage*255)/tmhi));
title(sprintf('Motion History Image\nSpeed - %f',speed),'FontSize',20);
subplot(1,4,4);
imshow(filledCannyMask);
drawEllipse(ellipse(1),ellipse(2),ellipse(3),ellipse(4),ellipse(5));
title(sprintf('Shape of body\nOrientation - %f',orientation),'FontSize',20);
else
%output
subplot(1,4,1);
imshow(frame);
title(sprintf('Original Video\nFrame no - %d',frameNo),'FontSize',20);
subplot(1,4,2);
imshow(fgMask);
title(sprintf('Human detection\nFrame no - %d',frameNo),'FontSize',20);
subplot(1,4,3);
imshow(uint8((mhimage*255)/tmhi));
title(sprintf('Motion History Image\nSpeed - '),'FontSize',20);
subplot(1,4,4);
imshow(filledCannyMask);
title(sprintf('Shape of body\nOrientation - '));
end
else
%output
subplot(1,4,1);
imshow(frame);
title(sprintf('Original Video\nFrame no - %d',frameNo),'FontSize',20);
subplot(1,4,2);
imshow(fgMask);
title(sprintf('Human detection\nFrame no - %d',frameNo),'FontSize',20);
subplot(1,4,3);
imshow(uint8((mhimage*255)/tmhi));
title(sprintf('Motion History Image\nSpeed - '),'FontSize',20);
subplot(1,4,4);
imshow(zeros(size(fgMask)));
title(sprintf('Shape of body\nOrientation - %f',orientation),'FontSize',20);
end
%no possible fall sequences
if isempty(posFalls)
if ~isempty(orientation) && ~isempty(area)
prevOr = orientation;
prevArea = area;
else
%do not increment last changed frame number
end
continue;
end
%no object is found in foreground mask
if isempty(speed) || isempty(orientation) || isempty(area)
% speed,orientation change, area change have to assigned a certain
% value
posFalls = struct([]);
%do not increment last changed frame number
continue;
end
if isempty(prevOr)
orChg = 0;
areaChg = 0;
else
orChg = findOrChg(orientation,prevOr);
areaChg = 20;%have to find it.
end
prevOr = orientation;
prevArea = area;
[fallDetected,posFalls] = updateCheckPosFalls(posFalls,size(posFalls,2),noFrFallSeq,orChg,areaChg,speed,thOrChg,thAreaChg,thSpeed);
if fallDetected == true
subplot(1,4,1);
imshow(frame);
title(sprintf('Original Video\nFrame no - %d',frameNo),'FontSize',20);
subplot(1,4,2);
imshow(fgMask);
title(sprintf('FALL DETECTED\n'),'FontSize',50);
subplot(1,4,3);
imshow(uint8((mhimage*255)/tmhi));
title(sprintf('Motion History Image\nSpeed - %f',speed),'FontSize',20);
subplot(1,4,4);
imshow(filledCannyMask);
title(sprintf('Shape of body\nOrientation - %f',orientation),'FontSize',20);
pause(2);
end
end
% C:\Users\lenovo\Desktop\programs\matlab\FallDetection>"C:\Program Files\MATLAB\R2013a\bin\matlab.exe" -nodisplay -nosplash -nodesktop -r "falldetection video.mp4";