Skip to content
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

fix camera intrinsics #568

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions scene/cameras.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from utils.graphics_utils import getWorld2View2, getProjectionMatrix

class Camera(nn.Module):
def __init__(self, colmap_id, R, T, FoVx, FoVy, image, gt_alpha_mask,
def __init__(self, colmap_id, R, T, FoVx, FoVy, cx, cy, image, gt_alpha_mask,
image_name, uid,
trans=np.array([0.0, 0.0, 0.0]), scale=1.0, data_device = "cuda"
):
Expand All @@ -27,6 +27,8 @@ def __init__(self, colmap_id, R, T, FoVx, FoVy, image, gt_alpha_mask,
self.T = T
self.FoVx = FoVx
self.FoVy = FoVy
self.cx = cx
self.cy = cy
self.image_name = image_name

try:
Expand All @@ -52,7 +54,9 @@ def __init__(self, colmap_id, R, T, FoVx, FoVy, image, gt_alpha_mask,
self.scale = scale

self.world_view_transform = torch.tensor(getWorld2View2(R, T, trans, scale)).transpose(0, 1).cuda()
self.projection_matrix = getProjectionMatrix(znear=self.znear, zfar=self.zfar, fovX=self.FoVx, fovY=self.FoVy).transpose(0,1).cuda()
self.projection_matrix = getProjectionMatrix(znear=self.znear, zfar=self.zfar,
fovX=self.FoVx, fovY=self.FoVy,
cx=self.cx, cy=self.cy).transpose(0, 1).cuda()
self.full_proj_transform = (self.world_view_transform.unsqueeze(0).bmm(self.projection_matrix.unsqueeze(0))).squeeze(0)
self.camera_center = self.world_view_transform.inverse()[3, :3]

Expand Down
11 changes: 10 additions & 1 deletion scene/dataset_readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class CameraInfo(NamedTuple):
T: np.array
FovY: np.array
FovX: np.array
cx: np.array
cy: np.array
image: np.array
image_path: str
image_name: str
Expand Down Expand Up @@ -84,21 +86,28 @@ def readColmapCameras(cam_extrinsics, cam_intrinsics, images_folder):

if intr.model=="SIMPLE_PINHOLE":
focal_length_x = intr.params[0]
cx = intr.params[1]
cy = intr.params[2]
FovY = focal2fov(focal_length_x, height)
FovX = focal2fov(focal_length_x, width)
elif intr.model=="PINHOLE":
cx = intr.params[2]
cy = intr.params[3]
focal_length_x = intr.params[0]
focal_length_y = intr.params[1]
FovY = focal2fov(focal_length_y, height)
FovX = focal2fov(focal_length_x, width)
else:
assert False, "Colmap camera model not handled: only undistorted datasets (PINHOLE or SIMPLE_PINHOLE cameras) supported!"

cx = (cx - width / 2) / width * 2
cy = (cy - height / 2) / height * 2

image_path = os.path.join(images_folder, os.path.basename(extr.name))
image_name = os.path.basename(image_path).split(".")[0]
image = Image.open(image_path)

cam_info = CameraInfo(uid=uid, R=R, T=T, FovY=FovY, FovX=FovX, image=image,
cam_info = CameraInfo(uid=uid, R=R, T=T, FovY=FovY, FovX=FovX, cx=cx, cy=cy, image=image,
image_path=image_path, image_name=image_name, width=width, height=height)
cam_infos.append(cam_info)
sys.stdout.write('\n')
Expand Down
3 changes: 2 additions & 1 deletion utils/camera_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ def loadCam(args, id, cam_info, resolution_scale):
loaded_mask = resized_image_rgb[3:4, ...]

return Camera(colmap_id=cam_info.uid, R=cam_info.R, T=cam_info.T,
FoVx=cam_info.FovX, FoVy=cam_info.FovY,
FoVx=cam_info.FovX, FoVy=cam_info.FovY,
cx=cam_info.cx, cy=cam_info.cy,
image=gt_image, gt_alpha_mask=loaded_mask,
image_name=cam_info.image_name, uid=id, data_device=args.data_device)

Expand Down
6 changes: 3 additions & 3 deletions utils/graphics_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def getWorld2View2(R, t, translate=np.array([.0, .0, .0]), scale=1.0):
Rt = np.linalg.inv(C2W)
return np.float32(Rt)

def getProjectionMatrix(znear, zfar, fovX, fovY):
def getProjectionMatrix(znear, zfar, fovX, fovY, cx, cy):
tanHalfFovY = math.tan((fovY / 2))
tanHalfFovX = math.tan((fovX / 2))

Expand All @@ -63,8 +63,8 @@ def getProjectionMatrix(znear, zfar, fovX, fovY):

P[0, 0] = 2.0 * znear / (right - left)
P[1, 1] = 2.0 * znear / (top - bottom)
P[0, 2] = (right + left) / (right - left)
P[1, 2] = (top + bottom) / (top - bottom)
P[0, 2] = cx
P[1, 2] = cy
P[3, 2] = z_sign
P[2, 2] = z_sign * zfar / (zfar - znear)
P[2, 3] = -(zfar * znear) / (zfar - znear)
Expand Down