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.
💡 In a hurry? Jump to this chapter’s 2 big lessons ↓

21.1 Inpainting, texture synthesis, and object removal⧉

Erase a person from a holiday snapshot and you are left with a person-shaped hole, a region of pixels you never photographed, surrounded by sand and sea you did. What should go in the hole? There is no measurement to consult; the camera recorded nothing there but the person who is now gone. And yet you have a strong sense of what should be there: more sand, more sea, the horizon line continuing unbroken from one side of the hole to the other. The visible sand, sea, and horizon constrain the answer; your prior, an internal model of beach photographs, explains how to continue them through the hole. Neither tells you whether an unseen shell or footprint lay behind the person.

This is the cleanest illustration in the whole book of the lesson that ran through super-resolution and deblurring: when the measurement genuinely gives you nothing, no amount of cleverness recovers it from the data, and a prior is what selects an answer. Super-resolution had to invent high frequencies the sensor folded away; inpainting has to invent whole regions. Here the problem is extreme: inside the hole there is zero data, so the fill depends on the prior conditioned on the visible context. It can be plausible without matching the hidden scene.

The chapter is one spectrum. At the weak end, a smoothness prior simply continues the surrounding color and edges inward: wonderful for a thin scratch, hopeless for a large hole. Stronger is a self-similarity prior that copies patches from elsewhere in the same image, which is what makes texture synthesis and object removal work. Stronger still is a database prior that pastes from other images entirely: a large photo database provides the prior. And at the strong end sits a learned generative prior that has compressed millions of images into weights and simply generates a completion. These families use progressively broader sources of information; we examine them in that order.

💡 Big lesson (recurrence of (L7.8))

The prior is not optional, and filling a hole is the purest case. There is zero data inside the hole, so 100% of the answer comes from the prior. Smoothness, self-similarity, a photo database, a learned net: each is a different prior, and the result is exactly as good (and as truthful) as the prior is. The split that organized super-resolution survives intact: a prior is reconstruction-leaning where it copies genuine structure from somewhere real, and hallucination where it invents detail that was never anywhere. (Registered as L7.8; developed in Chapter 14.1, What can super-resolution actually do?; here taken to its extreme: no data, all prior.)

21.1.1 Inpainting as filling unmeasured pixels: the spectrum of priors⧉

State the problem plainly. We are given an image $I$ with a masked region $\Omega$ (the hole) marked by the user or by a defect detector, and we want a complete image in which $\Omega$ is plausible and seamless: the eye should not be able to find it. The defining feature is the one we keep returning to: there is no data in $\Omega$, so the fill is inferred from context under a prior, rather than read from a direct measurement. The only thing the algorithm has to work with is the known surround and whatever assumptions it makes about how the hole relates to it.

Read that way, the four families of methods are just four different answers to the question "what does the missing region look like?" (Figure 21.1.1):

Those four assumptions (looks like (a) its neighbors, (b) another part of this image, (c) another image, (d) a typical image) are the four families, with increasing flexibility rather than a formal ordering of statistical prior strength. The rest of the chapter walks the ladder. Treat it plainly: the algorithm is answering a question it has no data for, by assuming the hole resembles one of those four things.

fig-inpaint-prior-spectrum
Figure 21.1.1. One hole, filled four ways, the weak→strong prior axis. A single masked region (say a removed object on a textured natural scene) is completed by: (1) diffusion / PDE, color and edges smoothed inward; correct near the rim but blurry and textureless in the middle; (2) exemplar / PatchMatch, patches copied from elsewhere in the same image; the right texture and copied real structure, limited to what this image contains; (3) database / Hays–Efros, a region from a different, scene-matched photograph grafted in; photoreal and coherent but a different scene's content; (4) diffusion-net / learned, a completion synthesized from a learned image prior; sharp and plausible but invented, not measured. The panels move left to right from weakest to strongest prior, and from "blurry but safe" to "sharp but possibly wrong."

21.1.2 PDE / diffusion-based inpainting⧉

The weakest rung borrows its idea from the art world. A museum conservator repairing a cracked painting does not invent new subject matter inside the crack; she continues the lines that arrive at its edges, carrying a contour or a brushstroke smoothly across the gap so the eye glides over it. Bertalmío, Sapiro, Caselles and Ballester 2000, in the paper that named image inpainting, made exactly this into an algorithm.

The naive version of "be smooth" is to solve Laplace's equation inside the hole, filling $\Omega$ with the harmonic function that interpolates its boundary values, the soap-film membrane we met in Chapter 10.8, Poisson image editing. That diffuses color inward, but it diffuses it in all directions equally, so an edge arriving at the boundary is smeared across into a soft blur rather than continued as an edge. The conservator's instinct is the opposite: diffuse along the contour, not across it.

Bertalmío's method encodes that instinct. The level-lines of an image (the curves of constant intensity, called isophotes) are perpendicular to the gradient $\nabla I$, so the direction along an isophote is $\nabla^\perp I$ (the gradient rotated by ninety degrees). The method propagates the image's smoothness (concretely, its Laplacian $\Delta I$, a local measure of intensity curvature) along those level-lines:

$$ \partial_t I \;=\; \nabla(\Delta I)\cdot \nabla^{\perp} I . $$

Read it back: push the smoothness information in the direction the level-lines run. An edge that meets the hole is an isophote; following it carries the edge straight across the gap instead of blurring it out. The original method alternates this transport with stabilizing diffusion. Run the coupled evolution and the hole fills with colors and contours that continue what surrounds it (Figure 21.1.2). Telea 2004 gave a different fast method built on fast marching and local intensity extrapolation, sweeping inward from the boundary in one pass instead of iterating a PDE to convergence, a related boundary-inward strategy, cheap enough for interactive scratch removal.

This works well for thin structures (scratches, scanner dust, text overlays, hairline cracks) where the two sides of the gap are close and there is genuinely little to invent: continuing the lines is almost as good as having measured them. But it has a hard ceiling. For a large hole there is nothing to propagate inward except flatness; the isophotes peter out long before they reach the center, and the fill goes blurry and textureless. Diffusion knows how to continue an edge but has no concept of texture: it cannot manufacture the grain of grass or the speckle of gravel, because those are not smooth at all. That failure is precisely the motivation for the next rung.

This rung is related to the gradient-domain / Poisson family, although its nonlinear transport equation is not a Poisson solve (cross-ref Chapter 10.8, Poisson image editing, 💡 L10.4): seamless cloning edits the gradient field where it has a source to copy from, and PDE inpainting edits it where there is no source at all: it manufactures the gradients to integrate from a smoothness assumption rather than copying them.

fig-pde-isophote-diffusion
Figure 21.1.2. PDE inpainting continues level-lines into the gap. Left: a scratch crosses an edge (say the boundary between a dark roof and a bright sky). Middle (naive harmonic fill): solving Laplace's equation diffuses color across the edge, smearing the sharp boundary into a soft gray blur inside the gap. Right (isophote-driven, Bertalmío): diffusing the Laplacian along the isophotes $\nabla^{\perp}I$ continues the roof–sky edge straight across the scratch; it stays an edge. Below: the same method on a large hole fails: with no nearby structure to propagate, the interior fills blurry and textureless, motivating the exemplar methods.

The original paper's own results make the same point on real and synthetic data.

fig-inpainting-4
Figure 21.1.3. The synthetic stress-test. A bright ring is painted over two overlapping dark ellipses (left); marking the ring as the hole and running the isophote-driven diffusion restores the ellipses' boundaries straight across it (right). With no texture to invent, only smooth contours to continue, this is exactly the regime where pure PDE inpainting shines (Bertalmío et al. 2000).
fig-inpainting-5
Figure 21.1.4. Restoring an old portrait. The deteriorated photograph of three children (top) is annotated with the scratches and creases to repair (bottom, in red); diffusing the surrounding tone and level-lines into those thin defects removes them without touching the faces. The thin-gap regime where continuing the lines is almost as good as having measured them (Bertalmío et al. 2000).
fig-inpainting-6
Figure 21.1.5. Object and overlay removal on a real scene. A New Orleans street photo defaced with scribbles and a block of overlaid text (left of each pair) is recovered (right) by treating the marked pixels as the hole: the building façade, flags, and crowd continue across the gaps. It also exposes the method's ceiling: where a removed region is large and textured, the fill softens, the very failure that motivates the exemplar methods next (Bertalmío et al. 2000).

21.1.3 Texture synthesis (Efros–Leung; Efros–Freeman quilting)⧉

The fix for a large hole in a textured region (grass, gravel, a knit sweater, foliage) is to stop trying to diffuse it and start trying to synthesize it. The useful observation is: texture is self-similar. A patch of lawn looks like every other patch of lawn; the missing piece is not smooth, but it is statistically a copy of what surrounds it. Use copied neighborhoods as an empirical model of the texture.

Non-parametric texture synthesis (Efros–Leung)⧉

Efros and Leung's 1999 method Efros & Leung 1999 makes this idea literal: it synthesizes the texture one pixel at a time, and the sample image itself is the model: no parameters are fit at all. To fill the next unknown pixel $p$, take its already-known neighborhood $N(p)$ (the ring of pixels around $p$ that are already filled or were never missing) and search the source texture for windows that match it, scoring each candidate window $N(q)$ by a Gaussian-weighted sum of squared differences (SSD) over only the known part of $p$'s neighborhood. Pick a window that matches well, and copy its center pixel into $p$:

$$ d(p,q)=\frac{\sum_{u\in K_p}w(u)[I(p+u)-I(q+u)]^2}{\sum_{u\in K_p}w(u)},\qquad q\sim\operatorname{Uniform}\{q:d(p,q)\le(1+\epsilon)d_{\min}\},\qquad \hat I(p)=I(q), $$

where $K_p$ contains the known offsets in the neighborhood, $w(u)$ is a Gaussian weight, $d_{\min}$ is the minimum candidate distance, and $\epsilon$ permits near-best matches. Thus the equation samples among matches just as the pseudocode does. Then $p$ is known, the frontier advances by one pixel, and you repeat:

Algorithm 21.1.1
while any pixel is unfilled:
    p ← unfilled pixel with the most known neighbours
    for each candidate window N(q) in the source:
        d[q] ← weighted mean squared difference over the known part of N(p)
    pick q at random among the windows with d[q] near the minimum
    Î(p) ← I(q)        # copy the matching window's centre pixel

In words: to decide what goes here, find the places in the image whose surroundings look most like this pixel's surroundings, and copy what they have in the middle. (Filling the most-surrounded pixel first keeps the synthesis frontier coherent; accepting a near-best match at random, rather than the single best, avoids verbatim repetition.) It is the Markov-random-field assumption made into an algorithm: a pixel's conditional distribution is modeled from its neighborhood, and the empirical distribution of "neighborhood → center" is the source image.

fig-shannon-ngram
Figure 21.1.6. Shannon's n-gram, interactively: the literary ancestor of Efros–Leung. Generate text by sampling each next symbol conditioned on the previous $n$, sweeping $n$ from $0$ (random letters) upward and watching the output slide from gibberish to almost-English as the context grows, the one-dimensional, temporal twin of texture synthesis's spatial neighborhood matching, and of how a modern language model samples its next token.

The main structural knob is the window size (Figure 21.1.7). Make it too small and the match sees only a few pixels, so it captures the texture's fine grain but loses any large-scale structure: bricks dissolve into a noisy mush of brick-colored speckle. Make it too large and the only windows that match are near-identical to the source, so the method copies the source verbatim, tiling it like wallpaper (and slowly, since each match is a full search). The window must straddle the texture's characteristic scale: large enough to see a whole brick, small enough not to lock onto one specific brick. That single trade-off (local randomness versus large-scale faithfulness) is the recurring theme of every patch method that follows.

The cost is speed: a full search over the source for every pixel is very slow, which is exactly the bottleneck Chapter 21.2, Patch match, later removes Barnes et al. 2009, and Wei & Levoy 2000 had already sped up by using fixed-shape neighborhoods amenable to tree search.

fig-efros-leung-growth
Figure 21.1.7. Texture grown pixel-by-pixel, and the window-size knob. Top: the growth process: for the next unfilled pixel $p$ (on the synthesis frontier), its known neighborhood $N(p)$ is matched by Gaussian-weighted SSD against candidate windows $N(q)$ throughout the source; the center of a near-best match is copied into $p$, and the frontier advances. Bottom, three results from one texture as window size grows: too small → the texture's large-scale structure is lost, output looks like noise of the right color; just right → straddles the texture's characteristic scale, output looks like more of the same texture; too large → matches force near-exact copies, output reproduces the source verbatim (and is slow).

The paper's own figures show the mechanism and the knob on real textures.

fig-texture-synthesis-1
Figure 21.1.8. The matching idea in one picture. To decide the value at a pixel, the method searches the sample for windows whose surround matches the pixel's known neighborhood and copies a matching center; here a brick-wall patch and the candidate windows (dashed) it is compared against. "Look where the surroundings agree, copy what is in the middle," made literal (Efros & Leung 1999).
fig-texture-synthesis-2
Figure 21.1.9. The window-size knob, three textures, four sizes. Each row grows one sample (rings, a diagonal weave, brickwork) at increasing neighborhood size: too small (left) and the large-scale structure dissolves into noise of the right color; widen the window and the synthesis locks onto the texture's characteristic scale and produces convincing more-of-the-same. The single trade-off that governs every patch method (Efros & Leung 1999).
fig-texture-synthesis-4
Figure 21.1.10. Constrained synthesis: texture as a hole-filler. Holes punched into textured images (a missing object, a region behind overlaid text) are completed by growing texture inward from the known surround, the conditional neighborhood matching naturally respecting the existing pixels at the rim. The same pixel-at-a-time engine, pointed at inpainting: the bridge from texture synthesis to exemplar object removal (Efros & Leung 1999).

Image quilting (Efros–Freeman)⧉

Synthesizing a single pixel per search is wasteful when whole patches of the source are reusable. Efros and Freeman's 2001 image quilting Efros & Freeman 2001 lays down overlapping blocks copied from the source, choosing each new block so that its overlap region matches what is already there. That alone would leave a visible seam wherever two blocks meet, so quilting adds a trick: in the overlap strip, it does not blend: it cuts along the minimum-error boundary, the path through the overlap where the two blocks disagree least, and takes one block on each side of the cut (Figure 21.1.11). Finding that least-disagreement path through a strip is a tiny dynamic program (a shortest path through a two-dimensional strip) and because the seam wanders through wherever the two patches happen to agree, it can become hard to see; a poor overlap match still leaves a visible seam.

Quilting is far faster than pixel-at-a-time synthesis (a whole block per search, not a pixel), and its min-error-seam idea is the direct ancestor of GraphCut textures (Kwatra et al. 2003 Kwatra et al. 2003), which generalize the one-dimensional cut to a full two-dimensional graph cut: place a patch and let min-cut/max-flow decide, per pixel, whether to keep the old texture or the new one, with the cut running along the seam of least visible disagreement.

Video 21.1.1. Video: Graphcut Textures: Image and Video Synthesis Using Graph Cuts. Vivek Kwatra, Arno Schödl, Irfan Essa, Greg Turk and Aaron Bobick, 2003 (YouTube).

That is not a coincidence; it is a big lesson. The patch-plus-min-error-seam view is discrete optimization on a graph, a match cost (how well a patch fits) plus a seam cost (how visible the boundary is), minimized by an off-the-shelf optimizer, with the energy doing all the design work.

💡 Big lesson (L10.5)

The quilting seam is a graph cut; the energy is the design. Stitching two overlapping patches by choosing the minimum-error boundary through their overlap is exactly discrete optimization on a graph: a dynamic program in one dimension (quilting), a min-cut/max-flow in two (GraphCut textures, Kwatra et al. 2003). The optimizer is off-the-shelf; what you design is the energy: match cost (does this patch belong here?) plus seam cost (is the boundary visible?). The same machinery cuts panorama and photomontage seams and segments objects; texture synthesis is one more instance. (See L10.5, image edits as discrete optimization on a graph; first placed in Chapter 10.9, Seam optimization.)

fig-quilting-seam
Figure 21.1.11. Image quilting stitches patches along a minimum-error boundary. Left: two source blocks are placed with an overlap region; naively butting them together leaves a hard seam down the middle of the overlap. Middle: the per-pixel squared difference within the overlap strip, bright where the two blocks disagree, dark where they agree. Right: a minimum-error path (a 1-D dynamic program / graph cut) is routed through the dark, low-disagreement valleys; the left block is kept on one side of the cut and the right block on the other. The seam follows wherever the textures already match, so it disappears, the prefiguring of GraphCut textures (Kwatra 2003) and 💡 L12.

Quilting places one block at a time. Texture optimization Kwatra, Essa, Bobick and Kwatra, 2005 instead improves every output patch at once: match each patch of the current output to its nearest exemplar patch, replace every pixel by the average of the matched patches that overlap it, and repeat, coarse to fine. The matching step is a nearest-neighbor field, which is why PatchMatch (Chapter 21.2, Patch match) made it fast. Adding a completeness term, so that every exemplar patch must also appear somewhere in the output, keeps the result from settling on the exemplar's plainest patches. The texture view of (Figure 21.2.8) runs this on an exemplar you drag over a photo.

fig-patchmatch-applications
Figure 21.2.8, the texture view (reproduced). Texture synthesis by patch optimization with PatchMatch as the search. Drag the square to choose the exemplar; the output starts as a collage of random exemplar blocks and is refined coarse to fine, each iteration re-matching every output patch and voting. The right panel colors each output pixel by where in the exemplar it came from, so the copied pieces show as patches of smooth color.

A note on the road not taken: instead of copying patches, one can fit an explicit parametric model of the texture's statistics: Heeger & Bergen 1995 match the histograms of steerable-pyramid subband responses, and Portilla & Simoncelli 2000 match a larger set of correlations among those responses. Synthesize by coercing white noise to have the same subband statistics. These can struggle with some structured textures that patch copying preserves, but they are conceptually important: matching the statistics of multiscale filter responses is the direct ancestor of neural style's Gram-matrix idea (cross-ref Chapter 25.1, Style transfer, Chapter 6.6, Pyramids and wavelets).

21.1.4 Exemplar inpainting: clone, healing brush, and object removal (Criminisi)⧉

Texture synthesis fills a textured hole; exemplar inpainting turns the same patch-copying machinery on real photographs, where a hole sits among structured content (an edge, a horizon, a corner) that must continue across it. The lineage starts with two manual tools every retoucher knows.

The clone brush is the bare exemplar prior under hand control: pick a source region and paint its pixels into the hole. Pure copying betrays itself instantly: wherever the source and destination differ in lighting or color, a hard seam appears. The healing brush (Photoshop 7, 2002) fixes exactly that by transferring the source's texture and detail while re-matching the destination's color and illumination: its effect is related to transferring source gradients and adapting low frequencies to the surround, as in gradient-domain / Poisson editing (cross-ref Chapter 10.8, Poisson image editing, 💡 L10.4). The relationship is the one we already drew: Pérez, Gangnet & Blake's gradient-domain cloning Pérez et al. 2003 was inspired by the healing brush and gave it its clean formulation (Georgiev 2005 later recast it as a covariant-derivative correction).

fig-clone-heal-brush
Figure 21.1.12. Clone vs heal, interactively. The clone brush copies pixels verbatim from a chosen source; wherever source and destination differ in tone or color, a hard seam appears. The heal brush copies the source's texture but re-matches the destination's local tone (a cheap stand-in for the gradient-domain re-leveling described above), so the patch melts in. Drag the green dot to pick what to copy, drag on the photo to paint, switch brushes, or upload your own image; reset restores the original.

Criminisi, Pérez and Toyama 2004 automated the clone brush into full object removal. The texture-synthesis idea (copy the best-matching patch) is sound, but applied in a careless order it fails on the very thing that matters in photographs: linear structure. If you fill flat regions before edges, a wall's edge or a horizon line arrives at the hole from two sides and the two extensions do not meet; the structure breaks. Criminisi's contribution is a fill order that processes the hole's boundary in a deliberate sequence, by assigning each boundary patch a priority

$$ P(p) \;=\; C(p)\,D(p), $$

the product of a confidence term $C(p)$ (how much of this patch's neighborhood is already reliably known, so we fill the most-surrounded regions first) and a data term $D(p)$, how strongly an image structure (an isophote, an edge) runs into the hole at $p$. The data term does the work: it pushes patches where a strong edge meets the boundary to the front of the queue, so linear structures are continued across the hole before the flat regions on either side are filled in (Figure 21.1.13). The effect is that a removed person's hole is closed by sand and sea that meet correctly at the horizon, not by two mismatched halves.

The limits are the limits of self-similarity itself: the search is slow and confined to this image's content. PatchMatch (Chapter 21.2, Patch match) removes the slowness, making the search interactive; a database (Hays & Efros) or a learned net removes the "only this image" ceiling.

fig-criminisi-priority
Figure 21.1.13. Criminisi's fill order on a photograph: a tree removed from across a hilltop line. The priority $P(p)=C(p)D(p)$ on the front of the hole, snapshots at 20, 50 and 80 percent, the finished fill, and the fill order, whose first-filled band runs along the hilltop line: the edge is carried across the hole before the sky and the slope on either side are filled. Enlarged, a small step in the hill line and a few faint leaf-colored specks in the sky show where the method's greedy patch copies fall short. The search is limited to a window around each target.

Criminisi's method fills the hole once, patch by patch, and a patch placed early is never revisited. The completion used with PatchMatch instead treats the whole hole as the unknown of one optimization and refines all of it at every iteration, from a coarse level of an image pyramid to the finest. The hole-filling view of the same figure paints the hole and shows the fill at every level.

fig-patchmatch-applications
Figure 21.2.8, the hole view (reproduced). Object removal by coarse-to-fine completion with PatchMatch. Paint over what to remove; at each pyramid level the hole's patches are matched to patches outside it, then every hole pixel becomes the weighted average of the matched patches that overlap it. The right panel shows, for each hole pixel, the offset to the patch it was copied from; large blocks of one color are regions copied whole.

21.1.5 Data-driven scene completion (Hays & Efros)⧉

Every method so far is trapped inside one image: if this photo does not contain the right grass, sky, or building façade, self-similarity has nothing to copy. Hays and Efros 2007 turned the simple idea of using another image into a system. To fill a hole, they retrieve, from millions of photographs, scenes whose surround matches the area around the hole, then take a good match, align it, cut a region out along a graph-cut seam, and Poisson-blend it into place. The completion is borrowed wholesale from a photo that was never of this scene at all.

The prior is a large image database. With a large enough database, some photograph already contains a plausible thing to put in the hole (a stretch of sky with the right clouds, a building wall of the right kind) so you retrieve the answer instead of modeling it. This is the non-parametric, copy-from-an-example prior taken to web scale: the exemplar is no longer a patch of this image but a region of some image.

The cost is steep: the grafted region is from a semantically different scene. The result is coherent and photoreal, but it is not this scene's truth: the clouds are real clouds, just not the clouds that were there. It is a clear demonstration of L7.8's "plausible ≠ measured." And it needs a genuinely huge database plus reliable scene matching to find a graft that fits.

Scene completion is also the conceptual hinge to deep learning. Both database retrieval and learned generation draw on collections of images. A generative network learns a distribution from its training data and can synthesize a completion without retrieving a particular stored photograph. Its generalization and failure modes therefore differ from nearest-neighbor retrieval, even when the two use similar training collections.

21.1.6 Deep inpainting (context encoders → partial/gated conv → diffusion)⧉

The strong end of the ladder replaces the hand-built prior with a learned one (💡 L7.6, Chapter 7.3, Deep learning): a network trained to regenerate masked regions, having seen enough images to know what plausibly belongs in a hole.

Context encoders Pathak et al. 2016 were an influential early convolutional neural network (CNN) inpainting method, an encoder–decoder trained with a reconstruction loss plus an adversarial loss (a generative adversarial network (GAN) discriminator pressuring the fill to look real). They captured semantics that no copy-based method could (completing a face with a plausible eye, not a patch of skin) but early versions were blurry, because a reconstruction loss averages over the many plausible completions.

Partial and gated convolutions Liu et al. 2018; @yu-etal-2019|Yu et al. 2019 fixed a quiet but crippling nuisance: a vanilla convolution applied near a hole mixes the hole's garbage masked pixels into its output, poisoning everything downstream. Partial convolution renormalizes each output by the fraction of valid (known) pixels in its receptive field and shrinks the hole layer by layer; gated convolution goes further and learns a soft per-pixel mask gate. The result is that it handles free-form holes of any shape: the practical "select this object and remove it" workhorse behind modern editing tools.

A second fix targets the size of the hole. A network with a small receptive field cannot relate structures on opposite sides of a large mask. LaMa (Suvorov et al. 2022) uses fast Fourier convolutions to make global context available early, along with a loss with a large receptive field and large training masks. Its spectral branch is not merely an ordinary local convolution computed by FFT: it processes Fourier features and maps them back into spatial features. This helps with periodic structures and large masks, but does not guarantee a correct completion at arbitrary size. One-tap object-removal products use learned inpainting, sometimes with generative components, but their public interfaces do not establish that they use LaMa or any particular research architecture. Google's phone tools show the progression from the user's side: Magic Eraser (2021) removes a selected distraction and fills the hole with a plausible continuation of the background, while Magic Editor (2023) uses a generative model to fill larger regions and can also move or resize the subject, which requires synthesizing what was behind it. The common task is to detect or select an object, mask it, and synthesize a compatible replacement.

Diffusion inpainting Lugmayr et al. 2022, RePaint is the strongest prior and closes the loop back to Chapter 7.5, Image priors. Treat the hole as a measurement constraint (the known pixels are the measurement, the masked pixels are unknown) and run a pretrained diffusion model's denoising loop, but at every step pin the known region to the real pixels (renoised to that step's noise level) while letting the network fill the rest:

$$ x^{\mathrm{known}}_{t-1}\sim q(x_{t-1}\mid x_0^{\mathrm{known}}),\qquad x^{\mathrm{gen}}_{t-1}\sim p_\theta(x_{t-1}\mid x_t),\qquad x_{t-1}=M\odot x^{\mathrm{known}}_{t-1}+(1-M)\odot x^{\mathrm{gen}}_{t-1}. $$

Here $M=1$ on known pixels, $q$ is forward noising to the indicated time, and $p_\theta$ is the learned reverse transition. RePaint also uses a resampling schedule with forward jumps and repeated reverse steps to improve agreement across the mask boundary.

This shares the separation of data constraints and learned image structure used in plug-and-play methods, but is not identical to RED optimization or a guarantee of exact posterior sampling (💡 L7.7) with the forward operator set to "keep the known pixels": the data-fit step re-injects the measurement, the denoise step imposes the prior, and the loop alternates, the same recipe as super-resolution and deblurring, with masking as the measurement. The architectures themselves live in Chapter 7.3, Deep learning / Chapter 7.4, Generative AI and diffusion; here they are simply the strong-prior end of the spectrum.

The caveat is strongest here, because this is where it bites: the learned prior is the most flexible and the most prone to hallucinate. It will confidently invent a face, a license plate, a sign's text that were never measured and may be wrong. An inpainted region is not evidence, a forensic and ethical line this part has drawn since super-resolution L7.8.

21.1.7 Highlight / specular recovery⧉

A last, less obvious instance closes the chapter, because it is inpainting in disguise. A blown highlight (a glaring specular spot on metal, a sky clipped to pure white, a sunlit window) is a region where the sensor saturated: one or more channels reached their ceiling, so the value above that threshold was not recorded. Saturation still supplies a lower-bound constraint, so a clipped region is not quite an unconstrained hole, and highlight recovery is inpainting it: filling the saturated pixels with a plausible surface color using the surroundings and whatever was not clipped as the prior (Figure 21.1.14).

fig-highlight-recovery
Figure 21.1.14. Highlight recovery on a camera original, shown as photosites. In a sky crop the green photosites clipped in about 6% of the pixels while red and blue clipped nowhere. White balance (red × 2.09, blue × 1.62) turns the clipped cloud magenta, because green stays at its ceiling while the other two are raised. Green is then rebuilt from the measured red and blue using the local ratio of unclipped cloud, and never set below full scale, since clipping says the true value was at least that. Measured and rebuilt pixels are marked. Each picture enlarges on its own.

There are two complementary angles. The first is specular–diffuse separation: a highlight is the specular reflection added on top of the underlying diffuse surface, so recovering the diffuse layer beneath it is the goal (cross-ref the illumination-in-a-single-image material). The second, and more directly inpainting-flavored, is per-channel clipping recovery: highlights rarely clip all three color channels at once, so when only the red channel has saturated, the unclipped green and blue still carry two channel measurements and local structure, but do not determine the missing red or full hue, and the missing red can be extrapolated from them and from the surround, a more constrained but still ambiguous inpainting whose prior is "the channels move together."

It lives here for the same L7.8 reason as everything else: recover what the measurement destroyed, destroyed by clipping rather than by masking, but unmeasured all the same. And it has a grounded, non-inpainting cousin: rather than guess the highlight, capture it with a shorter exposure and merge, the high-dynamic-range (HDR) move of trading a guess for a measurement (forward-ref Multiple exposure imaging). Inpainting the clip is what you do when you only have the one frame.

21.1.8 Epitomes: a compact patch model⧉

The exemplar methods above treat the image itself as the dictionary of patches to copy from. Epitomes (Jojic, Frey & Kannan 2003) instead learn a miniature (an "epitome" a fraction of the image's size) fit so that every patch of the original has a close match somewhere inside it. It is a generative patch model: the full image is explained as a quilt of overlapping epitome patches, and the usual tasks fall out of "find each patch's place in the epitome and read back a clean (or completed) version": denoising, inpainting, super-resolution, texture transfer. It is the same patch-nearest-neighbor idea that drives texture synthesis and Chapter 21.2, Patch match, but with a learned, compact dictionary in place of the raw image, and so the pre-deep cousin of today's learned patch and latent priors (L7.6 / L7.7), a bridge from exemplar copying to the modern generative models of Chapter 7.4, Generative AI and diffusion.


In CompPhotoRoom

The Remove tool of Chapter 8.13, A raw developer in the browser is this chapter's machinery at interactive size: click a blemish, and source selection plus a feathered fill do what the pseudocode here says.

Recap: big lessons of this chapter

💡 Big lesson (recurrence of (L7.8))

The prior is not optional, and filling a hole is the purest case. There is zero data inside the hole, so 100% of the answer comes from the prior. Smoothness, self-similarity, a photo database, a learned net: each is a different prior, and the result is exactly as good (and as truthful) as the prior is. The split that organized super-resolution survives intact: a prior is reconstruction-leaning where it copies genuine structure from somewhere real, and hallucination where it invents detail that was never anywhere. (Registered as L7.8; developed in Chapter 14.1, What can super-resolution actually do?; here taken to its extreme: no data, all prior.)

💡 Big lesson (L10.5)

The quilting seam is a graph cut; the energy is the design. Stitching two overlapping patches by choosing the minimum-error boundary through their overlap is exactly discrete optimization on a graph: a dynamic program in one dimension (quilting), a min-cut/max-flow in two (GraphCut textures, Kwatra et al. 2003). The optimizer is off-the-shelf; what you design is the energy: match cost (does this patch belong here?) plus seam cost (is the boundary visible?). The same machinery cuts panorama and photomontage seams and segments objects; texture synthesis is one more instance. (See L10.5, image edits as discrete optimization on a graph; first placed in Chapter 10.9, Seam optimization.)