# Bayer Color Filter Array to RGB values conversion

Bayer Color Filter Array is an array of values as shown in the following image:

When data is transmitted from a CMOS camera that uses the Bayer Color Filter Array (Bayer CFA) we get a stream of alternating values of Red (R) Green (G) for odd rows and alternating values of Green (G) and Blue (B) for even rows.

In order to construct an RGB picture, we need to calculate Green and Blue values for each Red pixel, Blue and Red values for each Green pixel and Red and Green values for each Blue pixel.
There are several ways to do that, From our own experiments and searching the internet we found that the simplest way of averaging neighbouring cells is the fastest and easiest way that give good enough results.

## Calculating Green value for a Red or Blue pixel:

In order to calculate the green values for a red or blue pixel we do an avarage of the neighbouring green pixels to the left and right of the red pixel (If there is no neigbhouring green to the left or right you can take the green value below or above the red pixel.

## Calculating Blue value for a Red pixel:

In order to calculate the blue values for a red pixel, you have to take the avarage of the four blue pixels that are diagonal to the red pixel.

## Calculating Red value for a Blue pixel:

This is done similar to calculating the blue value for a red pixel by averaging the four red pixels that are diagonal to the blue pixel.

## Calculating Red value for a Green pixel:

In order to calculate the red value for a green pixel you have to take the avarage of the two red pixels neibouring the green pixel, for odd rows the values are to the left and right, for even rows the values are above and below the pixel.

## Calculating Blue value for a Green pixel:

This is done similar to calculating the Red value for a Green pixel by averaging the values of the neighbouring blue pixels, for odd rows the values are above and below the pixel and for even rows the values are left and right of the pixel.