connected_components(input, num_iterations=100)[source]

Computes the Connected-component labelling (CCL) algorithm.


The implementation is an adaptation of the following repository:



This is an experimental API subject to changes and optimization improvements.


See a working example here.

  • input (Tensor) – the binarized input image with shape \((B, 1, H, W)\). The image must be in floating point with range [0, 1].

  • num_iterations (int, optional) – the number of iterations to make the algorithm to converge. Default: 100

Return type



The labels image with the same shape of the input image.


>>> img = torch.rand(2, 1, 4, 5)
>>> img_labels = connected_components(img, num_iterations=100)
extract_tensor_patches(input, window_size, stride=1, padding=0)[source]

Function that extract patches from tensors and stack them.

See ExtractTensorPatches for details.

Return type



class ExtractTensorPatches(window_size, stride=1, padding=0)[source]

Module that extract patches from tensors and stack them.

In the simplest case, the output value of the operator with input size \((B, C, H, W)\) is \((B, N, C, H_{out}, W_{out})\).

  • \(B\) is the batch size.

  • \(N\) denotes the total number of extracted patches stacked in

  • \(C\) denotes the number of input channels.

  • \(H\), \(W\) the input height and width of the input in pixels.

  • \(H_{out}\), \(W_{out}\) denote to denote to the patch size defined in the function signature. left-right and top-bottom order.

  • window_size is the size of the sliding window and controls the shape of the output tensor and defines the shape of the output patch.

  • stride controls the stride to apply to the sliding window and regulates the overlapping between the extracted patches.

  • padding controls the amount of implicit zeros-paddings on both sizes at each dimension.

The parameters window_size, stride and padding can be either:

  • a single int – in which case the same value is used for the height and width dimension.

  • a tuple of two ints – in which case, the first int is used for the height dimension, and the second int for the width dimension.

  • Input: \((B, C, H, W)\)

  • Output: \((B, N, C, H_{out}, W_{out})\)


the tensor with the extracted patches.


>>> input = torch.arange(9.).view(1, 1, 3, 3)
>>> patches = extract_tensor_patches(input, (2, 3))
>>> input
tensor([[[[0., 1., 2.],
          [3., 4., 5.],
          [6., 7., 8.]]]])
>>> patches[:, -1]
tensor([[[[3., 4., 5.],
          [6., 7., 8.]]]])