How to use Lucas-Kanade and Horn-Schnuck optical flow functions in 2017a

8 views (last 30 days)
I have written simple codes to do these optical flow computations, but I noticed that MATLAB 2017a has these functions built in now, but the MathWorks page gives no examples for their use. Since these methods depend on the comparison of a flow-on image to a flow-off image, would I simply give the functions these images, or do I have to specify block sizes and spacing and implement the functions on a block-by-block basis? Essentially, I'm not sure how to even start with these functions.

Answers (1)

Sebastian Castro
Sebastian Castro on 30 Aug 2017
There actually are examples! Check out the links under "Topics > MATLAB Workflow" at this page:
- Sebastian
  1 Comment
Jonathan Wells
Jonathan Wells on 31 Aug 2017
I saw those, and I've used the help command in MATLAB, and I have a script that returns something now. However, I have a pre-existing Lucas-Kanade code that I know is giving correct results, and the results of the MATLAB functions are pure nonsense.
I'm comparing a flow-on image to a reference flow-off image, so I only have two frames, and this method is supposed to compare an image to the previous image in the video, but the video consists of two images and the method returns two figures. I feel like it should only return one figure, which is a comparison of the 2nd image to the first. I'll attach the function I wrote to try this method. Maybe someone who is more familiar with MATLAB can tell me what I'm doing wrong.
function [LK_video] = OpticFlowLK(video,LK_filename)
% Read the video file
vidReader = VideoReader(video);
% Specify the method of optical flow
Optic_Flow = opticalFlowLK('NoiseThreshold',0.010);
% Create a save file for the video being created
LK_video = VideoWriter(LK_filename,'MPEG-4');
% Open the video object to write to it
open(LK_video);
% Counter for saving figures
ii = 1;
while hasFrame(vidReader)
% Read the image from the video
frameRGB = readFrame(vidReader);
frameGray = rgb2gray(frameRGB);
% Compute the optical flow
flowLK = estimateFlow(Optic_Flow,frameGray);
% Display the video frame with flow vectors
fig = figure;
imshow(frameRGB);
hold on;
plot(flowLK,'DecimationFactor',[5,5],'ScaleFactor',60);
drawnow;
hold off;
% Capture the current frame
frameTotal = getframe(gcf);
% Save the current frame as a figure
saveas(gcf,['LK_Flow',num2str(ii),'.png']);
% Update the counter
ii = ii+1;
% Write the current frame to the video file
writeVideo(LK_video,frameTotal);
end
close(LK_video);
close(fig);
end

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!