Skip to content

Segment Image Using RGB

SUMMARY

Segment Image Using RGB performs RGB color space segmentation.

RGB color segmentation identifies pixels within a specified RGB color range, making it ideal for segmenting objects with known color characteristics. This method works directly in the RGB color space, which is intuitive and matches how images are typically stored and displayed.

Use this Skill when you want to segment objects based on their RGB color values.

The Skill

python
from telekinesis import cornea

result = cornea.segment_image_using_rgb(
    image=image,
    lower_bound=(0, 0, 0),
    upper_bound=(255, 255, 255),
)

API Reference

Example

Input Image

Input image

Original image for RGB color segmentation

Output Image

Output image

Segmented image by RGB color range

The Code

python
import pathlib

from telekinesis import cornea
from datatypes import io

DATA_DIR = pathlib.Path("path/to/telekinesis-data")

# Load image
filepath = str(DATA_DIR / "images" / "cylinder_on_conveyor.jpg")
image = io.load_image(filepath=filepath)

# Perform RGB segmentation
result = cornea.segment_image_using_rgb(
    image=image,
    lower_bound=(0, 50, 50),
    upper_bound=(180, 255, 255),
)

# Access results
annotation = result["annotation"].to_dict()
mask = annotation['labeled_mask']

The Explanation of the Code

RGB segmentation identifies pixels that fall within a specified RGB color range. The algorithm creates a binary mask where pixels within the range are marked as foreground.

The code begins by importing the required modules and loading an image:

python
import pathlib

from telekinesis import cornea
from datatypes import io

DATA_DIR = pathlib.Path("path/to/telekinesis-data")
filepath = str(DATA_DIR / "images" / "cylinder_on_conveyor.jpg")
image = io.load_image(filepath=filepath)

The RGB segmentation parameters are configured:

  • lower_bound and upper_bound define the RGB color range (R, G, B) where each component ranges from 0 to 255
  • Pixels with RGB values within this range are included in the segmentation
python
result = cornea.segment_image_using_rgb(
    image=image,
    lower_bound=(0, 50, 50),
    upper_bound=(180, 255, 255),
)

The function returns a dictionary containing an annotation object in COCO panoptic format. Extract the mask as follows:

python
annotation = result["annotation"].to_dict()
mask = annotation['labeled_mask']

Running the Example

Runnable examples are available in the Telekinesis examples repository. Follow the README in that repository to set up the environment. Once set up, you can run this specific example with:

bash
cd telekinesis-examples
python examples/cornea_examples.py --example segment_image_using_rgb

How to Tune the Parameters

The segment_image_using_rgb Skill has 2 parameters:

lower_bound (default: (0, 0, 0)):

  • Lower bound for RGB range as a tuple (R, G, B)
  • Units: RGB values (0-255)
  • Decrease to include darker colors
  • Increase to exclude darker colors
  • Typical range: (0, 0, 0) to (255, 255, 255)

upper_bound (default: (255, 255, 255)):

  • Upper bound for RGB range as a tuple (R, G, B)
  • Units: RGB values (0-255)
  • Increase to include brighter colors
  • Decrease to exclude brighter colors
  • Typical range: (0, 0, 0) to (255, 255, 255)

TIP

Best practice: Use a color picker tool to determine the RGB values of your target object, then set bounds with some tolerance around those values.

Where to Use the Skill in a Pipeline

Segment Image Using RGB is commonly used in the following pipelines:

  • Color-based object detection - Identify objects by their known colors
  • Background removal - Segment foreground objects from colored backgrounds
  • Quality control - Detect color defects or variations
  • Material sorting - Separate objects by color in manufacturing

A typical pipeline for color-based object detection looks as follows:

python
from telekinesis import cornea
from datatypes import io

# 1. Load the image
image = io.load_image(filepath=...)

# 2. Segment Image Using RGB color range
result = cornea.segment_image_using_rgb(image=image, ...)

# 3. Extract mask for further processing
annotation = result["annotation"].to_dict()
mask = annotation['labeled_mask']

Related skills to build such a pipeline:

  • load_image: Load images from disk
  • segment_image_using_hsv: Alternative color space segmentation (often more robust to lighting)

Alternative Skills

Skillvs. Segment Image Using RGB
segment_image_using_hsvHSV is more robust to lighting variations. Use HSV when lighting conditions vary, RGB when you have controlled lighting.
segment_image_using_labLAB color space is perceptually uniform. Use LAB for more intuitive color selection, RGB for direct pixel values.
segment_image_using_ycrcbYCrCb is useful for skin detection. Use YCrCb for specific applications like face detection, RGB for general color segmentation.

When Not to Use the Skill

Do not use Segment Image Using RGB when:

  • Lighting conditions vary significantly (Use HSV or LAB instead)
  • You need perceptual color matching (Use LAB color space)
  • Color is not a distinguishing feature (Consider other segmentation methods)

WARNING

RGB segmentation is sensitive to lighting conditions. For robust color segmentation under varying lighting, consider using HSV or LAB color spaces instead.