ImageDev

BayerToRgb2d

Applies a demosaicing algorithm on grayscale image with color meaning acquired with a Bayer filter camera.

Access to parameter description

This algorithm creates an RGB image from a Bayer pattern image.
A Bayer pattern image is a grayscale image where each pixel contains information about only one color component. The filter pattern is 50% green, 25% red and 25% blue and follows one of the four arrangements shown in Figure 1.

<b>Figure 1.</b> Four possible arrangements of a Bayer sensor: GR/BG (a), GB/RG (b), BG/GR (c) and RG/GB (d).
Figure 1. Four possible arrangements of a Bayer sensor: GR/BG (a), GB/RG (b), BG/GR (c) and RG/GB (d).

To convert a Bayer grayscale image into an RGB image, several methods are available. The formulas given below are based on a 5x5 area of a Bayer image with red, green, and blue pixels arranged as shown in Figure 2.

<b>Figure 2.</b> Identification of pixels belonging to a 5x5 window of a GR/BG Bayer image.
Figure 2. Identification of pixels belonging to a 5x5 window of a GR/BG Bayer image.

Bilinear interpolation

The calculation of the missing intensities is carried out with the following formulas: The defect of this method is that the color of the blue and red pixels can strongly vary locally, and thus reveal moire effects.

Interpolation of color ratios

The calculation of the missing intensities is carried out with the following formulas: The defect of this method is that the blue and red pixels can have a very strong intensity locally if the intensity of the green pixels is very low.

Improved interpolation of color ratios

To improve the results, we carry out a mixing of the two preceding methods on the blue and red interpolated pixels. $$ I=\frac{G_m}{M}\times I_2+\frac{M-G_m}{M}\times I_1$$ The defect of this method is that it does not resolve all moire effects.

Interpolation by gradient and color ratios

This method consists in improving the interpolated values for the green pixels and therefore improving the previous interpolation of the red and blue pixels. $$ \Delta H_{G8}=\left |G_7-G_9 \right | ~~~~ \Delta V_{G8}=|G_3-G_{13}| $$ $$ \left\{\begin{matrix} if (\Delta H_{G8} < \Delta V_{G8}) & G_8=\frac{G_7+G_9}{2} \\ if (\Delta H_{G8} > \Delta V_{G8}) & G_8=\frac{G_3+G_{13}}{2} \\ elsewhere & G_8=\frac{G_7+G_9+G_3+G_{13}}{4} \end{matrix}\right. $$

See also

Function Syntax

This function returns outputColorImage.
// Function prototype
std::shared_ptr< iolink::ImageView > bayerToRgb2d( std::shared_ptr< iolink::ImageView > inputGrayImage, BayerToRgb2d::FilterMode filterMode, BayerToRgb2d::InterpolationMode interpolationMode, std::shared_ptr< iolink::ImageView > outputColorImage = nullptr );
This function returns outputColorImage.
// Function prototype.
bayer_to_rgb_2d(input_gray_image: idt.ImageType,
                filter_mode: BayerToRgb2d.FilterMode = BayerToRgb2d.FilterMode.GR_BG,
                interpolation_mode: BayerToRgb2d.InterpolationMode = BayerToRgb2d.InterpolationMode.BILINEAR,
                output_color_image: idt.ImageType = None) -> idt.ImageType
This function returns outputColorImage.
// Function prototype.
public static IOLink.ImageView
BayerToRgb2d( IOLink.ImageView inputGrayImage,
              BayerToRgb2d.FilterMode filterMode = ImageDev.BayerToRgb2d.FilterMode.GR_BG,
              BayerToRgb2d.InterpolationMode interpolationMode = ImageDev.BayerToRgb2d.InterpolationMode.BILINEAR,
              IOLink.ImageView outputColorImage = null );

Class Syntax

Parameters

Parameter Name Description Type Supported Values Default Value
input
inputGrayImage
The Bayer grayscale input image. Image Grayscale nullptr
input
filterMode
The Bayer filter pattern.
GR_BG The Bayer pattern is GR/BG.
GB_RG The Bayer pattern is GB/RG.
RG_GB The Bayer pattern is RG/GB.
BG_GR The Bayer pattern is BG/GR.
Enumeration GR_BG
input
interpolationMode
The interpolation algorithm for demosaicing.
BILINEAR Demosaicing is performed by bilinear interpolation.
COLOR_RATIO Demosaicing is performed by improved interpolation of color ratios.
GRADIENT Demosaicing is performed by gradient and color ratios interpolation.
Enumeration BILINEAR
output
outputColorImage
The color output image. Its spatial dimensions and type are forced to the same values as the input. Image nullptr
Parameter Name Description Type Supported Values Default Value
input
input_gray_image
The Bayer grayscale input image. image Grayscale None
input
filter_mode
The Bayer filter pattern.
GR_BG The Bayer pattern is GR/BG.
GB_RG The Bayer pattern is GB/RG.
RG_GB The Bayer pattern is RG/GB.
BG_GR The Bayer pattern is BG/GR.
enumeration GR_BG
input
interpolation_mode
The interpolation algorithm for demosaicing.
BILINEAR Demosaicing is performed by bilinear interpolation.
COLOR_RATIO Demosaicing is performed by improved interpolation of color ratios.
GRADIENT Demosaicing is performed by gradient and color ratios interpolation.
enumeration BILINEAR
output
output_color_image
The color output image. Its spatial dimensions and type are forced to the same values as the input. image None
Parameter Name Description Type Supported Values Default Value
input
inputGrayImage
The Bayer grayscale input image. Image Grayscale null
input
filterMode
The Bayer filter pattern.
GR_BG The Bayer pattern is GR/BG.
GB_RG The Bayer pattern is GB/RG.
RG_GB The Bayer pattern is RG/GB.
BG_GR The Bayer pattern is BG/GR.
Enumeration GR_BG
input
interpolationMode
The interpolation algorithm for demosaicing.
BILINEAR Demosaicing is performed by bilinear interpolation.
COLOR_RATIO Demosaicing is performed by improved interpolation of color ratios.
GRADIENT Demosaicing is performed by gradient and color ratios interpolation.
Enumeration BILINEAR
output
outputColorImage
The color output image. Its spatial dimensions and type are forced to the same values as the input. Image null

Object Examples

auto polystyrene = ioformat::readImage( std::string( IMAGEDEVDATA_IMAGES_FOLDER ) + "polystyrene.tif" );

BayerToRgb2d bayerToRgb2dAlgo;
bayerToRgb2dAlgo.setInputGrayImage( polystyrene );
bayerToRgb2dAlgo.setFilterMode( BayerToRgb2d::FilterMode::GR_BG );
bayerToRgb2dAlgo.setInterpolationMode( BayerToRgb2d::InterpolationMode::BILINEAR );
bayerToRgb2dAlgo.execute();

std::cout << "outputColorImage:" << bayerToRgb2dAlgo.outputColorImage()->toString();
polystyrene = ioformat.read_image(imagedev_data.get_image_path("polystyrene.tif"))

bayer_to_rgb_2d_algo = imagedev.BayerToRgb2d()
bayer_to_rgb_2d_algo.input_gray_image = polystyrene
bayer_to_rgb_2d_algo.filter_mode = imagedev.BayerToRgb2d.GR_BG
bayer_to_rgb_2d_algo.interpolation_mode = imagedev.BayerToRgb2d.BILINEAR
bayer_to_rgb_2d_algo.execute()

print("output_color_image:", str(bayer_to_rgb_2d_algo.output_color_image))
ImageView polystyrene = ViewIO.ReadImage( @"Data/images/polystyrene.tif" );

BayerToRgb2d bayerToRgb2dAlgo = new BayerToRgb2d
{
    inputGrayImage = polystyrene,
    filterMode = BayerToRgb2d.FilterMode.GR_BG,
    interpolationMode = BayerToRgb2d.InterpolationMode.BILINEAR
};
bayerToRgb2dAlgo.Execute();

Console.WriteLine( "outputColorImage:" + bayerToRgb2dAlgo.outputColorImage.ToString() );

Function Examples

auto polystyrene = ioformat::readImage( std::string( IMAGEDEVDATA_IMAGES_FOLDER ) + "polystyrene.tif" );

auto result = bayerToRgb2d( polystyrene, BayerToRgb2d::FilterMode::GR_BG, BayerToRgb2d::InterpolationMode::BILINEAR );

std::cout << "outputColorImage:" << result->toString();
polystyrene = ioformat.read_image(imagedev_data.get_image_path("polystyrene.tif"))

result = imagedev.bayer_to_rgb_2d(polystyrene, imagedev.BayerToRgb2d.GR_BG, imagedev.BayerToRgb2d.BILINEAR)

print("output_color_image:", str(result))
ImageView polystyrene = ViewIO.ReadImage( @"Data/images/polystyrene.tif" );

IOLink.ImageView result = Processing.BayerToRgb2d( polystyrene, BayerToRgb2d.FilterMode.GR_BG, BayerToRgb2d.InterpolationMode.BILINEAR );

Console.WriteLine( "outputColorImage:" + result.ToString() );