48. Optical Flow Estimation
48.1 Introduction
Now that we have seen how a moving three-dimensional (3D) scene (or camera) produces a two-dimensional (2D) motion field on the image, let’s see how can we measure the resulting 2D motion field using the recorded images by the camera. We want to measure the 2D displacement of every pixel in a sequence.
Unfortunately, we do not have a direct observation of the 2D motion field either, and not all the displacements in image intensities correspond to 3D motion. In some cases, we can have scene motion without producing changes in the image, such as when the camera moves in front of a completely white wall; in other cases, we will see motion in the image even when there is not motion in the scene such as when the illumination source moves.
In the previous chapter we discussed a matching-based algorithm for motion estimation but it is slow and assumes that the motion is only on discrete pixel locations. In this chapter we will discuss gradient-based approaches that allow for estimating continuous displacement values. These methods introduce many of the concepts later used by learning-based approaches that employ deep learning.
48.2 2D Motion Field and Optical Flow
Before we discuss how to estimate motion, let’s introduce a new concept: optical flow.
Optical flow is an approximation to the 2D motion field computed by measuring displacement of image brightness (Figure 48.1). The ideal optical flow is defined as follows: given two images and , the optical flow indicates the relative position of each pixel in and the corresponding pixel in . Note that optical flow will change if we reverse time. This definition assumes that there is a one-to-one mapping between two frames. This will not be true if an object appears in one frame, or when it disappears behind occlusions. The definition also assumes that one motion explains all pixel brightness changes. That assumption can be violated for many reasons, including, for example, if transparent objects move in different directions, or if an illumination source moves.
Figure 48.1 shows two frames and the optical flow between them. This visualization using a color code was introduced in [1]. In this chapter we will use arrows instead as it provides a more direct visualization and it is sufficient for the examples we will work with.
Figure 48.1: Two frames of a sequence, ground-truth optical flow (color coded), and the color code to read the vector at each pixel.
48.2.1 When Optical Flow and Motion Flow Are Not the Same
There are a number of scenarios where motion in the image brightness does not correspond to the motion of the 3D points in the scene. Here there are some examples where it is unclear how motion should be defined:
- Rotating Lambertian sphere with a static illumination will produce no changes in the image. If the sphere is static, and the light source moves, we will see motion in spite of the sphere being static.
- Moving in front of a textureless wall produces no change on the image.
- Waves in water: waves appear to move along the surface but the actual motion of the water is up and down (well, it is even more complicated than that).
- A rotating mirror will produce the appearance of a faster motion. And this will happen in general with any surface that has some specular component.
- A camera moving in front of a specular planar surface will not produce a motion field corresponding to a homography.
Motion estimation should not just measure pixel motion, it should also try to assign to each source of variation in the image the physical cause for that change. The models we will study in this chapter will not attempt to do this.
48.2.2 The Aperture Problem
One classical example of the limitations of motion estimation from images is the aperture problem. The aperture problem happens when observing the motion of a one-dimensional (1D) structure larger than the image frame, as shown in Figure 48.2. If none of the termination points are in view within the observation window (i.e., the aperture), it is impossible to measure the actual image motion. Only the component orthogonal to the moving structure can be measured.
Figure 48.2: Aperture problem when observing the motion of a one-dimensional (1D) structure larger than the image frame. The actual motion of the bar is upward, but the perception, when vision is limited to what is visible within the observation window, appears as if the motion of the bar is in the direction perpendicular to the bar.
The Barber-pole illusion is an illustration of the aperture problem. The barber pole induces the illusion of downward motion while rotating.
48.2.3 Representation of Optical Flow
We can model motion over time as a sequence of dense optical flow images:
An alternative representation is to model motion as a sparse set of moving points (tracks):
Both of those motion representations have limitations, and depending on the applications, one representation may be preferred over the other. Choosing the right representation might be challenging in certain situations. For instance, what representation would be the most appropriate to describe the flow of smoke or water over time? (We do not know the answer to this question, and the answer might depend on what do we want to do with it.)
In the rest of this chapter we will use the dense optical flow representation.
48.3 Model-Based Approaches
Let’s now describe a set of approaches for motion estimation that are not based on learning. Motion estimation equations will be derived from first principles and will rely on some simple assumptions.
In Section 46.3 we discussed matching-based optical flow estimation. We will discuss now gradient-based methods. These approaches rely on the brightness constancy assumption and use the reconstruction error as the loss to optimize.
48.3.1 Brightness Constancy Assumption
The brightness constancy assumption says that as a scene element moves in the world, the brightness captured by the camera from that element does not change over time. Mathematically, this assumption translates into the following relationship, given a sequence :
where and are the element displacement over one unit of time and are also a function of pixel location, and , but we will drop that dependency to simplify the notation. The previous relationship is equivalent to saying that . To make sure that the reader remains onboard without getting confused by the indices and how the translation works, here is a simple toy example of a translation with :
Figure 48.3: Translation to the right of a simple size image.
The constant brightness assumption only approximately holds in reality. For it to be exact, we should have a scene with Lambertian objects illuminated from a light source at infinity, with no occlusions, no shadows, and no interreflections. Few real scenes check any of those boxes. This equation assumes that all the pixels in are visible in , but in reality, some pixels might be occluded in the first frame and new pixels might appear around the image boundaries and behind occlusions.
48.3.2 Gradient-Based Optical Flow Estimation
The most popular version of a gradient-based method for optical flow estimation was introduced by Lucas and Kanade in 1981 [2].
Let’s start describing the method in words, and we will see next how it translates into math. We will start by approximating the change between two consecutive frames by a linear equation using a Taylor approximation. This linear approximation combined with the constant brightness assumption will result in a linear constraint for the optical flow at each pixel. We will then derive a big system of linear equations for the entire image that, when solved, will result in an estimated optical flow for each image pixel. Let’s now see, step by step, how this algorithm works.
If the motion is small in comparison to how fast the image changes spatially, we can use a first-order Taylor expansion of the image around :
Combining equations (Equation 48.1) and (Equation 48.2), and ignoring higher order terms, we arrive at the gradient constraint equation:
The solution that minimizes this loss can be obtained by computing where the derivatives of the loss with respect to and are equal to zero:
We can write the previous two equations in matrix form at each location as:
We will have an equivalent set of equations for each image location. The solution at each location can be computed analytically as where is the matrix of equation (Equation 48.3). The motion at location will only be uniquely defined if we can compute the inverse of . Note that the matrix is a function of the image structure around location . If the rank of the matrix is 1, we can not compute the inverse and the optical flow will be constrained along a 1D line, this is the aperture problem. This will happen if the image structure is 1D inside the region of analysis that will be defined by the size of the Gaussian window .
In order to implement this approach we can make use of convolutions in order to compute all the quantities efficiently in a compact algorithm Algorithm 48.1:
Algorithm 48.1: Gradient-based optical flow estimation using two input frames.
To see how optical flow is computed from gradients, let’s consider a simple sequence with two moving squares as shown in Figure 48.4 where the top square moves with a velocity of pixels/frame and the bottom one moves at pixels/frame (the figure shows frames and to make the motion more visible).
Figure 48.4: Toy sequence with two moving squares. The red arrows indicate the direction of motion of each square.
To apply the gradient-based optical flow algorithm we need to compute the gradients along , , and . In practice, the image derivatives, and , are computed by convolving the image with Gaussian derivatives (see Chapter 18). In the experiments here we first blur the two input frames with a Gaussian of , approximated with a five-tap kernel (i.e., a kernel with size values). For the spatial derivatives we use the kernel for the -derivative and its transposed for the -derivative. The temporal derivative can be computed as the difference of two consecutive blurred frames. Choosing the appropriate filters to compute the derivatives is critical to get correct motion estimates (the three derivatives need to be centered at the same spatial and temporal location in the volume). For the moving squares sequence, the spatial and temporal derivatives are shown in Figure 48.5.
Figure 48.5: Spatial and temporal derivatives for the sequence from Figure 48.4
The next step consists of computing , , , , and and blurring them with the Gaussian kernel, , which will then be used to build the matrix at each pixel. Figure 48.6 shows the results.
Figure 48.6: Computation of all the products between derivatives from Figure 48.5
In order to compute the optical flow, we need to compute the matrix inverse . This matrix depends only on the spatial derivatives and thus is independent of the motion present in the scene. If we compute optical flow at each pixel, the result looks like the image in Figure 48.7.
Figure 48.7: Estimated optical flow for the sequence in Figure 48.5
We can see that the result seems to be wrong for the motion estimated near the center of the side of each square. The inverse can only be computed in image regions with sufficient texture variations (i.e., near the corners). As in the Harris corner detector (see Section 40.3.2.1), the eigenvector with the smallest eigenvalue indicates the direction in which the image has the smallest possible change under translations. Regions with a small minimum eigenvalue are regions with a 1D image structure and will suffer from the aperture problem. To identify regions where the motion will be reliable, we can use the following quantity (proposed by Harris), which relates to the conditioning of the matrix :
Figure 48.8: Estimated optical flow in the regions with (around good features to track [3]
The regions for which will increase by making the apertures larger, which is achieved by using a Gaussian filter, , with larger . However, this will result in smoother estimated flows and the estimated motion will not respect the object boundaries.
One advantage of this approach over the matching-based algorithm is that the estimated flow is not discrete, but it does not work well if the displacement is large, as the Taylor approximation is not valid anymore. One solution to the problem of large displacements is to compute a Gaussian pyramid for the input sequence. The low-resolution scales make the motion smaller and the gradient-based approach will work better.
48.3.3 Iterative Refinement for Optical Flow
The gradient-based approach is efficient but it only provides an approximation to the motion field because it ignores higher order terms in the Taylor expansion in equation (Equation 48.2). A different approach consists of directly minimizing the photometric reconstruction error:
We can now run gradient descent on this loss. Running only one iteration would be similar to the gradient-based optical flow algorithm described in the previous section. However, adding iterations will provide a more accurate estimate of optical flow. At each iteration , the estimated optical flow will be used to compute the warped frame and we will compute an update, and , of the optical flow: and . To improve the results, the optical flow estimation is done on a Gaussian pyramid. First, we run a few iterations on the lowest resolution scale of the pyramid (where the motion will be the smallest). The estimated motion is then upsampled and used as initialization at the next level. We iterate this process until arriving at the highest possible resolution. This process is shown in Figure 48.9.
Figure 48.9: Multiscale iterative refinement for optical flow. Optical flow estimation is done on a Gaussian pyramid. (left) First, we run a few iterations on the lowest resolution scale of the pyramid (where the motion will be the smallest). The estimated motion is then upsampled and used as the initialization at the next level. (right) We iterate this process until arriving at the highest possible resolution.
Figure 48.10 compares the optical flow computed using the gradient-based algorithm (i.e., one iteration) and the multiscale iterative refinement approach. Note how the gradient-based approach underestimates the motion of the left car. The displacement between consecutive frames is close to four pixels and that makes the first-order Taylor approximation very poor. The multiscale method is capable of estimating large displacements.
Figure 48.10: Comparison between the optical flow estimated using the gradient-based algorithm and the multiscale iterative refinement approach.
The photometric reconstruction can incorporate a regularization term penalizing fast variations on the estimated optical flow:
There are several popular regularization terms. One penalizes large velocities (slow prior):
Another penalizes variations on the optical flow (smooth prior):
The photometric loss plays an important role in unsupervised learning-based methods for optical flow estimation as we will discuss later.
48.3.4 Layer-Based Motion Estimation
Until now we have not made use of any of the properties of the motion field derived from the 3D projection of the scene. One way of incorporating some of that knowledge is by making some strong assumptions about the moving scene. If the scene is composed of rigid objects, then we can assume that the motion field within each object will have the form described by equation (Equation 47.8).
In this case, instead of a moving camera we have rigid moving objects (which is equivalent). The 2D motion field can then be represented as a collection of superimposed layers, each layer containing one object and occluding the layers below. Each layer will be described by a different set of motion parameters. The parametric motion field can be incorporated into the gradient-based approach described previously. The motion parameters can then be estimated iteratively using an expectation-maximization (EM) style algorithm. At each step we will have to estimate, for each pixel, which layer it is likely to belong to, and then estimate the motion parameters of each layer. The idea of using layers to represent motion was first introduced by Wang and Adelson in 1994 [5].
48.4 Concluding Remarks
Motion estimation is an important task in image processing and computer vision. It is used in video denoising and compression. In computer vision it is a key attribute to understand scene dynamics and 3D structure. Despite being studied for a long time, accurate optical flow remains challenging, even when using state-of-the-art deep-learning techniques.
The approaches presented here require no training. In the next chapter, we will study several learning-based methods for motion estimation. The approaches presented in this chapter will become useful when exploring unsupervised learning methods.
[1]
S. Baker, S. Roth, D. Scharstein, M.J. Black, J.P. Lewis, R. Szeliski, A database and evaluation methodology for optical flow., in: Proceedings of the IEEE/CVF International Conference on Computer Vision, 2007: pp. 1–8.
[2]
B.D. Lucas, T. Kanade, An iterative image registration technique with an application to stereo vision., in: Proceedings of Imaging Understanding Workshop, 1981: pp. 121–130.
[3]
J. Shi, Tomasi, Good features to track., in: Cvpr, 1994: pp. 593–600.
[4]
B.K.P. Horn, B.G. Schunck, Determining optical flow., Artificial Intelligence 17 (1981) 185–203.
[5]
J.Y.A. Wang, E.H. Adelson, Representing moving images with layers., IEEE Transactions on Image Processing 3 (1994) 625–638.











