Blurring#
Functions#
- kornia.filters.bilateral_blur(input, kernel_size, sigma_color, sigma_space, border_type='reflect', color_distance_type='l1')[source]#
Blur a torch.Tensor using a Bilateral filter.
The operator is an edge-preserving image smoothing filter. The weight for each pixel in a neighborhood is determined not only by its distance to the center pixel, but also the difference in intensity or color.
- Convention:
A neighbour weighs \(g \, \exp(-d^2 / (2 \sigma_{color}^2))\). \(g\) is the Gaussian of
gaussian_blur2d()withsigma = sigma_space, \((\sigma_y, \sigma_x)\), over the wholekernel_sizerectangle, and \(d\) is the neighbour’s colour distance from the centre pixel: the sum of the absolute channel differences for'l1', their Euclidean norm for'l2'. Filtering compares this with OpenCV.sigma_coloris in the units of the input values: an image scaled bys > 0, filtered withsigma_color * s, gives the result scaled bys.The border modes are
filter2d()’s, but only in lower case; see its Convention block.A floating input keeps its dtype: a tensor
sigma_colororsigma_spaceis cast to it and moved to its device.An integer or bool input is differenced and averaged in float32 and the output is float32, as in
filter2d(), so uint8 differences do not wrap.On the CPU, a float32 or float64 input (after that conversion) with at least 65,536 values is filtered tap by tap from shifted slices of the padded input instead of from its unfolded windows, which avoids temporaries of
kernel_sizetimes the image, unless a reverse-mode gradient is recorded through any argument, ortorch.exportortorch.jit.tracecaptures the call, so an exported program keeps the windows for every input size. Undertorch.compile, only kernels of up to 25 taps are filtered tap by tap, since the compiled loop has a block of operations per tap. Both forms compute the same weights and add up the weighted values in a different order, so they agree to rounding.
- Parameters:
input (
Tensor) – the input torch.Tensor with shape \((B,C,H,W)\).kernel_size (
tuple[int,int] |int) – the size of the kernel. Each entry must be a positive odd integer.sigma_color (
float|Tensor) – the standard deviation for intensity/color Gaussian kernel. Smaller values preserve more edges. It must be positive. A float is shared by the batch; a torch.Tensor has shape \((1,)\), shared by the batch, or \((B,)\), one value per sample.sigma_space (
tuple[float,float] |Tensor) – the standard deviation for spatial Gaussian kernel. This is similar tosigmaingaussian_blur2d(). A tuple of two floats is shared by the batch; a torch.Tensor has shape \((1, 2)\), shared by the batch, or \((B, 2)\), one row per sample.border_type (
str, optional) – the padding mode to be applied before convolving. The expected modes are:'constant','reflect','replicate'or'circular'. Default:'reflect'.color_distance_type (
str, optional) – the type of distance to calculate intensity/color difference. Only'l1'or'l2'is allowed. Default:'l1'.
- Return type:
- Returns:
the blurred torch.Tensor with shape \((B, C, H, W)\).
- Raises:
Examples
>>> input = torch.rand(2, 4, 5, 5) >>> output = bilateral_blur(input, (3, 3), 0.1, (1.5, 1.5)) >>> output.shape torch.Size([2, 4, 5, 5])
- kornia.filters.blur_pool2d(input, kernel_size, stride=2)[source]#
Compute blurs and downsample a given feature map.
See
BlurPool2Dfor details.See [Zha19] for more details.
- Convention:
The blur is the normalised binomial (Pascal) kernel, which must be square. The blurred map is sampled every
stridepixels from index 0.The border is zero-padded,
(k - 1) // 2pixels before andk // 2after, and there is noborder_type, so a constant map comes out darker where the kernel reaches past the border. An evenkernel_sizeis thereby anchored at(k - 1) // 2, as infilter2d(). Filtering compares the padding with antialiased-cnns, and the sampling withpyrdown().
- Parameters:
- Shape:
Input: \((B, C, H, W)\)
Output: \((B, C, H_{out}, W_{out})\), where
\[H_{out} = \left\lceil\frac{H}{\text{stride}}\right\rceil, \quad W_{out} = \left\lceil\frac{W}{\text{stride}}\right\rceil\]
- Return type:
- Returns:
the transformed torch.Tensor.
Note
See a working example here.
Examples
>>> input = torch.eye(5)[None, None] >>> blur_pool2d(input, 3) tensor([[[[0.3125, 0.0625, 0.0000], [0.0625, 0.3750, 0.0625], [0.0000, 0.0625, 0.3125]]]])
- kornia.filters.box_blur(input, kernel_size, border_type='reflect', separable=True)[source]#
Blur an image using the box filter.
The function smooths an image using the kernel:
\[\begin{split}K = \frac{1}{\text{kernel_size}_x * \text{kernel_size}_y} \begin{bmatrix} 1 & 1 & 1 & \cdots & 1 & 1 \\ 1 & 1 & 1 & \cdots & 1 & 1 \\ \vdots & \vdots & \vdots & \ddots & \vdots & \vdots \\ 1 & 1 & 1 & \cdots & 1 & 1 \\ \end{bmatrix}\end{split}\]- Convention:
kernel_sizeis(kH, kW). An even extent is anchored at(k - 1) // 2and the output keeps the input’s size; see the Convention block onfilter2d()for the anchor and the border modes.An integer or bool input is blurred in float32 and the output is float32, as in
filter2d().
- Parameters:
input (
Tensor) – the image to blur with shape \((B,C,H,W)\).kernel_size (
tuple[int,int] |int) – the blurring kernel size.border_type (
str, optional) – the padding mode to be applied before convolving. The expected modes are:'constant','reflect','replicate'or'circular'. Default:'reflect'.separable (
bool, optional) – use two one-dimensional passes (the default), reducing work for larger kernels. The two implementations agree to floating-point roundoff while the sum of a window’s absolute values stays below the dtype’s largest finite value; beyond it the result can beinfor NaN. Default:True
- Return type:
- Returns:
the blurred torch.Tensor with shape \((B,C,H,W)\).
Note
See a working example here.
Example
>>> input = torch.rand(2, 4, 5, 7) >>> output = box_blur(input, (3, 3)) # 2x4x5x7 >>> output.shape torch.Size([2, 4, 5, 7])
- kornia.filters.edge_aware_blur_pool2d(input, kernel_size, edge_threshold=1.25, edge_dilation_kernel_size=3, epsilon=1e-6)[source]#
Blur the input torch.Tensor while maintaining its edges.
- Convention:
Nothing is downsampled: the output has the input’s shape.
A pixel is on an edge when, along
xory, the channel mean oflog2(input + epsilon)differs by more thanlog2(edge_threshold)between the pixels two before and two after it, so scaling the image does not move the edges while the intensities stay well aboveepsilon. Pixels on the edge map, dilated byedge_dilation_kernel_size, keep their input value; every other pixel takes the value blurred withblur_pool2d()’s kernel at stride 1, with the border reflected asborder_type='reflect'reflects it infilter2d(), so a constant image keeps its value to roundoff.The input is taken to be positive. Where
input + epsilonis negative its logarithm is NaN, which never exceeds the threshold, so a test that reads such a value never finds an edge, and those regions are blurred except where the dilation of an adjacent edge reaches them. Values in(-epsilon, 0]keep a finite logarithm and can form edges, andinput = -epsilongives-inf, an edge against any value above it.
- Parameters:
input (
Tensor) – the input image to blur with shape \((B, C, H, W)\).kernel_size (
tuple[int,int] |int) – the size of the square binomial blur kernel.edge_threshold (
float, optional) – intensity ratio between pixels 4 apart above which a pixel counts as an edge and keeps its value. It must be greater than 1. Default:1.25edge_dilation_kernel_size (
int, optional) – the kernel size for dilating the edges. It must be an odd positive integer. Default:3epsilon (
float, optional) – for numerical stability. Default:1e-6
- Return type:
- Returns:
The blurred torch.Tensor of shape \((B, C, H, W)\).
- kornia.filters.gaussian_blur2d(input, kernel_size, sigma, border_type='reflect', separable=True)[source]#
Create an operator that blurs a torch.Tensor using a Gaussian filter.
The operator smooths the given torch.Tensor with a gaussian kernel by convolving it to each channel. It supports batched operation.
- Convention:
kernel_sizeis(kH, kW)andsigmais \((\sigma_y, \sigma_x)\): both give the rows (y) first, sosigma[0]blurs alongHandsigma[1]alongW. Filtering maps both pairs onto OpenCV and scipy.The border modes are
filter2d()’s; see its Convention block.Float16 and bfloat16 inputs use float32 kernels and convolution, including both separable passes, and are rounded to the input dtype once at return. Enclosing autocast is disabled for this computation.
An integer or bool input is blurred in float32 and the output is float32, as in
filter2d();sigmais cast to float32 as well.
- Parameters:
input (
Tensor) – the input torch.Tensor with shape \((B,C,H,W)\).kernel_size (
tuple[int,int] |int) – the size of the kernel. Can be an integer or tuple of two integers (height, width).sigma (
tuple[float,float] |Tensor) – the standard deviation of the kernel. Can be a tuple of two floats or a torch.Tensor with shape \((1, 2)\), shared by the batch, or \((B, 2)\), one row per sample. Values must be positive.border_type (
str, optional) – the padding mode to be applied before convolving. The expected modes are:'constant','reflect','replicate'or'circular', case-insensitive. Default:'reflect'.separable (
bool, optional) – run as composition of two 1d-convolutions. Default:True.
- Return type:
- Returns:
the blurred torch.Tensor with shape \((B, C, H, W)\).
- Raises:
TypeCheckError – if input is not a torch.Tensor.
ShapeError – if input is not 4D.
BaseError – if sigma values are not positive.
BaseError – if the
sigmabatch is neither 1 nor the input batch.BaseError – if kernel_size is not a positive odd integer.
Note
See a working example here.
Examples
>>> import torch >>> input = torch.rand(2, 4, 5, 5) >>> output = gaussian_blur2d(input, (3, 3), (1.5, 1.5)) >>> output.shape torch.Size([2, 4, 5, 5])
>>> # Single kernel size applies to both dimensions >>> output = gaussian_blur2d(input, 3, (1.5, 1.5)) >>> output.shape torch.Size([2, 4, 5, 5])
>>> # Using batched sigma (different sigma per batch element) >>> sigma_batch = torch.tensor([[1.5, 1.5], [2.0, 2.0]]) >>> output = gaussian_blur2d(input[:2], (3, 3), sigma_batch) >>> output.shape torch.Size([2, 4, 5, 5])
>>> # Using torch.tensor sigma >>> output = gaussian_blur2d(input, (3, 3), torch.tensor([[1.5, 1.5]])) >>> output.shape torch.Size([2, 4, 5, 5])
- kornia.filters.guided_blur(guidance, input, kernel_size, eps, border_type='reflect', subsample=1, separable=False)[source]#
Blur a torch.Tensor using a Guided filter.
The operator is an edge-preserving image smoothing filter. See [HST10] and [HS15] for details. Guidance and input can have different number of channels.
- Convention:
guidancecomes first andinputsecond, the opposite ofjoint_bilateral_blur().kernel_sizeis the whole(kH, kW)window,2 * r + 1for the radiusrof [HST10]. The window means arebox_blur()’s, so the border modes and the anchor of an even size follow its Convention block.epsis added to the local variance ofguidance, so it is in squared guidance units: guidance and input scaled bys, filtered witheps * s**2, give the result scaled bys. Filtering maps the arguments onto OpenCV’sguidedFilter.An integer or bool
guidanceorinputis converted to float32 before the covariance products, on its own scale, and the result has the promoted dtype of the two: a float16inputwith a uint8guidancereturns float32, wherejoint_bilateral_blur()keepsinput’s dtype. With grayscale guidance, a tensor-valuedepsalso participates in result dtype promotion; multichannel guidance retains the promoted dtype ofguidanceandinput.A float16 or bfloat16
guidanceorinputis converted to float32 before the window statistics, and the coefficients are solved in float32 as well (in float64 with a float64 tensoreps). Autocast is disabled inside the filter, so a float32 call in an autocast region returns float32. The result has the dtype of the rule above.
- Parameters:
guidance (
Tensor) – the guidance torch.Tensor with shape \((B,C_g,H,W)\).input (
Tensor) – the input torch.Tensor with shape \((B,C_i,H,W)\).kernel_size (
tuple[int,int] |int) – the size of the kernel.eps (
float|Tensor) – regularization parameter. Smaller values preserve more edges.border_type (
str, optional) – the padding mode to be applied before convolving. The expected modes are:'constant','reflect','replicate'or'circular'. Default:'reflect'.subsample (
int, optional) – subsampling factor for Fast Guided filtering, a positive integer (aboolis rejected). Guidance and input are subsampled by nearest neighbour tomax(H // subsample, 1)rows andmax(W // subsample, 1)columns: everysubsample-th pixel whenHis a multiple ofsubsample(likewiseW), and otherwise, for rowi, the pixel nearest to(i + 0.5) * H / rows - subsample / 2(a tie rounds up), which keeps the subsampled grid aligned with the bilinear upsample of the coefficient maps as it is on a multiple; a single pixel, the first, on an axis shorter thansubsample.HandWneed not be multiples of it: the coefficient maps are resized back to the size of the input. Default: 1 (no subsampling)separable (
bool, optional) – use two one-dimensional box-filter passes, reducing work for large windows. Default: False
- Return type:
- Returns:
the blurred torch.Tensor with same shape as input \((B, C_i, H, W)\).
- Raises:
TypeCheckError – if
subsampleis not an integer (NumPy integers are accepted).BaseError – if
subsampleis not positive, or is abool.
Examples
>>> guidance = torch.rand(2, 3, 5, 5) >>> input = torch.rand(2, 4, 5, 5) >>> output = guided_blur(guidance, input, 3, 0.1) >>> output.shape torch.Size([2, 4, 5, 5])
- kornia.filters.joint_bilateral_blur(input, guidance, kernel_size, sigma_color, sigma_space, border_type='reflect', color_distance_type='l1')[source]#
Blur a torch.Tensor using a Joint Bilateral filter.
This operator is almost identical to a Bilateral filter. The only difference is that the color Gaussian kernel is computed based on another image called a guidance image. See
bilateral_blur()for more information.- Convention:
See the Convention block on
bilateral_blur(); the colour distances are taken inguidance, andinputis what gets averaged.inputcomes first andguidancesecond, the opposite ofguided_blur().guidancemay have its own channel count; its batch size and \((H, W)\) must equalinput’s.guidanceis cast to the dtype of a floatinginput, so the output keepsinput’s dtype. An integerinputis filtered in float32, as bybilateral_blur(), and itsguidanceis cast to float32.
- Parameters:
input (
Tensor) – the input torch.Tensor with shape \((B,C,H,W)\).guidance (
Tensor) – the guidance torch.Tensor with shape \((B,C_g,H,W)\).kernel_size (
tuple[int,int] |int) – the size of the kernel. Each entry must be a positive odd integer.sigma_color (
float|Tensor) – the standard deviation for intensity/color Gaussian kernel. Smaller values preserve more edges. It must be positive. A float is shared by the batch; a torch.Tensor has shape \((1,)\), shared by the batch, or \((B,)\), one value per sample.sigma_space (
tuple[float,float] |Tensor) – the standard deviation for spatial Gaussian kernel. This is similar tosigmaingaussian_blur2d(). A tuple of two floats is shared by the batch; a torch.Tensor has shape \((1, 2)\), shared by the batch, or \((B, 2)\), one row per sample.border_type (
str, optional) – the padding mode to be applied before convolving. The expected modes are:'constant','reflect','replicate'or'circular'. Default:'reflect'.color_distance_type (
str, optional) – the type of distance to calculate intensity/color difference. Only'l1'or'l2'is allowed. Default:'l1'.
- Return type:
- Returns:
the blurred torch.Tensor with shape \((B, C, H, W)\).
- Raises:
Examples
>>> input = torch.rand(2, 4, 5, 5) >>> guidance = torch.rand(2, 4, 5, 5) >>> output = joint_bilateral_blur(input, guidance, (3, 3), 0.1, (1.5, 1.5)) >>> output.shape torch.Size([2, 4, 5, 5])
- kornia.filters.max_blur_pool2d(input, kernel_size, stride=2, max_pool_size=2, ceil_mode=None)[source]#
Compute pools and blurs and downsample a given feature map.
See
MaxBlurPool2Dfor details.- Convention:
The result equals
blur_pool2d(F.max_pool2d(input, max_pool_size, stride=1), kernel_size, stride): the max pool runs at stride 1 without padding andstridebelongs to the blur. See the Convention block onblur_pool2d()for the kernel and the border.
- Parameters:
input (
Tensor) – torch.Tensor to apply operation to.kernel_size (
tuple[int,int] |int) – the size of the square binomial blur kernel.stride (
int, optional) – stride of the blur step. Default:2max_pool_size (
int, optional) – the kernel size for max pooling. Default:2ceil_mode (
bool|None, optional) – deprecated, has no effect. The max pool runs at stride 1, where floor and ceil rounding give the same size, and the strided blur has no rounding mode. Passing it emits aDeprecationWarning; it will be removed in a future release. Default:None
- Return type:
Note
See a working example here.
Examples
>>> input = torch.eye(5)[None, None] >>> max_blur_pool2d(input, 3) tensor([[[[0.5625, 0.3125], [0.3125, 0.8750]]]])
- kornia.filters.median_blur(input, kernel_size, border_type='reflect')[source]#
Blur an image using the median filter.
- Convention:
kernel_sizeis(kH, kW), rows first, and both entries are odd, so every window has one middle value.A window of more than one pixel that holds a NaN or an infinity returns NaN, even where the median of its values is finite; a 1x1 window returns its pixel, infinity included.
The border modes are
filter2d()’s; see its Convention block.An integer or bool image is filtered in float32 and the result is float32 on its own scale, like
box_blur(); a floating image keeps its dtype. A median selects one of its window’s values, so the result equals that ofinput.float()exactly.
- Parameters:
input (
Tensor) – the input image with shape \((B,C,H,W)\).kernel_size (
tuple[int,int] |int) – the blurring kernel size. Each entry must be a positive odd integer.border_type (
str, optional) – the padding mode to be applied before filtering. The expected modes are: ‘constant’, ‘reflect’, ‘replicate’ or ‘circular’. Default: ‘reflect’.
- Return type:
- Returns:
the blurred input torch.Tensor with shape \((B,C,H,W)\).
Note
See a working example here.
Example
>>> input = torch.rand(2, 4, 5, 7) >>> output = median_blur(input, (3, 3)) >>> output.shape torch.Size([2, 4, 5, 7])
- kornia.filters.motion_blur(input, kernel_size, angle, direction, border_type='reflect', mode='nearest')[source]#
Perform motion blur on torch.Tensor images.
- Convention:
The kernel is
get_motion_kernel2d()’s, correlated with the image byfilter2d(); their Convention blocks coverangle,direction(including the side on which the streak of a bright point is heaviest),mode, the tensor shapes and the border modes.
- Parameters:
input (
Tensor) – the input torch.Tensor with shape \((B, C, H, W)\).kernel_size (
int) – motion kernel width and height, an odd integer of at least 3.angle (Union[torch.Tensor, float]) – angle of the motion blur in degrees (anti-clockwise rotation). If torch.Tensor, it must be \((B,)\).
direction (
float|Tensor) – forward/backward direction of the motion blur. Lower values towards -1.0 will point the motion blur towards the back (with angle provided via angle), while higher values towards 1.0 will point the motion blur forward. A value of 0.0 leads to a uniformly (but still angled) motion blur. If torch.Tensor, it must be \((B,)\).border_type (
str, optional) – the padding mode to be applied before convolving. The expected modes are:'constant','reflect','replicate'or'circular'. Default:'reflect', which leaves a constant image constant likebox_blur()andgaussian_blur2d(), but needs each spatial axis longer thankernel_size // 2.'constant'zero-pads, so pixels whose kernel reaches past an edge (at mostkernel_size // 2from it) are pulled toward0.mode (
str, optional) – interpolation mode for rotating the kernel.'bilinear'or'nearest'. Default:"nearest"
- Return type:
- Returns:
the blurred image with shape \((B, C, H, W)\).
Example
>>> input = torch.randn(1, 3, 80, 90).repeat(2, 1, 1, 1) >>> # perform exact motion blur across the batch >>> out_1 = motion_blur(input, 5, 90., 1) >>> torch.allclose(out_1[0], out_1[1]) True >>> # perform element-wise motion blur across the batch >>> out_1 = motion_blur(input, 5, torch.tensor([90., 180,]), torch.tensor([1., -1.])) >>> torch.allclose(out_1[0], out_1[1]) False
- kornia.filters.motion_blur3d(input, kernel_size, angle, direction, border_type='replicate', mode='nearest')[source]#
Perform motion blur on 3D volumes (5D torch.Tensor).
- Convention:
The kernel is
get_motion_kernel3d()’s, correlated with the volume byfilter3d(); their Convention blocks coverangle,direction,modeand the border modes. Withdirection=1and a zeroanglethe streak of a bright voxel is heaviest toward increasingW; a positive roll alone turns it toward increasingH, and a positive pitch alone toward decreasingD.
- Parameters:
input (
Tensor) – the input torch.Tensor with shape \((B, C, D, H, W)\).kernel_size (
int) – motion kernel width, height and depth, an odd integer of at least 3.angle (
tuple[float,float,float] |Tensor) –(yaw, pitch, roll), one Rodrigues axis-angle vector(rx, ry, rz)in degrees, not Euler angles; seeget_motion_kernel3d(). If torch.Tensor, it must be \((B, 3)\).direction (
float|Tensor) – forward/backward direction of the motion blur. Lower values towards -1.0 will point the motion blur towards the back (with angle provided via angle), while higher values towards 1.0 will point the motion blur forward. A value of 0.0 leads to a uniformly (but still angled) motion blur. If torch.Tensor, it must be \((B,)\).border_type (
str, optional) – the padding mode to be applied before convolving. The expected modes are:'constant','reflect','replicate'or'circular'. Default:'replicate', which leaves a constant volume constant likefilter3d(), at any volume size ('reflect'needs each axis longer thankernel_size // 2, so it raises on a thin volume such asD = 1).'constant'zero-pads, so voxels whose kernel reaches past a face (at mostkernel_size // 2from it) are pulled toward0.mode (
str, optional) – interpolation mode for rotating the kernel.'bilinear'or'nearest'. Default:"nearest"
- Return type:
- Returns:
the blurred image with shape \((B, C, D, H, W)\).
Example
>>> input = torch.randn(1, 3, 120, 80, 90).repeat(2, 1, 1, 1, 1) >>> # perform exact motion blur across the batch >>> out_1 = motion_blur3d(input, 5, (0., 90., 90.), 1) >>> torch.allclose(out_1[0], out_1[1]) True >>> # perform element-wise motion blur across the batch >>> out_1 = motion_blur3d(input, 5, torch.tensor([[0., 90., 90.], [90., 180., 0.]]), torch.tensor([1., -1.])) >>> torch.allclose(out_1[0], out_1[1]) False
- kornia.filters.unsharp_mask(input, kernel_size, sigma, border_type='reflect')[source]#
Create an operator that sharpens a torch.Tensor by applying operation out = 2 * image - gaussian_blur2d(image).
- Convention:
The blur in the formula above is
gaussian_blur2d()with the samekernel_size,sigmaandborder_type, and the detail it removes is added back with a fixed gain of 1. See the Convention block ongaussian_blur2d()for the argument order.The output is not clamped: next to an edge it overshoots on both sides, so a step from 0 to 1 comes back below 0 and above 1. Filtering maps the call onto scikit-image.
An integer or bool image is filtered in float32 and the result is float32 on its own scale, like
gaussian_blur2d(); a floating image keeps its dtype.Under
torch.autocastthe blur runs in the autocast dtype, as ingaussian_blur2d()(autocast leaves a float64 image alone), and the result keeps the image’s dtype, as forsharpness().
- Parameters:
input (
Tensor) – the input torch.Tensor with shape \((B,C,H,W)\).kernel_size (
tuple[int,int] |int) – the size of the kernel.sigma (
tuple[float,float] |Tensor) – the standard deviation of the kernel.border_type (
str, optional) – the padding mode to be applied before convolving. The expected modes are:'constant','reflect','replicate'or'circular'. Default:'reflect'.
- Return type:
- Returns:
the sharpened torch.Tensor with shape \((B,C,H,W)\).
Examples
>>> input = torch.rand(2, 4, 5, 5) >>> output = unsharp_mask(input, (3, 3), (1.5, 1.5)) >>> output.shape torch.Size([2, 4, 5, 5])
Modules#
- class kornia.filters.BilateralBlur(kernel_size, sigma_color, sigma_space, border_type='reflect', color_distance_type='l1')[source]#
Blur a torch.Tensor using a Bilateral filter.
The operator is an edge-preserving image smoothing filter. The weight for each pixel in a neighborhood is determined not only by its distance to the center pixel, but also the difference in intensity or color.
- Convention:
See the Convention block on
bilateral_blur().
- Parameters:
kernel_size (
tuple[int,int] |int) – the size of the kernel. Each entry must be a positive odd integer.sigma_color (
float|Tensor) – the standard deviation for intensity/color Gaussian kernel. Smaller values preserve more edges. It must be positive. A float is shared by the batch; a torch.Tensor has shape \((1,)\), shared by the batch, or \((B,)\), one value per sample.sigma_space (
tuple[float,float] |Tensor) – the standard deviation for spatial Gaussian kernel. This is similar tosigmaingaussian_blur2d(). A tuple of two floats is shared by the batch; a torch.Tensor has shape \((1, 2)\), shared by the batch, or \((B, 2)\), one row per sample.border_type (
str, optional) – the padding mode to be applied before convolving. The expected modes are:'constant','reflect','replicate'or'circular'. Default:'reflect'.color_distance_type (
str, optional) – the type of distance to calculate intensity/color difference. Only'l1'or'l2'is allowed. Default:'l1'.
- Returns:
the blurred input torch.Tensor.
- Shape:
Input: \((B, C, H, W)\)
Output: \((B, C, H, W)\)
- Raises:
Examples
>>> input = torch.rand(2, 4, 5, 5) >>> blur = BilateralBlur((3, 3), 0.1, (1.5, 1.5)) >>> output = blur(input) >>> output.shape torch.Size([2, 4, 5, 5])
- class kornia.filters.BlurPool2D(kernel_size, stride=2)[source]#
Compute blur (anti-aliasing) and downsample a given feature map.
See [Zha19] for more details.
- Convention:
See the Convention block on
blur_pool2d().
- Parameters:
- Shape:
Input: \((B, C, H, W)\)
Output: \((B, C, H_{out}, W_{out})\), where
\[H_{out} = \left\lceil\frac{H}{\text{stride}}\right\rceil, \quad W_{out} = \left\lceil\frac{W}{\text{stride}}\right\rceil\]
Examples
>>> from kornia.filters.blur_pool import BlurPool2D >>> input = torch.eye(5)[None, None] >>> bp = BlurPool2D(kernel_size=3, stride=2) >>> bp(input) tensor([[[[0.3125, 0.0625, 0.0000], [0.0625, 0.3750, 0.0625], [0.0000, 0.0625, 0.3125]]]])
- class kornia.filters.BoxBlur(kernel_size, border_type='reflect', separable=True)[source]#
Blur an image using the box filter.
The function smooths an image using the kernel:
\[\begin{split}K = \frac{1}{\text{kernel_size}_x * \text{kernel_size}_y} \begin{bmatrix} 1 & 1 & 1 & \cdots & 1 & 1 \\ 1 & 1 & 1 & \cdots & 1 & 1 \\ \vdots & \vdots & \vdots & \ddots & \vdots & \vdots \\ 1 & 1 & 1 & \cdots & 1 & 1 \\ \end{bmatrix}\end{split}\]- Convention:
See the Convention block on
box_blur().
- Parameters:
kernel_size (
tuple[int,int] |int) – the blurring kernel size.border_type (
str, optional) – the padding mode to be applied before convolving. The expected modes are:'constant','reflect','replicate'or'circular'. Default:'reflect'.separable (
bool, optional) – use two one-dimensional passes (the default), reducing work for larger kernels. The two implementations agree to floating-point roundoff while the sum of a window’s absolute values stays below the dtype’s largest finite value; beyond it the result can beinfor NaN. Default:True
- Returns:
the blurred input torch.Tensor.
- Shape:
Input: \((B, C, H, W)\)
Output: \((B, C, H, W)\)
Example
>>> input = torch.rand(2, 4, 5, 7) >>> blur = BoxBlur((3, 3)) >>> output = blur(input) # 2x4x5x7 >>> output.shape torch.Size([2, 4, 5, 7])
- class kornia.filters.EdgeAwareBlurPool2D(kernel_size, edge_threshold=1.25, edge_dilation_kernel_size=3)[source]#
Apply an edge-aware anti-aliasing blur that keeps the input size.
This module blurs while preserving edges by using an edge-intensity threshold.
- Convention:
See the Convention block on
edge_aware_blur_pool2d().
- Parameters:
kernel_size (
tuple[int,int] |int) – The size of the square binomial blur kernel.edge_threshold (
float, optional) – The intensity ratio between pixels 4 apart above which a pixel counts as an edge and keeps its value. It must be greater than 1. Default: 1.25.edge_dilation_kernel_size (
int, optional) – The kernel size for dilating the edge map. It must be an odd positive integer. Default: 3.
- class kornia.filters.MaxBlurPool2D(kernel_size, stride=2, max_pool_size=2, ceil_mode=None)[source]#
Compute pools and blurs and downsample a given feature map.
Equivalent to
nn.Sequential(nn.MaxPool2d(max_pool_size, stride=1), BlurPool2D(kernel_size, stride)).See [Zha19] for more details.
- Convention:
See the Convention block on
max_blur_pool2d().
- Parameters:
kernel_size (
tuple[int,int] |int) – the size of the square binomial blur kernel.stride (
int, optional) – stride of the blur step. Default:2max_pool_size (
int, optional) – the kernel size for max pooling. Default:2ceil_mode (
bool|None, optional) – deprecated, has no effect. The max pool runs at stride 1, where floor and ceil rounding give the same size, and the strided blur has no rounding mode. Passing it emits aDeprecationWarning; it will be removed in a future release. Default:None
- Shape:
Input: \((B, C, H, W)\)
Output: \((B, C, H_{out}, W_{out})\), where
\[H_{out} = \left\lceil\frac{H - \text{max\_pool\_size} + 1}{\text{stride}}\right\rceil, \quad W_{out} = \left\lceil\frac{W - \text{max\_pool\_size} + 1}{\text{stride}}\right\rceil\]
- Returns:
the transformed torch.tensor.
- Return type:
Examples
>>> import torch.nn as nn >>> from kornia.filters.blur_pool import BlurPool2D >>> input = torch.eye(5)[None, None] >>> mbp = MaxBlurPool2D(kernel_size=3, stride=2, max_pool_size=2) >>> mbp(input) tensor([[[[0.5625, 0.3125], [0.3125, 0.8750]]]]) >>> seq = nn.Sequential(nn.MaxPool2d(kernel_size=2, stride=1), BlurPool2D(kernel_size=3, stride=2)) >>> seq(input) tensor([[[[0.5625, 0.3125], [0.3125, 0.8750]]]])
- class kornia.filters.MedianBlur(kernel_size, border_type='reflect')[source]#
Blur an image using the median filter.
- Convention:
See the Convention block on
median_blur().
- Parameters:
- Returns:
the blurred input torch.Tensor.
- Shape:
Input: \((B, C, H, W)\)
Output: \((B, C, H, W)\)
Example
>>> input = torch.rand(2, 4, 5, 7) >>> blur = MedianBlur((3, 3)) >>> output = blur(input) >>> output.shape torch.Size([2, 4, 5, 7])
- class kornia.filters.GaussianBlur2d(kernel_size, sigma, border_type='reflect', separable=True)[source]#
Create an operator that blurs a torch.Tensor using a Gaussian filter.
The operator smooths the given torch.Tensor with a gaussian kernel by convolving it to each channel. It supports batched operation.
- Convention:
See the Convention block on
gaussian_blur2d().
- Parameters:
kernel_size (
tuple[int,int] |int) – the size of the kernel.sigma (
tuple[float,float] |Tensor) – the standard deviation of the kernel.border_type (
str, optional) – the padding mode to be applied before convolving. The expected modes are:'constant','reflect','replicate'or'circular'. Default:'reflect'.separable (
bool, optional) – run as composition of two 1d-convolutions. Default:True
- Returns:
the blurred torch.Tensor.
- Shape:
Input: \((B, C, H, W)\)
Output: \((B, C, H, W)\)
Examples:
>>> input = torch.rand(2, 4, 5, 5) >>> gauss = GaussianBlur2d((3, 3), (1.5, 1.5)) >>> output = gauss(input) # 2x4x5x5 >>> output.shape torch.Size([2, 4, 5, 5])
- class kornia.filters.GuidedBlur(kernel_size, eps, border_type='reflect', subsample=1, separable=False)[source]#
Blur a torch.Tensor using a Guided filter.
The operator is an edge-preserving image smoothing filter. See [HST10] and [HS15] for details. Guidance and input can have different number of channels.
- Convention:
See the Convention block on
guided_blur().
- Parameters:
kernel_size (
tuple[int,int] |int) – the size of the kernel.eps (
float) – regularization parameter. Smaller values preserve more edges.border_type (
str, optional) – the padding mode to be applied before convolving. The expected modes are:'constant','reflect','replicate'or'circular'. Default:'reflect'.subsample (
int, optional) – subsampling factor for Fast Guided filtering, a positive integer (aboolis rejected). Guidance and input are subsampled by nearest neighbour tomax(H // subsample, 1)rows andmax(W // subsample, 1)columns: everysubsample-th pixel whenHis a multiple ofsubsample(likewiseW), and otherwise, for rowi, the pixel nearest to(i + 0.5) * H / rows - subsample / 2(a tie rounds up), which keeps the subsampled grid aligned with the bilinear upsample of the coefficient maps as it is on a multiple; a single pixel, the first, on an axis shorter thansubsample.HandWneed not be multiples of it: the coefficient maps are resized back to the size of the input. Default: 1 (no subsampling)separable (
bool, optional) – use two one-dimensional box-filter passes, reducing work for large windows. Default: False
- Returns:
the blurred input torch.Tensor.
- Raises:
TypeCheckError – if
subsampleis not an integer (NumPy integers are accepted).BaseError – if
subsampleis not positive, or is abool. Both are raised by the constructor.
- Shape:
Input: \((B, C_g, H, W)\), \((B, C_i, H, W)\)
Output: \((B, C_i, H, W)\)
Examples
>>> guidance = torch.rand(2, 3, 5, 5) >>> input = torch.rand(2, 4, 5, 5) >>> blur = GuidedBlur(3, 0.1) >>> output = blur(guidance, input) >>> output.shape torch.Size([2, 4, 5, 5])
- class kornia.filters.JointBilateralBlur(kernel_size, sigma_color, sigma_space, border_type='reflect', color_distance_type='l1')[source]#
Blur a torch.Tensor using a Joint Bilateral filter.
This operator is almost identical to a Bilateral filter. The only difference is that the color Gaussian kernel is computed based on another image called a guidance image. See
BilateralBlurfor more information.- Convention:
See the Convention block on
joint_bilateral_blur().
- Parameters:
kernel_size (
tuple[int,int] |int) – the size of the kernel. Each entry must be a positive odd integer.sigma_color (
float|Tensor) – the standard deviation for intensity/color Gaussian kernel. Smaller values preserve more edges. It must be positive. A float is shared by the batch; a torch.Tensor has shape \((1,)\), shared by the batch, or \((B,)\), one value per sample.sigma_space (
tuple[float,float] |Tensor) – the standard deviation for spatial Gaussian kernel. This is similar tosigmaingaussian_blur2d(). A tuple of two floats is shared by the batch; a torch.Tensor has shape \((1, 2)\), shared by the batch, or \((B, 2)\), one row per sample.border_type (
str, optional) – the padding mode to be applied before convolving. The expected modes are:'constant','reflect','replicate'or'circular'. Default:'reflect'.color_distance_type (
str, optional) – the type of distance to calculate intensity/color difference. Only'l1'or'l2'is allowed. Default:'l1'.
- Returns:
the blurred input torch.Tensor.
- Shape:
Input: \((B, C, H, W)\), \((B, C_g, H, W)\)
Output: \((B, C, H, W)\)
- Raises:
Examples
>>> input = torch.rand(2, 4, 5, 5) >>> guidance = torch.rand(2, 4, 5, 5) >>> blur = JointBilateralBlur((3, 3), 0.1, (1.5, 1.5)) >>> output = blur(input, guidance) >>> output.shape torch.Size([2, 4, 5, 5])
- class kornia.filters.MotionBlur(kernel_size, angle, direction, border_type='reflect', mode='nearest')[source]#
Blur 2D images (4D torch.Tensor) using the motion filter.
- Convention:
See the Convention block on
motion_blur().
- Parameters:
kernel_size (
int) – motion kernel width and height, an odd integer of at least 3.angle (
float) – angle of the motion blur in degrees (anti-clockwise rotation).direction (
float) – forward/backward direction of the motion blur. Lower values towards -1.0 will point the motion blur towards the back (with angle provided via angle), while higher values towards 1.0 will point the motion blur forward. A value of 0.0 leads to a uniformly (but still angled) motion blur.border_type (
str, optional) – the padding mode to be applied before convolving. The expected modes are:'constant','reflect','replicate'or'circular'. Default:'reflect', which leaves a constant image constant likebox_blur()andgaussian_blur2d(), but needs each spatial axis longer thankernel_size // 2.'constant'zero-pads, so pixels whose kernel reaches past an edge (at mostkernel_size // 2from it) are pulled toward0.mode (
str, optional) – interpolation mode for rotating the kernel.'bilinear'or'nearest'. Default:"nearest"
- Returns:
the blurred input torch.Tensor.
- Shape:
Input: \((B, C, H, W)\)
Output: \((B, C, H, W)\)
Examples
>>> input = torch.rand(2, 4, 5, 7) >>> motion_blur = MotionBlur(3, 35., 0.5) >>> output = motion_blur(input) # 2x4x5x7
- class kornia.filters.MotionBlur3D(kernel_size, angle, direction, border_type='replicate', mode='nearest')[source]#
Blur 3D volumes (5D torch.Tensor) using the motion filter.
- Convention:
See the Convention block on
motion_blur3d().
- Parameters:
kernel_size (
int) – motion kernel width, height and depth, an odd integer of at least 3.angle (
float|tuple[float,float,float] |list[float] |Tensor) – Components of one Rodrigues axis-angle vector(rx, ry, rz)in degrees, not Euler angles; seeget_motion_kernel3d(). A scalar sets all three components to the same value; a three-element sequence sets each component, and a tensor must have shape \((B, 3)\).direction (
float|Tensor) – forward/backward direction of the motion blur. Lower values towards -1.0 will point the motion blur towards the back (with angle provided via angle), while higher values towards 1.0 will point the motion blur forward. A value of 0.0 leads to a uniformly (but still angled) motion blur.border_type (
str, optional) – the padding mode to be applied before convolving. The expected modes are:'constant','reflect','replicate'or'circular'. Default:'replicate', which leaves a constant volume constant likefilter3d(), at any volume size ('reflect'needs each axis longer thankernel_size // 2, so it raises on a thin volume such asD = 1).'constant'zero-pads, so voxels whose kernel reaches past a face (at mostkernel_size // 2from it) are pulled toward0.mode (
str, optional) – interpolation mode for rotating the kernel.'bilinear'or'nearest'. Default:"nearest"
- Returns:
the blurred input torch.Tensor.
- Shape:
Input: \((B, C, D, H, W)\)
Output: \((B, C, D, H, W)\)
Examples
>>> input = torch.rand(2, 4, 5, 7, 9) >>> motion_blur = MotionBlur3D(3, 35., 0.5) >>> output = motion_blur(input) # 2x4x5x7x9
- class kornia.filters.UnsharpMask(kernel_size, sigma, border_type='reflect')[source]#
Create an operator that sharpens image with: out = 2 * image - gaussian_blur2d(image).
- Convention:
See the Convention block on
unsharp_mask().
- Parameters:
kernel_size (
tuple[int,int] |int) – the size of the kernel.sigma (
tuple[float,float] |Tensor) – the standard deviation of the kernel.border_type (
str, optional) – the padding mode to be applied before convolving. The expected modes are:'constant','reflect','replicate'or'circular'. Default:'reflect'.
- Returns:
the sharpened torch.Tensor with shape \((B,C,H,W)\).
- Shape:
Input: \((B, C, H, W)\)
Output: \((B, C, H, W)\)
Note
See a working example here.
Examples
>>> input = torch.rand(2, 4, 5, 5) >>> sharpen = UnsharpMask((3, 3), (1.5, 1.5)) >>> output = sharpen(input) >>> output.shape torch.Size([2, 4, 5, 5])
Dissolving#
- class kornia.filters.StableDiffusionDissolving(version='1.5', **kwargs)[source]#
Perform dissolving transformation using StableDiffusion models.
Based on [SZZ+24], the dissolving transformation is essentially applying one-step reverse diffusion. Our implementation currently supports HuggingFace implementations of SD 1.4, 1.5 and SD XL (replacing the discontinued SD 2.1). SD 1.X tends to remove more details than SD XL.
SD 1.4
SD 1.5
SD XL
SD XL example image pending — see #4601.
- Convention:
See the Convention block on
RandomDissolving.