BookNotesSlidesP-sets Syllabusdraft · v0.2.49.1
◀▲⌂▶
💬Comments welcome. To leave a note, select any text and click the note / highlight button that pops up — or open the panel with the tab at the top-right (‹). Notes are visible only inside our private review group.

10.3 Edge-preserving optimization — colorization⧉

A few color scribbles can colorize a grayscale portrait if the algorithm can propagate them to the right regions. An affinity-weighted optimization couples neighboring unknowns and lets those constraints influence distant pixels. The cost is a sparse solve rather than one local filtering pass.

The local model asks each unknown to agree approximately with a weighted average of its neighbors. Hard scribbles can prevent exact agreement, so the algorithm minimizes the total residual. This is related to bilateral affinity, but is a different operator.

10.3.1 The shift: filtering becomes optimization⧉

Hold the bilateral filter Tomasi & Manduchi 1998 in mind. To smooth a pixel $p$, it forms a weighted average of the neighbors $q$, where each weight, the affinity $a_{pq}$, is large when $p$ and $q$ are alike in value and small when an edge separates them. The output is that average, computed once, independently, for every pixel. Information crosses the image only as far as the filter's footprint reaches; a stroke of influence on one side of the picture has no way to reach the other in a single pass.

Neighboring residuals couple the unknowns across the image. On a connected region, one scribble can therefore influence distant pixels. Weak affinities reduce propagation across a luminance edge; they do not establish that the two sides have different true colors. Equal-luminance color boundaries remain ambiguous.

The price is that we no longer have a closed-form output per pixel; we have a system of coupled equations. The reward, as we will see, is that the system is exactly the kind we already know how to solve, and that the coupling is precisely what lets a handful of user marks fill an entire photograph.

Consider a long region with almost constant luminance and only one color scribble near its left end. One local averaging pass can transmit the scribble only as far as its neighborhood reaches. In a global optimization, adjacent unknown colors are coupled throughout the region. The solution can therefore carry the constraint much farther, even though each individual equation is local. A strong luminance boundary weakens the coupling if the affinity was designed that way. An unmarked disconnected region still needs a constraint or a convention; the solver cannot infer its intended color merely by being global.

10.3.2 Colorization: the canonical demonstration⧉

The crispest demonstration is colorization, due to Anat Levin, Dani Lischinski and Yair Weiss (2004). Start from a grayscale photograph, such as an old family portrait or a black-and-white film still, and the goal is to add plausible color. Doing it pixel by pixel would be hopeless. Instead the user scribbles a few coarse strokes of color roughly where each color belongs: a smear of red across the lips, blue on a sleeve, brown over the hair. The strokes need not be careful and need not fill anything; they are just hints. The algorithm then propagates the color from the scribbles to flood the rest of the image, under a single rule (Figure 10.3.1):

neighboring pixels of similar luminance should take similar color.

That rule is the bilateral's range idea stated verbatim: the same affinity, and the same idea that similar luminance means pixels belong together, but now expressed as something to minimize rather than average. It is a useful prior because many object boundaries coincide with luminance changes, while smooth shading often stays within an object. It can therefore propagate color without an explicit segmentation. It is still a prior: a shadow can create a luminance edge within one surface, and two different colors can meet at equal luminance. The user may need additional scribbles to resolve those cases.

The reason colorization is the canonical demonstration, rather than one application among many, is that it isolates the optimization idea cleanly. There is no clever color model, no segmentation, no learned prior. There is one quadratic energy, one affinity, and one linear solve, and yet a few seconds of scribbling colors an entire photograph. Everything edge-preserving and everything global about the method is on the surface.

fig-colorization-scribbles
Figure 10.3.1. Edge-aware color propagation, interactive. Left: a grayscale portrait carrying a few coarse color scribbles (brown on the hair, a warm tone on the skin, red on the lips, neutral on the backdrop), none of them filling or tracing anything. Right: the propagated result, each color spreading through regions of similar gray value and slowing sharply at luminance edges, so the brown fills the hair and mostly stops at the face. The demo is a simpler cousin of Levin, Lischinski and Weiss (2004), not their method: scribbles pin the chroma, and a fixed number of Gauss-Seidel sweeps minimize chroma differences between four-connected neighbors, weighted by how similar their gray values are, whereas their objective ties each pixel's chroma to a weighted average of its whole neighborhood. In both, an edge is strong resistance rather than a wall, and color leaks where the luminance edge is faint. Drag on the left panel to paint your own strokes (pick a brush color from the palette); the σ control sets how sharp a luminance edge must be to slow the color: raise σ and color bleeds across edges, lower it and even a faint luminance edge holds it back. Pick a different sample (portraits or landscapes) or upload your own photo; the affinity at work, live. The display keeps the input's gray channel and clips the recombined color to the display range. Photos by Frédo Durand.

The original paper's own results show the method on photographs.

fig-colorization-levin-1
Figure 10.3.2. The canonical example. A grayscale portrait of a child carrying a few coarse color scribbles (left) is flooded with plausible color by the optimization (center, then the finished result right), each color spreading through its region of smooth luminance and halting at the luminance edges, so the skin tone fills the face but stops at the lips and the yellow fills the shirt but not the background. A few seconds of scribbling, one sparse solve (Levin et al. 2004).
fig-colorization-levin-3
Figure 10.3.3. Robustness and recoloring. The same scene colorized and then re-colorized by editing only the scribbles: the affinity solve re-floods each region from the new hints, so swapping a few strokes restyles the whole photograph without re-segmenting it. Evidence that the propagation is driven entirely by luminance affinity, not by any object model (Levin et al. 2004).

10.3.3 Writing down the energy⧉

We are given the luminance $I$ everywhere (it is just the grayscale image) and the user's scribbles at a few pixels. We solve for the chrominance $U$ (the color part, the two color-difference channels) at every pixel. Luminance is known; only color is unknown, which is why the energy is over $U$ alone.

The rule "each pixel's color should match the affinity-weighted average of its neighbors' colors" becomes, written as a penalty to minimize,

$$ \min_{U}\ \sum_{p} \Bigl( U_p - \sum_{q\in N(p)} a_{pq}\,U_q \Bigr)^{2}, $$

where $N(p)$ is the small neighborhood of pixel $p$ and $a_{pq}$ is the affinity between $p$ and its neighbor $q$. Each summand is one pixel's deviation from its neighbors' weighted average, squared so that positive and negative deviations both cost and so that the whole thing is a clean quadratic. Minimizing it drives every pixel toward agreement with the neighbors it most belongs with.

Two details make the term mean exactly what we said. First, the affinity is large when $I_p \approx I_q$ in luminance and small across a luminance edge: that is the entire edge-preserving content, the L10.1 affinity keyed on intensity difference. Second, the affinities around each pixel are row-normalized, $\sum_{q\in N(p)} a_{pq} = 1$, so that the inner sum $\sum_{q} a_{pq} U_q$ is a genuine weighted average of the neighbors rather than a mere weighted sum. With both in place, read the term back: each pixel's color should equal the affinity-weighted average of its neighbors' colors.

A natural choice for the affinity, and the one in the original method, makes the dependence on luminance difference concrete:

$$ a_{pq} \;\propto\; \exp\!\Bigl( -\frac{(I_p - I_q)^2}{2\,\sigma_p^{2}} \Bigr), $$

normalized over $q \in N(p)$, where $\sigma_p$ is a local measure of how much luminance varies around $p$ (so the falloff adapts to each region's contrast). This is the same exponential-of-squared-difference that gave the bilateral its range weight; here it is the same number doing the same job inside an energy. Where luminance is flat, neighboring $I$ values are close, the exponent is near zero, and the affinity is strong. Color couples tightly and flows freely, flooding the region. Where luminance jumps at an object boundary, $(I_p - I_q)^2$ is large, the affinity collapses toward zero, the coupling weakens, and color halts. A strong luminance boundary can therefore reduce spill from red lips onto the chin; it does not guarantee that every color boundary will be respected. (Levin and colleagues note that a correlation-based affinity works comparably; the exponential form is the cleanest to picture.)

Finally, the scribbles. They enter as hard constraints: at every scribbled pixel, $U$ is fixed to the color the user painted, and the minimization runs over only the unscribbled pixels. Those fixed values are what the propagation propagates: they pin the color at a few locations, and the energy carries it everywhere else along the high-affinity paths. (One could instead add a soft penalty pulling scribbled pixels toward the user's color; the hard-constraint version is simplest to state and is what the figure shows.)

10.3.4 Solving it: a sparse linear system we already own⧉

Write $A_{pq}=a_{pq}$ and $B=I_n-A$, where $I_n$ is the identity matrix. The energy is $\|BU\|^2$. Partition columns into free and constrained pixels, $B=[B_F\ B_C]$, and let $U_C$ be the scribbled values. Then solve

$$B_F^\top B_F U_F=-B_F^\top B_C U_C.$$

The normal matrix is symmetric positive semidefinite. It is positive definite only when the constraints remove its nullspace. Constrain every otherwise free connected component or add a stated fidelity term. A row-normalized affinity matrix itself is generally not symmetric.

Each residual touches a small neighborhood. Its normal matrix can connect pixels that share a residual, so the stencil is wider than the original neighbor set. Apply the factors without explicitly forming the normal matrix when appropriate; least-squares solvers can also avoid squaring the condition number.

So the same toolkit applies, the machinery of Chapter 6.9, Efficient linear solvers:

These are the same tools that solve the Poisson equation for gradient-domain editing and that will solve the matting system in Part 19, Layers, transparency, compositing, and matting. The solver concepts transfer across these applications. Their operators and constraints still differ, so their nullspaces, conditioning and suitable preconditioners must be checked separately.

10.3.5 One affinity, two faces: the matting Laplacian connection⧉

The matting Laplacian Levin, Lischinski & Weiss 2008 (closed-form matting) is another sparse quadratic operator derived from local image statistics. It is not the same matrix as the row-normalized colorization affinity or its least-squares normal matrix. Chapter 10.5, Matting Laplacian derives its local affine color model.

The same energy template powers more than these two. Lischinski and colleagues Lischinski et al. 2006 used precisely this construction for interactive tonal and exposure editing: scribble "brighter here, darker there," and the affinity-weighted solve propagates a smooth, edge-respecting adjustment map across the photograph, so an exposure tweak floods a region and stops at its borders exactly as color does in colorization. In every case the recipe is the same: the user marks a few pixels, an affinity-weighted energy propagates the marks along smooth-luminance paths, a sparse solve fills the rest. Colorization is the cleanest face of a single, widely reused idea.

Everything above is an interactive method: the user supplies the scribbles, and a hand-built affinity prior carries them. The learned alternative dispenses with the scribbles entirely. Instead, train a CNN to predict plausible colors directly from a gray image (Zhang, Isola & Efros 2016), replacing the hand-designed affinity prior with one learned from millions of color photographs. That is the same "learned prior supersedes the hand-built one" move (Chapter 7.3, Deep learning, Chapter 7.4, Generative AI and diffusion) that overtakes denoising, deblurring, and inpainting, but the skeleton never changes: it is always data-fit + prior; this chapter has only shown what the prior looks like when it is an affinity and the solve is one sparse system.

fig-affinity-two-faces
Figure 10.3.4. One affinity, two faces. Top row, filtering (the bilateral): a pixel and its neighbors, each neighbor tagged with its affinity $a_{pq}$ (large within a region, near zero across the edge), and the output computed as one affinity-weighted average, a single local pass. Bottom row, optimization (colorization / matting): the same affinities, now the off-diagonal weights of a sparse matrix; the propagated solution fills the whole image from a few pinned scribbles via one global linear solve. Caption pointer: the very same weight matrix is the colorization operator, the matting Laplacian, and the graph-cut smoothness weights Boykov & Jolly 2001: averaging and solving are two uses of one affinity.