Skip to content

Segment Image Using Yen Threshold

SUMMARY

Segment Image Using Yen Threshold performs Yen threshold segmentation.

Yen's method is an automatic thresholding algorithm that determines the optimal threshold by maximizing a criterion function. It's an alternative to Otsu's method and can work well for images where Otsu may not perform optimally.

Use this Skill when you want to automatically determine threshold using Yen's method.

The Skill

python
from telekinesis import cornea

result = cornea.segment_image_using_yen_threshold(
    image=image)

API Reference

Example

Input Image

Input image

Original image for Yen threshold segmentation

Output Image

Output image

Segmented image using automatic Yen threshold

The Code

python
import pathlib

from telekinesis import cornea
from datatypes import io

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

# Load image as grayscale
filepath = str(DATA_DIR / "images" / "bolts_and_ nuts_white_bg.jpg")
image = io.load_image(filepath=filepath, as_gray=True)

# Perform Yen threshold segmentation
result = cornea.segment_image_using_yen_threshold(
    image=image,
)

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

The Explanation of the Code

Yen threshold automatically finds the optimal threshold by analyzing the image histogram using Yen's criterion function, which can sometimes perform better than Otsu for certain image types.

The code begins by importing the required modules and loading a grayscale 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" / "bolts_and_ nuts_white_bg.jpg")
image = io.load_image(filepath=filepath, as_gray=True)

Yen threshold segmentation is applied. No threshold value needs to be specified:

python
result = cornea.segment_image_using_yen_threshold(
    image=image,
)

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_yen_threshold

How to Tune the Parameters

The segment_image_using_yen_threshold Skill has no tunable parameters - the threshold is automatically determined.

TIP

Best practice: Try both Otsu and Yen threshold methods on your images to see which performs better. Yen may work better for certain histogram shapes.

Where to Use the Skill in a Pipeline

Segment Image Using Yen Threshold is commonly used in the following pipelines:

  • Automatic thresholding - When Otsu doesn't work well
  • Alternative to Otsu - For images with specific histogram characteristics
  • Document processing - Text extraction
  • Quality control - Automatic defect detection

A typical pipeline for automatic thresholding looks as follows:

python
from telekinesis import cornea
from datatypes import io

# 1. Load the image (as grayscale)
image = io.load_image(filepath=..., as_gray=True)

# 2. Apply Yen threshold (automatic)
result = cornea.segment_image_using_yen_threshold(image=image)

# 3. Process segmented 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_otsu_threshold: Alternative automatic method

Alternative Skills

Skillvs. Segment Image Using Yen Threshold
segment_image_using_otsu_thresholdOtsu is more common. Try both and use whichever works better for your images.
segment_image_using_thresholdManual threshold gives control. Use Yen for automatic, manual when you know the value.

When Not to Use the Skill

Do not use Segment Image Using Yen Threshold when:

  • You know the optimal threshold (Use manual threshold for speed)
  • Lighting is non-uniform (Use adaptive threshold instead)
  • Otsu works better (Use Otsu if it gives better results)

TIP

Yen threshold is a good alternative to Otsu when Otsu doesn't perform well on your specific images. It's worth trying both methods.