简体   繁体   中英

Optical Flow in Matlab

How do you compute and display the optical flow of two images in Matlab? Is their a built in function for this and if so, how do you implement this?

If you are merely interested in how to visualize optical flow as color and/or arrows, here is a simple script to show you how it can be done in Matlab:

% Generate an example flow field
[u,v] = meshgrid(linspace(-1,1)/sqrt(2));
mag = sqrt(u.^2 + v.^2);
an = (atan2(v,u)+pi)/(2*pi);

%display as arrows
subplot(1,2,2);
quiver(u(1:5:end,1:5:end),v(1:5:end,1:5:end));
axis image

%display as color
subplot(1,2,1);
imagesc(hsv2rgb(an,mag,mag));
axis image

My toolbox on optical flow in Matlab (which I am now advertising) has at least 3 different ways of visualizing optical flow with a combination of arrows and color coding. To use it with an avi movie file, do as follows:

in.movieType = 'example.avi'; % assumes a file 'example.avi' in current folder. 
% in.movieType = 'synthetic';  % generate synthetic video. 
% in.movieType = 'camera';     % assumes a camera available in the system. 

in.method =  @Flow1;       %Locally regularized and vectorized method
% in.method =  @FlowLK;      %Lucas and Kanade 
% in.method =  @FlowHS;      %Horn and Schunk, variational (global regularization)

in.bRecordFlow = 1; %record the optical flow, not only display it

% resolution of flow field [Height Width]:
in.flowRes = [24 24];     %flow resolution <= video resolution

% DO THE CALL TO THE TOOLBOX(initiate the session):
pathToSave = vidProcessing(in);

% view interactively the saved session:
FancyFlowPlayer(pathToSave);

If you have access to the Matlab Computer Vision Toolbox, you can use the built in functions of Lucas-Kanade Optical FLow, Horn-Schunck and Farnebäck. They both need a grayscale image sequence. They return a Flow object containing the flow in x,y direction aswell as magnitude and angle. For displaying you can use plot or quiver.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM