# kornia.morphology¶

dilation(tensor: torch.Tensor, kernel: torch.Tensor) → torch.Tensor[source]

Returns the dilated image applying the same kernel in each channel.

The kernel must have 2 dimensions, each one defined by an odd number. Dilation is equivalent to eroding the background thus dilation(x, k) == -erosion(-x, k).

Parameters
• tensor (torch.Tensor) – Image with shape $$(B, C, H, W)$$.

• kernel (torch.Tensor) – Structuring element with shape $$(k_x, k_y)$$.

Returns

Dilated image with shape $$(B, C, H, W)$$.

Return type

torch.Tensor

Example

>>> tensor = torch.rand(1, 3, 5, 5)
>>> kernel = torch.ones(3, 3)
>>> dilated_img = dilation(tensor, kernel)

erosion(tensor: torch.Tensor, kernel: torch.Tensor) → torch.Tensor[source]

Returns the eroded image applying the same kernel in each channel.

The kernel must have 2 dimensions, each one defined by an odd number.

Parameters
• tensor (torch.Tensor) – Image with shape $$(B, C, H, W)$$.

• kernel (torch.Tensor) – Structuring element with shape $$(k_x, k_y)$$.

Returns

Eroded image with shape $$(B, C, H, W)$$.

Return type

torch.Tensor

Example

>>> tensor = torch.rand(1, 3, 5, 5)
>>> kernel = torch.ones(5, 5)
>>> output = erosion(tensor, kernel)

open(tensor: torch.Tensor, kernel: torch.Tensor) → torch.Tensor[source]

Returns the opened image, (that means, dilation after an erosion) applying the same kernel in each channel.

The kernel must have 2 dimensions, each one defined by an odd number.

Parameters
• tensor (torch.Tensor) – Image with shape $$(B, C, H, W)$$.

• kernel (torch.Tensor) – Structuring element with shape $$(k_x, k_y)$$.

Returns

Dilated image with shape $$(B, C, H, W)$$.

Return type

torch.Tensor

Example

>>> tensor = torch.rand(1, 3, 5, 5)
>>> kernel = torch.ones(3, 3)
>>> opened_img = open(tensor, kernel)

close(tensor: torch.Tensor, kernel: torch.Tensor) → torch.Tensor[source]

Returns the closed image, (that means, erosion after a dilation) applying the same kernel in each channel.

The kernel must have 2 dimensions, each one defined by an odd number.

Parameters
• tensor (torch.Tensor) – Image with shape $$(B, C, H, W)$$.

• kernel (torch.Tensor) – Structuring element with shape $$(k_x, k_y)$$.

Returns

Dilated image with shape $$(B, C, H, W)$$.

Return type

torch.Tensor

Example

>>> tensor = torch.rand(1, 3, 5, 5)
>>> kernel = torch.ones(3, 3)
>>> closed_img = close(tensor, kernel)

gradient(tensor: torch.Tensor, kernel: torch.Tensor) → torch.Tensor[source]

Returns the morphological gradient of an image.

That means, (dilation - erosion) applying the same kernel in each channel. The kernel must have 2 dimensions, each one defined by an odd number.

Parameters
• tensor (torch.Tensor) – Image with shape $$(B, C, H, W)$$.

• kernel (torch.Tensor) – Structuring element with shape $$(k_x, k_y)$$.

Returns

Dilated image with shape $$(B, C, H, W)$$.

Return type

torch.Tensor

Example

>>> tensor = torch.rand(1, 3, 5, 5)
>>> kernel = torch.ones(3, 3)

top_hat(tensor: torch.Tensor, kernel: torch.Tensor) → torch.Tensor[source]

Returns the top hat tranformation of an image.

That means, (image - opened_image) applying the same kernel in each channel. The kernel must have 2 dimensions, each one defined by an odd number.

See open for details.

Parameters
• tensor (torch.Tensor) – Image with shape $$(B, C, H, W)$$.

• kernel (torch.Tensor) – Structuring element with shape $$(k_x, k_y)$$.

Returns

Top hat transformated image with shape $$(B, C, H, W)$$.

Return type

torch.Tensor

Example

>>> tensor = torch.rand(1, 3, 5, 5)
>>> kernel = torch.ones(3, 3)
>>> top_hat_img = top_hat(tensor, kernel)

black_hat(tensor: torch.Tensor, kernel: torch.Tensor) → torch.Tensor[source]

Returns the black hat tranformation of an image.

That means, (closed_image - image) applying the same kernel in each channel. The kernel must have 2 dimensions, each one defined by an odd number.

See close for details.

Parameters
• tensor (torch.Tensor) – Image with shape $$(B, C, H, W)$$.

• kernel (torch.Tensor) – Structuring element with shape $$(k_x, k_y)$$.

Returns

Top hat transformated image with shape $$(B, C, H, W)$$.

Return type

torch.Tensor

Example

>>> tensor = torch.rand(1, 3, 5, 5)
>>> kernel = torch.ones(3, 3)
>>> black_hat_img = black_hat(tensor, kernel)