harpy.im.merge_labels#
- harpy.im.merge_labels(sdata, candidate_labels_name, priority_labels_name, threshold=0.5, chunks=None, output_labels_name=None, output_shapes_name=None, scale_factors=None, overwrite=False)#
Merge two labels elements using a global object-level overlap rule.
This function treats
priority_labels_nameas the authoritative segmentation andcandidate_labels_nameas a labels element that can fill uncovered regions. For each non-zero candidate object, overlaps with all non-zero priority objects are accumulated globally across the full image. A candidate object is kept if:candidate_fraction = overlap_with_any_priority / area_candidate <= thresholdCandidate objects with no overlap with the priority segmentation therefore have
candidate_fraction = 0and are kept. Accepted candidate objects are written only at pixels where the priority segmentation is0.Accepted candidate labels are relabeled above the existing priority labels to avoid label-id collisions in the merged result.
- Parameters:
sdata (
SpatialData) – The SpatialData object containing the labels elements to be merged.candidate_labels_name (
str) – The name of the labels element containing candidate objects to add to the merged result when they satisfy the overlap rule.priority_labels_name (
str) – The name of the labels element that takes precedence in the merged result.threshold (
float(default:0.5)) – Maximum allowedcandidate_fractionfor keeping a candidate object. Must lie between 0 and 1. A value of0keeps only candidates with no overlap with the priority segmentation. A value of1keeps all candidate objects.chunks (
str|int|tuple[int,int] |None(default:None)) – Chunk specification used when rechunking the label arrays before overlap accumulation and rendering. If a tuple is provided, it is interpreted as the desired(y, x)chunk size. If set to"auto", Dask determines the chunking.output_labels_name (
str|None(default:None)) – The name of the output labels element where the merged results will be stored.output_shapes_name (
str|None(default:None)) – The name of the output shapes element where results will be stored if shape data is produced from the merge operation.scale_factors (
Sequence[dict[str,int] |int] |None(default:None)) – Scale factors to apply for multiscale processing.overwrite (
bool(default:False)) – If True, overwritesoutput_labels_nameoroutput_shapes_nameif it already exists insdata.
- Return type:
- Returns:
: The
sdataobject with the merged labels element added to the specified output element. Ifoutput_shapes_nameis provided, a shapes element will be created corresponding to this labels element.- Raises:
ValueError – If the provided labels elements do not have the same shape or transformations.
ValueError – If
thresholdis outside the interval[0, 1].ValueError – If candidate label ids can not be relabeled safely into
_SEG_DTYPEwithout colliding with priority label ids.
See also
harpy.im.match_labels_to_referencemap labels from a merged or processed labels element back to labels in one or more reference elements.
Example
sdata = hp.datasets.mibi_example() sdata = hp.im.merge_labels( sdata, candidate_labels_name="masks_whole", priority_labels_name="masks_nuclear", threshold=0.5, chunks=256, output_labels_name="masks_merged", output_shapes_name="masks_merged_boundaries", overwrite=True, )