-
Notifications
You must be signed in to change notification settings - Fork 0
/
makevideoanalysis.m
136 lines (88 loc) · 3.21 KB
/
makevideoanalysis.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
function [timevid,motionindexsq]=makevideoanalysis(date,filenum,varargin)
pathfolder=sprintf('C:/matlab_scripts/video/data/');
est1=sprintf('.m4v');
est2=sprintf('.mp4');
filename1=sprintf('%s%s_%s%s',pathfolder,date,filenum,est1);
filename2=sprintf('%s%s_%s%s',pathfolder,date,filenum,est2);
if exist(filename1,'file')
est=est1;
elseif exist(filename2,'file')
est=est2;
end
filename=sprintf('%s%s_%s%s',pathfolder,date,filenum,est);
fprintf('Opening video: %s \n\n',filename)
if exist(filename,'file')
xyloObj = VideoReader(filename);
nFrames = xyloObj.NumberOfFrames;
vidHeight = xyloObj.Height;
vidWidth = xyloObj.Width;
dtime=1./xyloObj.FrameRate;
if ~isempty(varargin)
xrange=varargin{1};
yrange=varargin{2};
else
xrange=[1;vidWidth];
yrange=[1;vidHeigth];
end
xrange=xrange(1):xrange(2);
yrange=yrange(1):yrange(2);
% Read one frame at a time.
k=1;
frameA = read(xyloObj, k);
motionindexsq=zeros(3,nFrames);
%nFrames=90;
tic
for k = 2 : nFrames
frameB=read(xyloObj, k);
difference=(frameB(:,:,1)-frameA(:,:,1));
differenceRoi=(frameB(yrange,xrange,1)-frameA(yrange,xrange,1));
motionindexsq(1,k)=sum(sum(difference.^2));
motionindexsq(2,k)=sum(sum(differenceRoi.^2));
clear difference
clear differenceRoi
frameA=frameB;
if motionindexsq(1,k)==0 && k>3
frameA(100,100,:)
beep
end
if rem(k,500)==0
toc
fprintf('Analysing %i / %i frames\n',k,nFrames);
tic
timevid=dtime:dtime:dtime*k;
[timevidreg,motionindexsqreg]=regularizemotion(timevid,motionindexsq);
pathfolder=sprintf('C:/matlab_scripts/video/data/');
est=sprintf('.csv');
filenamesave=sprintf('%s%s_%s_%d%s',pathfolder,date,filenum,k,est);
%fprintf('Saving .csv file: %s \n\n',filenamesave)
A=[timevidreg', motionindexsqreg(1,:)'];
%save(filenamesave,'A','-ascii','-double','-tabs')
end
end
timevid=dtime:dtime:dtime*nFrames;
motionindexsq(1,:)=motionindexsq(1,:)./(vidHeight*vidWidth);
motionindexsq(2,:)=motionindexsq(2,:)./(length(xrange)*length(yrange));
[timevidreg,motionindexsqreg]=regularizemotion(timevid,motionindexsq);
A=[timevidreg', motionindexsqreg(1,:)'];
Aroi=[timevidreg', motionindexsqreg(2,:)'];
pathfolder=sprintf('C:/matlab_scripts/video/data/');
est=sprintf('.csv');
filenamesavesq=sprintf('%s%s_%s%s',pathfolder,date,filenum,est);
fprintf('Saving .csv file: %s %s \n\n',pathfolder,filenamesavesq)
save(filenamesavesq,'A','-ascii','-double','-tabs')
filenamesaveroi=sprintf('%s%s_%s_Roi%s',pathfolder,date,filenum,est);
fprintf('\n\nSaving the .csv file: %s%s \n',pathfolder,filenamesaveroi)
save(filenamesaveroi,'Aroi','-ascii','-double','-tabs')
else
fprintf('Did not found .m4v video: %s %s \n',pathfolder,filename)
end
%cd(currentfolder);
figure
plot(timevidreg,motionindexsqreg(1,:));
hold on
plot(timevidreg,motionindexsqreg(2,:),'r');
xlabel('time (s)')
ylabel('M.I.')
legend('FullFrame','Roi')
clear xyloObj
end