Algorithmadvanced
Chambolle's projection algorithm
Also called Chambolle dual algorithm for total variation, Chambolle's semi-implicit dual iteration.
Solve total variation denoising by a fixed point iteration on a dual field, which computes the projection of the noisy image onto the set of bounded divergence fields.
- Solves
- Image denoising
- Time
- O(K N) worst
- Space
- O(N) extra
- Where
- K = the number of iterations; N = the number of pixels
- Model
- Real RAM
- Properties
- deterministicnot exactfeasible iteratesiterativemean preservingmonotone dual objective
- Evidence
- Proof sketchCode in 1 languages
- Origin
- Antonin Chambolle, 2004
Chambolle's projection algorithm computes the minimizer of the total variation denoising energy without touching the nonsmooth total variation directly. It uses one fact from convex duality: the discrete total variation is the largest value of over the vector fields with length at most 1 at every pixel. So the denoised image is , where is the projection onto the closed convex set of divergences of such fields. The algorithm finds this projection by a simple fixed point step on the dual field , with a denominator that keeps every iterate feasible. Chambolle proved in 2004 that the iterates converge for every step . The output is the proximal operator of , so the same routine is the inner step of many deblurring and reconstruction methods.
In plain words
A noisy image is a clean image plus a noise part. The method does not look for the clean image directly. It looks for the noise part, because the noise part has a simple description: it is built from a small arrow at each pixel, and no arrow may be longer than a fixed limit. The method keeps one arrow per pixel and improves all arrows at the same time. At each step every arrow turns toward the direction that removes more of the remaining noise, and a division shrinks it back so that it never passes the limit. When the arrows stop changing, the noise part is known, and the clean image is the noisy image minus the noise part. Each step only compares a pixel with its neighbors, so a step is cheap, but many steps can be needed.
The problem
The input is an image with pixels and a weight . The output is the unique minimizer of
where are forward differences with the Neumann rule (a difference that leaves the image is 0) and is the Euclidean length, which gives the isotropic total variation. The divergence is defined by , so for every image and field . With this choice
and the minimizer is , where is any solution of the dual problem
The dual solution need not be unique, but is. The weight is a design choice: it sets the size of the smallest feature that survives, and it is tuned for each class of images.
The algorithm
ChambolleProjection(f, λ, τ, K)- 1
p ← 0 ▷ one 2-vector per pixel - 2
for k = 1 to K - 3
v ← div p - f / λ ▷ the dual residual - 4
z ← ∇v - 5
at each pixel: p ← (p + τ z) / (1 + τ |z|) - 6
w ← λ div p ▷ the projection of f onto λK - 7
return u = f - w
The dual problem has the constraint , and its optimality condition at each pixel is with a multiplier . In both cases (, which forces , and ) the multiplier is . Chambolle's step is a gradient step on this condition, with the multiplier term taken at the new point:
Solving for gives line L5. The denominator keeps the iterates feasible: if , then . Line L4 is minus one half of the gradient of the dual objective, and lines L6 and L7 map the dual field back to the image. A practical stop is the duality gap with , which bounds .
Example
Take the one-row image with . In one dimension the field has three entries , one per difference, , and , so the proof below covers . Take .
Step 1: , so and . Line L5 gives , and the other entries stay 0. Then and . The edge has started to shrink, but the flat parts have not moved yet, because the field is still 0 next to them.
| gap | |||
|---|---|---|---|
| 1 | 2.0816 | ||
| 2 | 1.5057 | ||
| 3 | 1.0865 | ||
| 10 | 0.1315 | ||
| 20 | 0.0078 | ||
| 50 |
The limit is and : each side moves toward the other by divided by its length 2, as the total variation denoising page derives. At the edge and , so there, which is the optimality condition. With the same gap needs about 100 steps instead of 50.
Correctness
The full proof is in proofs/correctness.md. Let and let bound , with in two dimensions. Write one step as with . Then
so for the dual objective does not increase (a theorem). If it does not decrease, the step does not move: at each pixel either , or , and then the triangle inequality forces . This is the case that needs care. A fixed point satisfies at each pixel for , so , and this makes the minimizer with for all . The step map is continuous and the iterates are bounded, so every limit point of is a fixed point and . The field itself need not converge, because the dual solution is not unique.
Proof: Chambolle's dual iteration decreases the dual objective for a step of at most 1/8 and its primal iterates converge to the total variation denoised imagefull
Setting. Images are vectors in and fields are pairs , with and Euclidean length . The gradient uses forward differences with a zero difference at the last row and column, and , so . Let be a bound with for all . Let , , ,
The step map of lines L3 to L5 is: and . The algorithm sets , and .
Lemma 0 (the bound in two dimensions, in one). , where , , , and an entry outside the image or on the last row (for ) or column (for ) is replaced by 0. By the Cauchy-Schwarz inequality . Each entry of appears in the sums of at most two pixels, so . In one dimension there are two terms per pixel, the factor 4 becomes 2, and .
Lemma 1 (dual formula). For all , , so .
Proof. For , . Equality holds for where and elsewhere, and this is in .
Lemma 2 (a certificate of optimality). Let and with . Then for every ,
So is the unique minimizer of .
Proof. By Lemma 1, for every , with equality at by assumption. Since ,
The minimizer exists because is continuous and as , and it is unique because is strongly convex. Let , which exists because is the linear image of the compact convex set and so is compact and convex. The projection inequality gives for all , so by Lemma 1. Lemma 2 then gives . This is the dual problem: exactly when minimizes over .
Lemma 3 (feasibility). If then , and satisfies with at each pixel.
Proof. , so . Multiply the definition of by : .
By induction from , every iterate is in . This is the loop invariant of lines L2 to L5.
Lemma 4 (descent). Let , and . Then
Proof. Put , so . Then and
using . At each pixel , because . So
and both terms are at least 0 by Lemma 3 and .
Lemma 5 (no decrease means a fixed point). Let and . If then .
Proof. By Lemma 4 the bracket is 0, so at each pixel. If then by the formula. Otherwise , so . The triangle inequality then holds with equality and . Equality in the triangle inequality with and means with , so . Then and .
For the term alone gives . The argument above is what covers the end point , which is in two dimensions.
Lemma 6 (a fixed point gives the minimizer). If and , then is .
Proof. means at each pixel, so . Since , we have , so . Where this forces . Hence
With and , Lemma 2 applies.
Theorem. Let , so for images and for signals. Then is nonincreasing and , the minimizer of .
Proof. By Lemma 4 the sequence is nonincreasing and at least 0, so it has a limit . The iterates lie in the compact set (Lemma 3). Let be any convergent subsequence. The map is continuous, because its denominator is at least 1. So , and by continuity of , . Lemma 5 gives , and Lemma 6 gives . So along every convergent subsequence of , . If did not converge to , there would be and a subsequence with ; by compactness of it has a further subsequence on which converges, and on it , a contradiction.
Termination with a certificate. For let . With , Lemma 1 gives for every
So , and by Lemma 2 applied at ,
for every image and every . The gap is computable, and it tends to 0: , is continuous, and , the last equality by strong duality ( with ). So the loop with the stop rule "gap at most " ends after finitely many steps for every , and then .
Complexity
One iteration computes a divergence, a gradient and a pixelwise update: arithmetic operations and extra memory for and two work images. iterations cost . Theorem 3.1 of Chambolle's paper gives convergence but no rate. Treated as projected gradient descent on the dual, the same dual problem has objective error , and with the acceleration of Beck and Teboulle ; the gap bound above turns a dual error into . After steps the field at a pixel depends only on the data within about pixels, because each step looks at the neighbors twice. So a flat region of width needs at least about steps before the value at one end can depend on the data at the other end, which it must do to become the regional mean.
Properties
- The iterates stay feasible: for all , so and the dual value is a valid lower bound of at every step.
- For the dual objective is nonincreasing.
- The mean is kept: , so has the mean of at every step.
- The output is . Moreau's identity reads here , because the convex conjugate of is the indicator of .
- The method needs no matrix, no linear solve and no smoothing of the total variation.
- A warm start from a previous is allowed, because the proof only needs .
Variants and improvements
- Projected gradient on the dual: at each pixel. Chambolle later used this form with in two dimensions. It is projected gradient descent on with the step , and the gradient of is Lipschitz with constant , so the classic condition step gives .
- Accelerated dual: Beck and Teboulle (2009) apply the FISTA momentum to the projected dual gradient, which gives for the dual objective.
- Anisotropic total variation: with the constraint is and , and the step becomes a clip of each component.
- Weighted and color total variation: a pixel weight changes the bound to , and a vectorial total variation couples the channels in the length of .
- Inner solver: inside the proximal gradient method for deblurring, , each outer step needs , which this algorithm computes with a warm start.
- Primal-dual methods: the Chambolle-Pock algorithm and split Bregman solve the same model and also handle a forward operator directly.
When to use it
Use it when you need the proximal operator of total variation, as a short and safe subroutine, or as a reference solver for small images in tests. It has one parameter, every iterate is feasible, and the duality gap gives a stop rule with a guarantee. For large images or a high accuracy it is slow, and in reported experiments accelerated dual or primal-dual methods reach the same gap in fewer iterations; this is a measurement, not a theorem. If the problem has a forward operator, use a primal-dual method rather than nesting this one inside an outer loop.
Pitfalls
- The divergence must be exactly , including the border rows and columns. A divergence with a different border rule breaks the duality and the proof, and the result is wrong near the border. Test on random data.
- The proof covers in two dimensions. Chambolle remarks that works better in practice, but there is no proof for that step size with this update.
- Some papers put the weight on the data term, . Then .
- A stop rule on can stop too early when the steps are small. The duality gap is the safe stop rule.
- The field can move along a set of dual solutions without converging. Only and have a limit.
- For a large the solution is nearly flat over large regions, and the number of iterations grows with the size of these regions.
Implementations
Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/chambolle-projection-algorithm/. The authors are listed in prov/R2YTGF.jsonl.
"""Chambolle's projection algorithm for total variation denoising.
The model is min_u (1/2) ||u - f||^2 + lam * TV(u) on an n1 x n2 image f
(a list of rows), with the isotropic discrete total variation of forward
differences and a Neumann border (the last difference in each direction is 0).
The minimizer is u = f - P(f), where P is the projection onto the set
lam * K, K = {div p : |p_ij| <= 1 at every pixel}. The algorithm computes the
dual field p by Chambolle's semi-implicit fixed point step.
"""
import math
Image = list[list[float]]
Field = tuple[Image, Image]
def zeros(n1: int, n2: int) -> Image:
return [[0.0] * n2 for _ in range(n1)]
def grad(u: Image) -> Field:
"""Forward differences with a Neumann border. Cost O(n1 n2)."""
n1, n2 = len(u), len(u[0])
g1, g2 = zeros(n1, n2), zeros(n1, n2)
for i in range(n1):
for j in range(n2):
if i < n1 - 1:
g1[i][j] = u[i + 1][j] - u[i][j]
if j < n2 - 1:
g2[i][j] = u[i][j + 1] - u[i][j]
return g1, g2
def div(p: Field) -> Image:
"""The divergence, defined as minus the adjoint of grad. Cost O(n1 n2)."""
p1, p2 = p
n1, n2 = len(p1), len(p1[0])
d = zeros(n1, n2)
for i in range(n1):
for j in range(n2):
a = (p1[i][j] if i < n1 - 1 else 0.0) - (p1[i - 1][j] if i > 0 else 0.0)
b = (p2[i][j] if j < n2 - 1 else 0.0) - (p2[i][j - 1] if j > 0 else 0.0)
d[i][j] = a + b
return d
def total_variation(u: Image) -> float:
g1, g2 = grad(u)
return sum(math.hypot(a, b) for r1, r2 in zip(g1, g2) for a, b in zip(r1, r2))
def energy(u: Image, f: Image, lam: float) -> float:
"""The primal energy (1/2)||u - f||^2 + lam TV(u)."""
fit = sum((a - b) ** 2 for ru, rf in zip(u, f) for a, b in zip(ru, rf))
return 0.5 * fit + lam * total_variation(u)
def dual_energy(p: Field, f: Image, lam: float) -> float:
"""D(p) = (1/2)(||f||^2 - ||f - lam div p||^2), a lower bound of the energy for |p| <= 1."""
d = div(p)
ff = sum(a * a for r in f for a in r)
rr = sum((a - lam * b) ** 2 for rf, rd in zip(f, d) for a, b in zip(rf, rd))
return 0.5 * (ff - rr)
def chambolle_projection(f: Image, lam: float, tau: float = 0.125, iters: int = 200,
tol: float | None = None, p0: Field | None = None) -> tuple[Image, Field]:
"""Return (u, p): the TV denoised image and the dual field.
Precondition: lam > 0 and 0 < tau <= 1/8 for the proved convergence
(tau = 1/4 often works in practice but has no proof for this step).
With tol set, stop when the duality gap E(u) - D(p) is at most tol;
then ||u - u*||^2 <= 2 tol. Cost O(n1 n2) per iteration, O(n1 n2) space.
"""
n1, n2 = len(f), len(f[0])
p1, p2 = (zeros(n1, n2), zeros(n1, n2)) if p0 is None else ([r[:] for r in p0[0]], [r[:] for r in p0[1]]) # @line L1
for k in range(iters): # @line L2
d = div((p1, p2))
v = [[d[i][j] - f[i][j] / lam for j in range(n2)] for i in range(n1)] # @line L3
z1, z2 = grad(v) # @line L4
for i in range(n1):
for j in range(n2):
s = 1.0 + tau * math.hypot(z1[i][j], z2[i][j]) # @line L5
p1[i][j] = (p1[i][j] + tau * z1[i][j]) / s
p2[i][j] = (p2[i][j] + tau * z2[i][j]) / s
if tol is not None and (k + 1) % 10 == 0:
u = _primal(f, lam, (p1, p2))
if energy(u, f, lam) - dual_energy((p1, p2), f, lam) <= tol:
break
return _primal(f, lam, (p1, p2)), (p1, p2)
def _primal(f: Image, lam: float, p: Field) -> Image:
d = div(p)
w = [[lam * a for a in r] for r in d] # @line L6
return [[f[i][j] - w[i][j] for j in range(len(f[0]))] for i in range(len(f))] # @line L7
def dual_residual(f: Image, lam: float, p: Field) -> float:
"""||div p - f/lam||^2, the dual objective that the iteration decreases."""
d = div(p)
return sum((a - b / lam) ** 2 for rd, rf in zip(d, f) for a, b in zip(rd, rf))
def chambolle_step(f: Image, lam: float, tau: float, p: Field) -> Field:
"""One step of lines L3 to L5, returned as a new field (for tests of monotonicity)."""
_, q = chambolle_projection(f, lam, tau, iters=1, p0=p)
return q
test_chambolle_projection_algorithm.py 114 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/chambolle-projection-algorithm/. The authors are listed in prov/R2YTGF.jsonl.
import itertools
import math
import random
from chambolle_projection_algorithm import (
chambolle_projection,
chambolle_step,
div,
dual_energy,
dual_residual,
energy,
grad,
)
def brute_force_1d(f: list[float], lam: float) -> list[float]:
"""Exact 1D TV denoising: try every sign pattern of the differences.
For a pattern s, runs with s = 0 merge into segments; on a segment S the
value is mean(S) - lam (s_left - s_right) / |S|. The pattern of the true
minimizer gives the true minimizer, so the best candidate is exact.
"""
n = len(f)
best, best_val = None, math.inf
for s in itertools.product((-1, 0, 1), repeat=n - 1):
segs, start = [], 0
for b in range(n - 1):
if s[b] != 0:
segs.append((start, b + 1))
start = b + 1
segs.append((start, n))
u = []
for k, (a, b) in enumerate(segs):
sl = s[a - 1] if a > 0 else 0
sr = s[b - 1] if b < n else 0
v = sum(f[a:b]) / (b - a) - lam * (sl - sr) / (b - a)
u.extend([v] * (b - a))
val = energy([u], [f], lam)
if val < best_val:
best, best_val = u, val
return best
def test_adjoint() -> None:
rng = random.Random(1)
for n1, n2 in ((1, 5), (3, 4), (5, 2)):
u = [[rng.uniform(-1, 1) for _ in range(n2)] for _ in range(n1)]
p = ([[rng.uniform(-1, 1) for _ in range(n2)] for _ in range(n1)],
[[rng.uniform(-1, 1) for _ in range(n2)] for _ in range(n1)])
g1, g2 = grad(u)
lhs = sum(g1[i][j] * p[0][i][j] + g2[i][j] * p[1][i][j] for i in range(n1) for j in range(n2))
d = div(p)
rhs = -sum(u[i][j] * d[i][j] for i in range(n1) for j in range(n2))
assert abs(lhs - rhs) < 1e-12
def test_step_image() -> None:
f = [[0.0, 0.0, 4.0, 4.0] for _ in range(4)]
u, _ = chambolle_projection(f, 1.0, iters=3000, tol=1e-10)
for row in u:
for a, b in zip(row, (0.5, 0.5, 3.5, 3.5)):
assert abs(a - b) < 1e-4
def test_brute_force_1d() -> None:
rng = random.Random(7)
for _ in range(8):
n = rng.randint(2, 6)
f = [float(rng.randint(-3, 3)) for _ in range(n)]
lam = rng.choice((0.3, 0.7, 1.5))
u, _ = chambolle_projection([f], lam, iters=20000, tol=1e-11)
ref = brute_force_1d(f, lam)
assert max(abs(a - b) for a, b in zip(u[0], ref)) < 1e-4
def test_monotone_and_feasible() -> None:
rng = random.Random(3)
f = [[rng.uniform(0, 5) for _ in range(5)] for _ in range(4)]
lam = 0.8
p = ([[0.0] * 5 for _ in range(4)], [[0.0] * 5 for _ in range(4)])
r = dual_residual(f, lam, p)
for _ in range(200):
p = chambolle_step(f, lam, 0.125, p)
r2 = dual_residual(f, lam, p)
assert r2 <= r + 1e-12
r = r2
assert all(math.hypot(a, b) <= 1 + 1e-12 for r1, r2_ in zip(*p) for a, b in zip(r1, r2_))
def test_gap_certificate_and_mean() -> None:
rng = random.Random(5)
f = [[rng.uniform(0, 5) for _ in range(4)] for _ in range(4)]
lam = 0.5
u, p = chambolle_projection(f, lam, iters=5000, tol=1e-7)
gap = energy(u, f, lam) - dual_energy(p, f, lam)
assert -1e-9 <= gap <= 1e-7
mean_f = sum(map(sum, f)) / 16
mean_u = sum(map(sum, u)) / 16
assert abs(mean_f - mean_u) < 1e-12
# Small random moves do not lower the energy below the dual bound.
for _ in range(20):
v = [[a + rng.uniform(-0.01, 0.01) for a in row] for row in u]
assert energy(v, f, lam) >= dual_energy(p, f, lam) - 1e-12
def test_large_lambda_gives_mean() -> None:
f = [[1.0, 2.0], [3.0, 6.0]]
u, _ = chambolle_projection(f, 100.0, iters=2000, tol=1e-12)
for row in u:
for a in row:
assert abs(a - 3.0) < 1e-5
History
Rudin, Osher and Fatemi proposed total variation denoising in 1992, and Chambolle and Lions (1997) studied the model and its use for image recovery. Dual and primal-dual formulations followed, among them the Newton type primal-dual method of Chan, Golub and Mulet (1999). In 2004 Antonin Chambolle observed that total variation is the support function of the convex set , so its proximal operator is the identity minus a projection, and gave the semi-implicit dual step on this page with a short convergence proof. The method became a standard building block because it is simple and has no smoothing parameter. Beck and Teboulle (2009) accelerated the dual approach, and Chambolle and Pock (2011) gave a primal-dual method that handles a general forward operator.
References
- Antonin Chambolle, An algorithm for total variation minimization and applications, Journal of Mathematical Imaging and Vision 20(1-2), 2004, pages 89 to 97 (Section 3, the semi-implicit step and Theorem 3.1 for a step of at most 1/8)
- Leonid I. Rudin, Stanley Osher and Emad Fatemi, Nonlinear total variation based noise removal algorithms, Physica D 60(1-4), 1992, pages 259 to 268
- Antonin Chambolle and Pierre-Louis Lions, Image recovery via total variation minimization and related problems, Numerische Mathematik 76(2), 1997, pages 167 to 188
- Amir Beck and Marc Teboulle, Fast gradient-based algorithms for constrained total variation image denoising and deblurring problems, IEEE Transactions on Image Processing 18(11), 2009, pages 2419 to 2434 (the accelerated projected gradient on the same dual problem)
- Antonin Chambolle and Thomas Pock, An introduction to continuous optimization for imaging, Acta Numerica 25, 2016, pages 161 to 319
- Joan Duran, Bartomeu Coll and Catalina Sbert, Chambolle's projection algorithm for total variation denoising, Image Processing On Line, 2013
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H1 sketch |
|
| Lean | L0 no Lean part |
|
| Code (python) | I0 not checked yet |
|
| Visual | V0 none |
|
| Writing | W2 basic |
|
Tag R2YTGF Edit this page History Report an error Markdown