Implementation:Kornia Kornia Rgb To Grayscale
Appearance
| Knowledge Sources | |
|---|---|
| Domains | Vision, Color_Space |
| Last Updated | 2026-02-09 15:00 GMT |
Overview
Concrete tool for differentiable RGB-to-grayscale conversion provided by Kornia's color module.
Description
The rgb_to_grayscale function converts RGB tensors to single-channel grayscale using configurable weights. Default weights follow ITU-R BT.601 (0.299, 0.587, 0.114). The operation is differentiable and supports batched inputs. It accepts (*, 3, H, W) tensors and outputs (*, 1, H, W).
Usage
Import when preprocessing images for feature detection, edge detection, or any grayscale-requiring operation.
Code Reference
Source Location
- Repository: kornia
- File:
kornia/color/gray.py - Lines: L58-109
Signature
def rgb_to_grayscale(
image: torch.Tensor,
rgb_weights: Optional[torch.Tensor] = None
) -> torch.Tensor
Import
from kornia.color import rgb_to_grayscale
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| image | torch.Tensor | Yes | RGB tensor of shape (*, 3, H, W) |
| rgb_weights | Optional[torch.Tensor] | No | Custom channel weights for the grayscale conversion |
Outputs
| Name | Type | Description |
|---|---|---|
| output | torch.Tensor | Grayscale image of shape (*, 1, H, W) |
Usage Examples
Basic Conversion
import torch
from kornia.color import rgb_to_grayscale
rgb_image = torch.rand(1, 3, 256, 256)
gray_image = rgb_to_grayscale(rgb_image)
# gray_image.shape == (1, 1, 256, 256)
Custom Weights
import torch
from kornia.color import rgb_to_grayscale
rgb_image = torch.rand(1, 3, 256, 256)
weights = torch.tensor([0.333, 0.333, 0.334])
gray_image = rgb_to_grayscale(rgb_image, rgb_weights=weights)
Batch Conversion
import torch
from kornia.color import rgb_to_grayscale
batch = torch.rand(8, 3, 256, 256)
gray_batch = rgb_to_grayscale(batch)
# gray_batch.shape == (8, 1, 256, 256)
Related Pages
Implements Principle
Page Connections
Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment