Main Content


Construct full RGB pixel data from Bayer pattern pixels


visionhdl.DemosaicInterpolator provides a Bayer pattern interpolation filter for streaming video data. You can select a low complexity bilinear interpolation, or a moderate complexity gradient-corrected bilinear interpolation. The object implements the calculations using hardware-efficient algorithms for HDL code generation.

  • The object performs bilinear interpolation on a 3×3 pixel window using only additions and bit shifts.

  • The object performs gradient correction on a 5×5 pixel window. The object implements the calculation using bit shift, addition, and low order Canonical Signed Digit (CSD) multiply.


Starting in R2016b, instead of using the step method to perform the operation defined by the System object™, you can call the object with arguments, as if it were a function. For example, y = step(obj,x) and y = obj(x) perform equivalent operations.


D = visionhdl.DemosaicInterpolator returns a System object, D, that interpolates R'G'B' data from a Bayer pattern pixel stream.

D = visionhdl.DemosaicInterpolator(Name,Value) returns a System object, D, with additional options specified by one or more Name,Value pair arguments. Name is a property name and Value is the corresponding value. Name must appear inside single quotes (''). You can specify several name-value pair arguments in any order as Name1,Value1,...,NameN,ValueN. Properties not specified retain their default values.



Algorithm the object uses to calculate the missing pixel values.

  • Bilinear — Average of the pixel values in the surrounding 3×3 neighborhood.

  • Gradient-corrected linear (default) — Bilinear average, corrected for intensity gradient.


Color sequence of the pixels in the input stream.

Specify the sequence of R, G, and B pixels that correspond to the 2-by-2 block of pixels in the top-left corner of the input image. Specify the sequence in left-to-right, top-to-bottom order. For instance, the default value, RGGB, represents an image with this pattern.


Specify a power of two that accommodates the number of active pixels in a single horizontal line.

Choose a power of 2 that accommodates the number of active pixels in a horizontal line. If you specify a value that is not a power of two, the object uses the next largest power of two. When you set InterpolationAlgorithm to Bilinear, the object allocates 2-by-LineBufferSize memory locations. When you set InterpolationAlgorithm to Gradient-corrected linear, the object allocates 4-by-LineBufferSize memory locations.

Default: 2048


stepDemosaic a Bayer pattern video stream
Common to All System Objects

Allow System object property value changes


collapse all

This example constructs full RGB pixel data from a Bayer pattern thumbnail image.

Set the dimensions of the test image. Load the source image file. This image is in Bayer pattern: each pixel is represented by one value, alternating green values with red and blue values. Then select a portion of the image matching the desired test size. These offsets select the face of the woman in the image.

frmActivePixels = 256;
frmActiveLines = 192;
frmOrig = imread('mandi.tif');
frmInput = frmOrig(900:899+frmActiveLines, 2350:2349+frmActivePixels);
title 'Input Image'

Create a serializer object and specify size of the inactive pixel regions.

frm2pix = visionhdl.FrameToPixels(...

Create an interpolator object. Specify the sequence of color values matching the 2-by-2 pixels in the top-left corner of the image.

BayerInterpolator = visionhdl.DemosaicInterpolator(...
   'SensorAlignment', 'RGGB');

Serialize the test image. pixIn is a vector of pixel values. ctrlIn is a vector of control signal structures.

Note: This syntax runs only in R2016b or later. If you are using an earlier release, replace each call of an object with the equivalent step syntax. For example, replace myObject(x) with step(myObject,x).

[pixIn,ctrlIn] = frm2pix(frmInput);

Set up variables, and generate the {R,G,B} triplet for each pixel in the stream. This example prints a progress message every 32 lines.

[pixels,lines,numPixelsPerFrame] = getparamfromfrm2pix(frm2pix);
ctrlOut  = repmat(pixelcontrolstruct,numPixelsPerFrame,1);
pixOut = zeros(numPixelsPerFrame,3,'uint8');
lineCount = 1;
for p = 1:numPixelsPerFrame 
    if ctrlIn(p).hEnd
         lineCount = lineCount+1;
         if mod(lineCount,32)==0
              fprintf('Processing... line %d\n',lineCount)
    [pixOut(p,:),ctrlOut(p)] = BayerInterpolator(pixIn(p),ctrlIn(p));
Processing... line 32
Processing... line 64
Processing... line 96
Processing... line 128
Processing... line 160
Processing... line 192

Create a deserializer object with a format matching that of the serializer. Convert the pixel stream to an image frame, and display the result.

pix2frm = visionhdl.PixelsToFrame(...
[frmOutput,frmValid] = pix2frm(pixOut,ctrlOut);
if frmValid
    title 'Output Image'


This object implements the algorithms described on the Demosaic Interpolator block reference page.

See Also

| | (Image Processing Toolbox)

Introduced in R2015a