示例#1
0
    def test_exception(self, device, dtype):
        input = torch.ones(1, 1, 3, 4, device=device, dtype=dtype)
        kernel = torch.ones(3, 3, device=device, dtype=dtype)

        with pytest.raises(TypeError):
            assert open([0.], kernel)

        with pytest.raises(TypeError):
            assert open(input, [0.])

        with pytest.raises(ValueError):
            test = torch.ones(2, 3, 4, device=device, dtype=dtype)
            assert open(test, kernel)

        with pytest.raises(ValueError):
            test = torch.ones(2, 3, 4, device=device, dtype=dtype)
            assert open(input, test)
示例#2
0
文件: test_open.py 项目: djl11/kornia
    def test_exception(self, dev_str, dtype_str, call):
        input_ = ivy.ones((1, 1, 3, 4), dev_str=dev_str, dtype_str=dtype_str)
        kernel = ivy.ones((3, 3), dev_str=dev_str, dtype_str=dtype_str)

        with pytest.raises(ValueError):
            test = ivy.ones((2, 3, 4), dev_str=dev_str, dtype_str=dtype_str)
            assert open(test, kernel)

        with pytest.raises(ValueError):
            test = ivy.ones((2, 3, 4), dev_str=dev_str, dtype_str=dtype_str)
            assert open(input_, test)

        if call is not helpers.torch_call:
            return

        with pytest.raises(TypeError):
            assert open([0.], kernel)

        with pytest.raises(TypeError):
            assert open(input_, [0.])
示例#3
0
 def test_value(self, device, dtype):
     input = torch.tensor(
         [[0.5, 1., 0.3], [0.7, 0.3, 0.8], [0.4, 0.9, 0.2]],
         device=device,
         dtype=dtype)[None, None, :, :]
     kernel = torch.tensor([[0., 1., 0.], [1., 1., 1.], [0., 1., 0.]],
                           device=device,
                           dtype=dtype)
     expected = torch.tensor(
         [[0.5, 0.5, 0.3], [0.5, 0.3, 0.3], [0.4, 0.4, 0.2]],
         device=device,
         dtype=dtype)[None, None, :, :]
     assert_allclose(open(input, kernel), expected)
示例#4
0
def top_hat(tensor: torch.Tensor, kernel: torch.Tensor) -> torch.Tensor:

    r"""

        Returns the top hat tranformation of an image,
        (that means, image - opened_image) applying the same kernel in each channel.

        The kernel must have 2 dimensions, each one defined by an odd number.

        See :class:`~kornia.morphology.open` for details.

        Args

           tensor (torch.Tensor): Image with shape :math:`(B, C, H, W)`.
           kernel (torch.Tensor): Structuring element with shape :math:`(H, W)`.

        Returns:

           torch.Tensor: Top hat transformated image with shape :math:`(B, C, H, W)`.

        Example:

            >>> tensor = torch.rand(1, 3, 5, 5)
            >>> kernel = torch.ones(3, 3)
            >>> top_hat_img = top_hat(tensor, kernel)

        """

    if not isinstance(tensor, torch.Tensor):
        raise TypeError("Input type is not a torch.Tensor. Got {}".format(
            type(tensor)))

    if len(tensor.shape) != 4:
        raise ValueError("Input size must have 4 dimensions. Got {}".format(
            tensor.dim()))

    if not isinstance(kernel, torch.Tensor):
        raise TypeError("Kernel type is not a torch.Tensor. Got {}".format(
            type(kernel)))

    if len(kernel.shape) != 2:
        raise ValueError("Kernel size must have 2 dimensions. Got {}".format(
            kernel.dim()))

    return tensor - open(tensor, kernel)
示例#5
0
def top_hat(tensor: ivy.Array, kernel: ivy.Array) -> ivy.Array:
    r"""Returns the top hat tranformation of an image.

    That means, (image - opened_image) applying the same kernel in each channel.
    The kernel must have 2 dimensions, each one defined by an odd number.

    See :class:`~kornia.morphology.open` for details.

    Args:
       tensor (ivy.Array): Image with shape :math:`(B, C, H, W)`.
       kernel (ivy.Array): Structuring element with shape :math:`(H, W)`.

    Returns:
       ivy.Array: Top hat transformated image with shape :math:`(B, C, H, W)`.

    Example:
        >>> tensor = ivy.random_uniform(shape=(1, 3, 5, 5))
        >>> kernel = ivy.ones((3, 3))
        >>> top_hat_img = top_hat(tensor, kernel)
    """

    if ivy.backend == 'torch' and not isinstance(tensor, ivy.Array):
        raise TypeError("Input type is not a ivy.Array. Got {}".format(
            type(tensor)))

    if len(tensor.shape) != 4:
        raise ValueError("Input size must have 4 dimensions. Got {}".format(
            ivy.get_num_dims(tensor)))

    if ivy.backend == 'torch' and not isinstance(kernel, ivy.Array):
        raise TypeError("Kernel type is not a ivy.Array. Got {}".format(
            type(kernel)))

    if len(kernel.shape) != 2:
        raise ValueError("Kernel size must have 2 dimensions. Got {}".format(
            ivy.get_num_dims(kernel)))

    return tensor - open(tensor, kernel)
示例#6
0
文件: test_open.py 项目: djl11/kornia
 def test_cardinality(self, dev_str, dtype_str, call, shape, kernel):
     img = ivy.ones(shape, dtype_str=dtype_str, dev_str=dev_str)
     krnl = ivy.ones(kernel, dtype_str=dtype_str, dev_str=dev_str)
     assert open(img, krnl).shape == shape
示例#7
0
 def test_cardinality(self, device, dtype, shape, kernel):
     img = torch.ones(shape, device=device, dtype=dtype)
     krnl = torch.ones(kernel, device=device, dtype=dtype)
     assert open(img, krnl).shape == shape