File size: 4,983 Bytes
c165cd8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
import sys
sys.path.append("..")
import imageio
import numpy as np
import os
from plyfile import PlyData, PlyElement
from pycolmap import SceneManager
from scipy.ndimage.interpolation import zoom
#-------------------------------------------------------------------------------
def main(args):
suffix = ".photometric.bin" if args.photometric else ".geometric.bin"
image_file = os.path.join(args.dense_folder, "images", args.image_filename)
depth_file = os.path.join(
args.dense_folder, args.stereo_folder, "depth_maps",
args.image_filename + suffix)
if args.save_normals:
normals_file = os.path.join(
args.dense_folder, args.stereo_folder, "normal_maps",
args.image_filename + suffix)
# load camera intrinsics from the COLMAP reconstruction
scene_manager = SceneManager(os.path.join(args.dense_folder, "sparse"))
scene_manager.load_cameras()
scene_manager.load_images()
image_id, image = scene_manager.get_image_from_name(args.image_filename)
camera = scene_manager.cameras[image.camera_id]
rotation_camera_from_world = image.R()
camera_center = image.C()
# load image, depth map, and normal map
image = imageio.imread(image_file)
with open(depth_file, "rb") as fid:
w = int("".join(iter(lambda: fid.read(1), "&")))
h = int("".join(iter(lambda: fid.read(1), "&")))
c = int("".join(iter(lambda: fid.read(1), "&")))
depth_map = np.fromfile(fid, np.float32).reshape(h, w)
if (h, w) != image.shape[:2]:
depth_map = zoom(
depth_map,
(float(image.shape[0]) / h, float(image.shape[1]) / w),
order=0)
if args.save_normals:
with open(normals_file, "rb") as fid:
w = int("".join(iter(lambda: fid.read(1), "&")))
h = int("".join(iter(lambda: fid.read(1), "&")))
c = int("".join(iter(lambda: fid.read(1), "&")))
normals = np.fromfile(
fid, np.float32).reshape(c, h, w).transpose([1, 2, 0])
if (h, w) != image.shape[:2]:
normals = zoom(
normals,
(float(image.shape[0]) / h, float(image.shape[1]) / w, 1.),
order=0)
if args.min_depth is not None:
depth_map[depth_map < args.min_depth] = 0.
if args.max_depth is not None:
depth_map[depth_map > args.max_depth] = 0.
# create 3D points
#depth_map = np.minimum(depth_map, 100.)
points3D = np.dstack(camera.get_image_grid() + [depth_map])
points3D[:,:,:2] *= depth_map[:,:,np.newaxis]
# save
points3D = points3D.astype(np.float32).reshape(-1, 3)
if args.save_normals:
normals = normals.astype(np.float32).reshape(-1, 3)
image = image.reshape(-1, 3)
if image.dtype != np.uint8:
if image.max() <= 1:
image = (image * 255.).astype(np.uint8)
else:
image = image.astype(np.uint8)
if args.world_space:
points3D = points3D.dot(rotation_camera_from_world) + camera_center
if args.save_normals:
normals = normals.dot(rotation_camera_from_world)
if args.save_normals:
vertices = np.rec.fromarrays(
tuple(points3D.T) + tuple(normals.T) + tuple(image.T),
names="x,y,z,nx,ny,nz,red,green,blue")
else:
vertices = np.rec.fromarrays(
tuple(points3D.T) + tuple(image.T), names="x,y,z,red,green,blue")
vertices = PlyElement.describe(vertices, "vertex")
PlyData([vertices]).write(args.output_filename)
#-------------------------------------------------------------------------------
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("dense_folder", type=str)
parser.add_argument("image_filename", type=str)
parser.add_argument("output_filename", type=str)
parser.add_argument(
"--photometric", default=False, action="store_true",
help="use photometric depthmap instead of geometric")
parser.add_argument(
"--world_space", default=False, action="store_true",
help="apply the camera->world extrinsic transformation to the result")
parser.add_argument(
"--save_normals", default=False, action="store_true",
help="load the estimated normal map and save as part of the PLY")
parser.add_argument(
"--stereo_folder", type=str, default="stereo",
help="folder in the dense workspace containing depth and normal maps")
parser.add_argument(
"--min_depth", type=float, default=None,
help="set pixels with depth less than this value to zero depth")
parser.add_argument(
"--max_depth", type=float, default=None,
help="set pixels with depth greater than this value to zero depth")
args = parser.parse_args()
main(args)
|