kornia.contrib¶
- connected_components(input, num_iterations=100)[source]¶
Computes the Connected-component labelling (CCL) algorithm.
The implementation is an adaptation of the following repository:
https://gist.github.com/efirdc/5d8bd66859e574c683a504a4690ae8bc
Note
This is an experimental API subject to changes and optimization improvements.
Note
See a working example here.
- Parameters
- Return type
- Returns
The labels image with the same shape of the input image.
Example
>>> 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
Module¶
- 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})\).
- where
\(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
andpadding
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.
- Parameters
window_size (
Union
[int
,Tuple
[int
,int
]]) – the size of the sliding window and the output patch size.stride (
Union
[int
,Tuple
[int
,int
],None
], optional) – stride of the sliding window. Default:1
padding (
Union
[int
,Tuple
[int
,int
],None
], optional) – Zero-padding added to both side of the input. Default:0
- Shape:
Input: \((B, C, H, W)\)
Output: \((B, N, C, H_{out}, W_{out})\)
- Returns
the tensor with the extracted patches.
Examples
>>> 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.]]]])