Binary Morphology
Binary operators treat nonzero values as foreground and expect tensors shaped
as (B, C, Spatial...).
torchmorph.binary_dilation
binary_dilation(
input,
structure=None,
iterations=1,
mask=None,
output=None,
border_value=False,
origin=0,
)
Dilate binary objects in a batched CUDA tensor
A value is foreground when it is nonzero. The operation supports one to
eight spatial dimensions and always treats the first two dimensions as
batch and channel. When iterations is less than one, dilation continues
until the result no longer changes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input
|
Tensor
|
CUDA tensor with shape |
required |
structure
|
Tensor
|
Spatial structuring element. Nonzero
entries participate in the neighborhood. If |
None
|
iterations
|
int
|
Number of sequential dilations. Values less than |
1
|
mask
|
Tensor
|
Tensor with the same shape as |
None
|
output
|
Tensor
|
Preallocated tensor with the same shape
and device as |
None
|
border_value
|
bool
|
Value used outside the input boundary. |
False
|
origin
|
int or tuple[int, ...]
|
Offset of the structuring-element anchor. A scalar is applied to every spatial dimension. |
0
|
Returns:
| Type | Description |
|---|---|
Tensor
|
torch.Tensor: Boolean dilation result, or |
torchmorph.binary_erosion
binary_erosion(
input,
structure=None,
iterations=1,
mask=None,
output=None,
border_value=False,
origin=0,
)
Erode binary objects in a batched CUDA tensor
A value is foreground when it is nonzero. The operation supports one to
eight spatial dimensions and always treats the first two dimensions as
batch and channel. When iterations is less than one, erosion continues
until the result no longer changes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input
|
Tensor
|
CUDA tensor with shape |
required |
structure
|
Tensor
|
Spatial structuring element. Nonzero
entries participate in the neighborhood. If |
None
|
iterations
|
int
|
Number of sequential erosions. Values less than |
1
|
mask
|
Tensor
|
Tensor with the same shape as |
None
|
output
|
Tensor
|
Preallocated tensor with the same shape
and device as |
None
|
border_value
|
bool
|
Value used outside the input boundary. |
False
|
origin
|
int or tuple[int, ...]
|
Offset of the structuring-element anchor. A scalar is applied to every spatial dimension. |
0
|
Returns:
| Type | Description |
|---|---|
Tensor
|
torch.Tensor: Boolean erosion result, or |
torchmorph.binary_propagation
Propagate a binary seed through a mask until convergence
This repeatedly dilates input while allowing changes only where
mask is nonzero. It is useful for morphological reconstruction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input
|
Tensor
|
Binary seed CUDA tensor with shape
|
required |
structure
|
Tensor
|
Spatial structuring element. If
|
None
|
mask
|
Tensor
|
Tensor with the same shape as |
None
|
output
|
Tensor
|
Preallocated result tensor with the
same shape and device as |
None
|
border_value
|
bool
|
Value used outside the input boundary. |
False
|
origin
|
int or tuple[int, ...]
|
Structuring-element anchor offset. |
0
|
Returns:
| Type | Description |
|---|---|
Tensor
|
torch.Tensor: Boolean converged result, or |
torchmorph.binary_fill_holes
Fill holes enclosed by binary objects
Background connected to the tensor boundary is reconstructed and removed; enclosed background regions become foreground.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input
|
Tensor
|
CUDA tensor with shape |
required |
structure
|
Tensor
|
Spatial structuring element used to
determine background connectivity. If |
None
|
output
|
Tensor
|
Preallocated result tensor with the
same shape and device as |
None
|
origin
|
int or tuple[int, ...]
|
Structuring-element anchor offset. |
0
|
Returns:
| Type | Description |
|---|---|
Tensor
|
torch.Tensor: Boolean tensor with enclosed holes filled, or |
torchmorph.binary_hit_or_miss
Find binary configurations with the hit-or-miss transform
A location matches when structure1 fits the foreground and
structure2 fits the background at the same anchor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input
|
Tensor
|
CUDA tensor with shape |
required |
structure1
|
Tensor
|
Foreground structuring element. If
|
None
|
structure2
|
Tensor
|
Background structuring element. If
|
None
|
output
|
Tensor
|
Preallocated result tensor with the
same shape and device as |
None
|
origin1
|
int or tuple[int, ...]
|
Anchor offset for |
0
|
origin2
|
int or tuple[int, ...]
|
Anchor offset for
|
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
torch.Tensor: Boolean tensor marking matching locations, or |
torchmorph.binary_opening
binary_opening(
input,
structure=None,
iterations=1,
mask=None,
output=None,
border_value=False,
origin=0,
)
Open binary objects by erosion followed by dilation
Opening removes foreground features that cannot contain the structuring element while largely preserving larger objects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input
|
Tensor
|
CUDA tensor with shape |
required |
structure
|
Tensor
|
Spatial structuring element. If
|
None
|
iterations
|
int
|
Number of erosions followed by the same number of
dilations. Values less than |
1
|
mask
|
Tensor
|
Same-shaped tensor whose nonzero entries identify locations that may change. |
None
|
output
|
Tensor
|
Preallocated result tensor with the
same shape and device as |
None
|
border_value
|
bool
|
Value used outside the input boundary. |
False
|
origin
|
int or tuple[int, ...]
|
Structuring-element anchor offset. |
0
|
Returns:
| Type | Description |
|---|---|
Tensor
|
torch.Tensor: Boolean opened tensor, or |
torchmorph.binary_closing
binary_closing(
input,
structure=None,
iterations=1,
mask=None,
output=None,
border_value=False,
origin=0,
)
Close binary objects by dilation followed by erosion
Closing fills small gaps and joins nearby foreground regions while largely preserving the extent of larger objects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input
|
Tensor
|
CUDA tensor with shape |
required |
structure
|
Tensor
|
Spatial structuring element. If
|
None
|
iterations
|
int
|
Number of dilations followed by the same number of
erosions. Values less than |
1
|
mask
|
Tensor
|
Same-shaped tensor whose nonzero entries identify locations that may change. |
None
|
output
|
Tensor
|
Preallocated result tensor with the
same shape and device as |
None
|
border_value
|
bool
|
Value used outside the input boundary. |
False
|
origin
|
int or tuple[int, ...]
|
Structuring-element anchor offset. |
0
|
Returns:
| Type | Description |
|---|---|
Tensor
|
torch.Tensor: Boolean closed tensor, or |