Bayer RAW#

Convert between a one-channel Bayer mosaic (*, 1, H, W) and an RGB image. The CFA enum gives the pattern of the mosaic.

Functions#

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

Sample an RGB image into a Bayer raw image.

raw = rgb_to_raw(image, cfa) keeps one channel at each pixel of a (*, 3, H, W) RGB image, the one that the pattern cfa puts there, and returns the (*, 1, H, W) mosaic.

Convention:

The inverse conversion requires even height and width.

Parameters:
  • image (Tensor) – RGB image with shape (*, 3, H, W).

  • cfa (CFA) – pattern of the mosaic, see CFA.

Return type:

Tensor

Returns:

raw mosaic with shape (*, 1, H, W).

See also

Example

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

Demosaic a Bayer raw image to RGB.

rgb = raw_to_rgb(image, cfa) converts a (*, 1, H, W) mosaic in \([0, 1]\) to a full-resolution (*, 3, H, W) RGB image. It interpolates red and blue bilinearly and green from its four nearest green samples.

Convention:

Height and width must be even. The mosaic samples are kept, so rgb_to_raw(raw_to_rgb(x, cfa), cfa) returns x. Away from the outermost pixel ring the result equals OpenCV’s COLOR_BAYER_*2RGB up to its 8-bit rounding (checked against OpenCV 5.0). In that ring OpenCV repeats the values of the ring inside it, and this function keeps the samples.

Parameters:
  • image (Tensor) – raw mosaic with shape (*, 1, H, W).

  • cfa (CFA) – pattern of the mosaic, see CFA.

Return type:

Tensor

Returns:

RGB image with shape (*, 3, H, W).

See also

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 a Bayer raw image to RGB at half the height and width.

rgb = raw_to_rgb_2x2_downscaled(image, cfa) turns each 2x2 cell of a (*, 1, H, W) mosaic in \([0, 1]\) into one RGB pixel: red and blue are the cell’s samples and green is the mean of its two green samples.

Convention:

Height and width must be even.

Parameters:
  • image (Tensor) – raw mosaic with shape (*, 1, H, W).

  • cfa (CFA) – pattern of the mosaic, see CFA.

Return type:

Tensor

Returns:

RGB image with shape (*, 3, H / 2, W / 2).

See also

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]#

Pattern of the Bayer color filter array of a raw image.

CFA.BG and its siblings name the 2x2 cell that tiles the mosaic. As in OpenCV, the name reads pixel (1, 1) and its right neighbor, so BG is the sensor usually called RGGB. The members, with the colors of the top-left cell read row by row:

  • BG: R G / G B.

  • GB: G R / B G.

  • RG: B G / G R.

  • GR: G B / R G.

Rotating or flipping a mosaic of even height and width changes its pattern, and so does cropping an odd number of rows or columns from its top or left.

See also

References

[1] https://en.wikipedia.org/wiki/Color_filter_array

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

Sample an RGB image into a Bayer raw image (module form).

conv = RgbToRaw(cfa) stores the pattern, and out = conv(image) is rgb_to_raw() with it.

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

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

See also

Example

>>> rgbinput = torch.rand(2, 3, 4, 6)
>>> raw = RgbToRaw(CFA.GB)
>>> output = raw(rgbinput)  # 2x1x4x6

References

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

class kornia.color.RawToRgb(cfa)[source]#

Demosaic a Bayer raw image to RGB (module form).

conv = RawToRgb(cfa) stores the pattern, and out = conv(image) is raw_to_rgb() with it.

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

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

See also

Example

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

Convert a Bayer raw image to RGB at half the height and width (module form).

conv = RawToRgb2x2Downscaled(cfa) stores the pattern, and out = conv(image) is raw_to_rgb_2x2_downscaled() with it.

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

  • output: (*, 3, H / 2, W / 2)

See also

Example

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