ref:https://blog.csdn.net/shuoshuge/article/details/86131557
clc;
clear;
%obj = VideoReader('filename.mp4');
obj = VideoReader('filename.avi'); %读取视频
% obj.CurrentTime = 0.5;
% currAxes = axes;
% while hasFrame(obj)
% vidFrame = readFrame(obj);
% image(vidFrame, 'Parent', currAxes);
% currAxes.Visible = 'off';
% pause(1/obj.FrameRate);
% end
%读取第几帧
frame1 = readFrame(obj); %读取第x帧
frame2 = readFrame(obj); %读取下一帧
a=frame2-frame1; %帧差图
a1=rgb2gray(a); %把帧差图变为灰度图像
flow=estimateFlow(opticalFlowHS,a1); %此处使用Horn-Schunck光流法
flow1=estimateFlow(opticalFlowLK,a1); %此处使用LK光流法
%画图。
figure(1)
subplot(1,2,1),imshow(frame1);
subplot(1,2,2),imshow(frame2);
figure(2)
imshow(a);
figure(3)
plot(flow,'DecimationFactor',[5 5],'ScaleFactor',20);view(0,270);
title('HS法 5*5');
figure(4)
plot(flow,'DecimationFactor',[8 8],'ScaleFactor',20);view(0,270);
title('HS法 8*8');
figure(5)
plot(flow1,'DecimationFactor',[5 5],'ScaleFactor',6);view(0,270);
title('LK法 5*5');
figure(6)
plot(flow1,'DecimationFactor',[8 8],'ScaleFactor',6);view(0,270);
title('LK法 8*8');