Bayer RAW#

Note

The CFA enum selects the color filter array layout.

Functions#

kornia.color.rgb_to_raw(image, cfa)[source]#

Convert a RGB image to RAW version of image with the specified color filter array.

The image data is assumed to be in the range of (0, 1).

Convention:

Selects samples into a one-channel Bayer mosaic using the same OpenCV cfa naming as raw_to_rgb. The inverse conversion requires even height and width.

Parameters:
  • image (Tensor) – RGB image to be converted to bayer raw with shape \((*,3,H,W)\).

  • cfa (CFA) – Which color filter array do we want the output to mimic. I.e. which pixels are red/green/blue.

Return type:

Tensor

Returns:

raw version of the image with shape \((*,1,H,W)\).

Example

>>> rgbinput = torch.rand(2, 3, 4, 6)
>>> raw = rgb_to_raw(rgbinput, CFA.BG) # 2x1x4x6
kornia.color.raw_to_rgb(image, cfa)[source]#

Convert a raw bayer image to RGB version of image.

We are assuming a CFA with 2 green, 1 red, 1 blue. A bilinear interpolation is used for R/G and a fix convolution for the green pixels. To simplify calculations we expect the Height Width to be evenly divisible by 2.

The image data is assumed to be in the range of (0, 1). Image H/W is assumed to be evenly divisible by 2. for simplicity reasons

Convention:

Input is a single-channel Bayer mosaic at axis -3. cfa uses OpenCV’s Bayer names, which read the pattern from pixel (1, 1): CFA.BG, GB, RG and GR are sensors whose top-left 2x2 cell is RGGB, GRBG, BGGR and GBRG. Height and width must be even.

Parameters:
  • image (Tensor) – raw image to be converted to RGB with shape \((*,1,H,W)\).

  • cfa (CFA) – The configuration of the color filter.

Return type:

Tensor

Returns:

RGB version of the image with shape \((*,3,H,W)\).

Example

>>> rawinput = torch.randn(2, 1, 4, 6)
>>> rgb = raw_to_rgb(rawinput, CFA.RG) # 2x3x4x6
kornia.color.raw_to_rgb_2x2_downscaled(image, cfa)[source]#

Convert the raw bayer image to RGB version of it and resize width and height by half.

This is done efficiently by converting each superpixel of bayer image to the corresponding rgb triplet. R and B channels of the raw image are left as are, while two G channels of raw image are averaged to obtain the output G channel.

We are assuming a CFA with 2 green, 1 red, 1 blue. The image data is assumed to be in the range of (0, 1). Image H/W is assumed to be evenly divisible by 2 for simplicity reasons.

Convention:

Converts each 2x2 Bayer cell into one RGB pixel, so an even (H, W) mosaic produces (H/2, W/2). cfa uses the same OpenCV naming as raw_to_rgb.

Parameters:
  • image (Tensor) – raw image to be converted to RGB and downscaled with shape \((*,1,H,W)\).

  • cfa (CFA) – The configuration of the color filter.

Return type:

Tensor

Returns:

downscaled RGB version of the image with shape \((*,3,\frac{H}{2},\frac{W}{2})\).

Example

>>> rawinput = torch.randn(2, 1, 4, 6)
>>> rgb = raw_to_rgb_2x2_downscaled(rawinput, CFA.RG) # 2x3x2x3

Modules#

class kornia.color.CFA(value)[source]#

Define the configuration of the color filter array.

So far only bayer images is supported and the enum sets the pixel order for bayer. Note that this can change due to things like rotations and cropping of images. Take care if including the translations in pipeline. This implementations is optimized to be reasonably fast, look better than simple nearest neighbour. On top of this care is taken to make it reversible going raw -> rgb -> raw. the raw samples remain intact during conversion and only unknown samples are interpolated.

The names are based on the OpenCV convention where the BG indicates pixel 1,1 (counting from 0,0) is blue and its neighbour to the right is green. In that case the top left pixel is red. Other options are GB, RG and GR

reference:

https://en.wikipedia.org/wiki/Color_filter_array

BG = 0#
GB = 1#
GR = 3#
RG = 2#
class kornia.color.RgbToRaw(cfa)[source]#

nn.Module to convert a RGB image to bayer raw version of image.

See the Convention block on rgb_to_raw().

The image data is assumed to be in the range of (0, 1).

Shape:
  • image: \((*, 3, H, W)\)

  • output: \((*, 1, H, W)\)

reference:

https://docs.opencv.org/4.0.1/de/d25/imgproc_color_conversions.html

Example

>>> rgbinput = torch.rand(2, 3, 4, 6)
>>> raw = RgbToRaw(CFA.GB)
>>> output = raw(rgbinput)  # 2x1x4x6
class kornia.color.RawToRgb(cfa)[source]#

nn.Module to convert a bayer raw image to RGB version of image.

See the Convention block on raw_to_rgb().

The image data is assumed to be in the range of (0, 1).

Shape:
  • image: \((*, 1, H, W)\)

  • output: \((*, 3, H, W)\)

Example

>>> rawinput = torch.rand(2, 1, 4, 6)
>>> rgb = RawToRgb(CFA.RG)
>>> output = rgb(rawinput)  # 2x3x4x6
class kornia.color.RawToRgb2x2Downscaled(cfa)[source]#

nn.Module version of the raw_to_rgb_2x2_downscaled() function.

See the Convention block on raw_to_rgb_2x2_downscaled().

The image width and height have to be divisible by two. The image data is assumed to be in the range of (0, 1).

Shape:
  • image: \((*, 1, H, W)\)

  • output: \((*, 3, \frac{H}{2}, \frac{W}{2})\)

Example

>>> rawinput = torch.rand(2, 1, 4, 6)
>>> rgb_downscale = RawToRgb2x2Downscaled(CFA.RG)
>>> output = rgb_downscale(rawinput)  # 2x3x2x3