Skip to content

Segment Image Using YCrCb

SUMMARY

Segment Image Using YCrCb performs YCrCb color space segmentation.

YCrCb (Y, Cr, Cb) is a color space commonly used in video and image compression. It separates luminance (Y) from chrominance (Cr, Cb), making it useful for applications like skin detection, face detection, and other scenarios where chrominance information is more important than absolute color values.

Use this Skill when you want to segment objects based on chrominance information, particularly for skin detection or similar applications.

The Skill

python
from telekinesis import cornea

result = cornea.segment_image_using_ycrcb(
    image=image,
    lower_bound=(0, 133, 77),
    upper_bound=(255, 173, 127))

API Reference

Example

Input Image

Input image

Original image for YCrCb color segmentation

Output Image

Output image

Segmented image by YCrCb 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" / "David_Schwimmer.jpg")
image = io.load_image(filepath=filepath)

# Perform YCrCb segmentation
result = cornea.segment_image_using_ycrcb(
    image=image,
    lower_bound=(0, 133, 77),
    upper_bound=(255, 173, 127),
)

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

The Explanation of the Code

YCrCb segmentation converts the image to YCrCb color space and identifies pixels within a specified YCrCb range. YCrCb separates:

ChannelNameRangeDescription
YLuminance0-255Brightness component
CrRed-difference0-255Red chrominance (128 = neutral)
CbBlue-difference0-255Blue chrominance (128 = neutral)

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" / "David_Schwimmer.jpg")
image = io.load_image(filepath=filepath)

The YCrCb segmentation parameters are configured. The bounds (0, 133, 77) to (255, 173, 127) are commonly used for skin detection:

python
result = cornea.segment_image_using_ycrcb(
    image=image,
    lower_bound=(0, 133, 77),
    upper_bound=(255, 173, 127),
)

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_ycrcb

How to Tune the Parameters

The segment_image_using_ycrcb Skill has 2 parameters:

lower_bound (default: (0, 133, 77)):

  • Lower bound for YCrCb range as a tuple (Y, Cr, Cb)
  • Units: Y: 0-255, Cr: 0-255, Cb: 0-255
  • Default values are optimized for skin detection
  • Adjust based on target chrominance characteristics

upper_bound (default: (255, 173, 127)):

  • Upper bound for YCrCb range as a tuple (Y, Cr, Cb)
  • Units: Y: 0-255, Cr: 0-255, Cb: 0-255
  • Default values are optimized for skin detection
  • Adjust based on target chrominance characteristics

TIP

Best practice: For skin detection, the default YCrCb ranges work well. For other applications, convert sample colors to YCrCb to determine appropriate bounds.

Where to Use the Skill in a Pipeline

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

  • Skin detection - Identify human skin regions
  • Face detection preprocessing - Segment potential face regions
  • Human-robot interaction - Detect human presence
  • Video compression applications - Chrominance-based segmentation

A typical pipeline for skin 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 YCrCb (skin detection)
result = cornea.segment_image_using_ycrcb(image=image, ...)

# 3. Process detected skin regions
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 for skin detection

Alternative Skills

Skillvs. Segment Image Using YCrCb
segment_image_using_hsvHSV is more general-purpose. Use HSV for general color segmentation, YCrCb for skin/chrominance detection.
segment_image_using_rgbRGB is simpler. Use RGB for general color segmentation, YCrCb for chrominance-based applications.

When Not to Use the Skill

Do not use Segment Image Using YCrCb when:

  • You need general color segmentation (Use HSV or RGB instead)
  • Color is not a distinguishing feature (Consider other segmentation methods)

TIP

YCrCb is particularly well-suited for skin detection and other applications where chrominance information is more important than absolute color values.