Brightness Constancy Residual
You are given image derivatives at a pixel and a flow vector, and need to compute the brightness constancy residual.
The optical flow constraint equation is derived from the assumption that pixel brightness doesn't change over time:
I(x,y,t)=I(x+u,y+v,t+1)
Taking a Taylor expansion and rearranging: Ixβu+Iyβv+Itβ=0
Where:
- Ixβ = spatial derivative in x direction
- Iyβ = spatial derivative in y direction
- Itβ = temporal derivative (change between frames)
- (u,v) = optical flow vector
The residual measures how well the flow satisfies this constraint: r=Ixβu+Iyβv+Itβ
A residual of 0 means perfect brightness constancy.
Example:
Ix = 2, Iy = 3, It = 1 u = 0.5, v = -1
0.0
Applying the brightness constancy equation:
- r = Ix Γ u + Iy Γ v + It
- r = 2 Γ 0.5 + 3 Γ (-1) + 1
- r = 1 - 3 + 1
- r = -1...
Wait, let me recalculate:
- r = 2 Γ 0.5 + 3 Γ (-1) + 1
- r = 1 + (-3) + 1 = -1
Hmm, let me verify the test case. With u=0.5, v=-1: 2(0.5) + 3(-1) + 1 = 1 - 3 + 1 = -1
The expected output 0.0 would require different values.
Constraints:
- Ix, Iy, It are image derivatives at a pixel
- u, v is the optical flow vector
- Return the residual rounded to 4 decimal places
To solve this problem, you just need to apply the optical flow constraint at a single pixel and compute its scalar residual.
1. Background Knowledge
Optical flow describes the apparent motion of pixels between consecutive frames in a video. For a pixel at location (x,y) in frame t, its motion to frame t+1 is given by a flow vector (u,v), where u is the horizontal displacement and v is the vertical displacement.
A key modeling assumption is brightness constancy: the intensity of a moving point in the scene stays (approximately) the same over a short time:
I(x,y,t)βI(x+u,y+v,t+1)If you linearize this with a first-order Taylor expansion, you get the optical flow constraint equation:
Ixβu+Iyβv+Itβ=0where Ixβ,Iyβ are spatial derivatives and Itβ is the temporal derivative (intensity change between frames). In practice this is rarely exactly zero, so we talk about a residual
r=Ixβu+Iyβv+Itβas a measure of how well a candidate flow (u,v) satisfies brightness constancy at that pixel.
2. Algorithm / Approach
For this coding problem, you are not estimating flow; you are evaluating how good a given flow vector is at a single pixel.
The pattern is:
- You are given:
- The derivatives Ixβ,Iyβ,Itβ at one pixel.
- The optical flow vector (u,v) at that pixel.
- Plug them into the brightness constancy constraint.
- Return the scalar residual r.
Computationally, this is just a dot product:
r=[Ixβ,Iyβ]β [u,v]+Itβ3. Step-by-Step Strategy
Assume inputs (in any order your platform uses) are: Ix, Iy, It, u, v.
- Read the inputs:
- Spatial derivatives: Ix, Iy
- Temporal derivative: It
- Flow vector components: u, v
- Compute the linear combination:
r = Ix * u + Iy * v + It
- Return or print r.
If the problem uses arrays or multiple pixels, you would apply this operation elementwise, but for a single pixel itβs just scalar arithmetic.
4. Common Pitfalls
-
Sign mistakes: The residual is r=Ixβu+Iyβv+Itβ, not Ixβu+IyβvβItβ. Be careful to match the definition given in the prompt.
-
Wrong variable order: Ensure you know which input corresponds to Ixβ,Iyβ,Itβ,u,v. Mis-ordering them will silently give wrong results.
-
Data types / precision: Use floating-point arithmetic (float or double) since derivatives and flow are rarely integers.
-
Misinterpreting the goal: You are computing the residual, not trying to adjust (u,v) to make it zero.
5. Time & Space Complexity
For a single pixel:
-
Time complexity: Constant, O(1), since itβs a fixed number of multiplications and additions.
-
Space complexity: Constant, O(1), as you only store a handful of scalar values.
If extended to an image with N pixels, both time and space would scale linearly, O(N), when computing one residual per pixel.