Demosaicing¶
| Fact | Value |
|---|---|
| Also called | Debayering, Bayer demosaicing, CFA interpolation, Malvar-He-Cutler demosaicing |
| Status | Implemented and tested |
| Family | Enhancement, illumination and color |
| Engine | Browser |
| Type | Linear, spatial, color filter array sampling and interpolation |
| Output | Image |
| Operator shown as | algorithm (exact) |
| Support radius | 2 for Malvar-He-Cutler, 1 for bilinear |
| Code | web/src/core/filters/enhancement/demosaic.ts, demosaicFilter |
| Operator code | web/src/core/filters/enhancement/demosaic.ts, DEMOSAIC_TAPS |
Overview¶
Most camera sensors record only one color at each pixel, through a mosaic of red, green and blue filters called a Bayer pattern. Half of the pixels see green, a quarter red and a quarter blue. Demosaicing rebuilds the two missing colors at every pixel. This filter first makes the mosaic from a full color image, then rebuilds it, so you can compare the result with the original.
How it works¶
- Keep at each pixel only the color that the Bayer pattern puts there.
- For each missing color, take the mean of the nearest pixels that recorded it.
- With Malvar-He-Cutler, add a correction from how the recorded color changes around the pixel.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(\hat G^{bl}, \hat R^{bl}\): The bilinear estimates
- \(\Delta_R, \Delta_G, \Delta_B\): Laplacians of the channel known at the pixel
- \(\alpha, \beta, \gamma\): The weights of the corrections
The hat with bl is the bilinear estimate. For G it is the mean of the 4 axial neighbors. For R or B at a green pixel it is the mean of the 2 nearest samples. For R at B or B at R it is the mean of the 4 diagonal samples. Malvar, He and Cutler add a Laplacian of the channel that is known at the pixel. Delta_R is the red value minus the mean of the 4 red samples 2 pixels away. Delta_G is a 9-point Laplacian of green and Delta_B a 5-point Laplacian of blue. Blue is estimated like red. They chose alpha, beta and gamma for the smallest mean squared error on the Kodak image set, rounded to dyadic fractions. The result is eight 5 x 5 filters with weights in eighths, as Getreuer's IPOL article shows them. Bilinear is the same without the correction.
Bilinear interpolation treats the three colors as unrelated. But in natural images the colors change together: an edge in red is usually an edge in green too. Malvar, He and Cutler use this. Say the red value at a pixel is above the mean of the red values around it. Then green there is probably above its bilinear mean too, by about half as much. The Laplacian of the known channel measures that difference.
The 5 x 5 filters, with weights over 8, are these. For G at a red or blue pixel, the center is 4, the four axial neighbors 2, and the four pixels two steps away on the axes -1. For R at a green pixel with red to its left and right, those two red neighbors are 4 and the center is 5. The four diagonals are -1, the two pixels two steps left and right are -1, and the two pixels two steps up and down are 1/2. With red above and below, the filter is turned by 90 degrees. For R at a blue pixel, the center is 6, the four red diagonals 2, and the four pixels two steps away on the axes -3/2. Blue works the same way.
Kernel or operator¶
The Inspector shows the mosaic step and the interpolation rule with the chosen pattern.
Algorithm¶
- Keep at each pixel only the channel that the Bayer pattern puts there.
- At each pixel, estimate each missing channel with the 5 x 5 filter for that case, or with the bilinear mean.
- Keep the recorded channel as it is.
Parameters¶
| Parameter | Default | Values | Meaning |
|---|---|---|---|
Pattern pattern |
RGGB | RGGB, BGGR, GRBG, GBRG | The colors of the top-left 2 x 2 block of the Bayer filter, row by row. |
Method method |
Malvar-He-Cutler | Malvar-He-Cutler, Bilinear | How the missing colors are estimated. |
Show output |
Demosaiced image | Demosaiced image, Bayer mosaic | Show the rebuilt image, or the mosaic that the sensor would record, each pixel in its own color. |
Implementation¶
demosaicFilter in web/src/core/filters/enhancement/demosaic.ts holds the filters as lists of taps in DEMOSAIC_TAPS. The pattern comes from the image row and column, so tiles and the whole image agree.
Worked example¶
| Method | PSNR (dB) | False color in the star |
|---|---|---|
| Bilinear | 22.28 | 25.48 |
| Malvar, He and Cutler | 26.26 | 15.98 |
The image of the figure, 240 by 120 pixels, through an RGGB mosaic and back. The PSNR compares all three channels with the original, without the 2 pixels next to the edge. The false color is the mean of |R - G| + |B - G| over the star, which is gray in the original, so every bit of it is an error.
Malvar, He and Cutler report a gain of more than 5.5 dB over bilinear on the Kodak images. On this test image the gain is about 4 dB, and the false color in the gray star drops by more than a third.
Complexity¶
- Time: \(O(N)\)
- Memory: \(O(N)\)
N is the number of pixels. At most 11 taps per missing channel.
Artifacts and limitations¶
Limitations
- The input is a full color image. A real raw file would also need black level, white balance and the camera's own pattern.
- Fine detail near the sampling limit still turns into false color and moire.
Artifacts
- Zipper patterns along sharp edges, strongest with bilinear.
- False color on fine gray detail.
Tests¶
The fixture cases come from a NumPy reference with the same filters, for all four patterns and both methods, and for the mosaic view. Before the cases are written, the bilinear result is checked against OpenCV's Bayer conversion away from the edges, for all four patterns. Both methods must also give back an image whose channels change linearly across it. Unit tests check the pattern positions, that each filter's weights sum to 8, and the mosaic view.
web/src/core/filters/filters.test.tsweb/src/core/filters/run.test.tsweb/src/core/filters/enhancement/cfa.test.ts
References¶
- Malvar, H. S., He, L.-w. and Cutler, R. High-quality linear interpolation for demosaicing of Bayer-patterned color images. IEEE International Conference on Acoustics, Speech, and Signal Processing, volume 3, 485 to 488, 2004. DOI 10.1109/ICASSP.2004.1326587.
- Getreuer, P. Malvar-He-Cutler Linear Image Demosaicking. Image Processing On Line 1, 83 to 89, 2011. DOI 10.5201/ipol.2011.g_mhcd.
- OpenCV documentation, color conversions, Bayer to RGB. https://docs.opencv.org/4.x/de/d25/imgproc_color_conversions.html