TV-regularized Stereo Disparity in OpenCV


Overview

The TV-regularized stereo disparity implementation in this repo is based on the following paper:

Inputs:

Image Left

Image right

Output results:

Duality gap

Output disparity


Dependencies

Version
cmake 3.22
Python 3.8.10
OpenCV 4.5.1
libtbb-dev 2020.1-2
OpenMP 4.5

How to build and run

$ git clone https://github.com/raymondngiam/tv-regularized-stereo-disparity-in-opencv.git
$ cd tv-regularized-stereo-disparity-in-opencv
$ mkdir build && cd build
$ cmake ..
$ make
$ make install
$ cd ../install/bin/release/
$ ./main

Implementation Details

Let u(x,y)u(x,y) be a vector field representing the disparity between image I1(x,y)I_1(x,y) and I2(x,y)I_2(x,y).

I1(x,y)=I2(x+u(x,y),y)I_1(x,y) = I_2(x+u(x,y),y)

From here onwards, we represent the input space as vector x=[xy]\mathbf{x} = \begin{bmatrix}x \\ y \end{bmatrix}

Let ρ(x,u(x))\rho(\mathbf{x},u(\mathbf{x})) is a function that maps to the error function:

ρ(x,u(x))=∣I1([xy])−I2([x+u(x)y])∣\rho(\mathbf{x},u(\mathbf{x})) = |I_1(\begin{bmatrix}x \\ y \end{bmatrix}) - I_2(\begin{bmatrix}x + u(\mathbf{x}) \\ y \end{bmatrix})|

The variational stereo disparity problem with total variation regularization is thus given by

min⁡u{∫Ωρ(x,u(x))dx+λ∫Ω∣∇u(x)∣dx}  (Eq. 1)\min_u \biggl\{ \int_{\Omega} \rho(\mathbf{x},u(\mathbf{x})) d\mathbf{x} + \lambda \int_{\Omega} |\nabla u(\mathbf{x})|d\mathbf{x}\biggr\}\space \space \text{(Eq. 1)}

where λ\lambda is the smoothness regularization parameter.

Functional lifting

The key idea is to lift the original problem formulation to a higher dimensional space by representing uu in terms of its level sets.

Let the characteristic function 1u>γ(x):Ω→{0,1}\mathbf{1}_{u>\gamma} (\mathbf{x}): \Omega \rightarrow \{0,1\} be the indicator for the γ\gamma - super-levels of uu:

1u>γ(x)={1,if u(x)>γ0otherwise.\mathbf{1}_{u>\gamma} (\mathbf{x}) = \Bigl\{ \begin{matrix} 1 & \text{,if }u(\mathbf{x})>\gamma\\ 0 & \text{otherwise.} \end{matrix}

Next, we make use of the above defined characteristic functions to construct a binary function ϕ\phi which resembles the graph of uu.

Let ϕ:[Ω×Γ]→{0,1}\phi : [\Omega \times \Gamma] \rightarrow \{0,1\} be a binary function defined as

ϕ(x,γ)=1u>γ(x)\phi(\mathbf{x},\gamma) = \mathbf{1}_{u>\gamma} (\mathbf{x})

We can see that ϕ(x,γmin)=1\phi(x, \gamma_{min}) = 1 and ϕ(x,γmax)=0\phi(x, \gamma_{max}) = 0.
Hence, the feasible set of functions ϕ\phi is given by,

D′={ϕ:[Ω×Γ]→{0,1} ∣ ϕ(x,γmin)=1,ϕ(x,γmax)=0}D' = \{ \phi : [\Omega \times \Gamma] \rightarrow \{0,1\} \space | \space \phi(x, \gamma_{min}) = 1, \phi(x, \gamma_{max}) = 0 \}

Note that the function uu can be recovered from ϕ\phi using the following layer cake formula,

u(x)=γmin+∫Γϕ(x,γ)dγu(\mathbf{x}) = \gamma_{min} + \int_{\Gamma} \phi(\mathbf{x},\gamma) d\gamma

The previous variational stereo disparity problem in Eq.1 can be rewritten as,

min⁡ϕ∈D′{∫Ω∫Γρ(x,u(x))∣∂γϕ(x,γ)∣ dγdx+λ∫Γ∫Ω∣∇xϕ(x,γ)∣ dxdγ}\min_{\phi \in D'} \biggl\{ \int_{\Omega}\int_{\Gamma} \rho(\mathbf{x},u(\mathbf{x})) |\partial_\gamma \phi(\mathbf{x},\gamma)| \space d\gamma d\mathbf{x} + \lambda \int_{\Gamma}\int_{\Omega} |\nabla_{\mathbf{x}} \phi(\mathbf{x},\gamma)|\space d\mathbf{x} d\gamma \biggr\}

min⁡ϕ∈D′{∫Ω∫Γ{ρ(x,u(x))∣∂γϕ(x,γ)∣ +λ∣∇xϕ(x,γ)∣} dxdγ}  (Eq. 2)\min_{\phi \in D'} \biggl\{ \int_{\Omega}\int_{\Gamma} \biggl\{\rho(\mathbf{x},u(\mathbf{x})) |\partial_\gamma \phi(\mathbf{x},\gamma)| \space + \lambda |\nabla_{\mathbf{x}} \phi(\mathbf{x},\gamma)| \biggr\}\space d\mathbf{x} d\gamma \biggr\} \space \space \text{(Eq. 2)}

The second part (or smoothness term) of the equation above is based on the co-area formula, which essentially states that the TV norm can be decomposed into a sum of the length of the level sets of uu.

Convex relaxation

Although the current formulation is convex in ϕ\phi, the variational problem is still non-convex since the minimization is carried out over D′D' which is a non-convex set.

The idea is now to relax the variational problem by allowing ϕ\phi to vary smoothly in the interval [0,1][0, 1]. This leads to the following convex set of feasible solutions of ϕ\phi.

D={ϕ:[Ω×Γ]→[0,1] ∣ ϕ(x,γmin)=1,ϕ(x,γmax)=0}D = \{\phi : [\Omega \times \Gamma] \rightarrow [0,1] \space | \space \phi(\mathbf{x}, \gamma_{min}) = 1, \phi(\mathbf{x}, \gamma_{max}) = 0 \}

Hence the associated variational problem is now,

min⁡ϕ∈D{∫Ω∫Γ{ρ(x,u(x))∣∂γϕ(x,γ)∣ +λ∣∇xϕ(x,γ)∣} dxdγ}  (Eq. 3)\min_{\phi \in D} \biggl\{ \int_{\Omega}\int_{\Gamma} \biggl\{\rho(\mathbf{x},u(\mathbf{x})) |\partial_\gamma \phi(\mathbf{x},\gamma)| \space + \lambda |\nabla_{\mathbf{x}} \phi(\mathbf{x},\gamma)| \biggr\}\space d\mathbf{x} d\gamma \biggr\} \space \space \text{(Eq. 3)}

Discontinuity of non-smooth functions

The fundamental approach to minimize Eq. 3 is to solve its associated Euler-Lagrange differential equation.

E(ϕ,ϕ′)=∫L(ϕ,ϕ′)dxE(\phi,\phi')=\int \mathcal{L}(\phi,\phi')dx

L(ϕ,ϕ′)=ρ(x,u(x))∣∂γϕ(x,γ)∣ +λ∣∇xϕ(x,γ)∣\mathcal{L}(\phi,\phi') = \rho(\mathbf{x},u(\mathbf{x})) |\partial_\gamma \phi(\mathbf{x},\gamma)| \space + \lambda |\nabla_{\mathbf{x}} \phi(\mathbf{x},\gamma)|

dEdϕ=∂L∂ϕ−div∂L∂ϕ′\frac{dE}{d\phi} = \frac{\partial \mathcal{L}}{\partial \phi} - \text{div}\frac{\partial \mathcal{L}}{\partial \phi'}

dEdϕ=−∂γ(ρ(x,u(x))∂γϕ(x,γ)∣∂γϕ(x,γ)∣)−λ div∇xϕ(x,γ)∣∇xϕ(x,γ)∣\frac{dE}{d\phi} = -\partial_\gamma (\rho(\mathbf{x},u(\mathbf{x}))\frac{\partial_\gamma \phi(\mathbf{x},\gamma)}{|\partial_\gamma \phi(\mathbf{x},\gamma)|}) - \lambda \space \text{div}\frac{\nabla_{\mathbf{x}} \phi(\mathbf{x},\gamma)}{|\nabla_{\mathbf{x}} \phi(\mathbf{x},\gamma)|}

It is easy to see that these equations are not defined either as ∣∇xϕ(x,γ)∣→0|\nabla_{\mathbf{x}} \phi(\mathbf{x},\gamma)| \rightarrow 0 or ∣∂γϕ(x,γ)∣→0|\partial_\gamma \phi(\mathbf{x},\gamma)| \rightarrow 0.

In order to resolve these discontinuities, one could use regularized variants of these terms, e.g. ∣∇xϕ(x,γ)∣ϵ=∣∇xϕ(x,γ)∣2+ϵ2|\nabla_{\mathbf{x}} \phi(\mathbf{x},\gamma)|_\epsilon = \sqrt{|\nabla_{\mathbf{x}} \phi(\mathbf{x},\gamma)|^2 + \epsilon^2} and ∣∂γϕ(x,γ)∣ϵ=∣∂γϕ(x,γ)∣2+ϵ2|\partial_\gamma \phi(\mathbf{x},\gamma)|_\epsilon = \sqrt{|\partial_\gamma \phi(\mathbf{x},\gamma)|^2 + \epsilon^2}, for some small constant ϵ\epsilon. However, for small values of ϵ\epsilon the equations are still nearly degenerate and for larger values the properties of the model get lost.

To overcome the non-differentiability of the term ρ(x,u(x))∣∂γϕ(x,γ)∣+∣∇xϕ(x,γ)∣\rho(\mathbf{x},u(\mathbf{x}))|\partial_\gamma \phi(\mathbf{x},\gamma)|+|\nabla_{\mathbf{x}} \phi(\mathbf{x},\gamma)| we employ its dual formulation

ρ(x,u(x))∣∂γϕ(x,γ)∣+λ∣∇xϕ(x,γ)∣≡max⁡p{p⋅∇3ϕ(x,γ)} s.t. p12+p22≤λ, ∣p3∣≤ ρ(x,γ)\rho(\mathbf{x},u(\mathbf{x}))|\partial_\gamma \phi(\mathbf{x},\gamma)|+ \lambda |\nabla_{\mathbf{x}} \phi(\mathbf{x},\gamma)| \equiv \max_{\mathbf{p}} \bigg\{\mathbf{p} \cdot \nabla_3 \phi(\mathbf{x},\gamma)\bigg\} \space \text{s.t.} \space \sqrt{p_1^2 + p_2^2} \leq \lambda , \space |p_3| \leq \ \rho(\mathbf{x},\gamma)

where p=(p1,p2,p3)T\mathbf{p} = (p_1, p_2, p_3)^T is the dual variable and ∇3\nabla_3 is the full (three dimensional) gradient operator.

This, in turn, leads us to the following primal-dual formulation of the functional:

min⁡ϕ∈D{max⁡p∈C{∫Ω∫Γp⋅∇3ϕ(x,γ) dγdx}}\min_{\mathbf{\phi}\in D} \bigg\{\max_{\mathbf{p}\in C} \bigg\{\int_{\Omega}\int_{\Gamma} \mathbf{p}\cdot \nabla_3 \phi(\mathbf{x},\gamma)\space d\gamma d\mathbf{x}\bigg\}\bigg\}

where

{C:[Ω×Γ]→R3 ∣ p1(x,γ)2+p2(x,γ)2≤λ, ∣p3(x,γ)∣≤ρ(x,γ)}\{C : [\Omega \times \Gamma] \rightarrow \mathbb{R}^3 \space | \space \sqrt{p_1(\mathbf{x},\gamma)^2 + p_2(\mathbf{x},\gamma)^2} \leq \lambda, \space |p_3(\mathbf{x},\gamma)| \leq \rho(\mathbf{x},\gamma)\}

Primal-dual proximal steps

E(ϕ,ϕ′,p,p′)=∫L(ϕ,ϕ′,p,p′)dxE(\phi,\phi',\mathbf{p},\mathbf{p}')=\int \mathcal{L}(\phi,\phi',\mathbf{p},\mathbf{p}')dx

L(ϕ,ϕ′p,p′)=p(x,γ)⋅∇3ϕ(x,γ)\mathcal{L}(\phi,\phi'\mathbf{p},\mathbf{p}') = \mathbf{p}(\mathbf{x},\gamma)\cdot\nabla_3 \phi(\mathbf{x},\gamma)

dEdϕ=∂L∂ϕ−div∂L∂ϕ′\frac{dE}{d\phi} = \frac{\partial \mathcal{L}}{\partial \phi} - \text{div}\frac{\partial \mathcal{L}}{\partial \phi'}

dEdϕ=−div3p(x,γ)\frac{dE}{d\phi} = -\text{div}_3 \mathbf{p}(\mathbf{x},\gamma)

dEdp=∂L∂p−div∂L∂p′\frac{dE}{d\mathbf{p}} = \frac{\partial \mathcal{L}}{\partial \mathbf{p}} - \text{div}\frac{\partial \mathcal{L}}{\partial \mathbf{p}'}

dEdp=∇3ϕ(x,γ)\frac{dE}{d\mathbf{p}} = \nabla_3 \phi(\mathbf{x},\gamma)

  1. Primal step: For fixed p\mathbf{p}, compute a proximal primal step for ϕ\phi.
    ϕk+1=ϕk−(−τpdEdϕ)\phi^{k+1} = \phi^k - (-\tau_p \frac{dE}{d\phi})

    ϕk+1(x,γ)=ϕk(x,γ)+τpdiv3p(x,γ)\phi^{k+1}(\mathbf{x},\gamma) = \phi^k(\mathbf{x},\gamma) + \tau_p \text{div}_3 \mathbf{p}(\mathbf{x},\gamma)

    Note, that the scheme does not ensure that ϕk+1∈D\phi^{k+1} \in D. Therefore we have to reproject ϕk+1\phi^{k+1} onto DD using the projection Pϕ(ϕk+1)\mathcal{P}_\phi(\phi^{k+1}):

    P(ϕk+1)={0, ϕk+1(x,γ)<01, ϕk+1(x,γ)>1ϕk+1(x,γ), otherwise\mathcal{P}(\phi^{k+1}) = \Bigg\{ \begin{matrix} 0 & \text{, }\phi^{k+1}(\mathbf{x},\gamma)< 0\\ 1 & \text{, }\phi^{k+1}(\mathbf{x},\gamma)> 1\\ \phi^{k+1}(\mathbf{x},\gamma) & \text{, otherwise}\end{matrix}

  2. Dual step: For fixed ϕ\phi, compute a proximal dual step for p\mathbf{p}.

    pk+1=pk+τddEdp\mathbf{p}^{k+1} = \mathbf{p}^k + \tau_d \frac{dE}{d\mathbf{p}}

    pk+1=pk+τd∇3ϕ(x,γ)\mathbf{p}^{k+1} = \mathbf{p}^k + \tau_d \nabla_3 \phi(\mathbf{x},\gamma)

    Since we need to ensure that pk+1∈C\mathbf{p}^{k+1} \in C we reproject pk+1\mathbf{p}^{k+1} onto CC using the projections Pp(pk+1)\mathcal{P}_\mathbf{p}(\mathbf{p}^{k+1}):

    Pp(p1k+1)=p1k+1max(1,p12+p22λ)\mathcal{P}_\mathbf{p}(p_1^{k+1}) = \frac{p_1^{k+1}}{\text{max}(1,\frac{\sqrt{p_1^{2}+p_2^{2}}}{\lambda})}

    Pp(p2k+1)=p2k+1max(1,p12+p22λ)\mathcal{P}_\mathbf{p}(p_2^{k+1}) = \frac{p_2^{k+1}}{\text{max}(1,\frac{\sqrt{p_1^{2}+p_2^{2}}}{\lambda})}

    Pp(p3k+1)=p3k+1max(1,∣p3∣ρ)\mathcal{P}_\mathbf{p}(p_3^{k+1}) = \frac{p_3^{k+1}}{\text{max}(1,\frac{|p_3|}{\rho})}

Step size

From empirical observation, the algorithm converges as long as the product τpτd≤13\tau_p \tau_d \leq \frac{1}{3}. Therefore the following choice of step size was chosen:
τp=τd=13\tau_p = \tau_d = \frac{1}{\sqrt{3}}


References