This guide describes the Python-specific input, output, ownership, and naming contracts of mmcfilters. The subsystem guides define the corresponding tree, attribute, filter, contour, saliency, and interoperability semantics.
Input contract
Python exposes one ValuedMorphologicalTree abstraction while retaining the native altitude representation required by each hierarchy:
- image inputs are two-dimensional C-contiguous np.uint8 arrays;
- component-tree and residual-tree altitude arrays are one-dimensional C-contiguous np.uint8 arrays or integer sequences in [0, 255];
- tree-of-shapes node altitudes follow the altitude_encoding declared by the topographic convention. The default TopographicAltitudeEncoding.UINT8 publishes unchanged source gray levels as np.uint8; TopographicAltitudeEncoding.EXACT_DOUBLED publishes exact doubled gray units as np.uint16, where an original gray level g is represented by 2*g and odd values represent exact half levels;
- factories return ValuedMorphologicalTree;
- a separate topology-only MorphologicalTree Python type is not exposed.
Normalize an image at the application boundary:
import numpy as np
import mmcfilters
image = np.ascontiguousarray(
[
[3, 3, 2, 2],
[3, 4, 4, 2],
[1, 4, 5, 2],
[1, 1, 5, 0],
],
dtype=np.uint8,
)
Methods that accept node attributes require one-dimensional C-contiguous np.float32 or np.float64 arrays with one entry per internal node slot.
Building trees
On a 2D square grid, radius=1.0 selects 4-connectivity and radius=1.5 selects 8-connectivity:
max_tree = mmcfilters.MorphologicalTreeFactory.create_max_tree(
image,
radius=1.5,
)
min_tree = mmcfilters.MorphologicalTreeFactory.create_min_tree(
image,
radius=1.5,
)
residual_tree = mmcfilters.MorphologicalTreeFactory.create_extremum_residual_tree(
image,
radius=1.5,
)
tree_of_shapes = mmcfilters.MorphologicalTreeFactory.create_tree_of_shapes(image)
assert tree_of_shapes.node_altitudes.dtype == np.uint8
assert np.array_equal(tree_of_shapes.reconstruct_from_node_altitudes(), image)
self_dual_tree_of_shapes = (
mmcfilters.MorphologicalTreeFactory.create_tree_of_shapes(
image,
mmcfilters.self_dual_span_convention(),
)
)
assert self_dual_tree_of_shapes.node_altitudes.dtype == np.uint16
assert np.array_equal(
self_dual_tree_of_shapes.reconstruct_from_node_altitudes(),
2 * image.astype(np.uint16),
)
See Morphological trees for the default topographic convention, altitude encodings, and connectivity semantics. In Python, convention fields may also be passed directly to the factory:
unpadded_self_dual = mmcfilters.MorphologicalTreeFactory.create_tree_of_shapes(
image,
domain_extension=mmcfilters.TopographicDomainExtension.NONE,
altitude_encoding=mmcfilters.TopographicAltitudeEncoding.UINT8,
)
assert unpadded_self_dual.node_altitudes.dtype == np.uint8
assert np.array_equal(unpadded_self_dual.reconstruct_from_node_altitudes(), image)
Selects the self-dual span-valued tree-of-shapes immersion.
mmcfilters.self_dual_span_convention(...) builds the same convention as a value when one has to be stored or reused. Passing both a complete convention and individual fields to the factory is rejected.
Combining the self-dual span with EXTERIOR_RING and UINT8 is rejected.
Residual-tree factories use a fixed row-major spatial order. See Self-dual residual trees for scheduling and construction policies.
Hierarchy import
Higra parent/altitude arrays are the only supported public hierarchy import format. Supply the image dimensions and the declared tree kind:
tree = mmcfilters.MorphologicalTreeFactory.create_from_higra_parent(
parent=[3, 4, 2, 2, 2],
node_altitudes=np.array([3, 20, 10, 3, 20], dtype=np.uint8),
rows=1,
columns=2,
kind=mmcfilters.MorphologicalTreeKind.GENERIC,
)
assert tree.reconstruct_from_node_altitudes().tolist() == [[3, 20]]
Max-tree and min-tree imports also require an adjacency radius. Imported hierarchies have the supplied regular 2D domain.
Tree-of-shapes semantics retain a TopographicConvention. A complementary immersion carries ComplementaryAdjacencies(min_adjacency, max_adjacency); self-dual span immersion is represented explicitly by SelfDualSpanImmersion. Attribute requirements can be inspected before computation:
requirements = mmcfilters.Attribute.requirements(
mmcfilters.Attribute.CENTRAL_MOMENT_20
)
assert requirements["grid_domain_2d"]
assert not requirements["altitude"]
assert requirements["adjacency"] == "none"
assert not requirements["monotone_altitude_order"]
Node IDs and queries
Tree queries, attributes, altitude buffers, predicates, and mutations use the dense internal NodeId domain. Edits may leave dead slots, so dense results keep max_tree.num_internal_node_slots rows. Iterate live IDs when appropriate:
root = max_tree.root
for node_id in max_tree.alive_node_ids:
children = max_tree.children(node_id)
direct_pixels = max_tree.proper_part(node_id)
subtree = max_tree.subtree_nodes(node_id)
entry_index = max_tree.dfs_entry_index(node_id)
exit_index = max_tree.dfs_exit_index(node_id)
Identifier collections returned by tree queries are owned, 1D np.int32 arrays. max_tree.parents has one entry per internal slot: the root points to itself and dead slots contain -1. These arrays are snapshots; query again after tree edits.
The DFS entry and exit indices belong to one interleaved event sequence. They characterize the node interval, so num_descendants(node_id) == (exit_index - entry_index - 1) // 2.
Pixels use row-major indices. The canonical structural queries are:
pixel_id = 10
smallest = max_tree.smallest_node(pixel_id)
support_pixels = list(max_tree.node_support(smallest))
component_mask = max_tree.reconstruct_node(smallest)
Python exposes safe local mutations and checked altitude setters:
max_tree.prune_node(node_id)
max_tree.merge_node_into_parent(node_id)
max_tree.set_node_altitude(node_id, value)
max_tree.node_altitudes = altitude
TreeEditor, ValuedMorphologicalTreeEditor, unchecked setters, and a mutable topology handle are not exposed in Python. See Editing API for mutation and derived-state lifetime.
Attributes
Use Attribute.compute(tree, attributes, output_space=NodeIdSpace.MORPHOLOGICAL_TREE,
*, dtype=np.float32) for automatic selection of the computation path. The tree is a ValuedMorphologicalTree. The registry selects the topology/support path when no requested attribute requires altitude; otherwise the request uses the altitude-aware path. Other capability requirements are still validated.
A scalar attribute value or name returns a 1D array. A group or iterable returns (layout, values), where layout maps names to columns and values is 2D. A one-element sequence keeps the column dimension. Groups expand to their scalar members, and repeated attributes share one column in canonical scalar-enumerator order. An empty sequence returns an empty layout and an array with zero columns.
Rows follow output_space, which defaults to dense internal node slots, including dead slots after edits. Returned arrays own their values and remain valid after the tree changes or is released. dtype accepts np.float32 or np.float64 and defaults to np.float32. Both dtypes use the same internal double computation pipeline; the result is converted to the requested NumPy storage.
Every method that accepts an Attribute or Attribute.Group also accepts its stable symbolic name as a string. The accepted names are exactly the ones that layout already uses as keys, so the request and the result share one vocabulary:
area = mmcfilters.Attribute.compute(max_tree, "AREA")
layout, values = mmcfilters.Attribute.compute(
max_tree,
["AREA", "VOLUME", "BOUNDARY"],
)
Enums and symbolic names may be mixed in one request. Matching is exact and case-sensitive. An unknown name raises ValueError and reports the closest known names.
area = mmcfilters.Attribute.compute(
max_tree,
mmcfilters.Attribute.AREA,
)
altitude = max_tree.node_altitude(node_id)
gray_level_height = mmcfilters.Attribute.compute(
max_tree,
mmcfilters.Attribute.GRAY_LEVEL_HEIGHT,
)
layout, values = mmcfilters.Attribute.compute(
max_tree,
[
mmcfilters.Attribute.AREA,
mmcfilters.Attribute.VOLUME,
mmcfilters.Attribute.RELATIVE_VOLUME,
],
dtype=np.float64,
)
area_column = values[:, layout["AREA"]]
A sliced column is generally not C-contiguous. Copy it before passing it to a filter helper, or compute the scalar attribute directly as above.
Groups expand to stable scalar sets:
boundary_layout, boundary_values = (
mmcfilters.Attribute.compute(
max_tree,
mmcfilters.Attribute.Group.BOUNDARY,
)
)
Stable scalar and group membership, input contracts, and family-specific guides are indexed by the Attribute catalog.
Per-pixel distance fields and all-node sparse profiles have a separate result API. Values in a field and profile thresholds are squared distances:
field = mmcfilters.DistanceTransform.compute_node_field(
max_tree,
max_tree.root,
accuracy="exact",
)
dense_squared_distance = field.to_dense(outside_value=-1)
profiles = mmcfilters.DistanceTransform.compute_profiles(
max_tree,
accuracy=mmcfilters.DistanceTransformAccuracy.APPROXIMATE,
)
root_histogram = profiles.histogram(max_tree.root)
root_survival = profiles.survival_profile(max_tree.root)
The methods are topology-only even though Python receives the public ValuedMorphologicalTree wrapper.
Altitude-based node-attribute sampling returns current-node, ancestor, and representative-descendant columns. Missing values use a typed policy:
sample_layout, sampled_values = (
mmcfilters.Attribute.compute_sampled_node_attribute(
max_tree,
mmcfilters.Attribute.AREA,
1,
1,
missing_sample_policy=(
mmcfilters.MissingNodeAttributeSamplePolicy.NOT_A_NUMBER
),
)
)
The two integer arguments are altitude_step and sampling_radius. Serialized layout labels use _ANCESTOR_n and _DESCENDANT_n.
Use compute_attribute_mapping to project a node attribute to the image domain. The complete attribute and layout contracts are in Attributes and the Attribute catalog.
Concrete C++ computers, AttributePipeline, and finite-window intermediate storage are not exposed in Python.
Filters and stability
Filter objects capture the topology mutation version at construction. Create them after the edits that should affect the result and do not reuse them after a topology mutation.
box_height = mmcfilters.Attribute.compute(
max_tree,
mmcfilters.Attribute.BOUNDING_BOX_HEIGHT,
)
keep_large = area >= 4.0
direct = filters.filtering_by_direct_rule(keep_large)
subtractive = filters.filtering_by_subtractive_rule(keep_large)
pruned_min = filters.filtering_by_pruning_min_rule(attribute=box_height, threshold=2.0)
pruned_max = filters.filtering_by_pruning_max_rule(attribute=box_height, threshold=2.0)
altitude_adjusted = mmcfilters.adjust_node_preservation_mask_by_altitude_stability(
max_tree,
keep_large,
altitude_window_radius=2,
)
depth_adjusted = mmcfilters.adjust_node_preservation_mask_by_depth_stability(
max_tree,
keep_large,
depth_window_radius=2,
)
Family of attribute-based image filtering operators on morphological trees.
Use DepthStableRegionComputer when the variation scores or selected mask are needed directly:
depth_mask = depth.compute_by_depth(depth_window_radius=2)
depth_variation = depth.get_variations()
depth_filtered = filters.filtering_by_direct_rule(depth_mask)
Detects stable regions from a topological depth window.
compute_by_depth returns a Boolean node mask, so the preservation-mask filtering rules accept it directly. Its result getters raise RuntimeError until compute_by_depth succeeds.
Extinction selection and Ultimate Attribute Opening (UAO) use the same dense attribute-buffer convention:
strongest = mmcfilters.ExtinctionSelectionPolicy.by_top_k(8)
filtered = extinction.filtering(strongest)
uao.execute(maximum_attribute_threshold=image.shape[0])
max_contrast = uao.get_max_contrast_image()
associated = uao.get_associated_image()
Computes and stores extinction values for regional extrema.
Computes an Ultimate Attribute Opening by accumulating maximal contrasts.
See Filters for rule selection, extinction contracts, stability, and UAO outputs.
Saliency maps
The Python surface exposes three distinct operations:
| Need | API |
| project a monotone hierarchy valuation | HierarchySaliencyMap |
| compute a persistence hierarchy from extinction values | ExtinctionValues.compute_formal_saliency_edge_map |
| compute extinction in tree-node shape space | ShapeSpaceSaliency |
edge_map = mmcfilters.HierarchySaliencyMap.compute_normalized_altitude_edge_map(
max_tree,
)
max_tree,
area,
mmcfilters.ShapeSpaceExtremaPolarity.MINIMA,
)
static ShapeSpaceSaliencyResult< Real > compute(const MorphologicalTree &tree, std::span< const Real > attribute, ShapeSpaceExtremaPolarity polarity, const RegularGridAdjacency2D &adjacency)
Computes extinction values, sparse representative scores, and contours.
Edge maps return an EdgeMap with read-only NumPy views sources, targets, and values, plus num_rows, num_columns, and adjacency_radius. The object owns the buffers independently of the tree; views keep them alive. Use edge_map.to_pixel_image() for display and edge_map.threshold(t) to select contour edges with value >= t, returned as EdgeContours. Use .copy() to obtain a writable array.
node_contour_edges() returns NodeContourEdges; compute_incremental_node_contours() returns IncrementalNodeContours, with contours.project_node_valuation(values) and contours.threshold_by_node_valuation(values, t). Their arrays are read-only views. Xu returns ShapeSpaceExtinctionResult or ShapeSpaceSaliencyResult, exposing .extrema (a tuple of immutable records), .node_scores, and .edge_map for the complete result. These results own their buffers independently of the tree.
Contours
Pixel contours support incremental post-order iteration and queries for one node. Each result is an independently owned NumPy array:
root_contour = contours.contour(max_tree.root)
for node_id, contour_pixels in contours:
process(node_id, contour_pixels)
Incremental foreground A4 contours on the image domain.
Use contour traces for oriented pixel-side edges grouped into closed boundaries. Iteration, callbacks, and node queries return owned ContourTrace objects; edge, pixel, and boundary collections are independent lists:
root_trace = contour_traces.trace(max_tree.root)
for boundary in root_trace.boundaries():
boundary_edges = root_trace.boundary_edges(boundary)
for node_id, trace in contour_traces:
process(node_id, trace)
Incremental ordered contour traces on the image domain.
See Pixel contours and Contour traces.
Higra interoperability
export_higra_hierarchy() returns a compact (parent, altitude) snapshot of the live tree. Its node domain differs from internal NodeId values:
parent, altitude = max_tree.export_higra_hierarchy()
area_exported = max_tree.project_node_values_to_exported_higra(
area,
mmcfilters.Attribute.AREA,
)
Import preserves the supplied Higra domain until the topology is edited:
roundtrip = mmcfilters.MorphologicalTreeFactory.create_from_higra_parent(
parent,
altitude,
max_tree.num_rows,
max_tree.num_columns,
mmcfilters.MorphologicalTreeKind.MAX_TREE,
radius=1.5,
)
area_in_higra_space = mmcfilters.Attribute.compute(
roundtrip,
mmcfilters.Attribute.AREA,
mmcfilters.NodeIdSpace.HIGRA,
)
See Higra interoperability for the two external node domains and projection rules.
Connected alternating sequential filter
The connected alternating sequential filter (CASF) API is exposed as CasfComponentTrees. It constructs paired component trees and applies thresholds for a selected attribute:
casf = mmcfilters.CasfComponentTrees(
image,
mmcfilters.CasfComponentTreesAttribute.AREA,
radius=1.5,
)
casf_result = casf.filter([2.0, 4.0])
min_parent, min_altitude = casf.export_min_tree()
max_parent, max_altitude = casf.export_max_tree()
Paired adjustment requires ValuedMorphologicalTree instances for a min-tree and max-tree over the same image domain:
adjust = mmcfilters.DualMinMaxTreeIncrementalFilter(min_tree, max_tree)
candidate = next(
node_id
for node_id in max_tree.alive_node_ids
if node_id != max_tree.root
)
adjust.prune_max_tree_and_update_min_tree([candidate])
updated_min_tree = adjust.min_tree
updated_max_tree = adjust.max_tree
Failure modes
Python bindings reject:
- non-np.uint8, non-contiguous, or non-2D factory images;
- altitude arrays with an invalid dtype, shape, contiguity, length, or range;
- topology/support calls that request altitude-dependent attributes;
- non-contiguous attribute arrays passed to filter helpers;
- cached helper objects used after topology mutation;
- NodeIdSpace.HIGRA on trees without a valid imported Higra domain.