Main Content

demosaic

Convert Bayer image to truecolor image

Description

RGB = demosaic(bayer,sensorAlignment) converts the Bayer image, bayer, to the truecolor image, RGB. The sensorAlignment argument specifies the Bayer pattern.

A Bayer filter mosaic is a type of color filter array (CFA), or an arrangement of color filters on the photosensors of a single-chip digital camera. The color filters let each photosensor record only red, green, or blue data. A Bayer pattern is the repeating arrangement of the four color filters, consisting of two green, one red, and one blue filter, that comprise the Bayer filter mosaic.

A Bayer pattern image, or Bayer image, is the image captured by a camera with a Bayer filter mosaic. Demosaicing a Bayer image consists of combining the signals from the photosensors to form a 3-channel truecolor image, rather than a single-channel intensity image.

example

RGB = demosaic(bayer,sensorAlignment,BitsPerSample=bitsPerSample) also specifies the actual bit depth, bitsPerSample, of the input Bayer image. Use this syntax when the data contains fewer bits than the data type (since R2025a).

Examples

collapse all

Convert a Bayer pattern encoded image that was photographed by a camera with a sensor alignment of 'bggr'.

I = imread("mandi.tif");
J = demosaic(I,"bggr");
imshow(I)

Figure contains an axes object. The hidden axes object contains an object of type image.

imshow(J)

Figure contains an axes object. The hidden axes object contains an object of type image.

Read a color filter array (CFA) image and the metadata from a RAW file. This data was captured using a 12-bit sensor and is stored using data type uint16.

file = "colorCheckerTestImage.NEF";
cfa = rawread(file);
info = rawinfo(file);

To verify that this is 12-bit data, check the maximum pixel value. The maximum value is within the range of 12-bit data (2^12-1, or 4095).

maxValue = max(cfa(:))
maxValue = uint16

3366

The file metadata, which includes information about the value of white pixels, is also consistent with 12-bit data.

whiteValue = info.ColorInfo.WhiteLevel
whiteValue = 1×4 uint16 row vector

   3827   3827   3827   3827

Get the Bayer pattern from the metadata. Demosaic the image using 12 bits per sample.

sensorAlignment = info.CFALayout;
rgbout = demosaic(cfa,sensorAlignment,BitsPerSample=12);
imageshow(rgbout,DisplayRange=[0 2^12])

Input Arguments

collapse all

Bayer pattern encoded image, specified as an M-by-N numeric array. bayer must have at least 5 rows and 5 columns.

Data Types: uint8 | uint16 | uint32

Bayer pattern, specified as one of the values in the following table. Each value represents the order of the red, green, and blue sensors by describing the four pixels in the upper-left corner of the image (left-to-right, top-to-bottom).

Pattern2–by-2 Sensor Alignment
"gbrg"

Top left pixel is green, top right pixel is blue, bottom left pixel is red, and bottom right pixel is green.

"grbg"

Top left pixel is green, top right pixel is red, bottom left pixel is blue, and bottom right pixel is green.

"bggr"

Top left pixel is blue, top right pixel is green, bottom left pixel is green, and bottom right pixel is red.

"rggb"

Top left pixel is red, top right pixel is green, bottom left pixel is green, and bottom right pixel is blue.

Data Types: char | string

Since R2025a

Bits per sample, specified as a positive integer. The maximum value of the demosaiced image RGB is equal to 2^bitsPerSample – 1.

By default, bitsPerSample is equal to the number of bits of the data type of bayer: 8 for uint8 images, 16 for uint16 images, and 32 for uint32 images.

Example: 12 uses 12 bits per sample and sets the maximum value of the RGB image to 2^12 – 1, or 4095, for uint16 and uint32 Bayer images.

Output Arguments

collapse all

RGB image, returned as an M-by-N-by-3 numeric array of the same data type as bayer.

Algorithms

The demosaic function uses gradient-corrected linear interpolation to convert the two-dimensional Bayer image into the truecolor image.

References

[1] Malvar, H.S., L. He, and R. Cutler, High quality linear interpolation for demosaicing of Bayer-patterned color images. ICASPP, Volume 34, Issue 11, pp. 2274-2282, May 2004.

Extended Capabilities

expand all

Version History

Introduced in R2007b

expand all