π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix warp_perspective with src/dst normalization
edgarriba9b123c893c1e
diff --git a/test/test_imgwarp.py b/test/test_imgwarp.pyindex 25dcb17ab..85cb38777 100644--- a/test/test_imgwarp.py+++ b/test/test_imgwarp.py@@ -41,6 +41,66 @@ class Tester(unittest.TestCase):mask_warped_inv * patch_warped_inv)self.assertTrue(res)+ def test_warp_perspective_crop(self):+ # generate input data+ batch_size = 1+ src_h, src_w = 3, 4+ dst_h, dst_w = 3, 2++ # [x, y] origin+ # top-left, top-right, bottom-right, bottom-left+ points_src = torch.FloatTensor([[+ [1, 0], [2, 0], [2, 2], [1, 2],+ ]])++ # [x, y] destination+ # top-left, top-right, bottom-right, bottom-left+ points_dst = torch.FloatTensor([[+ [0, 0], [dst_w - 1, 0], [dst_w - 1, dst_h - 1], [0, dst_h - 1],+ ]])++ # compute transformation between points+ dst_pix_trans_src_pix = tgm.get_perspective_transform(+ points_src, points_dst)++ # create points grid in normalized coordinates+ grid_src_norm = tgm.create_meshgrid(src_h, src_w,+ normalized_coordinates=True)+ grid_src_norm = torch.unsqueeze(grid_src_norm, dim=0)++ # create points grid in pixel coordinates+ grid_src_pix = tgm.create_meshgrid(src_h, src_w,+ normalized_coordinates=False)+ grid_src_pix = torch.unsqueeze(grid_src_pix, dim=0)++ src_norm_trans_src_pix = tgm.normal_transform_pixel(src_h, src_w)+ src_pix_trans_src_norm = tgm.inverse(src_norm_trans_src_pix)++ dst_norm_trans_dst_pix = tgm.normal_transform_pixel(dst_h, dst_w)++ # transform pixel grid+ grid_dst_pix = tgm.transform_points(+ dst_pix_trans_src_pix, grid_src_pix)+ grid_dst_norm = tgm.transform_points(+ dst_norm_trans_dst_pix, grid_dst_pix)++ # transform norm grid+ dst_norm_trans_src_norm = torch.matmul(dst_norm_trans_dst_pix,+ torch.matmul(dst_pix_trans_src_pix, src_pix_trans_src_norm))+ grid_dst_norm2 = tgm.transform_points(+ dst_norm_trans_src_norm, grid_src_norm)++ # grids should be equal+ self.assertTrue(utils.check_equal_torch(+ grid_dst_norm, grid_dst_norm2))++ # warp tensor+ patch = torch.rand(batch_size, 1, src_h, src_w)+ patch_warped = tgm.warp_perspective(patch,+ dst_pix_trans_src_pix, (dst_h, dst_w))+ self.assertTrue(utils.check_equal_torch(+ patch[:, :, :3, 1:3], patch_warped))+def test_warp_perspective_gradcheck(self):# generate input databatch_size = 1diff --git a/torchgeometry/homography_warper.py b/torchgeometry/homography_warper.pyindex 3ac9b53aa..224d7f9fe 100644--- a/torchgeometry/homography_warper.py+++ b/torchgeometry/homography_warper.py@@ -52,10 +52,10 @@ class HomographyWarper(nn.Module):batch_size = H.shape[0] # expand grid to match the input batch sizegrid = self.grid.repeat(batch_size, 1, 1, 1) # NxHxWx2if len(H.shape) == 3: # local homography case- grid = grid.view(batch_size, -1, 2) # Nx(H*W)x2+ H = H.view(batch_size, 1, 1, 3, 3) # NxHxWx3x3# perform the actual grid transformation,# the grid is copied to input device and casted to the same type- flow = transform_points(H, grid.to(H.device).type_as(H)) # Nx(...)x2+ flow = transform_points(H, grid.to(H.device).type_as(H)) # NxHxWx2return flow.view(batch_size, self.height, self.width, 2) # NxHxWx2def random_warp(self, patch, dist):diff --git a/torchgeometry/imgwarp.py b/torchgeometry/imgwarp.pyindex 41cbb6318..82ad3cdf9 100644--- a/torchgeometry/imgwarp.py+++ b/torchgeometry/imgwarp.py@@ -13,30 +13,6 @@ __all__ = []-def center_transform(transform, height, width):- assert len(transform.shape) == 3, transform.shape- # move points from origin- center_mat_origin = torch.unsqueeze(- torch.eye(- 3,- device=transform.device,- dtype=transform.dtype),- dim=0)- center_mat_origin[..., 0, 2] = float(width) / 2- center_mat_origin[..., 1, 2] = float(height) / 2- # move points to origin- origin_mat_center = torch.unsqueeze(- torch.eye(- 3,- device=transform.device,- dtype=transform.dtype),- dim=0)- origin_mat_center[..., 0, 2] = -float(width) / 2- origin_mat_center[..., 1, 2] = -float(height) / 2- return torch.matmul(center_mat_origin,- torch.matmul(transform, origin_mat_center))--def normal_transform_pixel(height, width):return torch.tensor([[[2. / (width - 1), 0., -1.],@@ -45,16 +21,23 @@ def normal_transform_pixel(height, width):]) # 1x3x3-def normalize_transform_to_pix(transform, height, width):- assert len(transform.shape) == 3, transform.shape- normal_trans_pix = torch.tensor([[- [2. / (width - 1), 0., -1.],- [0., 2. / (height - 1), -1.],- [0., 0., 1.]]],- device=transform.device, dtype=transform.dtype) # 1x3x3- pix_trans_normal = inverse(normal_trans_pix) # 1x3x3- return torch.matmul(normal_trans_pix,- torch.matmul(transform, pix_trans_normal))+def dst_norm_to_dst_norm(dst_pix_trans_src_pix, dsize_src, dsize_dst):+ # source and destination sizes+ src_h, src_w = dsize_src+ dst_h, dst_w = dsize_dst+ # the devices and types+ device = dst_pix_trans_src_pix.device+ dtype = dst_pix_trans_src_pix.dtype+ # compute the transformation pixel/norm for src/dst+ src_norm_trans_src_pix = normal_transform_pixel(+ src_h, src_w).to(device).to(dtype)+ src_pix_trans_src_norm = inverse(src_norm_trans_src_pix)+ dst_norm_trans_dst_pix = normal_transform_pixel(+ dst_h, dst_w).to(device).to(dtype)+ # compute chain transformations+ dst_norm_trans_src_norm = torch.matmul(dst_norm_trans_dst_pix,+ torch.matmul(dst_pix_trans_src_pix, src_pix_trans_src_norm))+ return dst_norm_trans_src_normdef warp_perspective(src, M, dsize, flags='bilinear', border_mode=None,@@ -98,12 +81,12 @@ def warp_perspective(src, M, dsize, flags='bilinear', border_mode=None,if not (len(M.shape) == 3 or M.shape[-2:] == (3, 3)):raise ValueError("Input M must be a Bx3x3 tensor. Got {}".format(src.shape))- # center the transformation and normalize- _, _, height, width = src.shape- M_new = center_transform(M, height, width)- M_new = normalize_transform_to_pix(M_new, height, width)- # warp and return- return homography_warp(src, M_new, dsize)+ # compute the transform in normalized cooridnates+ dsize_src, dsize_dst = (src.shape[-2:]), dsize+ dst_pix_trans_src_pix = M+ dst_norm_trans_dst_norm = dst_norm_to_dst_norm(+ dst_pix_trans_src_pix, dsize_src, dsize_dst)+ return homography_warp(src, inverse(dst_norm_trans_dst_norm), dsize_dst)def get_perspective_transform(src, dst):