
if( 1 )

  opticFlow = opticalFlowFarneback;
  opticFlow.FilterSize = 100;
  
    % h = figure;
    % movegui(h);
    % hViewPanel = uipanel(h,'Position',[0 0 1 1],'Title','Plot of Optical Flow Vectors');
    % hPlot = axes(hViewPanel);
  
  for(i=1:length(VBsave))
  
    figure;
    movegui(gcf);
    hViewPanel = uipanel(gcf,'Position',[0 0 1 1],'Title','Plot of Optical Flow Vectors');
    hPlot = axes(hViewPanel);
    flow = estimateFlow(opticFlow,VBsave{i});
    imshow(VBsave{i})
    hold on
    plot(flow,'DecimationFactor',[10 10],'ScaleFactor',2,'Parent',hPlot);
    hold off
    pause(10^-3)
    pause(1)
  end

else
  %img1 = im2double(rgb2gray(imread('onion.png')));
  blocksize = 51;   % must be an odd number
  hbm = vision.BlockMatcher( ...
      'ReferenceFrameSource', 'Input port', 'BlockSize', [blocksize blocksize]);
  hbm.OutputValue = ...
      'Horizontal and vertical components in complex form';
  halphablend = vision.AlphaBlender;
  % Offset the first image by [5 5] pixels to create second image
  %img2 = imtranslate(img1, [5 5]);
  % Compute motion for the two images
  img1 = double(VBsave{1});
  img2 = double(VBsave{2});
  motion = step(hbm, img1, img2);  
  % Blend two images
  img12 = step(halphablend, img2, img1);
  % Use quiver plot to show the direction of motion on the images   
  [X, Y] = meshgrid(1:blocksize:size(img1, 2), 1:blocksize:size(img1, 1));         
  figure;
  imshow(img12); hold on;
  quiver(X(:), Y(:), real(motion(:)), imag(motion(:)), 0); hold off;  
end
