forked from AugustPa/VRDataAnalysis
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoptic_flow_calculator.m
29 lines (27 loc) · 959 Bytes
/
optic_flow_calculator.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
% A test code to calculate optic flow of video on Matlab
vidReader = VideoReader('Demo_008.mp4');
opticFlow = opticalFlowHS;
h = figure;
movegui(h);
hViewPanel = uipanel(h,'Position',[0 0 1 1],'Title','Plot of Optical Flow Vectors');
hPlot = axes(hViewPanel);
flow_x=nan(vidReader.Width,vidReader.Height,vidReader.NumFrames);
flow_y=nan(vidReader.Width,vidReader.Height,vidReader.NumFrames);
flow_ori=nan(vidReader.Width,vidReader.Height,vidReader.NumFrames);
flow_mag=nan(vidReader.Width,vidReader.Height,vidReader.NumFrames);
k=1;
while hasFrame(vidReader)
frameRGB = readFrame(vidReader);
frameGray = im2gray(frameRGB);
flow = estimateFlow(opticFlow,frameGray);
flow_x(:,:,k)=flow.Vx;
flow_y(:,:,k)=flow.Vy;
flow_ori(:,:,k)=flow.Orientation;
flow_mag(:,:,k)=flow.Magnitude;
imshow(frameRGB)
hold on
plot(flow,'DecimationFactor',[5 5],'ScaleFactor',60,'Parent',hPlot);
hold off
pause(10^-3)
k=k+1;
end