% ideas, optical flow script
clear; close; clc
vid=VideoReader('testvideo.mp4');

fps=(vid.NumFrames/vid.Duration);         
fpsOriginal=fps/2;

h = figure;
movegui(h);
hViewPanel = uipanel(h,'Position',[0 0 1 1],'Title','Plot of Optical Flow');
hPlot = axes(hViewPanel);
opticFlow = opticalFlowLK('NoiseThreshold', 1e-4);

CurFrame = 0;
counter = 0;
GetFrame =  1:1000:vid.NumFrames;         % jeder 1000. Frame
while hasFrame(vid)
    CurGrayImage = rgb2gray(readFrame(vid));
    CurFrame = CurFrame+1;
     if ismember(CurFrame, GetFrame)
         counter = counter+1;
         imwrite(CurGrayImage, sprintf('frame%d.jpg', counter));
         
         loopflow = estimateFlow(opticFlow,CurGrayImage);     % optical Flow
         imshow(CurGrayImage)
         hold on
         plot(loopflow,'DecimationFactor',[5 5],'ScaleFactor',2,'Parent',hPlot);
         hold off
     end
end
