def __init__( self, ksize: int, angle: float, direction: float, border_type: str = 'constant' ) -> None: super(MotionBlur, self).__init__() self.ksize = ksize self.angle: float = angle self.direction: float = direction assert border_type in ["constant", "reflect", "replicate", "circular"] self.border_type: str = border_type self.kernel: torch.Tensor = torch.unsqueeze( get_motion_kernel2d(self.ksize, self.angle, self.direction), dim=0 )
def motion_blur(input: torch.Tensor, kernel_size: int, angle: Union[float, torch.Tensor], direction: Union[float, torch.Tensor], border_type: str = 'constant') -> torch.Tensor: r""" Function that blurs a tensor using the motion filter. See :class:`~kornia.filters.MotionBlur` for details. """ assert border_type in ["constant", "reflect", "replicate", "circular"] kernel: torch.Tensor = torch.unsqueeze(get_motion_kernel2d( kernel_size, angle, direction), dim=0) return filter2D(input, kernel, border_type)
def motion_blur( input: torch.Tensor, kernel_size: int, angle: Union[float, torch.Tensor], direction: Union[float, torch.Tensor], border_type: str = 'constant', mode: str = 'nearest', ) -> torch.Tensor: r"""Perform motion blur on 2D images (4D tensor). Args: input (torch.Tensor): the input tensor with shape :math:`(B, C, H, W)`. kernel_size (int): motion kernel width and height. It should be odd and positive. angle (Union[torch.Tensor, float]): angle of the motion blur in degrees (anti-clockwise rotation). If tensor, it must be :math:`(B,)`. direction (tensor or 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. If tensor, it must be :math:`(B,)`. border_type (str): the padding mode to be applied before convolving. The expected modes are: ``'constant'``, ``'reflect'``, ``'replicate'`` or ``'circular'``. Default: ``'constant'``. mode (str): interpolation mode for rotating the kernel. ``'bilinear'`` or ``'nearest'``. Default: ``'nearest'`` Return: torch.Tensor: the blurred image with shape :math:`(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 """ assert border_type in ["constant", "reflect", "replicate", "circular"] kernel: torch.Tensor = get_motion_kernel2d(kernel_size, angle, direction, mode) return kornia.filter2d(input, kernel, border_type)