C++ Demosaicing Engine

with OpenCV and LibRaw

GitHub repository

Digital cameras capture images using sensors overlaid with a color filter array, most commonly the Bayer pattern. Each pixel records only one color channel — red, green, or blue — requiring a process called demosaicing to reconstruct a full-color image. As part of a recent project, I developed a C++ engine to simulate the Bayer pattern and perform demosaicing via OpenCV’s cvtColor function.

Demosaicing demonstration using a photo of my desk. (Left) Simulated RAW image from RGGB Bayer pattern. (Middle) Demosaiced image. (Right) Zoomed-in RAW image with overlaid Bayer pattern.

I used LibRaw to read real RAW images, enabling the engine to process actual sensor data. Demosaicing alone, however, is not sufficient to produce accurate color for real RAW images. Additional steps like black level correction, white balancing, and gamma adjustment are likely necessary.

Demosaicing a real RAW image. My Fujifilm X-A3 allows saving both .jpg and RAW (as .RAF) simultaneously. (Left) Real RAW image. (Right) Demosaiced image.

There could be future updates with other parts of the Image Signal Processing pipeline.