Bayer RAW#
Tip
Learn more: https://en.wikipedia.org/wiki/Bayer_filter
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 patterncfaputs there, and returns the(*, 1, H, W)mosaic.- Convention:
The inverse conversion requires even height and width.
- Parameters:
- Return type:
- Returns:
raw mosaic with shape
(*, 1, H, W).
See also
raw_to_rgb(): the inverse, which interpolates the missing samples.RgbToRaw: the module form.
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)returnsx. Away from the outermost pixel ring the result equals OpenCV’sCOLOR_BAYER_*2RGBup 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:
- Return type:
- Returns:
RGB image with shape
(*, 3, H, W).
See also
raw_to_rgb_2x2_downscaled(): one RGB pixel per 2x2 cell, without interpolation.rgb_to_raw(): samples an RGB image back into a mosaic.RawToRgb: the module form.
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:
- Return type:
- Returns:
RGB image with shape
(*, 3, H / 2, W / 2).
See also
raw_to_rgb(): keeps the resolution and interpolates.RawToRgb2x2Downscaled: the module form.
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.BGand its siblings name the 2x2 cell that tiles the mosaic. As in OpenCV, the name reads pixel (1, 1) and its right neighbor, soBGis 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
raw_to_rgb(): demosaics a mosaic with this pattern.rgb_to_raw(): samples an RGB image with this pattern.
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, andout = conv(image)isrgb_to_raw()with it.- Convention:
See the convention block of
rgb_to_raw().
- Shape:
image:
(*, 3, H, W)output:
(*, 1, H, W)
See also
rgb_to_raw(): the one-call form.RawToRgb: the inverse.
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, andout = conv(image)israw_to_rgb()with it.- Convention:
See the convention block of
raw_to_rgb().
- Shape:
image:
(*, 1, H, W)output:
(*, 3, H, W)
See also
raw_to_rgb(): the one-call form.RawToRgb2x2Downscaled: half the resolution, without interpolation.RgbToRaw: the inverse.
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, andout = conv(image)israw_to_rgb_2x2_downscaled()with it.- Convention:
See the convention block of
raw_to_rgb_2x2_downscaled().
- Shape:
image:
(*, 1, H, W)output:
(*, 3, H / 2, W / 2)
See also
raw_to_rgb_2x2_downscaled(): the one-call form.RawToRgb: keeps the resolution and interpolates.
Example
>>> rawinput = torch.rand(2, 1, 4, 6) >>> rgb_downscale = RawToRgb2x2Downscaled(CFA.RG) >>> output = rgb_downscale(rawinput) # 2x3x2x3