IMAGE-PROCESSING
watershed_ift
Apply watershed from markers using image foresting transform algorithm.
Example of usage
Detect, enhance or quantify structures in a synthetic quality-inspection grayscale image.
Usage / limitations
+Use when: Transform and analyse images.
−Limitations: Check input requirements, mathematical assumptions and version-specific behaviour before using.
Mathematical formula
See mathematical definition for watershed_ift; this example verifies numerical properties.
Sample dataShow
{
"variable": "image_data",
"domain": "Industrial visual quality inspection",
"feature_names": [
"pixel_row",
"pixel_column",
"intensity_level"
],
"pixels": [
[
0,
0,
0,
0,
0,
0,
0
],
[
0,
0,
0,
0,
0,
0,
0
],
[
0,
0,
2,
3,
2,
0,
0
],
[
0,
0,
3,
9,
3,
0,
0
],
[
0,
0,
2,
3,
2,
0,
0
],
[
0,
0,
0,
0,
0,
0,
0
],
[
0,
0,
0,
0,
0,
0,
0
]
],
"pixel_unit": "relative grayscale intensity (0–9)"
}
Code example (Python)
# DataLens.ai sample: named industrial inspection image.
image_data = {'variable': 'image_data', 'domain': 'Industrial visual quality inspection', 'feature_names': ['pixel_row', 'pixel_column', 'intensity_level'], 'pixels': [[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 2, 3, 2, 0, 0], [0, 0, 3, 9, 3, 0, 0], [0, 0, 2, 3, 2, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]], 'pixel_unit': 'relative grayscale intensity (0–9)'}
# DataLens.ai example — watershed_ift for visual quality inspection.
# Input image_data is provided by Copy input + code + test.
import numpy as np
from scipy import ndimage as ndi
# Construct a small grayscale inspection image with a bright object.
image = np.asarray(image_data['pixels'], dtype=float)
mask = image > 0
# Apply the selected image processing algorithm.
gradient = np.uint8(np.clip(ndi.gaussian_gradient_magnitude(image, sigma=1.0) * 20, 0, 255))
markers = np.zeros(image.shape, dtype=np.int32); markers[2, 2] = 1; markers[4, 4] = 2
result = ndi.watershed_ift(gradient, markers)
# Expected output: verify the defined numerical and shape properties.
assert result.shape == image.shape and {1, 2}.issubset(set(np.unique(result)))
print("Expected output verified")
# DataLens.ai example — watershed_ift for visual quality inspection.
# Input image_data is provided by Copy input + code + test.
import numpy as np
from scipy import ndimage as ndi
# Construct a small grayscale inspection image with a bright object.
image = np.asarray(image_data['pixels'], dtype=float)
mask = image > 0
# Apply the selected image processing algorithm.
gradient = np.uint8(np.clip(ndi.gaussian_gradient_magnitude(image, sigma=1.0) * 20, 0, 255))
markers = np.zeros(image.shape, dtype=np.int32); markers[2, 2] = 1; markers[4, 4] = 2
result = ndi.watershed_ift(gradient, markers)
# Expected output: verify the defined numerical and shape properties.
assert result.shape == image.shape and {1, 2}.issubset(set(np.unique(result)))
print("Expected output verified")