gaussian-splatting/utils/camera_utils.py

97 lines
3.6 KiB
Python
Raw Permalink Normal View History

#
# Copyright (C) 2023, Inria
# GRAPHDECO research group, https://team.inria.fr/graphdeco
# All rights reserved.
#
# This software is free for non-commercial, research and evaluation use
# under the terms of the LICENSE.md file.
#
# For inquiries contact george.drettakis@inria.fr
#
2023-07-04 08:00:48 +00:00
from scene.cameras import Camera
import numpy as np
from utils.graphics_utils import fov2focal
2024-08-21 12:30:43 +00:00
from PIL import Image
import cv2
2023-07-04 08:00:48 +00:00
WARNED = False
def loadCam(args, id, cam_info, resolution_scale, is_nerf_synthetic, is_test_dataset):
2024-08-21 12:30:43 +00:00
image = Image.open(cam_info.image_path)
2023-07-04 08:00:48 +00:00
2024-08-21 12:30:43 +00:00
if cam_info.depth_path != "":
try:
if is_nerf_synthetic:
invdepthmap = cv2.imread(cam_info.depth_path, -1).astype(np.float32) / 512
else:
invdepthmap = cv2.imread(cam_info.depth_path, -1).astype(np.float32) / float(2**16)
2024-08-21 12:30:43 +00:00
except FileNotFoundError:
print(f"Error: The depth file at path '{cam_info.depth_path}' was not found.")
raise
except IOError:
print(f"Error: Unable to open the image file '{cam_info.depth_path}'. It may be corrupted or an unsupported format.")
raise
except Exception as e:
print(f"An unexpected error occurred when trying to read depth at {cam_info.depth_path}: {e}")
raise
else:
invdepthmap = None
orig_w, orig_h = image.size
2023-07-04 08:00:48 +00:00
if args.resolution in [1, 2, 4, 8]:
resolution = round(orig_w/(resolution_scale * args.resolution)), round(orig_h/(resolution_scale * args.resolution))
else: # should be a type that converts to float
if args.resolution == -1:
if orig_w > 1600:
global WARNED
if not WARNED:
2023-07-09 15:39:01 +00:00
print("[ INFO ] Encountered quite large input images (>1.6K pixels width), rescaling to 1.6K.\n "
"If this is not desired, please explicitly specify '--resolution/-r' as 1")
WARNED = True
global_down = orig_w / 1600
else:
global_down = 1
else:
global_down = orig_w / args.resolution
2024-08-21 12:30:43 +00:00
2023-07-04 08:00:48 +00:00
scale = float(global_down) * float(resolution_scale)
resolution = (int(orig_w / scale), int(orig_h / scale))
2024-08-21 12:30:43 +00:00
return Camera(resolution, colmap_id=cam_info.uid, R=cam_info.R, T=cam_info.T,
FoVx=cam_info.FovX, FoVy=cam_info.FovY, depth_params=cam_info.depth_params,
image=image, invdepthmap=invdepthmap,
image_name=cam_info.image_name, uid=id, data_device=args.data_device,
train_test_exp=args.train_test_exp, is_test_dataset=is_test_dataset, is_test_view=cam_info.is_test)
2023-07-04 08:00:48 +00:00
def cameraList_from_camInfos(cam_infos, resolution_scale, args, is_nerf_synthetic, is_test_dataset):
2023-07-04 08:00:48 +00:00
camera_list = []
for id, c in enumerate(cam_infos):
camera_list.append(loadCam(args, id, c, resolution_scale, is_nerf_synthetic, is_test_dataset))
2023-07-04 08:00:48 +00:00
return camera_list
def camera_to_JSON(id, camera : Camera):
Rt = np.zeros((4, 4))
Rt[:3, :3] = camera.R.transpose()
Rt[:3, 3] = camera.T
Rt[3, 3] = 1.0
W2C = np.linalg.inv(Rt)
pos = W2C[:3, 3]
rot = W2C[:3, :3]
serializable_array_2d = [x.tolist() for x in rot]
camera_entry = {
'id' : id,
'img_name' : camera.image_name,
'width' : camera.width,
'height' : camera.height,
'position': pos.tolist(),
'rotation': serializable_array_2d,
'fy' : fov2focal(camera.FovY, camera.height),
'fx' : fov2focal(camera.FovX, camera.width)
}
2024-08-21 12:30:43 +00:00
return camera_entry