Пример #1
0
def tsdf_voxel_volume(fx, fy, cx, cy):
    print("Estimating voxel volume bounds....")
    cam_intrinsics = cam.camera_intrinsics(fx, fy, cx, cy)
    vol_bnds = np.zeros((3, 2))
    depth_image = np.load(
        "/home/aditya/PycharmProjects/OpenCV-python/Project_2/TDCV-Project-2/trial1.npz"
    )['depth']
    camera_pose = np.identity(4)

    view_frust_pts = fusion.get_view_frustum(depth_image, cam_intrinsics,
                                             camera_pose)
    vol_bnds[:, 0] = np.minimum(vol_bnds[:, 0], np.amin(view_frust_pts,
                                                        axis=1))
    vol_bnds[:, 1] = np.maximum(vol_bnds[:, 1], np.amax(view_frust_pts,
                                                        axis=1))

    print("Initializing voxel volume...")
    tsdf_vol = fusion.TSDFVolume(vol_bnds, voxel_size=0.01)

    tsdf_vol.integrate(depth_image,
                       depth_image,
                       cam_intrinsics,
                       camera_pose,
                       obs_weight=1.0)
    print("Saving mesh to mesh.ply...")
    verts, faces, norms, colors = tsdf_vol.get_mesh()
    fusion.meshwrite("mesh.ply", verts, faces, norms, colors)
    """
Пример #2
0
def merge_meshes():

    print("mering meshes, ", datetime.datetime.now())

    meshes = set()

    for file in os.listdir("meshes"):
        meshes.add("meshes/" + file)

    verts = []
    faces = []
    norms = []
    colors = []

    total_verts = 0

    for mesh in meshes:
        f = open(mesh, 'r')
        f.readline()
        f.readline()
        vertex_line = f.readline()
        face_line = f.readline()
        while ('face' not in face_line):
            face_line = f.readline()
        f.readline()
        f.readline()

        num_vertices = int(vertex_line.split(" ")[2])
        num_faces = int(face_line.split(" ")[2])

        for i in range(num_vertices):
            line = f.readline().split(" ")
            verts.append([float(line[0]), float(line[1]), float(line[2])])
            norms.append([float(line[3]), float(line[4]), float(line[5])])
            colors.append([int(line[6]), int(line[7]), int(line[8])])

        for i in range(num_faces):
            line = f.readline().split(" ")
            faces.append([
                int(line[1]) + total_verts,
                int(line[2]) + total_verts,
                int(line[3]) + total_verts
            ])

        total_verts = len(verts)

    verts = np.array(verts)
    faces = np.array(faces)
    norms = np.array(norms)
    colors = np.array(colors)

    fusion.meshwrite("mesh_combined.ply", verts, faces, norms, colors)
Пример #3
0
            final_mask = final_mask.astype(np.float)
            color_image = cv2.cvtColor(cv2.imread(rgb_file), cv2.COLOR_BGR2RGB)
            color_image = (color_image * final_mask).astype(np.uint8)
            depth_im = cv2.imread(depth_file,
                                  cv2.IMREAD_ANYCOLOR | cv2.IMREAD_ANYDEPTH)
            depth_im = (depth_im * final_mask[:, :, 0]).astype(np.float)
            depth_im /= 5000.
            #depth_im[depth_im == 65.535] = 0
            img_no_int = int(img_no)
            cam_pose = cam_poses[4 * img_no_int:4 * (img_no_int + 1), :]

            # Integrate observation into voxel volume (assume color aligned with depth)
            tsdf_vol.integrate(color_image,
                               depth_im,
                               cam_intr,
                               cam_pose,
                               obs_weight=1.)

    file.close()
    fps = n_imgs / (time.time() - t0_elapse)
    print("Average FPS: {:.2f}".format(fps))

    # Get mesh from voxel volume and save to disk (can be viewed with Meshlab)
    print("Saving mesh to mesh.ply...")
    verts, faces, norms, colors = tsdf_vol.get_mesh()
    fusion.meshwrite("suitcase-mesh.ply", verts, faces, norms, colors)

    # Get point cloud from voxel volume and save to disk (can be viewed with Meshlab)
    print("Saving point cloud to pc.ply...")
    point_cloud = tsdf_vol.get_point_cloud()
    fusion.pcwrite("suitcase-pcd.ply", point_cloud)
Пример #4
0
            mask = tsdf_vol.integrate(color_image,
                                      depth_im,
                                      cam_intr,
                                      cam_pose,
                                      mask,
                                      obs_weight=1.)

        fps = imgs_per_scene / (time.time() - t0_elapse)
        print("Average FPS: {:.2f}".format(fps))

        # Get the TSDF volume
        tsdf_grid, color_grid = tsdf_vol.get_volume()
        os.mkdir(dir_output / f'scene-{scene_number}')
        # f_mask = dir_output / f'scene-{scene_number}' / f'mask{ext_mask}'
        # np.save(str(f_mask), mask)
        # print(f'Saving Mask {f_mask}')

        f_tsdf_grid = dir_output / f'scene-{scene_number}' / f'tsdf{ext_tsdf}'
        np.save(str(f_tsdf_grid), tsdf_grid)
        print(f'Saving TSDF {f_tsdf_grid}')

        # f_color_grid = dir_output / f'scene-{idx_scene:06d}' / f'col_grid{ext_col_grid}'
        # np.save(str(f_color_grid), color_grid)
        # print(f'Saving Color Voxel Grid {f_color_grid}')

        # Get mesh from voxel volume and save to disk (can be viewed with Meshlab)
        verts, faces, norms, colors = tsdf_vol.get_mesh()
        f_mesh = dir_output / f'scene-{scene_number}' / f'mesh.ply'
        fusion.meshwrite(str(f_mesh), verts, faces, norms, colors)
        print(f'Saving mesh to {f_mesh}')
Пример #5
0
    for i in range(n_imgs):
        print("Fusing frame %d/%d" % (i + 1, n_imgs))

        # Read RGB-D image and camera pose
        color_image = cv2.cvtColor(
            cv2.imread("data/frame-%06d.color.jpg" % (i)), cv2.COLOR_BGR2RGB)
        depth_im = cv2.imread("data/frame-%06d.depth.png" % (i),
                              -1).astype(float)
        depth_im /= 1000.
        depth_im[depth_im == 65.535] = 0
        cam_pose = np.loadtxt("data/frame-%06d.pose.txt" % (i))

        # Integrate observation into voxel volume (assume color aligned with depth)
        tsdf_vol.integrate(color_image,
                           depth_im,
                           cam_intr,
                           cam_pose,
                           obs_weight=1.)

    fps = n_imgs / (time.time() - t0_elapse)
    print("Average FPS: {:.2f}".format(fps))

    # Get mesh from voxel volume and save to disk (can be viewed with Meshlab)
    print("Saving mesh to mesh.ply...")
    verts, faces, norms, colors = tsdf_vol.get_mesh()
    fusion.meshwrite("mesh.ply", verts, faces, norms, colors)

    # Get point cloud from voxel volume and save to disk (can be viewed with Meshlab)
    print("Saving point cloud to pc.ply...")
    point_cloud = tsdf_vol.get_point_cloud()
    fusion.pcwrite("pc.ply", point_cloud)
Пример #6
0
            final_mask = final_mask.astype(np.float)
            color_image = cv2.cvtColor(cv2.imread(rgb_file), cv2.COLOR_BGR2RGB)
            color_image = (color_image * final_mask).astype(np.uint8)
            depth_im = cv2.imread(depth_file,
                                  cv2.IMREAD_ANYCOLOR | cv2.IMREAD_ANYDEPTH)
            depth_im = (depth_im * final_mask[:, :, 0]).astype(np.float)
            depth_im /= 5000.
            #depth_im[depth_im == 65.535] = 0
            img_no_int = int(img_no)
            cam_pose = cam_poses[4 * img_no_int:4 * (img_no_int + 1), :]

            # Integrate observation into voxel volume (assume color aligned with depth)
            tsdf_vol.integrate(color_image,
                               depth_im,
                               cam_intr,
                               cam_pose,
                               obs_weight=1.)

    file.close()
    fps = n_imgs / (time.time() - t0_elapse)
    print("Average FPS: {:.2f}".format(fps))

    # Get mesh from voxel volume and save to disk (can be viewed with Meshlab)
    print("Saving mesh to mesh.ply...")
    verts, faces, norms, colors = tsdf_vol.get_mesh()
    fusion.meshwrite("all-objs.ply", verts, faces, norms, colors)

# # Get point cloud from voxel volume and save to disk (can be viewed with Meshlab)
#   print("Saving point cloud to pc.ply...")
#   point_cloud = tsdf_vol.get_point_cloud()
#   fusion.pcwrite("all-objs.ply", point_cloud)
voxel_size = float(sys.argv[2])

block_length = float(sys.argv[3])

grid_bounds = np.array([[grid[0], grid[0] + block_length],
                        [grid[1], grid[1] + block_length],
                        [grid[2], grid[2] + block_length]])

plydata = PlyData.read(sys.argv[1])
print('getting vertices')
vertices = np.array(plydata.elements[0].data)

print('initializing tsdf with grid_bounds: ', grid_bounds)

mesh_name = "meshes/mesh_constructed_" + str(grid[0]) + "_" + str(
    grid[1]) + "_" + str(grid[2]) + ".ply"

print(mesh_name)

if os.path.exists(mesh_name):
    print("already done")
    exit()

tsdf_vol = fusion.TSDFVolume(grid_bounds, voxel_size=voxel_size)

tsdf_vol.fill_grid(vertices)

verts, faces, norms, colors = tsdf_vol.get_mesh()

fusion.meshwrite(mesh_name, verts, faces, norms, colors)
Пример #8
0
            mask_index_file.close()
            if flag:
                final_mask = final_mask.astype(np.float)
                color_image = cv2.cvtColor(cv2.imread(rgb_file),
                                           cv2.COLOR_BGR2RGB)
                color_image = (color_image * final_mask).astype(np.uint8)
                depth_im = cv2.imread(
                    depth_file, cv2.IMREAD_ANYCOLOR | cv2.IMREAD_ANYDEPTH)
                depth_im = (depth_im * final_mask[:, :, 0]).astype(np.float)
                depth_im /= 5000.
                #depth_im[depth_im == 65.535] = 0
                img_no_int = int(img_no)
                cam_pose = cam_poses[4 * img_no_int:4 * (img_no_int + 1), :]

                # Integrate observation into voxel volume (assume color aligned with depth)
                tsdf_vol.integrate(color_image,
                                   depth_im,
                                   cam_intr,
                                   cam_pose,
                                   obs_weight=1.)

        file.close()
        fps = n_imgs / (time.time() - t0_elapse)
        print("Average FPS: {:.2f}".format(fps))

        # Get mesh from voxel volume and save to disk (can be viewed with Meshlab)
        mesh_str = str(class_ids[index])
        print("Saving mesh to mesh.ply...")
        verts, faces, norms, colors = tsdf_vol.get_mesh()
        fusion.meshwrite(mesh_str + '.ply', verts, faces, norms, colors)
Пример #9
0
        print("Fusing frame %d/%d" % (i + 1, n_imgs))

        # Read RGB-D image and camera pose
        color_image = cv2.cvtColor(cv2.imread(rgb_file), cv2.COLOR_BGR2RGB)
        depth_im = cv2.imread(depth_file,
                              cv2.IMREAD_ANYCOLOR | cv2.IMREAD_ANYDEPTH)
        depth_im = depth_im.astype(np.float)
        depth_im /= 5000.
        #depth_im[depth_im == 65.535] = 0
        cam_pose = cam_poses[4 * i:4 * (i + 1), :]

        # Integrate observation into voxel volume (assume color aligned with depth)
        tsdf_vol.integrate(color_image,
                           depth_im,
                           cam_intr,
                           cam_pose,
                           obs_weight=1.)

    file.close()
    fps = n_imgs / (time.time() - t0_elapse)
    print("Average FPS: {:.2f}".format(fps))

    # Get mesh from voxel volume and save to disk (can be viewed with Meshlab)
    print("Saving mesh to mesh.ply...")
    verts, faces, norms, colors = tsdf_vol.get_mesh()
    fusion.meshwrite("mesh-blender-table.ply", verts, faces, norms, colors)

    # Get point cloud from voxel volume and save to disk (can be viewed with Meshlab)
    print("Saving point cloud to pc.ply...")
    point_cloud = tsdf_vol.get_point_cloud()
    fusion.pcwrite("pc-blender-table.ply", point_cloud)
Пример #10
0
def generate_mesh_blocks():

    plydata = PlyData.read(sys.argv[1])
    print('getting vertices')
    vertices = np.array(plydata.elements[0].data)

    if precise:

        print('creating output directory')
        if os.path.isdir("meshes"):
            print("meshes already exists")
        else:
            os.mkdir("meshes", mode=0o777)

        if os.path.isdir("blocks"):
            print("blocks already exists")
        else:
            os.mkdir("blocks", mode=0o777)

        print('generating blocks')

        block_dict = dict()

        for v in vertices:

            x = int(math.floor(
                (v[0] - half_len) /
                block_side_length)) * block_side_length + half_len
            y = int(math.floor(
                (v[1] - half_len) /
                block_side_length)) * block_side_length + half_len
            z = int(math.floor(
                (v[2] - half_len) /
                block_side_length)) * block_side_length + half_len

            pos = (x, y, z)

            if pos in block_dict:
                block_dict.get(pos).append(v)
            else:
                block_dict.update({pos: [v]})

    else:
        grid_bounds = np.array([[-2, 2], [-2, 2], [-2, 2]])

    if precise:

        already_done_blocks = set()

        for file in os.listdir("meshes"):
            already_done_blocks.add("meshes/" + file)

        print(already_done_blocks)

        for key in block_dict.keys():

            verts = block_dict.get(key)

            file_name = "blocks/" + str(key[0]) + "_" + str(
                key[1]) + "_" + str(key[2]) + ".ply"
            ply_file = open(file_name, "w")
            ply_file.write("ply\n")
            ply_file.write("format ascii 1.0\n")
            ply_file.write("element vertex %d\n" % (len(verts)))
            ply_file.write("property float x\n")
            ply_file.write("property float y\n")
            ply_file.write("property float z\n")
            ply_file.write("property uchar red\n")
            ply_file.write("property uchar green\n")
            ply_file.write("property uchar blue\n")
            ply_file.write("end_header\n")

            for i in range(len(verts)):
                ply_file.write("%f %f %f %d %d %d\n" %
                               (verts[i][0], verts[i][1], verts[i][2],
                                verts[i][3], verts[i][4], verts[i][5]))

            ply_file.close()

        bad_parallel()

    else:

        print('initializing tsdf for what reason idk')

        print(grid_bounds)

        tsdf_vol = fusion.TSDFVolume(grid_bounds, voxel_size=voxel_size)

        tsdf_vol.fill_grid(vertices)

        verts, faces, norms, colors = tsdf_vol.get_mesh()

        fusion.meshwrite("mesh_constructed.ply", verts, faces, norms, colors)
Пример #11
0
def main(args):
    if args.example == 'cpp':
        print("Using PyTorch CPP.")
        from cpp.integrate import integrate
    elif args.example == 'jit':
        print("Using PyTorch JIT.")
        from jit.integrate import integrate
    elif args.example == 'py':
        print("Using vanilla PyTorch.")
        from python.integrate import integrate
    else:
        pass

    # ======================================================================================================== #
    # (Optional) This is an example of how to compute the 3D bounds
    # in world coordinates of the convex hull of all camera view
    # frustums in the dataset
    # ======================================================================================================== #
    print("Estimating voxel volume bounds...")
    n_imgs = 15
    cam_intr = np.loadtxt("data/camera-intrinsics.txt", delimiter=' ')
    vol_bnds = np.zeros((3, 2))
    for i in range(n_imgs):
        # Read depth image and camera pose
        depth_im = cv2.imread("data/frame-%06d.depth.png" % (i),
                              -1).astype(float)
        depth_im /= 1000.  # depth is saved in 16-bit PNG in millimeters
        depth_im[
            depth_im ==
            65.535] = 0  # set invalid depth to 0 (specific to 7-scenes dataset)
        cam_pose = np.loadtxt("data/frame-%06d.pose.txt" %
                              (i))  # 4x4 rigid transformation matrix

        # Compute camera view frustum and extend convex hull
        view_frust_pts = fusion.get_view_frustum(depth_im, cam_intr, cam_pose)
        vol_bnds[:, 0] = np.minimum(vol_bnds[:, 0],
                                    np.amin(view_frust_pts, axis=1))
        vol_bnds[:, 1] = np.maximum(vol_bnds[:, 1],
                                    np.amax(view_frust_pts, axis=1))
    # ======================================================================================================== #

    # Initialize voxel volume
    print("Initializing voxel volume...")
    tsdf_vol = fusion.TSDFVolume(vol_bnds, 0.02, integrate)

    # ======================================================================================================== #
    # Integrate
    # ======================================================================================================== #
    # Loop through RGB-D images and fuse them together
    t0_elapse = time.time()
    times = []
    for i in range(n_imgs):
        print("Fusing frame %d/%d" % (i + 1, n_imgs))

        # Read RGB-D image and camera pose
        color_image = cv2.cvtColor(
            cv2.imread("data/frame-%06d.color.jpg" % (i)), cv2.COLOR_BGR2RGB)
        depth_im = cv2.imread("data/frame-%06d.depth.png" % (i),
                              -1).astype(float)
        depth_im /= 1000.
        depth_im[depth_im == 65.535] = 0
        cam_pose = np.loadtxt("data/frame-%06d.pose.txt" % (i))

        # Integrate observation into voxel volume (assume color aligned with depth)
        tic = time.time()
        tsdf_vol.integrate(color_image,
                           depth_im,
                           cam_intr,
                           cam_pose,
                           obs_weight=1.)
        toc = time.time()
        times.append(toc - tic)

    fps = n_imgs / (time.time() - t0_elapse)
    print("Average FPS: {:.2f}".format(fps))

    times = [t * TIME_SCALES[args.scale] for t in times]
    print("Average integration time: {:.3f} {}".format(np.mean(times),
                                                       args.scale))

    # Extract pointcloud
    point_cloud = tsdf_vol.extract_point_cloud()
    fusion.pcwrite("pc.ply", point_cloud)

    # Get mesh from voxel volume and save to disk (can be viewed with Meshlab)
    print("Saving to mesh.ply...")
    verts, faces, norms, colors = tsdf_vol.extract_triangle_mesh()
    fusion.meshwrite("mesh.ply", verts, faces, norms, colors)