Kernels#
Kernel generators, useful with the Filtering API or to inspect what a filter applies.
- kornia.filters.gaussian(window_size, sigma, *, mean=None, device=None, dtype=None)[source]#
Compute the gaussian values based on the window and sigma values.
- Convention:
See the Convention block on
get_gaussian_kernel1d(), which validates the size and calls this function;gaussiandoes not validate it. In a floatingdtype, for an evenwindow_sizethe Gaussian is centred atmean - 0.5: the defaultmeancentres the kernel on the middle of the window, halfway between the two middle samples, and an explicitmean=mcentres it atm - 0.5.
- Parameters:
window_size (
int) – the size which drives the filter amount.sigma (
Tensor|float) – gaussian standard deviation. If a tensor, should be in a shape \((B, 1)\).mean (
Union[float,Tensor,None], optional) – Mean of the Gaussian function (center); see the Convention block for an evenwindow_size. If not provided, it defaults towindow_size // 2. A Pythonintis treated as itsfloat. If a tensor, should be in a shape \((B, 1)\). Default:Nonedevice (
Optional[device], optional) – This value will be used if sigma is an int or float. Device desired to compute. Default:Nonedtype (
Optional[dtype], optional) – This value will be used if sigma is an int or float. Dtype desired for compute. Default:None
- Return type:
- Returns:
A tensor with shape \((B, \text{kernel_size})\), with Gaussian values.
Note
A
sigmaof zero – and anysigmatoo small for the window to hold a representable weight – returns the unit-impulse limit of the kernel: all the mass on the sample nearest the mean, split evenly between the two centre taps whenwindow_sizeis even. The gradient with respect tosigmais zero there, matching the continuous limit.
- kornia.filters.get_gaussian_kernel1d(kernel_size, sigma, force_even=False, *, device=None, dtype=None)[source]#
Return Gaussian filter coefficients.
- Convention:
In a floating
dtypethe kernel samples \(\exp(-n^2 / 2\sigma^2)\) at the offsets \(n\) of its taps from its centre, integers for an odd size and half-integers for an even one, and is normalized to sum 1; Filtering names the matching scipy and OpenCV kernels.get_gaussian_erf_kernel1d()integrates the Gaussian over each pixel instead, andget_gaussian_discrete_kernel1d()is the discrete Gaussian.force_even=Truealso accepts an evenkernel_size, and in a floatingdtypethe kernel is then symmetric about the middle of the window,(kernel_size - 1) / 2.A tensor
sigmaof shape \((B, 1)\) gives one kernel per row.A Python integer
sigmais treated as its floating-point equivalent. Withdtype=None, both use the default floating-point dtype.An integer or bool
dtyperaises: the taps are fractional, and an integerdtypewould also truncate a fractionalsigma(#5155).
- Parameters:
kernel_size (
int) – filter size. It should be odd and positive.force_even (
bool, optional) – overrides requirement for odd kernel size. Default:Falsedevice (
Optional[device], optional) – This value will be used if sigma is an int or float. Device desired to compute. Default:Nonedtype (
Optional[dtype], optional) – This value will be used if sigma is an int or float. Dtype desired for compute. Default:None
- Return type:
- Returns:
gaussian filter coefficients with shape \((B, \text{kernel_size})\).
Examples
>>> get_gaussian_kernel1d(3, 2.5) tensor([[0.3243, 0.3513, 0.3243]]) >>> get_gaussian_kernel1d(5, 1.5) tensor([[0.1201, 0.2339, 0.2921, 0.2339, 0.1201]]) >>> get_gaussian_kernel1d(5, torch.tensor([[1.5], [0.7]])) tensor([[0.1201, 0.2339, 0.2921, 0.2339, 0.1201], [0.0096, 0.2054, 0.5699, 0.2054, 0.0096]])
A
sigmaof zero, or one too small for the window to hold a representable weight, returns the unit impulse rather than a kernel of NaN:>>> get_gaussian_kernel1d(5, 0.0) tensor([[0., 0., 1., 0., 0.]]) >>> get_gaussian_kernel1d(4, 0.0, force_even=True) tensor([[0.0000, 0.5000, 0.5000, 0.0000]])
- kornia.filters.get_gaussian_erf_kernel1d(kernel_size, sigma, force_even=False, *, device=None, dtype=None)[source]#
Return Gaussian filter coefficients by interpolating the error function.
Adapted from: Project-MONAI/MONAI.
- Convention:
See the Convention block on
get_gaussian_kernel1d(). In a floatingdtypethis kernel integrates the Gaussian over each pixel, \(\Phi((n + 1/2) / \sigma) - \Phi((n - 1/2) / \sigma)\) with \(\Phi\) the normal CDF, so it blurs more than the sampled kernel: for \(\sigma\) of about 1 or more, on a window wide enough for the tails, its variance is \(\sigma^2 + 1/12\).With
force_even=Truean even kernel is symmetric about the middle of the window,(kernel_size - 1) / 2, as the sampled kernel is.
- Parameters:
kernel_size (
int) – filter size. It should be odd and positive.sigma (
float|Tensor) – gaussian standard deviation. If a tensor, should be in a shape \((B, 1)\)force_even (
bool, optional) – overrides requirement for odd kernel size. Default:Falsedevice (
Optional[device], optional) – This value will be used if sigma is an int or float. Device desired to compute. Default:Nonedtype (
Optional[dtype], optional) – This value will be used if sigma is an int or float. Dtype desired for compute. Default:None
- Return type:
- Returns:
1D tensor with gaussian filter coefficients. Shape \((B, \text{kernel_size})\)
Examples
>>> get_gaussian_erf_kernel1d(3, 2.0) tensor([[0.3195, 0.3611, 0.3195]]) >>> get_gaussian_erf_kernel1d(5, 1.5) tensor([[0.1226, 0.2331, 0.2887, 0.2331, 0.1226]]) >>> get_gaussian_erf_kernel1d(5, torch.tensor([[1.5], [2.1]])) tensor([[0.1226, 0.2331, 0.2887, 0.2331, 0.1226], [0.1574, 0.2198, 0.2456, 0.2198, 0.1574]])
- kornia.filters.get_gaussian_discrete_kernel1d(kernel_size, sigma, force_even=False, *, device=None, dtype=None)[source]#
Return Gaussian filter coefficients based on the modified Bessel functions.
Adapted from: Project-MONAI/MONAI.
- Convention:
See the Convention block on
get_gaussian_kernel1d(). In a floatingdtypethis kernel is Lindeberg’s discrete Gaussian \(e^{-\sigma^2} I_{|n|}(\sigma^2)\), with \(I_n\) the modified Bessel function of the first kind, normalized over the window: the smoothing kernel of discrete scale space. A float16 or bfloat16 kernel is computed in float32 and rounded to itsdtype.The kernel has
kernel_sizetaps, andkernel_size=1is the single tap[1.0]. The discrete Gaussian is defined at integer offsets from the centre tap, so there is no even window: an evenkernel_sizeraises with and withoutforce_even.
- Parameters:
kernel_size (
int) – filter size. It should be odd and positive.sigma (
float|Tensor) – gaussian standard deviation. If a tensor, should be in a shape \((B, 1)\)force_even (
bool, optional) – accepted for parity with the other Gaussian builders. The discrete Gaussian needs an oddkernel_sizeeither way. Default:Falsedevice (
Optional[device], optional) – This value will be used if sigma is an int or float. Device desired to compute. Default:Nonedtype (
Optional[dtype], optional) – This value will be used if sigma is an int or float. Dtype desired for compute. Default:None
- Return type:
- Returns:
1D tensor with gaussian filter coefficients. Shape \((B, \text{kernel_size})\)
Examples
>>> get_gaussian_discrete_kernel1d(3, 2.5) tensor([[0.3235, 0.3531, 0.3235]]) >>> get_gaussian_discrete_kernel1d(5, 1.5) tensor([[0.1096, 0.2323, 0.3161, 0.2323, 0.1096]]) >>> get_gaussian_discrete_kernel1d(5, torch.tensor([[1.5],[2.4]])) tensor([[0.1096, 0.2323, 0.3161, 0.2323, 0.1096], [0.1635, 0.2170, 0.2389, 0.2170, 0.1635]])
- kornia.filters.get_gaussian_kernel2d(kernel_size, sigma, force_even=False, *, device=None, dtype=None)[source]#
Return Gaussian filter matrix coefficients.
- Convention:
See the Convention block on
get_gaussian_kernel1d().kernel_sizeis(k_y, k_x)andsigmais \((\sigma_y, \sigma_x)\), y first as in the output \((B, k_y, k_x)\), which is the outer product of the two 1d kernels. A tensorsigmaof shape \((B, 2)\) gives one kernel per row.
- Parameters:
kernel_size (
tuple[int,int] |int) – filter sizes in the y and x direction. Sizes should be odd and positive.sigma (
tuple[float,float] |Tensor) – gaussian standard deviation in the y and x.force_even (
bool, optional) – overrides requirement for odd kernel size. Default:Falsedevice (
Optional[device], optional) – This value will be used if sigma is a float. Device desired to compute. Default:Nonedtype (
Optional[dtype], optional) – This value will be used if sigma is a float. Dtype desired for compute. Default:None
- Return type:
- Returns:
2D tensor with gaussian filter matrix coefficients.
- Shape:
Output: \((B, \text{kernel_size}_y, \text{kernel_size}_x)\)
Examples
>>> get_gaussian_kernel2d((5, 5), (1.5, 1.5)) tensor([[[0.0144, 0.0281, 0.0351, 0.0281, 0.0144], [0.0281, 0.0547, 0.0683, 0.0547, 0.0281], [0.0351, 0.0683, 0.0853, 0.0683, 0.0351], [0.0281, 0.0547, 0.0683, 0.0547, 0.0281], [0.0144, 0.0281, 0.0351, 0.0281, 0.0144]]]) >>> get_gaussian_kernel2d((3, 5), (1.5, 1.5)) tensor([[[0.0370, 0.0720, 0.0899, 0.0720, 0.0370], [0.0462, 0.0899, 0.1123, 0.0899, 0.0462], [0.0370, 0.0720, 0.0899, 0.0720, 0.0370]]]) >>> get_gaussian_kernel2d((5, 5), torch.tensor([[1.5, 1.5]])) tensor([[[0.0144, 0.0281, 0.0351, 0.0281, 0.0144], [0.0281, 0.0547, 0.0683, 0.0547, 0.0281], [0.0351, 0.0683, 0.0853, 0.0683, 0.0351], [0.0281, 0.0547, 0.0683, 0.0547, 0.0281], [0.0144, 0.0281, 0.0351, 0.0281, 0.0144]]])
- kornia.filters.get_gaussian_kernel3d(kernel_size, sigma, force_even=False, *, device=None, dtype=None)[source]#
Return Gaussian filter matrix coefficients.
- Convention:
See the Convention block on
get_gaussian_kernel1d().kernel_sizeis(k_z, k_y, k_x)andsigmais \((\sigma_z, \sigma_y, \sigma_x)\), z first as in the output \((B, k_z, k_y, k_x)\), which is the outer product of the three 1d kernels. A tensorsigmaof shape \((B, 3)\) gives one kernel per row.
- Parameters:
kernel_size (
tuple[int,int,int] |int) – filter sizes in the z, y and x direction. Sizes should be odd and positive.sigma (
tuple[float,float,float] |Tensor) – gaussian standard deviation in the z, y and x direction.force_even (
bool, optional) – overrides requirement for odd kernel size. Default:Falsedevice (
Optional[device], optional) – This value will be used if sigma is a float. Device desired to compute. Default:Nonedtype (
Optional[dtype], optional) – This value will be used if sigma is a float. Dtype desired for compute. Default:None
- Return type:
- Returns:
3D tensor with gaussian filter matrix coefficients.
- Shape:
Output: \((B, \text{kernel_size}_z, \text{kernel_size}_y, \text{kernel_size}_x)\)
Examples
>>> get_gaussian_kernel3d((3, 3, 3), (1.5, 1.5, 1.5)) tensor([[[[0.0292, 0.0364, 0.0292], [0.0364, 0.0455, 0.0364], [0.0292, 0.0364, 0.0292]], [[0.0364, 0.0455, 0.0364], [0.0455, 0.0568, 0.0455], [0.0364, 0.0455, 0.0364]], [[0.0292, 0.0364, 0.0292], [0.0364, 0.0455, 0.0364], [0.0292, 0.0364, 0.0292]]]]) >>> torch.allclose(get_gaussian_kernel3d((3, 3, 3), (1.5, 1.5, 1.5)).sum(), torch.tensor(1.0)) True >>> get_gaussian_kernel3d((3, 3, 3), (1.5, 1.5, 1.5)).shape torch.Size([1, 3, 3, 3]) >>> get_gaussian_kernel3d((3, 7, 5), torch.tensor([[1.5, 1.5, 1.5]])).shape torch.Size([1, 3, 7, 5])
- kornia.filters.get_box_kernel1d(kernel_size, *, device=None, dtype=None)[source]#
Return a 1-D box filter.
- Convention:
For a floating
dtypeevery tap is1 / kernel_size; an evenkernel_sizeis accepted.The kernel is a contiguous tensor, so an in-place edit changes only the taps it addresses.
An integer or bool
dtyperaises: it would truncate1 / kernel_sizeto 0 (#5155).
- Parameters:
- Return type:
- Returns:
A tensor with shape \((1, \text{kernel\_size})\), filled with the value \(\frac{1}{\text{kernel\_size}}\) for a floating
dtype.
- kornia.filters.get_box_kernel2d(kernel_size, *, device=None, dtype=None)[source]#
Return a 2-D box filter.
- Convention:
See the Convention block on
get_box_kernel1d();kernel_sizeis(k_y, k_x).
- Parameters:
- Return type:
- Returns:
A tensor with shape \((1, \text{kernel\_size}[0], \text{kernel\_size}[1])\), filled with the value \(\frac{1}{\text{kernel\_size}[0] \times \text{kernel\_size}[1]}\) for a floating
dtype.
- kornia.filters.get_binary_kernel2d(window_size, *, device=None, dtype=torch.float32)[source]#
Create a binary kernel to extract the patches.
If the window size is HxW will create a (H*W)x1xHxW kernel.
- Convention:
Channel
iis one-hot at(i // k_x, i % k_x)forwindow_size=(k_y, k_x), in row-major order, sotorch.nn.functional.conv2d()of an image with the kernel stacks each pixel’s neighbourhood in that order, the layoutmedian_blur()builds on.
- Parameters:
- Return type:
- Returns:
the kernel with shape \((k_y k_x, 1, k_y, k_x)\).
- kornia.filters.get_hanning_kernel1d(kernel_size, device=None, dtype=None)[source]#
Return Hanning (also known as Hann) kernel.
\[w(n) = 0.5 - 0.5 \cos\left(\frac{2\pi n}{M - 1}\right) \qquad 0 \leq n \leq M - 1\]- Convention:
This is the symmetric window of size \(M\): both end taps are 0, so only \(M - 2\) samples carry weight, and it is not normalized, its taps summing to \((M - 1) / 2\). An even size is accepted and is symmetric about \((M - 1) / 2\). Filtering names the matching numpy and torch windows.
- Parameters:
- Return type:
- Returns:
1D tensor with Hanning filter coefficients. Shape \((\text{kernel_size})\).
Examples
>>> get_hanning_kernel1d(4) tensor([0.0000, 0.7500, 0.7500, 0.0000])
- kornia.filters.get_hanning_kernel2d(kernel_size, device=None, dtype=None)[source]#
Return 2d Hanning kernel.
- Convention:
See the Convention block on
get_hanning_kernel1d(). Forkernel_size=(k_y, k_x)the kernel is the outer product of the 1d windows of sizesk_y(alongH) andk_x(alongW).
- Parameters:
kernel_size (
tuple[int,int] |int) – The size of the kernel for the filter, an integer or(k_y, k_x), each greater than 2.device (
Union[str,device,None], optional) – tensor device desired to create the kernel Default:Nonedtype (
Optional[dtype], optional) – tensor dtype desired to create the kernel Default:None
- Return type:
- Returns:
2D tensor with Hanning filter coefficients. Shape \((k_y, k_x)\).
- kornia.filters.get_laplacian_kernel1d(kernel_size, *, device=None, dtype=torch.float32)[source]#
Return the coefficients of a 1D Laplacian filter.
- Convention:
In a floating
dtype, or a signed integer one wide enough to hold1 - kernel_size, the kernel is all ones with the centre tap set to1 - kernel_size, so it sums to 0. Size 3 is the second difference[1, -2, 1]; a larger size is not a wider second difference, and size 5 answers 5 to a unit second derivative.The negative centre makes the response positive where the values curve upwards.
An integer or bool
dtypethat cannot hold1 - kernel_sizeraises, so uint8 and bool always do, rather than wrap the centre (to 252 for size 5 in uint8) (#5155).
- Parameters:
- Return type:
- Returns:
1D tensor with laplacian filter coefficients.
- Raises:
BaseError – if
kernel_sizeis even, not positive, or1: a single tap is the all-zero kernel.
- Shape:
Output: \((\text{kernel_size})\)
Examples
>>> get_laplacian_kernel1d(3) tensor([ 1., -2., 1.]) >>> get_laplacian_kernel1d(5) tensor([ 1., 1., -4., 1., 1.])
- kornia.filters.get_laplacian_kernel2d(kernel_size, *, device=None, dtype=torch.float32)[source]#
Return Laplacian filter matrix coefficients.
- Convention:
See the Convention block on
get_laplacian_kernel1d(): in a floatingdtype, or a signed integer one wide enough to hold \(1 - k_y k_x\), all ones with the centre set to \(1 - k_y k_x\) forkernel_size=(k_y, k_x). Size 3 is the 8-neighbour stencil, which answers 3 to a unit \(\partial_{xx}\) or \(\partial_{yy}\), so it estimates \(3 \nabla^2\); size 5 estimates \(25 \nabla^2\). Filtering compares it with the scipy, OpenCV and scikit-image Laplacians.
- Parameters:
- Return type:
- Returns:
2D tensor with laplacian filter matrix coefficients.
- Raises:
BaseError – if a size is even or not positive, if
kernel_sizeis a sequence of other than 2 sizes, or if it is1or(1, 1): a \(1 \times 1\) kernel is the all-zero kernel.
- Shape:
Output: \((\text{kernel_size}_y, \text{kernel_size}_x)\)
Examples
>>> get_laplacian_kernel2d(3) tensor([[ 1., 1., 1.], [ 1., -8., 1.], [ 1., 1., 1.]]) >>> get_laplacian_kernel2d(5) tensor([[ 1., 1., 1., 1., 1.], [ 1., 1., 1., 1., 1.], [ 1., 1., -24., 1., 1.], [ 1., 1., 1., 1., 1.], [ 1., 1., 1., 1., 1.]])
- kornia.filters.laplacian_1d(window_size, *, device=None, dtype=torch.float32)[source]#
Return the 1D Laplacian kernel of
get_laplacian_kernel1d()without checking the size.- Convention:
See the Convention block on
get_laplacian_kernel1d(), which validates the size and calls this function.laplacian_1daccepts any positive size; an even one puts the negative tap atwindow_size // 2, off the middle of the kernel.
- Parameters:
- Return type:
- Returns:
1D tensor with shape \((\text{window_size},)\).
- kornia.filters.get_spatial_gradient_kernel2d(mode, order, *, device=None, dtype=None)[source]#
Return kernel for 1st or 2nd order image gradients.
Uses one of the following operators: sobel, diff.
- Convention:
order=1stacks \((\partial_x, \partial_y)\) along the first axis andorder=2stacks \((\partial_{xx}, \partial_{xy}, \partial_{yy})\). Correlated with an image, asfilter2d()does, anorder=1channel is positive where the values increase with the column (x) or the row (y), and anorder=2channel is positive on \(x^2\), \(xy\) and \(y^2\) respectively.The kernels are raw integer stencils, not derivative estimates. Per unit slope Sobel answers 8 and
'diff'answers 2; per unit second derivative Sobel answers 64 on all three channels, while'diff'answers 1 on \(\partial_{xx}\) and \(\partial_{yy}\) and 4 on \(\partial_{xy}\).modeis case-insensitive:'Sobel'is'sobel'.
- Parameters:
- Return type:
- Returns:
the kernels with shape \((2, 3, 3)\) for
order=1, and \((3, 5, 5)\) (Sobel) or \((3, 3, 3)\) (diff) fororder=2.
- kornia.filters.get_spatial_gradient_kernel3d(mode, order, device=None, dtype=None)[source]#
Return kernel for 1st or 2nd order gradients of a volume.
- Convention:
Only
mode='diff'exists;'sobel'passes the mode check and raisesNotImplementedError.order=1stacks \((\partial_x, \partial_y, \partial_z)\) andorder=2stacks \((\partial_{xx}, \partial_{yy}, \partial_{zz}, \partial_{xy}, \partial_{yz}, \partial_{xz})\), an order that differs from the 2d \((\partial_{xx}, \partial_{xy}, \partial_{yy})\). The stack has a singleton second axis thatget_spatial_gradient_kernel2d()lacks.In a floating
dtypeevery channel is in derivative units: it answers 1 to a unit slope or a unit second derivative, unlike the raw 2d stencils.modeis case-insensitive:'Diff'is'diff'.An integer or bool
dtyperaises: it would truncate the half and quarter taps to 0, leaving the first-order channels and the mixed second-order ones all zero (#5155).
- Parameters:
- Return type:
- Returns:
the kernels with shape \((3, 1, 3, 3, 3)\) for
order=1and \((6, 1, 3, 3, 3)\) fororder=2.
- kornia.filters.get_sobel_kernel2d(*, device=None, dtype=None)[source]#
Return 1st order gradient for sobel operator.
- Return type:
- Convention:
See the Convention block on
get_spatial_gradient_kernel2d(); this isget_spatial_gradient_kernel2d('sobel', 1), of shape \((2, 3, 3)\).
- kornia.filters.get_diff_kernel2d(*, device=None, dtype=None)[source]#
Return 1st order gradient for diff operator.
- Return type:
- Convention:
See the Convention block on
get_spatial_gradient_kernel2d(); this isget_spatial_gradient_kernel2d('diff', 1), of shape \((2, 3, 3)\).
- kornia.filters.get_motion_kernel2d(kernel_size, angle, direction=0.0, mode='nearest')[source]#
Return 2D motion blur filter.
- Convention:
directionis clamped to[-1, 1]and weighs the line linearly before it is rotated:1gives[0.4, 0.3, 0.2, 0.1, 0]along the middle row of a size-5 kernel, heavy at the left end and 0 at the right, so onlykernel_size - 1taps carry weight;-1mirrors it and0is uniform.angleis in degrees and turns the line counter-clockwise as displayed (row 0 at the top), asrotate()does: at90the heavy end moves from the left to the bottom.Because
filter2d()correlates, the streak that the kernel draws from a bright point is heaviest on the side opposite the kernel’s heavy end: to the point’s right atangle=0,direction=1.The rotation resamples the line with
mode:'nearest'copies each pixel from the nearest tap, so the number of taps changes with the angle (3 at 45 degrees, 7 at 25 for a size-5 line), and'bilinear'spreads the weight off the line.A floating tensor
angleof shape \((B,)\) gives \((B, k, k)\) in its dtype. A tensordirectionmust match it in length, dtype and device; a floatdirectionis not broadcast, so it raises forB > 1.With
'nearest', each pixel copies the tap nearest to its source under the inverse rotation, picked with exact arithmetic: where the source falls exactly between two taps, such as at 30 or 120 degrees, the tie goes to the even coordinate, astorch.nn.functional.grid_samplebreaks it. The kernel is the same on every device and dtype, for every full turn added to the angle, and withtorch.compile.
- Parameters:
kernel_size (
int) – motion kernel width and height, an odd integer of at least 3.angle (
Tensor|float) – angle of the motion blur in degrees (anti-clockwise rotation).direction (
Tensor|float, optional) – 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. Default:0.0mode (
str, optional) – interpolation mode for rotating the kernel.'bilinear'or'nearest'. Default:"nearest"
- Return type:
- Returns:
The motion blur kernel of shape \((B, k_\text{size}, k_\text{size})\).
Examples
>>> get_motion_kernel2d(5, 0., 0.) tensor([[[0.0000, 0.0000, 0.0000, 0.0000, 0.0000], [0.0000, 0.0000, 0.0000, 0.0000, 0.0000], [0.2000, 0.2000, 0.2000, 0.2000, 0.2000], [0.0000, 0.0000, 0.0000, 0.0000, 0.0000], [0.0000, 0.0000, 0.0000, 0.0000, 0.0000]]])
>>> get_motion_kernel2d(3, 215., -0.5) tensor([[[0.0000, 0.0000, 0.1667], [0.0000, 0.3333, 0.0000], [0.5000, 0.0000, 0.0000]]])
- kornia.filters.get_motion_kernel3d(kernel_size, angle, direction=0.0, mode='nearest')[source]#
Return 3D motion blur filter.
- Convention:
See the Convention block on
get_motion_kernel2d()fordirectionandmode.angleis a Rodrigues axis-angle vector(rx, ry, rz)in degrees, as applied byrotate3d(): its direction is the rotation axis and its norm is the rotation angle.rotate3d()and the 3D motion blurs call the componentsyaw,pitchandroll, but they are not composed Euler rotations. The unrotated line lies along x, so with the defaultmode='nearest'yaw alone leaves the kernel unchanged ('bilinear'resamples it off the line); a positive pitch alone turns the line’s -x end, the heavy end for a positivedirection, towards +z, and a positive roll alone turns the line clockwise as displayed, the opposite of the 2dangle. With'nearest', each voxel copies the tap nearest to its source under the inverse rotation, picked with exact arithmetic: where the source falls exactly between two taps, such as at a pitch or roll of 30 or 120 degrees, the tie goes to the even coordinate, astorch.nn.functional.grid_samplebreaks it. The kernel is the same on every device and dtype and withtorch.compile.
- Parameters:
kernel_size (
int) – motion kernel width, height and depth, an odd integer of at least 3.angle (
Tensor|tuple[float,float,float]) – Rodrigues axis-angle vector(rx, ry, rz)in degrees, not Euler angles. If tensor, it must be \((B, 3)\). If tuple, it must be(rx, ry, rz).direction (
Tensor|float, optional) – 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. Default:0.0mode (
str, optional) – interpolation mode for rotating the kernel.'bilinear'or'nearest'. Default:"nearest"
- Return type:
- Returns:
The motion blur kernel with shape \((B, k_\text{size}, k_\text{size}, k_\text{size})\).
Examples
>>> get_motion_kernel3d(3, (0., 0., 0.), 0.) tensor([[[[0.0000, 0.0000, 0.0000], [0.0000, 0.0000, 0.0000], [0.0000, 0.0000, 0.0000]], [[0.0000, 0.0000, 0.0000], [0.3333, 0.3333, 0.3333], [0.0000, 0.0000, 0.0000]], [[0.0000, 0.0000, 0.0000], [0.0000, 0.0000, 0.0000], [0.0000, 0.0000, 0.0000]]]])
>>> get_motion_kernel3d(3, (90., 90., 0.), -0.5) tensor([[[[0.0000, 0.0000, 0.0000], [0.0000, 0.0000, 0.0000], [0.0000, 0.5000, 0.0000]], [[0.0000, 0.0000, 0.0000], [0.0000, 0.3333, 0.0000], [0.0000, 0.0000, 0.0000]], [[0.0000, 0.1667, 0.0000], [0.0000, 0.0000, 0.0000], [0.0000, 0.0000, 0.0000]]]])