question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

warp_perspective produced wrong results with integer input

See original GitHub issue

Describe the bug

got a runtime error when I test warp_perspective

Reproduction steps

run



def test_warp_perspective()        :
    import kornia as K
    import torch

    input = torch.zeros(1,3,1000,1000).cuda()

    print(input.shape)

    h, w = 64, 128  # destination size
    points_src = torch.tensor([[
    [100, 100], [200, 100], [200, 200], [100, 200],
    ]])

    # points_src = torch.tensor([[
    # [125., 150.], [562., 40.], [562., 282.], [54., 328.],
    # ]])

    points_dst = torch.tensor([[
        [0., 0.], [w - 1., 0.], [w - 1., h - 1.], [0., h - 1.],
    ]])
    print(points_src, points_dst)
    # compute perspective transform
    M: torch.tensor = K.get_perspective_transform(points_src, points_dst)

    print(input.shape, M)
    # warp the original image by the found transform
    img_warp: torch.tensor = K.warp_perspective(input, M.cuda(), dsize=(h, w))
    print(img_warp.shape)

if __name__ == "__main__":
    test_warp_perspective()


File “test2.py”, line 33, in <module> test_warp_perspective() File “test2.py”, line 29, in test_warp_perspective img_warp: torch.tensor = K.warp_perspective(input, M.cuda(), dsize=(h, w)) File “/usr/local/lib/python3.6/dist-packages/kornia/geometry/transform/imgwarp.py”, line 96, in warp_perspective dst_norm_trans_src_norm: torch.Tensor = normalize_homography(M, (H, W), (h_out, w_out)) # Bx3x3 File “/usr/local/lib/python3.6/dist-packages/kornia/geometry/transform/homography_warper.py”, line 378, in normalize_homography src_pix_trans_src_norm = _torch_inverse_cast(src_norm_trans_src_pix) File “/usr/local/lib/python3.6/dist-packages/kornia/utils/helpers.py”, line 50, in _torch_inverse_cast return torch.inverse(input.to(dtype)).to(input.dtype) RuntimeError: inverse_cuda: For batch 0: U(1,1) is zero, singular U.

Expected behavior

no RuntimeError

Environment

>>> torch.__version__
'1.9.1+cu102'
>>> kornia.__version__
'0.5.11'


### Additional context

_No response_

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:17 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
anirudhb11commented, Oct 21, 2021

@anirudhb11 looks like you found it - Please, send a PR to fix.

#1413

1reaction
ShiyangZhangcommented, Oct 18, 2021

get_perspective_transform got wrong result when points_src is type of int

torch.int64 tensor([[[   1,    0, -127],
         [   0,    0,  -63],
         [   0,    0,    1]]])

when points_src is type of float: the result seems normal

         torch.float32 tensor([[[ 1.2700e+00,  0.0000e+00, -1.2700e+02],
         [-8.3447e-09,  6.3000e-01, -6.3000e+01],
         [ 5.0144e-10, -1.0169e-10,  1.0000e+00]]])

6.3000e-01 vs 0

Read more comments on GitHub >

github_iconTop Results From Across the Web

OpenCV warpperspective - Stack Overflow
The problem occurs because the homography maps part of the image to negative x,y values which are outside the image area so cannot...
Read more >
[Bug] warp_perspective does not return the input if used with ...
Hi, that is indeed a complex issue. The problem is the following: When transform is "non-descructive", i.e. results in sampling ONLY integer ......
Read more >
WarpPerspective API in TIOVX, Also issue with ... - TI E2E
The generated matrix from getPerspectiveTransform function of OpenCV ... The result is incorrect and doesn't match with the OpenCV output.
Read more >
Apply geometric transformation to image - MATLAB imwarp
This MATLAB function transforms the numeric, logical, or categorical image ... Choosing false (not padding) the input image can result in a sharper...
Read more >
OpenCV warpPerspective implementation
I do not want to just use the OpenCV method because I want to learn how to implement it. What am I getting...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found