Motion Estimation - Learning Reflection

Author: Tony Fu
Date: August 27, 2023
Device: MacBook Pro 16-inch, Late 2021 (M1 Pro)
Code: GitHub
Reference: Chapter 8 Digital Image Processing with C++: Implementing Reference Algorithms with the CImg Library by Tschumperlé, Tilmant, Barra

I also recommend Professor Shree Nayar's lecture series on the on Optical Flow.

1. Horn-Schunck Optical Flow

Problem Formulation

The Horn-Schunck method frames optical flow as an energy minimization problem by defining an energy function that encapsulates two main terms:

  1. Data Term: This measures how well the flow is consistent with the pixel intensities in the given images. It's based on the brightness constancy constraint: , which states that the intensity of a point in an image should remain constant over time.

    The book also shows a variant of the data term (called the "direct method") that minize the squared difference between the shifted image and the original image.

  2. Smoothness Term: To encourage smoothness in the flow field, Horn and Schunck include a regularization term. This term imposes a penalty on abrupt changes in and .

The combined energy function to be minimized is:

Minimization Process

To find the flow fields and that minimize this energy function, Horn and Schunck uses the Euler-Lagrange equations derived from .

  1. Take the first variation of with respect to and and set them to zero.

  2. This results in a set of PDEs that are solved iteratively:

    In the original paper, Horn and Schunck approximated the Laplacians with:

    where and are the averages of and in the 3-neighborhood of the current pixel.

  3. The PDEs are solved iteratively until convergence (in the code, I set the maximum number of iterations to 100). The iterative update equations are:

Example

I use two frames from the following GIF as input to the Horn-Schunck optical flow algorithm:

driveby_gif

The result optical flow is shown below:

horn_schunck

2. Multi-Scale Optical Flow

Leveraging Multi-Scale Approaches for Robust Optical Flow

Optical flow estimation involves capturing pixel-level movement between consecutive images. However, real-world scenarios often include varied and complex motions which may not be accurately captured at just a single scale. This is where our multi-scale approach comes in.

An example implementation of the algorithm scales down the image iteratively by factors of 2, beginning with the coarsest scale and moving towards the finest. At each scale, the Horn-Schunck algorithm is applied to estimate optical flow. The algorithm first captures larger motion patterns at these coarser scales and then refines these estimates as it proceeds to finer scales.

Example

horn_schunck_multiscale

Notice that most arrows, which represent the optical flow, are concentrated on the moving car. However, you might also see that the arrow directions are not entirely accurate.

3. Lucas-Kanade Optical Flow

In the Horn-Schunck method, the energy function is minimized globally over the entire image. This makes the problem severely under-determined, as there are two unknowns and for each pixel. To overcome this limitation, Lucas and Kanade proposed a local method that minimizes the energy function within a local window around each pixel.

Problem Formulation

Before introducing the window , let's first take a closer look at the data term used in the Horn-Schunck method. It is based on the brightness constancy constraint: , which states that the intensity of a point in an image should remain constant over time. Here, we take some steps to derive the optical flow equation. This derivation is also covered by Professor Shree Nayar in his lecture.

  1. First-Order Taylor Series Expansion: We approximate the right-hand side of the equation around :

  2. Combining Equations: Using the initial assumption , we get:

  3. Simplification: We subtract from both sides:

  4. Optical Flow Variables: Finally, by dividing by and introducing and , we arrive at the familiar optical flow equation:

Minimization Process

The brightness constancy assumption, upon which the optical flow equation is based, may not hold true in all real-world scenarios. Therefore, it is unlikely that the equation will be exactly zero. Instead, we minimize the squared error term to find the best possible solution.

As alluded to above, the equation provides only a single constraint for the two unknowns and , making the problem under-determined. Lucas-Kanade overcomes this limitation by applying this constraint within a local window of pixels. We sum up the squared differences in brightness to form the error term , making the problem mathematically tractable by providing enough equations to solve for the unknowns. This also improves the robustness of the optical flow estimate by averaging out inconsistencies and noise in the local region.

We can further expand the equation to:

To minimize , we set its partial derivatives with respect to and to zero:

Rewriting these equations in matrix form, we get:

Where:

To find the velocities and , we typically solve the equation by computing . But wait, is most likely non-square (and therefore non-invertible), so we can't compute its inverse. In such cases, we aim to find that minimizes the residual . This least squares technique is commonly used in optimization problems:

Here, is the transpose of , and becomes a square matrix, making it possible to find an exact solution. The resulting is the least squares solution to the original equation.

Examples

Applying the Lucas-Kanade optical flow algorithm to the same two frames from the previous section, we get the following result:

lucas_kanade

Here is another example:

shuffleboard

And the result:

lucas_kanade

4. Lucas-Kanade Optical Flow with Eigenelement Analysis

Why Eigenvalues?

Recall that in the Harris-Stephen corner detection section, we introduced the concept of a structure tensor, denoted , to capture local image structures. Similarly, in the Lucas-Kanade method, we utilize another structure tensor . This matrix serves to encapsulate the local texture around a pixel.

The eigenvalues of this structure tensor guide us in determining how to process each pixel. The following table outlines the actions based on different conditions:

Condition Action
Both eigenvalues Compute the velocity using .
One eigenvalue Project the estimated velocity onto the direction corresponding to the large eigenvalue.
Both eigenvalues Ignore the pixel, as it is too smooth or noisy to provide reliable motion information.

For a deeper dive into eigenvalue analysis, you can refer to Professor Shree Nayar's lecture.

Examples

To demonstrate the benefits of incorporating eigenvalue analysis, let's apply the Lucas-Kanade optical flow algorithm to the same sets of frames we used in previous sections. Below are the results:

lucas_kanade_eigen_driveby

lucas_kanade_eigen_shuffleboard

I find the output to be cleaner compared to earlier methods.

5. Object Tracking by Cross-Correlation

Algorithm Overview

Object tracking in a sequence of images can be accomplished by first identifying the object in the initial frame and creating a "template" of it. In subsequent frames, we cross-correlate this template with a local neighborhood of pixels to find the location that yields the highest correlation. This newly identified location represents the object's new position. This method is considered a form of sparse motion estimation because it doesn't involve comparing the entire image, but rather a small localized area.

Examples

In the following image, we first identify the object in motion and enclose it within a box to create a template:

cross_correlation_driveby_input

We then apply the cross-correlation algorithm to subsequent frames to track the object:

cross_correlation_driveby_output

Here's another example:

cross_correlation_shuffleboard_input

And the tracking result:

cross_correlation_shuffleboard_output

6. Object Tracking by Phase Correlation

Theoretical Background

Assume that two images differ only by a translation:

This can be viewed as the convolution of the first image with a delta function:

Applying the Fourier transform to both sides gives:

This equation will be crucial for deriving and , the translational offsets between the images. We will later see how to go from and to .

Algorithm Overview

The phase correlation approach is similar to cross-correlation. It involves creating an object template in the initial frame and then locating this object in subsequent frames. Instead of using the cross-correlation function, the method employs frequency domain transformations:

The cross-power spectrum ( is formulated as:

The proof (see the book) show that this eventually simplifies to , which can be inverse Fourier transformed to get . In the code, the real and imaginary parts of the cross-power spectrum are separately handled as follows:

The term is added to avoid division by zero.

Examples

We apply the phase correlation algorithm to the same frame sets used previously. The results demonstrate that it effectively tracks moving objects.

driveby_subplots

However, the method is less reliable when the object lacks a distinct texture. For example, phase correlation fails in the following example:

shuffleboard_subplots

7. Improve Object Tracking with Linear Kalman Filter

Algorithm Overview

The idea of using phase correlation over spatial correlation comes from the fact that phase captures translation information across the entire region of interest. So why not extend this temporally? Instead of relying solely on the current frame to track an object, we could use previous frames to predict the object's location in the current one. This is the underlying concept of the Kalman filter.

The Kalman filter is a recursive algorithm designed to estimate the state of a system using a series of noisy measurements. It's a staple in control systems where the objective is to predict the system's future state based on past measurements. In our case, the system is the moving object, and the measurements are the object's positions in each frame.

Before diving deeper, let's first familiarize ourselves with the notations used in the Kalman filter:

Notations

  • State : Here, and represent the object's coordinates in the -th frame, and and signify its velocities.

  • Estimated Position : This is calculated using phase correlation and represents the object's estimated location in the -th frame.

  • State Transition Model : This matrix describes how the state evolves over time. In our implementation, it's a constant matrix :

  • Observation Model : It translates the state into position. Our observation model is simply the constant matrix , meaning that it simply extracts and from :

  • Model Covariance and Measurement Covariance : These signify the uncertainty in the model and the measurements, respectively.

    The values are set to represent our belief about the model and measurement noise. Essentially, larger values indicate greater uncertainty. They serve to balance the model prediction against new observations.

  • State Covariance : This measures the uncertainty in our state estimate.

  • Kalman Gain : This weighting factor decides how much we should trust the new measurements relative to our model prediction.

Kalman Filter Equations

Prediction

Here we predict the state and its uncertainty.

Update/Correction

In this phase, we update the predicted state based on new measurements.

Notice that the larger the , the smaller the Kalman gain . This means that we trust the model more than the measurements. On the other hand, if the measurement noise is small, the Kalman gain will be large, and we will trust the measurements more than the model.

Examples

When the Kalman filter is combined with phase correlation, tracking may initially seem sluggish but eventually catches up. This adaptation period is common as the Kalman filter attempts to balance noisy measurements from the phase correlation with its own predictions.

kalman_driveby_subplots

As expected, when phase correlation fails in the shuffleboard example, the Kalman filter also fails to track the object.

kalman_shuffleboard_subplots