Python script (segmentation)

Segmentation of measurement using an external python script.

On a Windows machine, you need to install Python version 3 or higher and the py Python launcher

First, select Segmentation as node type and then select Python script as the method. Select the python script file using the Browse button or type the path to the script file directly.

Parameters

Python file

Select python file.

Python interpreter (optional)

Select a custom interpreter executable, empty will use py on Windows and python command on Linux and macOS.

Min area

The minimum number of pixels for an object to be included.

Max area

The maximum number of pixels for an object to be included.

If 0 no maximum area is defined.

Object filter

Use an expression to further exclude unwanted objects based on shape.

Properties that can be used for the Expression:

  • Area

  • Length

  • Width

  • Circumference

  • Regularity

  • Roundness

  • Angle

  • D1

  • D2

  • X

  • Y

  • MaxBorderDistance

  • BoundingBoxArea

For details on each available property see: Object properties Details

Shrink

Takes away x numbers of pixels at the borders of the objects included in images.

Separate

The Separate parameter defines how pixels that pass your segmentation threshold are grouped into distinct object samples.

Pixel Connectivity Rule

Breeze uses 8-connectivity. Pixels are considered connected if they touch horizontally, vertically, or diagonally on a single corner.

Parameter Options

1. Normal

  • Behavior: Standard connected-component grouping.

  • Result: Pixels touching by an edge or corner form a single object. Physically isolated clusters form separate objects.

2. Separate adjacent objects

  • Behavior: Applies a distance transform to find the inner centers (geometric peaks) of pixel clusters, then splits them using a watershed-style growth algorithm.

  • Best Used For: Round or spherical objects that are touching or overlapping.

  • Limitation: Elongated, branched, or irregular shapes may be incorrectly split into multiple objects because the algorithm detects multiple internal peaks within a single physical item.

3. Merge all objects into one

  • Behavior: Disregards spacing; combines all valid pixels across the entire image.

  • Result: Evaluates the entire frame as one single object for collective regional analysis.

4. Merge all objects per row

  • Behavior: Groups detected objects based on the spatial grid of their vertical center points ($Y$-coordinates).

  • Result: All objects residing within the same horizontal row lane are merged into a single object.

5. Merge all objects per column

  • Behavior: Groups detected objects based on the spatial grid of their horizontal center points ($X$-coordinates).

  • Result: All objects residing within the same vertical column lane are merged into a single object.

Only visible when applicable

Link output objects from two or more segmentations to top segmentation. Descriptors can then be added to the common object output and will be calculated for objects from all segmentations.

image-20230303-095316.png
Descriptors after object will be calculated for all three segmentations (Sample1, Sample2 and Sample3)

The segmentations must be at same level to be available for linking.

Simple python script for segmentation

Python
def perform(frame_data, num_bands):
    result = []
    count = len(frame_data)

    if count == 0:
        return result

    for i in range(0, count, num_bands):
        if (i + 1) < count and frame_data[i + 1] < 0.5:
            result.append(1.0)
        else:
            result.append(0.0)

    return result
  

Example input to the script

Data is streamed directly into the Python script's memory. The frame_data input is a contiguous, flat 1D array of floats containing all spectral bands for all pixels.

Because the data does not contain line breaks, the script receives a num_bands parameter. You use this parameter as the step size in your loop to mathematically jump from one pixel to the next.

For a 3-band camera capturing three pixels, the raw input array looks like this:

[17.0, 16.0, 19.0, 15.0, 14.0, 13.0, 15.0, 14.0, 16.0]

Example output from the script

Your script simply returns a standard Python list from the perform function. The list must contain the binary segmentation mask (1.0 for included, 0.0 for excluded) representing each spatial pixel.

[1.0, 1.0, 0.0]

Migrating from Breeze older than 2026.1

If you are updating an older Python script that used file parsing, you must align it with the new memory stream format.

  • Remove file reading: Do not import sys, read sys.argv, or open temporary text files.

  • Wrap your logic: Place your code inside a def perform(frame_data, num_bands): or def perform(frame_data): function.

  • Return, do not print: Return your final list directly from the function instead of using print(result).

  • Update array access: Pixels are no longer separated by rows. To access a specific band (e.g., the second band), use frame_data[i + 1] inside your stepped loop instead of splitting a string. Exceptions and error formatting are now handled automatically by the system wrapper.

570818561

Everything inside the yellow line has now been selected as a sample.