def warp(self, depth_src, rgb_dst, cam_intrinsic, pose_src, pose_dest): cam_intrinsic = cam_intrinsic.squeeze(1) pose_src = pose_src.squeeze(1) pose_dest = pose_dest.squeeze(1) height = rgb_dst.size()[2] width = rgb_dst.size()[3] height_tensor = torch.tensor([height]) width_tensor = torch.tensor([width]) # pinholes camera models pinhole_dst = kornia.PinholeCamera(cam_intrinsic, pose_dest, height_tensor, width_tensor) pinhole_src = kornia.PinholeCamera(cam_intrinsic, pose_src, height_tensor, width_tensor) # create the depth warper, compute the projection matrix warper = kornia.DepthWarper(pinhole_dst, height, width) warper.compute_projection_matrix(pinhole_src) # warp the destionation frame to reference by depth recon_rgb_src = warper(depth_src, rgb_dst) # NxCxHxW return recon_rgb_src
def test_pinhole_camera_attributes_batch2(self): batch_size = 2 height, width = 4, 6 fx, fy, cx, cy = 1, 2, width / 2, height / 2 tx, ty, tz = 1, 2, 3 intrinsics = self._create_intrinsics(batch_size, fx, fy, cx, cy) extrinsics = self._create_extrinsics(batch_size, tx, ty, tz) height = torch.ones(batch_size) * height width = torch.ones(batch_size) * width pinhole = kornia.PinholeCamera(intrinsics, extrinsics, height, width) assert pinhole.batch_size == batch_size assert pinhole.fx.shape[0] == batch_size assert pinhole.fy.shape[0] == batch_size assert pinhole.cx.shape[0] == batch_size assert pinhole.cy.shape[0] == batch_size assert pinhole.tx.shape[0] == batch_size assert pinhole.ty.shape[0] == batch_size assert pinhole.tz.shape[0] == batch_size assert pinhole.height.shape[0] == batch_size assert pinhole.width.shape[0] == batch_size assert pinhole.rt_matrix.shape == (batch_size, 3, 4) assert pinhole.camera_matrix.shape == (batch_size, 3, 3) assert pinhole.rotation_matrix.shape == (batch_size, 3, 3) assert pinhole.translation_vector.shape == (batch_size, 3, 1)
def test_pinhole_camera_translation_setters(self): batch_size = 1 height, width = 4, 6 fx, fy, cx, cy = 1, 2, width / 2, height / 2 tx, ty, tz = 1, 2, 3 intrinsics = self._create_intrinsics(batch_size, fx, fy, cx, cy) extrinsics = self._create_extrinsics(batch_size, tx, ty, tz) height = torch.ones(batch_size) * height width = torch.ones(batch_size) * width pinhole = kornia.PinholeCamera(intrinsics, extrinsics, height, width) assert pinhole.tx.item() == tx assert pinhole.ty.item() == ty assert pinhole.tz.item() == tz # add offset pinhole.tx += 3. pinhole.ty += 2. pinhole.tz += 1. assert pinhole.tx.item() == tx + 3. assert pinhole.ty.item() == ty + 2. assert pinhole.tz.item() == tz + 1. # set to zero pinhole.tx = 0. pinhole.ty = 0. pinhole.tz = 0. assert pinhole.tx.item() == 0. assert pinhole.ty.item() == 0. assert pinhole.tz.item() == 0.
def test_smoke(self): intrinsics = torch.eye(4)[None] extrinsics = torch.eye(4)[None] height = torch.ones(1) width = torch.ones(1) pinhole = kornia.PinholeCamera(intrinsics, extrinsics, height, width) assert isinstance(pinhole, kornia.PinholeCamera)
def test_pinhole_camera_scale_inplace(self): batch_size = 2 height, width = 4, 6 fx, fy, cx, cy = 1, 2, width / 2, height / 2 tx, ty, tz = 1, 2, 3 scale_val = 2.0 intrinsics = self._create_intrinsics(batch_size, fx, fy, cx, cy) extrinsics = self._create_extrinsics(batch_size, tx, ty, tz) height = torch.ones(batch_size) * height width = torch.ones(batch_size) * width scale_factor = torch.ones(batch_size) * scale_val pinhole = kornia.PinholeCamera(intrinsics, extrinsics, height, width) pinhole_scale = pinhole.clone() pinhole_scale.scale_(scale_factor) assert_allclose(pinhole_scale.intrinsics[..., 0, 0], pinhole.intrinsics[..., 0, 0] * scale_val) # fx assert_allclose(pinhole_scale.intrinsics[..., 1, 1], pinhole.intrinsics[..., 1, 1] * scale_val) # fy assert_allclose(pinhole_scale.intrinsics[..., 0, 2], pinhole.intrinsics[..., 0, 2] * scale_val) # cx assert_allclose(pinhole_scale.intrinsics[..., 1, 2], pinhole.intrinsics[..., 1, 2] * scale_val) # cy assert_allclose(pinhole_scale.height, pinhole.height * scale_val) assert_allclose(pinhole_scale.width, pinhole.width * scale_val)
def test_pinhole_camera_attributes(self, device, dtype): batch_size = 1 height, width = 4, 6 fx, fy, cx, cy = 1, 2, width / 2, height / 2 tx, ty, tz = 1, 2, 3 intrinsics = self._create_intrinsics(batch_size, fx, fy, cx, cy, device=device, dtype=dtype) extrinsics = self._create_extrinsics(batch_size, tx, ty, tz, device=device, dtype=dtype) height = torch.ones(batch_size, device=device, dtype=dtype) * height width = torch.ones(batch_size, device=device, dtype=dtype) * width pinhole = kornia.PinholeCamera(intrinsics, extrinsics, height, width) assert pinhole.batch_size == batch_size assert pinhole.fx.item() == fx assert pinhole.fy.item() == fy assert pinhole.cx.item() == cx assert pinhole.cy.item() == cy assert pinhole.tx.item() == tx assert pinhole.ty.item() == ty assert pinhole.tz.item() == tz assert pinhole.height.item() == height assert pinhole.width.item() == width assert pinhole.rt_matrix.shape == (batch_size, 3, 4) assert pinhole.camera_matrix.shape == (batch_size, 3, 3) assert pinhole.rotation_matrix.shape == (batch_size, 3, 3) assert pinhole.translation_vector.shape == (batch_size, 3, 1)
def test_smoke(self, device, dtype): intrinsics = torch.eye(4, device=device, dtype=dtype)[None] extrinsics = torch.eye(4, device=device, dtype=dtype)[None] height = torch.ones(1, device=device, dtype=dtype) width = torch.ones(1, device=device, dtype=dtype) pinhole = kornia.PinholeCamera(intrinsics, extrinsics, height, width) assert isinstance(pinhole, kornia.PinholeCamera)
def test_smoke(self, device): intrinsics = torch.eye(4)[None].to(device) extrinsics = torch.eye(4)[None].to(device) height = torch.ones(1).to(device) width = torch.ones(1).to(device) pinhole = kornia.PinholeCamera(intrinsics, extrinsics, height, width) assert isinstance(pinhole, kornia.PinholeCamera)
def test_pinhole_camera_scale(self, device, dtype): batch_size = 2 height, width = 4, 6 fx, fy, cx, cy = 1, 2, width / 2, height / 2 tx, ty, tz = 1, 2, 3 scale_val = 2.0 intrinsics = self._create_intrinsics(batch_size, fx, fy, cx, cy, device=device, dtype=dtype) extrinsics = self._create_extrinsics(batch_size, tx, ty, tz, device=device, dtype=dtype) height = torch.ones(batch_size, device=device, dtype=dtype) * height width = torch.ones(batch_size, device=device, dtype=dtype) * width scale_factor = torch.ones(batch_size, device=device, dtype=dtype) * scale_val pinhole = kornia.PinholeCamera(intrinsics, extrinsics, height, width) pinhole_scale = pinhole.scale(scale_factor) assert_close( pinhole_scale.intrinsics[..., 0, 0], pinhole.intrinsics[..., 0, 0] * scale_val, atol=1e-4, rtol=1e-4 ) # fx assert_close( pinhole_scale.intrinsics[..., 1, 1], pinhole.intrinsics[..., 1, 1] * scale_val, atol=1e-4, rtol=1e-4 ) # fy assert_close( pinhole_scale.intrinsics[..., 0, 2], pinhole.intrinsics[..., 0, 2] * scale_val, atol=1e-4, rtol=1e-4 ) # cx assert_close( pinhole_scale.intrinsics[..., 1, 2], pinhole.intrinsics[..., 1, 2] * scale_val, atol=1e-4, rtol=1e-4 ) # cy assert_close(pinhole_scale.height, pinhole.height * scale_val, atol=1e-4, rtol=1e-4) assert_close(pinhole_scale.width, pinhole.width * scale_val, atol=1e-4, rtol=1e-4)