[2D] Optical flow
Assumptions behind the Lucas-Kanade and Horn-Schunck Optical Flow Estimation methods Both the Horn Schunck and Lucas-Kanade method assumes that brightness constancy does not change over time. Also, the Horn Schunck method assumes that the flowfield is globally smooth (or that neighboring velocities are nearly identical). The Lucas-Kanade method assumes that the velocity is locally constant, and neighboring points belong to the same patch that have similar motion.
Differences between Lucas-Kanade and Horn-Schunck methodsThe Lucas-Kanade method is a sparse/local method , while the Horn-Schunck method is a dense/global one. The Lucas-Kanade would obtain less noise compared to the dense method as Horn-Schunck. The Horn-Schunck is a dense one, which means it will have denser flow fields. The pixel displacement of the Lucas-Kanade is constant within a small vicinity, whilst that of Horn- Schunck is smooth over the time domain.
The threshold to constrain Lucas-Kanade Optical Flow estimationThis is done to ensure there is a numerical stability. We obtain a useful measure of reliability of motion for each computation. Fields that have a fairly undefined structure are excluded from potential error because of this (flow while there seems to be no flow).
Tradeoffs associated with a smaller or larger window sizeThe function takes quite a while, mostly due to the fact that I did not put much effort in making it fast and efficient. Because of that, a larger window size makes the computation take even longer because with a larger window size it means it has more values to process. A larger window will also produce a less ‘sharp’ directional image. Because the windows are larger, more ‘windows’ will include the parts that are actually moving. This means that the area with direction will get bigger. On the other hand, a larger window will include the parts that might be missed by smaller windows in terms of brightness constancy. For instance, a small part (for instance the back of the dog) is quite large and black, and for small windows this would not seem to move. Big windows on the other hand will include these potential problem area’s as well, so that these areas are also moving as they are. So there is a tradeoff between local accuracy and robustness when choosing this window size. On the follow page I have shown window sizes of 2,5,10,20,30 to illustrate the differences. Also see that i use the “quiver” function below.
%Tim Zaman, TU Delft ME, 12-12-2010
im1=imread('image1.bmp'); %Read image 1
im1=double(rgb2gray(im1))/255; %Convert to gray & range 0-1
im2=imread('image2.bmp'); %Read image 2
im2=double(rgb2gray(im2))/255; %Convert to gray & range 0-1
Optical flow function Matlab
%initialize for speed
u = zeros(size(x_c));
v = u;
for x = 1:size(x_c,1)-windowSize %fpr all x
for y = 1:size(x_c,2)-windowSize
%Get the windows of the dimensions
win_x=imcrop(x_c,[x y windowSize windowSize]);
win_y=imcrop(y_c,[x y windowSize windowSize]);
win_t=imcrop(t_c,[x y windowSize windowSize]);
%Convert windows to vectors to produce A for solving
A = [win_x(:) win_y(:)];
%Compute threshold t (smallest eigenvalue of A'A)
%Optical flow is only valid in regions with t<0.01 and
%if true, then it should not be computed
if th<0.01 || rank(A'*A)~=2
unk = pinv(A'*A)*A'*win_t(:); %compute unkown or in
%put in u and v
function quiver_uv (u, v)
% Resize u and v so we can actually see something in the quiver plot
scalefactor = 50/size(u,2);
u_ = scalefactor*imresize(u,scalefactor,'bilinear');
v_ = scalefactor*imresize(v,scalefactor,'bilinear');
% Run quiver taking into account matlab coordinate system quirkiness
% and scaling the magnitude of (u,v) by 2 so it is more visible.