Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Kornia Kornia Raw Bayer Conversion

From Leeroopedia
Revision as of 15:22, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Kornia_Kornia_Raw_Bayer_Conversion.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


Knowledge Sources
Domains Vision, Color_Processing
Last Updated 2026-02-09 15:00 GMT

Overview

This module provides differentiable conversion between raw Bayer mosaic images and RGB images, supporting all four standard color filter array (CFA) configurations.

Description

raw.py is a module in the Kornia library's color subpackage that implements bidirectional conversion between single-channel Bayer raw images and three-channel RGB images. It defines a CFA enum for the four Bayer patterns (BG, GB, RG, GR) based on the OpenCV naming convention. The module offers three functional APIs: raw_to_rgb (full-resolution demosaicing using bilinear interpolation for R/B and a fixed convolution kernel for G), rgb_to_raw (mosaicing an RGB image into a raw Bayer pattern), and raw_to_rgb_2x2_downscaled (efficient 2x downscaled demosaicing using superpixel averaging). Each function has a corresponding nn.Module wrapper (RawToRgb, RgbToRaw, RawToRgb2x2Downscaled). The conversion is designed to be reversible: raw samples are preserved through the raw-to-RGB-to-raw round trip.

Usage

Import this module when working with raw camera sensor data, for demosaicing Bayer images into RGB, for simulating Bayer patterns from RGB, or for quickly previewing raw images at half resolution.

Code Reference

Source Location

Signature

class CFA(Enum):
    BG = 0
    GB = 1
    RG = 2
    GR = 3

def raw_to_rgb(image: torch.Tensor, cfa: CFA) -> torch.Tensor: ...
def rgb_to_raw(image: torch.Tensor, cfa: CFA) -> torch.Tensor: ...
def raw_to_rgb_2x2_downscaled(image: torch.Tensor, cfa: CFA) -> torch.Tensor: ...

class RawToRgb(nn.Module):
    def __init__(self, cfa: CFA) -> None: ...
    def forward(self, image: torch.Tensor) -> torch.Tensor: ...

class RgbToRaw(nn.Module):
    def __init__(self, cfa: CFA) -> None: ...
    def forward(self, image: torch.Tensor) -> torch.Tensor: ...

class RawToRgb2x2Downscaled(nn.Module):
    def __init__(self, cfa: CFA) -> None: ...
    def forward(self, image: torch.Tensor) -> torch.Tensor: ...

Import

from kornia.color import raw_to_rgb, rgb_to_raw, raw_to_rgb_2x2_downscaled, CFA
from kornia.color import RawToRgb, RgbToRaw, RawToRgb2x2Downscaled

I/O Contract

Inputs

Name Type Required Description
image (raw_to_rgb) torch.Tensor Yes Raw Bayer image with shape (*, 1, H, W). H and W must be evenly divisible by 2. Values in range (0, 1).
image (rgb_to_raw) torch.Tensor Yes RGB image with shape (*, 3, H, W). Values in range (0, 1).
image (raw_to_rgb_2x2_downscaled) torch.Tensor Yes Raw Bayer image with shape (*, 1, H, W). H and W must be evenly divisible by 2.
cfa CFA Yes Color filter array configuration: CFA.BG, CFA.GB, CFA.RG, or CFA.GR.

Outputs

Name Type Description
raw_to_rgb return torch.Tensor RGB image with shape (*, 3, H, W).
rgb_to_raw return torch.Tensor Raw Bayer image with shape (*, 1, H, W).
raw_to_rgb_2x2_downscaled return torch.Tensor Downscaled RGB image with shape (*, 3, H/2, W/2).

Usage Examples

Basic Usage

import torch
from kornia.color import raw_to_rgb, rgb_to_raw, raw_to_rgb_2x2_downscaled, CFA

# Demosaic a raw Bayer image to full-resolution RGB
raw_image = torch.rand(1, 1, 64, 64)
rgb = raw_to_rgb(raw_image, CFA.RG)
print(rgb.shape)  # torch.Size([1, 3, 64, 64])

# Mosaic an RGB image back to raw Bayer
raw_out = rgb_to_raw(rgb, CFA.RG)
print(raw_out.shape)  # torch.Size([1, 1, 64, 64])

# Quick preview: demosaic with 2x downscale
rgb_small = raw_to_rgb_2x2_downscaled(raw_image, CFA.RG)
print(rgb_small.shape)  # torch.Size([1, 3, 32, 32])

# Using nn.Module wrappers in a pipeline
from kornia.color import RawToRgb
demosaic = RawToRgb(CFA.BG)
rgb_batch = demosaic(torch.rand(4, 1, 128, 128))
print(rgb_batch.shape)  # torch.Size([4, 3, 128, 128])

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment