|
|
import open3d as o3d |
|
|
import matplotlib.pyplot as plt |
|
|
import numpy as np |
|
|
import os |
|
|
import math |
|
|
import torch |
|
|
from PIL import Image |
|
|
import random |
|
|
from einops import einsum |
|
|
|
|
|
|
|
|
@torch.no_grad() |
|
|
def get_frustum_mask(points, H, W, intrinsics, view_matrices, near = 0.02, far = 10.): |
|
|
|
|
|
ones = torch.ones_like(points[:, 0]).unsqueeze(-1) |
|
|
homo_points = torch.cat([points, ones], dim=-1) |
|
|
|
|
|
view_points = einsum(view_matrices, homo_points, "b c, N c -> N b") |
|
|
view_points = view_points[:, :3] |
|
|
|
|
|
uv_points = einsum(intrinsics, view_points, "b c, N c -> N b") |
|
|
|
|
|
z = uv_points[:, -1:] |
|
|
uv_points = uv_points[:, :2] / z |
|
|
u, v = uv_points[:, 0], uv_points[:, 1] |
|
|
depth = view_points[:, -1] |
|
|
|
|
|
cull_near_fars = (depth >= near) & (depth <= far) |
|
|
|
|
|
mask = cull_near_fars & (u >= 0) & (u <= W-1) & (v >= 0) & (v <= H-1) |
|
|
return mask |
|
|
|
|
|
|
|
|
def config_parser(): |
|
|
|
|
|
import configargparse |
|
|
parser = configargparse.ArgumentParser() |
|
|
|
|
|
|
|
|
|
|
|
parser.add_argument("--near", type=float, default=0., |
|
|
help='near distance') |
|
|
parser.add_argument("--far", type=float, default=10., |
|
|
help='far distance') |
|
|
parser.add_argument("--camera_height", type=int, default=24, |
|
|
help='height of the feature map') |
|
|
parser.add_argument("--camera_width", type=int, default=24, |
|
|
help='width of the feature map') |
|
|
parser.add_argument("--feature_fields_search_radius", type=float, default=1., |
|
|
help='search radius for near features') |
|
|
parser.add_argument("--feature_fields_search_num", type=int, default=4, |
|
|
help='The number of searched near features') |
|
|
parser.add_argument("--mlp_net_layers", type=int, default=8, |
|
|
help='layers in mlp network') |
|
|
parser.add_argument("--mlp_net_width", type=int, default=768, |
|
|
help='channels per layer in mlp net') |
|
|
|
|
|
|
|
|
parser.add_argument("--N_samples", type=int, default=512, |
|
|
help='number of coarse samples per ray') |
|
|
parser.add_argument("--N_importance", type=int, default=16, |
|
|
help='number of fine samples per ray') |
|
|
|
|
|
return parser |
|
|
|
|
|
|
|
|
parser = config_parser() |
|
|
args, unknown = parser.parse_known_args() |
|
|
|
|
|
|
|
|
scene_list = os.listdir('3RScan') |
|
|
image_list = [] |
|
|
for scene_id in scene_list: |
|
|
for image_id in range(1000): |
|
|
image_path = '3RScan/'+scene_id+'/sequence/frame-'+str(image_id).zfill(6)+'.color.jpg' |
|
|
if not os.path.exists(image_path): |
|
|
break |
|
|
image_list.append('3RScan/'+scene_id+'/sequence/frame-'+str(image_id).zfill(6)) |
|
|
|
|
|
random.shuffle(image_list) |
|
|
image_list = image_list[:30] |
|
|
|
|
|
|
|
|
pcd_all = o3d.geometry.PointCloud() |
|
|
for image_path in image_list: |
|
|
intrinsic = np.eye(4) |
|
|
with open('3RScan/'+scene_id+'/sequence/_info.txt', 'r') as file: |
|
|
intrinsic_raw = [line.strip() for line in file] |
|
|
intrinsic_raw = intrinsic_raw[9].split(" ")[2:] |
|
|
|
|
|
for i in range(4): |
|
|
for j in range(4): |
|
|
intrinsic[i][j] = float(intrinsic_raw[i*4+j]) |
|
|
|
|
|
extrinsic = np.eye(4) |
|
|
with open(image_path+'.pose.txt', 'r') as file: |
|
|
extrinsic_raw = [line.strip() for line in file] |
|
|
for i in range(4): |
|
|
for j in range(4): |
|
|
extrinsic[i][j] = float(extrinsic_raw[i].split()[j]) |
|
|
|
|
|
R = extrinsic[:3,:3] |
|
|
T = extrinsic[:3,3:4] |
|
|
|
|
|
color_raw = o3d.io.read_image(image_path + ".color.jpg") |
|
|
depth_raw = o3d.geometry.Image(np.asarray(Image.open(image_path + ".depth.pgm")).astype(np.uint16)) |
|
|
|
|
|
|
|
|
|
|
|
pcd = o3d.geometry.PointCloud.create_from_depth_image(depth_raw, o3d.camera.PinholeCameraIntrinsic(224,172,intrinsic[0][0],intrinsic[1][1],intrinsic[0][2],intrinsic[1][2]), depth_scale=1000.0, depth_trunc=1000.0) |
|
|
|
|
|
points = np.asarray(pcd.points) |
|
|
|
|
|
points = (R @ points.T + T).T |
|
|
pcd.points = o3d.utility.Vector3dVector(points) |
|
|
pcd_all += pcd |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
o3d.visualization.draw_geometries([pcd_all]) |