KeepLargestComponent
Bases: Transform
Keep only the largest connected component of each label.
For each specified label value, connected-component analysis is performed and all but the largest component are removed (set to the background value). This is useful for cleaning up noisy segmentation predictions.
Only single-channel LabelMap images are
affected.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
labels
|
Sequence[int] | None
|
Label values to filter. |
None
|
background_label
|
int
|
Value used for removed components. |
0
|
fully_connected
|
bool
|
If |
True
|
**kwargs
|
Any
|
See |
{}
|
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If a label map has more than one channel. |
Examples:
>>> import torchio as tio
>>> transform = tio.KeepLargestComponent()
>>> transform = tio.KeepLargestComponent(labels=[1, 2])
Source code in src/torchio/transforms/label/keep_largest.py
supports_per_instance_params
property
Whether this transform can sample parameters per batch element.
Defaults to False. Transforms that implement per-instance
parameter sampling override this to return True. When False,
the transform always uses batch-shared parameters regardless of
the per_instance flag, preserving the legacy behavior.
supports_per_instance_p
property
Whether this transform can gate each batch element independently.
Defaults to False. Shape-preserving transforms that implement
per-element probability override this to return True.
Shape-changing transforms must leave it False because masked
and unmasked elements would have incompatible shapes.
invertible
property
Whether this transform can be inverted.
forward(data)
Apply the transform.
The output type always matches the input type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Any
|
Input data to transform. |
required |
Source code in src/torchio/transforms/transform.py
inverse(params)
Return a transform that undoes this one.
Override in invertible subclasses. The returned transform, when applied, reverses the effect of the forward pass with the given parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
dict[str, Any]
|
The parameters recorded in the forward pass. |
required |
Returns:
| Type | Description |
|---|---|
Transform
|
A new |
Source code in src/torchio/transforms/transform.py
to_hydra()
Export as a Hydra-compatible config dict.
Returns a dict with _target_ set to the fully qualified
class name and only non-default field values included.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dict suitable for |
Source code in src/torchio/transforms/transform.py
make_params(batch)
apply_transform(batch, params)
Keep only the largest connected component per label.