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 ↓

15.1 Basic traditional demosaicking⧉

This chapter covers the hand-designed reconstructions, the ones that encode their assumptions about images in the algorithm itself. They start from the layout of the Bayer tile Bayer 1976: the two green-filtered photosites of each 2×2 block sit on one diagonal, red and blue on the other (Figure 15.0.1, at the start of Part 15, Demosaicking and joint methods). Each filter has a broad spectral response, so a photosite records a camera-color component, not a wavelength.

Green gets the extra samples because camera green correlates strongly with brightness, where the eye wants the most detail. It is not colorimetric luminance, though; Chapter 15.3, Mosaics and aliasing makes the split between a combined brightness signal and color explicit.

The crux of the whole problem is that the three colors are measured in different places. Red sits on one sparse grid, blue on another shifted by (1,1) pixel diagonally, green on a denser checkerboard, and no two of them coincide. When the scene has a sharp edge, the three channels cross it at slightly different pixels, and such misregistration is the source of most demosaicking artifacts: zippering, false color and color moiré all come from it, while noise amplification and aliasing already present in the samples have other causes.

We will illustrate demosaicking with a scene composed of one dark corner on a light background (Figure 15.1.1). It starts from the mosaic itself.

fig-demosaick-corner-steps
Figure 15.1.1, stage 1. Interactive. One corner through three ways of demosaicking, cell by cell, shown in stages through this chapter. A dark corner on a light background is area-sampled onto 8 × 8 cells and kept through an RGGB mosaic; by default its first dark cell is a measured green, and every cell prints its value. Each stage is one technique and one step in it, and the three techniques' final results (downsampling, bilinear and green-based) sit at the right with their largest error, the current one framed, each appearing once its last step has been reached. The first stage is the input: each cell keeps only its own channel, so the three colors see the corner on three different grids. Interactive: step through the stages, drag and turn the corner, and switch the scene between black on white, red on white and blue on yellow.

Test a synthetic constant field first, including unequal constants such as $(0.2,0.4,0.7)$: a correct reconstruction returns each channel's own constant, including at the borders. A real neutral target need not give equal raw channel values before white balance. Then use an achromatic rectangle and a colored edge, and test all four Bayer phases. Those cases expose indexing errors, zippering and failures of the cross-channel assumption.

Keep in mind where demosaicking sits in the pipeline. Raw values are linear in light, and demosaicking works on them in that form; the color-difference methods below depend on it. Its output is therefore still linear camera RGB, not a picture ready for a screen. Before display, it still needs the color matrix to a standard RGB space, usually a tone curve that gives the camera its look (the beauty-curve chapter), and the gamma encoding the display system expects (Chapter 4.3, Encoding: linear, gamma or log). Shown directly, a linear demosaicked image looks dark and flat.

15.1.1 Downsampling and bilinear interpolation, and how they fail⧉

Two simple reconstructions come first, and their failure illustrates the demosaicking challenge.

The simplest is not to interpolate at all. Make each 2 × 2 tile one pixel, taking its red, the mean of its two greens and its blue. Every output value is a measurement, the two greens are averaged, which lowers their noise, and the operation costs almost nothing, which is why raw decoders such as dcraw offer it as a half-size mode and fast previews often use something like it. The first price is resolution: the image has half as many pixels in each direction, so detail that the sensor recorded is thrown away. And downsampling unfortunately does not remove the underlying problem: The red, green and blue of a tile were still measured at different positions, so a tile that an edge cuts in two combines red from one side with blue from the other and gets a color the scene does not have (Figure 15.1.1, stage downsample).

fig-demosaick-corner-steps
Figure 15.1.1, stage 2. The same corner, downsampled. Each 2 × 2 tile becomes one pixel with its red, the mean of its two greens and its blue. Nothing is interpolated, but the resolution halves, and a tile cut by the edge gets a color the scene does not have: orange here, largest error 1. Interactive: the same controls at every stage.

The same happens on real pictures and on test charts, where the comparison with the scene is side by side (Figure 15.1.2, stage downsample).

fig-demosaick-compare
Figure 15.1.2, stage 1. Downsampling against the scene. Left of the split, the scene; right of it, the scene through a Bayer mosaic and reconstructed by one method, here downsampling, with its PSNR. The corner of Figure 15.1.1 opens turned by 20 degrees and zoomed in on its tip; the other inputs are a zone plate, the Prudential tower, a NO PARKING sign from a real raw file, Stata, the artifact target of Figure 15.1.6, the Golden Gate, and fine colored stripes that make mazes. The zone plate and the star are band-limited: their finest rings and spokes fade to gray at the sensor's sampling limit, so the ground truth carries no aliasing of its own. The buttons above switch the method: the mosaic itself, downsampling, bilinear, green interpolated along edges, green-based, AHD, residual interpolation (RI) and adaptive residual interpolation (ARI). Interactive: the split follows the pointer and a click parks it, with a magnifier on the pointer; pick the input by its thumbnail, set the corner's angle and orientation, the sensor noise, or the resolution, which gives the sensor fewer pixels over the same scene; pinch or hold ⌘ or ctrl while scrolling to zoom, drag to pan, double-click for the whole picture, and switch the method with the buttons.

Bilinear interpolation keeps the full resolution instead. It treats the three color channels as three independent, sparsely sampled images and interpolates each one. For a red pixel missing green, average the four green neighbors; for a green pixel missing red, average the two horizontal or two vertical red neighbors, and so on.

It is a useful inexpensive baseline. It reproduces some smooth signals well, but its errors on fine structure illustrate the consequences of sparse color sampling.

fig-demosaick-corner-steps
Figure 15.1.1, stage 3. Bilinear green. Each missing green is the average of its four green neighbors, which gives .25 and .75 along the edge of a scene that holds only 0s and 1s. Interactive: the same controls at every stage.

Zippering is an alternating light-dark pattern along a sharp horizontal or vertical edge. It comes from averaging across the edge: at one pixel the four neighbors are two on each side, at the next they are three and one, so the interpolated value alternates. The name comes from what it looks like. The zipper is a form of aliasing (Figure 15.1.3). Green's samples lie on a checkerboard, and a checkerboard is itself a regular square grid, of spacing $\sqrt2$ pixels and turned by 45 degrees, a pattern called a quincunx. To that turned grid, a horizontal or vertical edge of the scene is a diagonal line, and a regular grid samples a diagonal line as a staircase, the same jaggies a diagonal shows on a screen. Bilinear interpolation reconstructs the staircase rather than the edge, and the steps of the staircase are the zipper's teeth.

fig-green-quincunx
Figure 15.1.3. Interactive. The green channel is a turned grid. Left, the corner's green samples on the Bayer grid: square cells, green on the checkerboard, the red and blue sites empty. Right, the same samples as a grid of their own: each green sample owns the diamond whose corners are the four red and blue sites around it, and the diamonds tile the plane with nothing missing. The numbers are the same in both panels. Turned 45 degrees, the green lattice is an ordinary square grid, and the corner's straight edges run diagonally across it as a staircase: to the green channel, the horizontal and vertical edges of the scene are 45-degree lines, and they alias like one. Interactive: turn both panels from 0 degrees, where the Bayer grid is axis-aligned, to 45 degrees, where the green lattice is; drag the corner; hide or show the values.

False color is a colored fringe on fine detail that is gray in the scene. It happens because the three channels are sampled at different positions, so a thin structure lands on a red pixel and misses the blue ones, and the interpolation reports a color the scene did not have.

fig-demosaick-corner-steps
Figure 15.1.1, stage 4. Bilinear red and blue: from two neighbors at a green site, from the four diagonal ones at the other color's site. The three planes are interpolated from different cells, so they disagree at the edge and the corner comes out fringed, largest error .5. Interactive: the same controls at every stage.

Maze-like patterns often involve unstable direction choices in adaptive methods. They should not be attributed to a bilinear filter changing its interpolation direction.

These artifacts can involve aliasing, cross-channel disagreement and interpolation rules. Looking at their appearance helps diagnosis, but does not uniquely identify the algorithm or cause.

Concretely, for green, whose measured samples lie on a checkerboard, the four nearest measured neighbors sit above, below, left and right:

$$ G_\text{new} = \tfrac14\,\big(G_\text{up} + G_\text{down} + G_\text{left} + G_\text{right}\big). $$

Red and blue are sparser, so the geometry varies: at some empty sites the nearest red samples are the four diagonal ones, at others the two horizontal or two vertical. The principle is the same, and it is a small tent kernel applied per channel.

The zipper is worth doing with numbers once. Take a vertical black-and-white edge, value 0 against value 1, and look only at green. At a missing green site just on the white side, three green neighbors can be white and one black, giving 0.75 instead of 1, while the measured green site beside it stays at 1. The reconstruction therefore alternates between measured and interpolated positions, which is exactly the run of teeth. On the black side the same averaging lifts missing values above zero.

The corner showed the same numbers along a whole edge (Figure 15.1.1, stages bilinear-green and bilinear-rb).

fig-demosaick-real-bayer
Figure 15.1.4, stage 1. A real mosaic, demosaicked by bilinear interpolation: the NO PARKING sign from problem set 3, a genuine BGGR mosaic straight from the sensor. First, the mosaic, each pixel painted in its filter's color. Then naive per-channel bilinear interpolation, with colored fringes along every letter, color that is not in the sign, because red, green and blue are sampled at different positions. The last picture enlarges the lettering. The sign is black on white, so the correct answer is known without a reference image: gray everywhere. Each picture enlarges on its own.
fig-demosaick-before-after
Figure 15.1.5. Demosaicking, naive versus edge-aware. Left, the Bayer mosaic input. Center, per-channel linear interpolation: colors are right in smooth areas but strong edges show two artifacts: a zipper (the alternating light/dark teeth running along a near-horizontal or near-vertical edge) and color fringing (spurious colored speckles, e.g. orange and cyan, straddling a black-and-white boundary). Right, an edge-aware, green-guided result: the zipper and most of the fringing are gone and the edge stays crisp. The artifacts come from interpolating across an edge instead of along it, and from the three channels being sampled at different locations.
fig-demosaic-artifact-zoo
Figure 15.1.6. Interactive. The three artifacts, labeled and reproducible. A synthetic target with a sharp edge, a thin gray line and a fine radial pattern is mosaicked and interpolated bilinearly, at a zoom where zippering, false color and the maze are each unmistakable, each marked by a numbered marker with a legend beside the image. A scale slider shrinks the target toward the sampling limit and the reader watches each artifact appear at the spatial frequency that produces it. Real photographic crops with the same three artifacts sit beside the synthetic ones. Interactive: choose the target or a real crop, set the feature scale and the edge orientation, toggle each artifact's marker, and switch between the mosaic, the interpolation and the error.

15.1.2 Interpolate along the edge, not across it⧉

The first classical idea: decide, per pixel, which direction is safe to interpolate in.

For a missing green value, compute a horizontal and a vertical gradient from the available samples, and interpolate along whichever direction varies less. The test goes back to Robert Hibbard's Kodak patent Hibbard 1995 (Kodak patent): compare the differences between the green neighbors horizontally and vertically, and interpolate horizontally, vertically or from all four neighbors depending on which gradient falls below a threshold. Hamilton and Adams's method (Hamilton & Adams 1997) does this with a test that uses both the green neighbors and the same-color neighbors two pixels away, which is what lets a red pixel estimate the second derivative of the red channel and use it to correct the green interpolation.

$$ \hat G = \frac{G_{\text{left}} + G_{\text{right}}}{2} + \frac{2R_{\text{center}} - R_{\text{left2}} - R_{\text{right2}}}{4} \quad \text{when the horizontal gradient is smaller.} $$

The second term is the correction that makes this work, and it is the second classical idea arriving early: it uses the red channel's curvature at the center to sharpen the green estimate, on the assumption that the channels share their structure.

Variants differ in how the direction is chosen. Variable number of gradients (Chang, Cheung & Pan 1999 (VNG)) computes gradients in eight directions and averages the ones below a threshold, which handles corners and diagonals better than a two-way choice. Adaptive homogeneity-directed interpolation (Hirakawa & Parks 2005 (AHD)) reconstructs the image both ways, converts both to a perceptual color space, and keeps the version that is locally more homogeneous in color, which is a defensible criterion because the eye tolerates luminance detail and not chrominance noise.

The plain two-direction form of the same idea is worth seeing before the correction term arrives:

$$ G_\text{new} = \begin{cases} \tfrac12\,(G_\text{up} + G_\text{down}) & \text{if } |G_\text{up} - G_\text{down}| \le |G_\text{left} - G_\text{right}| \\[4pt] \tfrac12\,(G_\text{left} + G_\text{right}) & \text{otherwise.} \end{cases}$$

Interpolate along whichever axis its two neighbors look most alike, on the bet that the other axis crosses an edge. The bet rests on the world being piecewise smooth: mostly flat regions separated by a relatively small number of strong, roughly one-dimensional edges. Get those right and the cases the eye notices are handled. A tie can take the four-neighbor average rather than preferring an axis, and continuous directional weights avoid abrupt switches. Fine periodic texture can make both directional differences misleading, and noise can flip the choice.

On the corner, the rule picks the right pair at every cell (Figure 15.1.1, stage edge-green).

fig-demosaick-corner-steps
Figure 15.1.1, stage 5. Edge-directed green on the corner. At each red or blue site the rule averages the green pair across which green changes least; the mark in each cell shows the direction it chose. On this corner the estimate is exact everywhere. Interactive: drag the corner so its first dark cell is red or blue, and the estimate misses at the corner.
fig-demosaick-green-choice
Figure 15.1.7. Edge-directed green, input beside output. Left, green as measured: the green-ringed checkerboard, and the red and blue sites where it is missing. Right, green everywhere: each missing cell is filled with the average of the green pair across which green changes least, along the edge rather than across it, and marked as interpolated; where the two differences are equal, in flat areas, all four neighbors are averaged and no pair is drawn. The pairs are drawn in both panels, blue for horizontal and orange for vertical. The input can be the corner of Figure 15.1.1, black on white or on colored backgrounds and turned by any angle, or the problem-set raw mosaic of the NO PARKING sign, where the pairs run along the edge of the sign's border. Interactive: choose the input, drag the corner or pan the sign, turn the corner, change the zoom, and hide or show the values.
fig-demosaick-real-bayer
Figure 15.1.4, stage 2. The NO PARKING sign again, now with green-based (color-difference) demosaicking beside bilinear: green first, interpolated along edges, then red and blue as differences from it. The fringes on the lettering are almost gone and the edges stay gray, which is what the sign's black and white require.
💡 Big lesson (L15.1) — let the data pick the direction

Edge-directed demosaicking is a first, very concrete instance of a theme that returns throughout the book: adapt the operation to the local structure of the image rather than applying the same fixed filter everywhere. Here the structure is an edge and the adaptation is choosing the interpolation direction; later, in edge-preserving filtering and denoising, the same instinct (use the similarity between pixels to decide how much they should influence each other) becomes the bilateral filter and the idea of affinity. Whenever a fixed linear filter blurs across something it should have respected, the cure is to look at the data first.

Algorithm 15.1.1
# edge-directed green interpolation, the Hamilton-Adams test
dH = abs(G[x-1,y] - G[x+1,y]) + abs(2*R[x,y] - R[x-2,y] - R[x+2,y])
dV = abs(G[x,y-1] - G[x,y+1]) + abs(2*R[x,y] - R[x,y-2] - R[x,y+2])
if   dH < dV: G_est = (G[x-1,y]+G[x+1,y])/2 + (2*R[x,y]-R[x-2,y]-R[x+2,y])/4
elif dV < dH: G_est = (G[x,y-1]+G[x,y+1])/2 + (2*R[x,y]-R[x,y-2]-R[x,y+2])/4
else:         G_est = average_of_both

This pseudocode is for a red center; exchange red and blue at a blue center. Copy measured green samples in noiseless interpolation and use a CFA-aware boundary extension that preserves color parity. Malvar–He–Cutler provides a useful fixed linear alternative with cross-channel Laplacian corrections (Malvar et al. 2004); it differs from selecting a direction per pixel.

Edge-directed green is the simplest case of a more general idea, steered reconstruction (Chapter 6.5, Resampling and upsampling). Reconstructing a sampled image means choosing, at each output pixel, weights for the nearby samples: a reconstruction kernel. A fixed kernel, like bilinear's, uses the same weights everywhere, so it blurs across edges and aliases along them. A steered kernel is shaped by the local image, long along an edge and narrow across it, so it averages where the signal is smooth and keeps the change sharp (Figure 6.5.19). The two-direction green rule is its coarsest version: two candidate kernels, one horizontal and one vertical, and a gradient test to choose between them. The variants above refine the choice. VNG considers eight directions, AHD compares whole reconstructions, and the Hamilton–Adams correction adds information from the other channels to the kernel it chose.

fig-sampling-steered-recon
Figure 6.5.19 (reproduced). Steered reconstruction, from the resampling chapter. The reconstruction kernel is an ellipse you aim by hand: aligned along an undersampled edge, it removes most of the staircase; misaligned, it brings the aliasing back; a round kernel only blurs it. Edge-directed green interpolation is the coarsest version of the same idea, a choice between a horizontal and a vertical kernel made by a gradient test.

Working out the local reconstruction weights from the local pattern is the key idea of traditional demosaicking, and it reaches beyond it. Kernel regression makes the steering continuous, estimating an orientation and an elongation at every pixel from the local gradients (Chapter 10.7, Locally adaptive regression kernel (LARK)). RAISR learns a bank of filters and picks one per pixel by hashing the local gradient pattern (Chapter 14.4, Single-image super-resolution: classical methods); applied to a mosaic, the same scheme would amount to a learned gradient test. None of these methods writes down an image prior, yet each encodes one in the rule that chooses the weights: that the image is smooth along some direction at every pixel. Chapter 15.6, Learned demosaicking replaces the hand-written rule with one fitted to data.

15.1.3 Interpolate differences, not channels⧉

The second classical idea does more work than the first and is less often stated plainly.

Color differences can vary more slowly than individual channels, especially near neutral structure. They are not invariant to shading: if $R=rI$ and $G=gI$ for a common shading signal $I$, then $G-R=(g-r)I$, which varies unless $g=r$. The ratio $R/G=r/g$ is constant under this simplified model, but becomes unstable near zero green and changes at material or illumination boundaries.

So interpolate the difference. Estimate green everywhere first, since it is sampled twice as densely, then form $G - R$ at the red pixels, interpolate that smooth quantity, and recover red as $\hat R = \hat G - \widehat{(G-R)}$. The hard, high-frequency part of the problem is solved once, in the channel with the best sampling, and the other two channels inherit it.

On the corner, the differences are formed at the red and blue sites (Figure 15.1.1, stage differences).

fig-demosaick-corner-steps
Figure 15.1.1, stage 6. The color differences R − G and B − G at the red and blue sites, against the edge-directed green. On the gray scene they are zero wherever that green is exact. Interactive: switch to a colored edge and the differences stop being zero.

Green-first reconstruction is common, but other joint and frequency-domain methods do not follow that ordering. Difference interpolation fails when the assumed shared structure is weak: strongly colored edges, channel clipping and chromatic displacement are important cases.

As a recipe, in four steps:

  1. Reconstruct green everywhere, using the edge-directed method above, since green has the densest sampling.
  2. Form the difference $R - G$ at every pixel where red was measured, and $B - G$ at every measured blue pixel, which is possible now that green exists everywhere.
  3. Interpolate the difference with plain linear interpolation and no edge logic, because the difference is usually the smoother quantity. Sharp color boundaries remain the failure case.
  4. Add green back: $R = (R-G) + G$, and likewise for blue.
fig-demosaick-corner-steps
Figure 15.1.1, stage 7. The differences interpolated bilinearly to every cell, the same rule as bilinear green, now applied to a quantity that is nearly constant across the edge. Interactive: the same controls at every stage.

An achromatic edge has zero true color differences, so this keeps it gray, but only if the reconstructed green is also accurate at the measured red and blue sites. Green errors leave residual false color even on that simple target.

fig-demosaick-corner-steps
Figure 15.1.1, stage 8. Green added back, the end of green-based (color-difference) demosaicking. On the gray scene the differences are zero wherever green is exact, so the result is exact: largest error 0, against .5 for bilinear and 1 for downsampling. Drag the corner so its first dark cell is red or blue and the green estimate misses at the corner, and the error follows it into all three channels. On a colored edge the differences are no longer constant across it: on red on white the method still beats bilinear (.35 against .45), on blue on yellow it does worse (.63 against .43). Interactive: step back through the stages to see where each number comes from, drag and turn the corner, and change the scene.
fig-demosaick-bw-artifacts
Figure 15.1.8. Color out of a black-and-white scene: the cleanest way to see the problem and the fix. The target is purely achromatic (black shapes on white), so a correct demosaicker must keep it gray. Naive bilinear does not: orange and cyan fringes appear along every edge, plus zipper teeth: color invented from nothing, because red, green, and blue are sampled at different pixel positions and a sharp gray edge lands a little differently in each channel. Green-based (color-difference) demosaicking keeps it almost perfectly gray: with $R-G$ and $B-G$ constant across the achromatic edges, interpolating them is exact and the fringe collapses. The zoom row is the same corner enlarged.
💡 Big lesson (L15.2) — luminance carries detail, chrominance is smooth

Channel differences are often smoother than luminance-like structure, which makes guided reconstruction useful. Preserve that distinction as an image model rather than a law: fine color texture and saturated boundaries can break it.

Even green-based, edge-directed demosaicking is not perfect. Fine, high-frequency color texture, a tiled roof, a striped shirt, a picket fence, breaks the smooth-color assumption and produces the residual false color often called maze or labyrinth artifacts, because at those scales the channels really are doing different things. That is where the modern methods earn their keep.

Where the maze comes from shows up best on a small patch of fine texture (Figure 15.1.9). At a pixel where the horizontal and vertical tests come out nearly equal, a little noise decides which one wins, so neighboring pixels choose differently, and the pattern of choices is the labyrinth.

fig-demosaick-maze
Figure 15.1.9. Interactive. Fine colored stripes, 4 pixels apart at 45 degrees, kept through an RGGB mosaic with a little sensor noise, and reconstructed by bilinear interpolation and by edge-directed green (Hamilton and Adams' test, with the curvature correction) followed by color differences. The right panel is the direction each red and blue site chose: at 45 degrees the two tests are equal by symmetry, so without noise every site ties, and with noise the choices split about evenly and flip from one site to the next, a labyrinth. In the picture it shows as broken, blotchy stripes and lost color, and the edge-directed result ends up worse than bilinear (mean error .123 against .103). Take the color out and the edge-directed error falls to under a third (.037): when the channels agree, the curvature correction makes either direction nearly right, and the maze stays in the choices. Interactive: set the stripe period and angle, the color in the stripes and the sensor noise; the patch is 64 pixels square, the central 32 shown.
fig-color-difference-smoothness
Figure 15.1.10. Interactive. The assumption, measured on a real photograph. Three panels: the red channel, the green channel, and their difference, each with a scanline plot under it. The channels are full of structure and the difference varies far less, until the reader drags the scanline across a saturated color boundary, where it does not. Each plot shows the standard deviation along the line for its channel or difference, on the full range of values; a small spread for the difference is evidence for the assumption along that line, not proof that the difference is smooth everywhere. A second view shows the demosaicking error with and without the difference trick on the same crop, which is the same information as a result rather than as an argument. Interactive: move the scanline, switch between $G-R$ and $G-B$, toggle the difference-based reconstruction, and hunt for the regions where the assumption breaks.

A red flower against green leaves is a useful counterexample to the shared-edge assumption: the boundary can have very different contrast in red and green. A clipped specular highlight breaks the linear relationship for another reason, because one channel stops increasing while another still responds. Lateral chromatic aberration breaks it geometrically by placing the same boundary at different coordinates. These cases need different remedies even when their demosaicked outputs all show colored fringes.

15.1.4 Demosaicking and other aspects of imaging⧉

Demosaicking is one part of an imaging system. The optics and the sensor decide what the mosaic records, and the stages of the camera pipeline around it (Part 8, Basic image processing and ISP) decide what it is handed and what happens to its output. Each method in this chapter and in Chapter 15.2, Advanced traditional demosaicking assumes something about its input, and these other parts decide whether the assumption holds.

The anti-aliasing filter. Many cameras place an optical low-pass filter in front of the sensor. It blurs the image slightly before sampling, so that less fine detail aliases into color moiré, the failure no demosaicker can fully undo because the mosaic cannot tell it from real color. Many recent cameras have weakened or dropped it and leave moiré to the demosaicker. Chapter 15.3, Mosaics and aliasing explains what the filter can and cannot remove, and why it has been disappearing.

Grouped layouts: Quad and Nona. Phone sensors that group same-color photosites in 2 × 2 (Quad-Bayer) or 3 × 3 (Nona-Bayer) blocks change what a demosaicker receives. In low light the sensor bins each block and hands over an ordinary Bayer mosaic at a quarter or a ninth of the pixel count, and the methods of this chapter apply unchanged. At full resolution the same color is repeated over neighboring sites, so red, for example, is missing from larger regions than in a Bayer mosaic, and the nearest red sample can be three photosites away. A Bayer demosaicker applied directly to that layout uses the wrong colors at most sites; the usual route is to remosaic the grouped layout into an estimated Bayer mosaic first, which is itself an interpolation, then demosaick, or to demosaic the grouped layout directly (Chapter 15.10, Quad-Bayer and Nona-Bayer processing).

Color routers. A nano-prism or metasurface color router keeps the Bayer layout but steers each band of light toward the photosite that measures it instead of absorbing it (Chapter 4.6, Sensing color: multiplexing strategies). Each photosite then collects light from a wider area than its own, which acts as a mild anti-aliasing filter (Chapter 15.3, Mosaics and aliasing), and part of each band lands on neighbors of the wrong color, which broadens and mixes the channels' spectral responses. The demosaicking algorithms still apply, but the color correction that follows has to undo more mixing.

Black level. The sensor adds an offset to every sample, and it has to be subtracted first. Color differences and ratios are meaningful only on values proportional to light: with the offset left in, a gray edge no longer has a constant $R-G$ across it, and the ratio model of the "difference or ratio?" sidebar is wrong everywhere.

Defects and lens shading. A hot or dead photosite is one wrong sample, and interpolation copies it into its neighbors' missing colors, so a single bad value becomes a colored speck several pixels across; repair it on the mosaic. Lens shading darkens the corners by a different amount in each channel, which demosaicking would read as color. Both are covered in Chapter 15.7, Neighboring problems in the raw stage.

White balance. The color-difference methods work best when a neutral surface has equal values in all three channels, because then the difference across a gray edge is zero on both sides and interpolating it is exact. Raw values under most lights are far from equal for a neutral, so many pipelines apply the white-balance gains to the mosaic before demosaicking (Chapter 8.10, Auto-exposure and auto white balance). The gains then scale clipped samples too: a highlight that clipped in green is no longer at a common ceiling once red and blue are multiplied, which is where pink highlights come from, so saturated samples need to be marked before the gains are applied.

Chromatic aberration. Lateral chromatic aberration shifts the color channels relative to one another toward the edges of the frame. The cross-channel assumption then fails exactly at edges, where it is needed, and a demosaicker reads the misregistration as a colored edge. Correcting it before or during demosaicking avoids fringes that are much harder to remove afterward (Chapter 22.2, Aberrations correction).

Noise. Interpolation mixes neighboring samples, so independent sensor noise becomes spatially correlated color noise, which is harder for a denoiser to recognize. Noise also flips the edge-direction decisions of this chapter. Denoising the mosaic first, or demosaicking and denoising together, avoids both (Chapter 15.8, Joint demosaicking and denoising).

After demosaicking. The color matrix, tone curve and sharpening all act on the reconstructed image, and sharpening in particular amplifies zippering and maze patterns along with real detail (Chapter 8.8, Sharpening). A demosaicker that leaves small artifacts can look acceptable until the rest of the pipeline has run, so judge it on the finished rendering as well as on its direct output.

The practical rule is to know which of these steps have been applied to the data a demosaicker receives, in what units, and to tell the algorithm.

And all of it should happen on linear values. Black-level subtraction, defect repair, lens-shading and white-balance gains, and demosaicking itself operate on numbers proportional to light, and the image becomes nonlinear only afterward, when the tone curve and the gamma encoding prepare it for display. The reasons are the ones this chapter keeps meeting. Interpolation averages, and an average of gamma-encoded values is not the encoding of the average light, so a demosaicker run on encoded values darkens fine detail and shifts colors at edges (Chapter 6.5, Resampling and upsampling). Color differences and ratios describe the scene only when the values are proportional to light. And the noise model that joint methods rely on is simple only in linear units, before a curve stretches the shadows and compresses the highlights.

Recap: big lessons of this chapter

💡 Big lesson (L15.1) — let the data pick the direction

Edge-directed demosaicking is a first, very concrete instance of a theme that returns throughout the book: adapt the operation to the local structure of the image rather than applying the same fixed filter everywhere. Here the structure is an edge and the adaptation is choosing the interpolation direction; later, in edge-preserving filtering and denoising, the same instinct (use the similarity between pixels to decide how much they should influence each other) becomes the bilateral filter and the idea of affinity. Whenever a fixed linear filter blurs across something it should have respected, the cure is to look at the data first.

💡 Big lesson (L15.2) — luminance carries detail, chrominance is smooth

Channel differences are often smoother than luminance-like structure, which makes guided reconstruction useful. Preserve that distinction as an image model rather than a law: fine color texture and saturated boundaries can break it.