-
-
Notifications
You must be signed in to change notification settings - Fork 947
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Meaning of the Homography in HomographyWarper #157
Comments
@stefat77 thanks to try kornia :D I see one thing in your code example: when you normalize the sample point, what are your coordinates convention ? According to our convention, I suspect that you normalize in the other way around since we assume {x, y} Check how we create the base grid: https://github.com/arraiyopensource/kornia/blob/master/kornia/utils/grid.py#L38 Hope this helps. |
@edgarriba Thank you for the reply!
In this case with...
|
@stefat77 I think you are normalizing wrong. I compared it with kornia.normalize_pixel_coordinates and the sign you get is inverted. |
another question, is this homography for normalized coordinates ? |
@edgarriba thank you for your patience!
But again, the point is not warped as the image.
It's just a homography I made up randomly. Why should the homography be specific for normalized coordinates? |
@edgarriba I solved the problem. I don't know why, but if I warp the image with the homography H and the point with the inverse homograpy H^-1 then the point and the image are warped in the same way. Here's my code:
|
@stefat77 oh, now I see what you want to check. I played around with you code and I can confirm that for going from a -> b you need to pass the homography from b -> a. The reason is that since we want to generate patch b, we need to sample from a and for this we need to pass the inverse transform since we have to retrieve pixels in the other direction. I encourage you to go in deep to the homography warper code to fully understand. However, the documentation is wrong since the homography should from dst to src. I open a ticket to fix it. In addition, I found that to pass homographies in pixel coordinates space we need to add an extra normalization step as I show in the example below. import cv2
import kornia
import torch
import torch.nn.functional as F
img_a = torch.ones(1, 1, 60, 60)
img_a[..., 20:40, 20:40] = 0.5
h = img_a.shape[2]
w = img_a.shape[3]
b_H_a = torch.tensor([[
[1.0, 0.0, 15.],
[0.0, 1.0, 15.],
[0.0, 0.0, 1.0]
]]) # in pixel
a_H_b = torch.inverse(b_H_a)
print(b_H_a)
print(a_H_b)
# define point in frame a and transform to b
point_a = torch.ones(1, 1, 2)
point_a[..., 0] = 21 # j or X
point_a[..., 1] = 11 # i or Y
point_b = kornia.transform_points(b_H_a, point_a)
# encode point a to img_a
img_a[..., int(point_a[..., 1]), int(point_a[..., 0])] = 0.75
# create warper in pixel space
# TODO: call normalize pixel coordinate inside warper
# when normalized_coordinates is False
warper = kornia.HomographyWarper(
h, w, normalized_coordinates=False)
# NOTE: b -> a
# TODO: fix docs or call torch.inverse in case when want to
# keep this convention which seems more natural.
grid_pix = warper.warp_grid(a_H_b)
grid_norm = kornia.normalize_pixel_coordinates(grid_pix, h, w)
img_b = F.grid_sample(img_a, grid_norm)
assert img_a[..., int(point_a[..., 1]), int(point_a[..., 0])] == \
img_b[..., int(point_b[..., 1]), int(point_b[..., 0])]
# write to disk
img_a_vis = kornia.tensor_to_image((img_a[0] * 255).byte())
img_b_vis = kornia.tensor_to_image((img_b[0] * 255).byte())
cv2.imwrite("img_a.png", img_a_vis)
cv2.imwrite("img_b.png", img_b_vis) |
closing in favor of #159 |
Hello,
I'm doing some tests with the HomographyWarper function to find out how it uses the homography to warp the tensor.
In the following code I'm trying to track a point in a image. Both the image and the point are warped using the same homography. The warped point must be mapped at the same location of the warped image:
In this case the image is translated to the left while the point is moved down, so the point moved in a different way with respect the image.
If I use this homography to move the point I match the image movement:
Why does this happen?
The normalized coordinates means that the top left corner of the image has indices [-1,-1], and the bottom right corner has indices [1,1], right?
It seems like in a point [v1, v2, 1] the first value (v1) describes the column index and the second (v2) describes the row index.
There's also something wrong with the sign of the homography values...
The text was updated successfully, but these errors were encountered: