A compact project that reconstructs full‑color images from a Bayer sensor mosaic using two classical approaches:
(A) local least‑squares regression on
$5\times5$ patches and (B) Gaussian weighted averaging. Both are compared against MATLAB’s built‑indemosaic, with RMSE reporting.
Digital cameras with a Bayer filter record one color per pixel (R, G, or B). Demosaicing infers the two missing colors at each pixel to produce an RGB image. This repo implements and compares two demosaicers:
-
Algorithm 1 — Patchwise Least‑Squares (LS): learn a linear filter for each Bayer phase that best predicts the center pixel’s missing color from its
$5\times5$ neighborhood. Coefficients are computed once from ground‑truth patches, then applied to a simulated mosaic. -
Algorithm 2 — Gaussian Weighted Average (GAUSS): fill missing colors by convolving a
$5\times5$ Gaussian kernel ($\sigma=1$ ) over local neighborhoods—simple, smooth, and fast.
Both pipelines generate a Bayer mosaic from a clean RGB image for controlled testing, using the pattern with B at
From a ground‑truth RGB image
This synthetic setup allows “apples‑to‑apples” evaluation: demosaic the mosaic and measure RMSE against the original RGB frame. The scripts also compare to MATLAB’s
demosaic(...,'bggr')(or...,'rggb'for raw inputs) and report RMSE.
For each of the four Bayer phases and for each output color channel, learn a linear filter that predicts the center pixel value from its local
Let
with coefficients
Here, columns of
What those symbols mean, practically: the vector
At inference time, for each pixel
Pseudocode (LS, single cell):
phase = f(r,c) # which of 4 Bayer offsets
P = M[r-2:r+2, c-2:c+2] # 5x5 neighborhood
for color in {R,G,B}:
if color is missing at (r,c):
w = W[phase,color] # learned 25-vector
Ihat[r,c,color] = sum(P .* reshape(w,5,5))
else:
Ihat[r,c,color] = M[r,c] # keep observed channel
Replace the learned filter with a fixed
Let
evaluated only for channels that are missing at fspecial('gaussian',[5,5],1) creates
Pseudocode (GAUSS, single cell):
phase = f(r,c)
P = M[r-2:r+2, c-2:c+2]
for color in {R,G,B}:
if color missing at (r,c):
Ihat[r,c,color] = sum(P .* G)
else:
Ihat[r,c,color] = M[r,c]
Metric. Root Mean Squared Error (RMSE) between the demosaiced image and ground truth:
computed channel‑wise and averaged (the helper routine follows exactly this).
Comparison to MATLAB demosaic. In tests, the Gaussian method often lands within about 0.07–0.12 RMSE of MATLAB’s built‑in algorithm—close for a simple baseline. The built‑in likely adds edge‑aware tricks that reduce artifacts further. The LS method typically preserves edges/fine detail better if the training patches are representative.
--
- GAUSS: simple, robust, and noise‑reducing; best when computational budget is tiny and scenes are not edge‑dense.
- LS: preserves edges/fine detail better by adapting to local color relationships—but costs more to train/apply.
-
algo_1_.txt— MATLAB for Algorithm 1 (LS): makes mosaic, builds per‑phase coefficient vectors by least squares, demosaics by convolving the learned$5\times5$ kernels, and reports RMSE & figures. See the generate coefficients routine and apply loop. -
algo_2.txt— MATLAB for Algorithm 2 (GAUSS): usesfspecial('gaussian',[5,5],1)and a neighborhood sum for missing channels; also computes RMSE and side‑by‑side plots.
- Both custom methods are credible baselines: GAUSS trails MATLAB by ~0.1 RMSE on average, while LS offers a path to edge‑aware quality if coefficients are well learned.
- The Bayer synthesis + RMSE harness makes it easy to swap in other demosaicers (edge‑directed, gradient‑based, CNNs) for fair comparisons.
MIT — see LICENSE.

