Segment Image Using Focus Region
SUMMARY
Segment Image Using Focus Region segments the in-focus regions of an image.
Focus region segmentation identifies areas of an image that are in focus by analyzing local sharpness. This method is useful for depth estimation, focus stacking, and identifying the sharpest regions in an image.
Use this Skill when you want to identify in-focus regions based on image sharpness.
The Skill
from telekinesis import cornea
result = cornea.segment_image_using_focus_region(
image=image,
blur_kernel_size=151,
threshold=5,
)Example
Input Image

Original image for focus region segmentation
Output Image

In-focus regions segmented from the image
The Code
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" / "Matt_LeBlanc.jpg")
image = io.load_image(filepath=filepath)
# Perform focus region segmentation
result = cornea.segment_image_using_focus_region(
image=image,
blur_kernel_size=151,
threshold=5,
)
# Access results
annotation = result["annotation"].to_dict()
mask = annotation['labeled_mask']The Explanation of the Code
Focus region segmentation detects in-focus areas by comparing the original image with a blurred version. Regions with high difference are considered in focus, while regions with low difference are considered out of focus.
The code begins by importing the required modules and loading an image:
import pathlib
from telekinesis import cornea
from datatypes import io
DATA_DIR = pathlib.Path("path/to/telekinesis-data")
filepath = str(DATA_DIR / "images" / "Matt_LeBlanc.jpg")
image = io.load_image(filepath=filepath)The focus region segmentation parameters are configured:
blur_kernel_sizecontrols the size of the blur kernel used for comparisonthresholdcontrols the sensitivity of focus detection
result = cornea.segment_image_using_focus_region(
image=image,
blur_kernel_size=151,
threshold=5,
)The function returns a dictionary containing an annotation object in COCO panoptic format. Extract the mask as follows:
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:
cd telekinesis-examples
python examples/cornea_examples.py --example segment_image_using_focus_regionHow to Tune the Parameters
The segment_image_using_focus_region Skill has 2 parameters:
blur_kernel_size (default: 10):
- Kernel size for blur operation
- Units: Pixels (odd integer)
- Increase for larger blur, detecting broader focus regions
- Decrease for smaller blur, detecting finer focus details
- Typical range: 5-25
threshold (default: 1):
- Threshold for focus detection
- Units: Pixel intensity difference
- Increase to require higher sharpness (more selective)
- Decrease to include less sharp regions (more inclusive)
- Typical range: 1-10
TIP
Best practice: Start with default values and adjust threshold based on your image's sharpness characteristics. Increase blur_kernel_size for images with larger depth of field.
Where to Use the Skill in a Pipeline
Segment Image Using Focus Region is commonly used in the following pipelines:
- Depth estimation - Identify regions at different depths
- Focus stacking - Combine in-focus regions from multiple images
- Quality control - Detect out-of-focus regions
- Autofocus systems - Determine focus quality
A typical pipeline for focus stacking looks as follows:
from telekinesis import cornea
from datatypes import io
# 1. Load multiple images at different focus distances
images = [io.load_image(filepath=f"image_{i}.png") for i in range(n)]
# 2. Segment in-focus regions for each image
focus_masks = [
cornea.segment_image_using_focus_region(image=img, ...)['mask']
for img in images
]
# 3. Combine in-focus regions
combined = combine_focus_regions(images, focus_masks)Related skills to build such a pipeline:
load_image: Load images from disksegment_image_using_threshold: Alternative segmentation methods
Alternative Skills
| Skill | vs. Segment Image Using Focus Region |
|---|---|
| segment_image_using_threshold | Threshold is simpler. Use threshold for intensity-based, focus region for sharpness-based segmentation. |
When Not to Use the Skill
Do not use Segment Image Using Focus Region when:
- All regions are in focus (Use other segmentation methods)
- You need color-based segmentation (Use color segmentation methods)
- Speed is critical (Focus detection can be slower)
TIP
Focus region segmentation is particularly useful for applications involving depth of field, such as macro photography or microscopy.

