コード例 #1
0
ファイル: hw3p5a.py プロジェクト: santhosh-r/Assorted
def main():
    system('')  # Enable VT100 Emulation for colors in Windows
    # color_cube.py header modified so that Open3D can read the colors (eg. 'diffuse_red' -> 'r')
    mesh = open3d.read_triangle_mesh('input/extra/color_cube_mod.ply')
    he_mesh = HalfEdgeDS(mesh)
    print('Displaying Original Mesh...')
    print('Vertices:\x1B[32m', len(he_mesh.vertices), '\x1B[0mEgdes:\x1B[32m',
          int(len(he_mesh.halfedges) / 2), '\x1B[0mFaces:\x1B[32m',
          len(he_mesh.faces), '\x1B[0m')
    mesh.compute_vertex_normals()
    open3d.draw_geometries([mesh])
    for i in range(5):
        print('Deleting random edge.')
        deletion_index = randint(0, len(he_mesh.halfedges) - 1)
        delete_halfedge(he_mesh, he_mesh.halfedges[deletion_index])
        print('Displaying Mesh with {} random edge(s) deleted...'.format(i +
                                                                         1))
        print('Vertices:\x1B[32m',
              len(he_mesh.vertices), '\x1B[0mEgdes:\x1B[32m',
              int(len(he_mesh.halfedges) / 2), '\x1B[0mFaces:\x1B[32m',
              len(he_mesh.faces), '\x1B[0m')
        mesh = he_mesh.convert_to_IFS()
        mesh.compute_vertex_normals()
        if (i == 0):
            open3d.write_triangle_mesh('output/hw3p5a.ply', mesh)
        open3d.draw_geometries([mesh])
コード例 #2
0
def transfer_meshes(instructions, sessions, include_objects, dest_host,
                    dest_user, dest_dir):
    logger = logging.getLogger(__name__)
    if instructions is None:
        logger.error('No instructions specified')
        return

    for instruction in instructions:
        data_dirs = getattr(dataset_utils,
                            '{:s}_data_dirs'.format(instruction))

        if sessions is None:
            sessions = [
                '{:d}'.format(idx + 1) for idx in range(len(data_dirs))
            ]

        with paramiko.SSHClient() as ssh:
            ssh.load_system_host_keys()
            ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            ssh.connect(hostname=dest_host, username=dest_user)

            with SCPClient(ssh.get_transport(), socket_timeout=20) as scp:
                for session_id in sessions:
                    session_name = 'full{:s}_{:s}'.format(
                        session_id, instruction)
                    logger.info('Session {:s}'.format(session_name))
                    idx = int(session_id) - 1
                    mesh_filenames = dataset_utils.get_session_mesh_filenames(
                        session_name, data_dirs[idx])
                    for object_name, mesh_filename in mesh_filenames.items():
                        if include_objects is not None:
                            if object_name not in include_objects:
                                continue
                        m = open3d.read_triangle_mesh(mesh_filename)
                        m.compute_vertex_normals()
                        m.compute_triangle_normals()
                        colors = np.asarray(m.vertex_colors)[:, 0]
                        colors = texture_proc(colors,
                                              invert=('full14'
                                                      in session_name))
                        m.vertex_colors = open3d.Vector3dVector(colors)
                        open3d.write_triangle_mesh(
                            '/tmp/tmp_contactdb_mesh.ply', m)
                        dest_filename = osp.join(
                            dest_dir, 'meshes',
                            '{:s}_{:s}.ply'.format(session_name, object_name))
                        scp.put('/tmp/tmp_contactdb_mesh.ply', dest_filename)
                        print('Written {:s}'.format(dest_filename))
                        time.sleep(1)
コード例 #3
0
def main():
    # mesh = open3d.read_triangle_mesh('input/extra/icosahedron.ply') # Every edge has the same length -> Output shape = Input shape
    mesh = open3d.read_triangle_mesh('input/extra/color_cube_mod.ply')
    # mesh = open3d.read_triangle_mesh('input/Armadillo.ply')
    mesh.paint_uniform_color([0.428, 0.428, 0.428])
    mesh.compute_vertex_normals()
    print('Displaying Original Mesh.')
    open3d.draw_geometries([mesh])
    print_alt('Converting to Half-Edge Data Structure...')
    he_mesh = HalfEdgeDS(mesh)
    print_alt('Done.\nSmoothing Mesh...')
    smoothen_mesh(he_mesh)
    print('Done.\nDisplaying Smoothened Mesh.')
    mesh = he_mesh.convert_to_IFS()
    mesh.compute_vertex_normals()
    open3d.draw_geometries([mesh])
    open3d.write_triangle_mesh('output/hw3p6.ply', mesh)
コード例 #4
0
    for i in range(len(depth_image_path)):
        depth = open3d.read_image(depth_image_path[i])
        color = open3d.read_image(os.path.join(path, 'image', color_image_path[i]))
        rgbd_image = open3d.create_rgbd_image_from_color_and_depth(color, depth,
                convert_rgb_to_intensity = False)
        if debug_mode:
            pcd = open3d.create_point_cloud_from_rgbd_image(rgbd_image,
                    open3d.PinholeCameraIntrinsic(open3d.PinholeCameraIntrinsicParameters.PrimeSenseDefault))
            open3d.draw_geometries([pcd])
        rgbd_images.append(rgbd_image)

    # Before full optimization, let's just visualize texture map
    # with given geometry, RGBD images, and camera poses.
    option = open3d.ColorMapOptmizationOption()
    option.maximum_iteration = 0
    open3d.color_map_optimization(mesh, rgbd_images, camera, option)
    open3d.draw_geometries([mesh])
    open3d.write_triangle_mesh(os.path.join(path, "scene",
        "color_map_before_optimization.ply"), mesh)

    # Optimize texture and save the mesh as texture_mapped.ply
    # This is implementation of following paper
    # Q.-Y. Zhou and V. Koltun,
    # Color Map Optimization for 3D Reconstruction with Consumer Depth Cameras,
    # SIGGRAPH 2014
    option.maximum_iteration = 300
    option.non_rigid_camera_coordinate = False
    open3d.color_map_optimization(mesh, rgbd_images, camera, option)
    open3d.draw_geometries([mesh])
    open3d.write_triangle_mesh(os.path.join(path, "scene",
        "color_map_after_optimization.ply"), mesh)
コード例 #5
0
def map_texture(object_name, session_name, base_dir, models_dir,
    debug_mode=False, show_textured_mesh=False,
    depth_thresh_for_visibility=1e-2, depth_thresh_for_discontinuity=0.035,
    max_vertex_normal_angle=70, real_depth_maps=False):
  data_dir = osp.join(base_dir, session_name, object_name)
  try:
    with open(osp.join(data_dir, 'object_name.txt'), 'r') as f:
      object_name = f.readline().rstrip()
  except IOError:
    print('{:s} does not have object_name.txt, skipping'.format(data_dir))
    return

  # read mesh file
  mesh_filename = osp.join(models_dir, '{:s}.ply'.format(object_name))
  mesh = open3d.read_triangle_mesh(mesh_filename)
  if not mesh.has_vertex_normals():
    mesh.compute_vertex_normals()

  # names of views
  rgb_im_dir = osp.join(data_dir, 'thermal_images')
  pose_dir = osp.join(data_dir, 'poses')
  names = []
  for filename in os.listdir(pose_dir):
    if '.txt' not in filename:
      continue
    if 'camera_pose' not in filename:
      continue
    name = '_'.join(filename.split('.')[0].split('_')[2:])
    names.append(name)
  names = sorted(names)

  # camera extrinsics
  Ts = []
  for name in names:
    filename = osp.join(pose_dir, 'camera_pose_{:s}.txt'.format(name))
    T = np.eye(4)
    with open(filename, 'r') as f:
      f.readline()
      T[0, 3] = float(f.readline().strip())
      T[1, 3] = float(f.readline().strip())
      T[2, 3] = float(f.readline().strip())
      f.readline()
      f.readline()
      T[0, :3] = [float(v) for v in f.readline().strip().split()]
      T[1, :3] = [float(v) for v in f.readline().strip().split()]
      T[2, :3] = [float(v) for v in f.readline().strip().split()]
    Ts.append(np.linalg.inv(T))

  # RGB images
  rgb_ims = []
  im_intensities = []
  for name in names:
    rgb_im = open3d.read_image(osp.join(rgb_im_dir, '{:s}.png'.format(name)))
    rgb_im = np.asarray(rgb_im)
    rgb_ims.append(rgb_im)
    im_shape = rgb_im.shape
    intensity = rgb_im[:200, :200, 0].mean()
    im_intensities.append(intensity)
  target_intensity = np.mean(im_intensities)
  for i, rgb_im in enumerate(rgb_ims):
    rgb_im = rgb_im * target_intensity / im_intensities[i]
    rgb_im = np.clip(rgb_im, a_min=0, a_max=255)
    rgb_ims[i] = open3d.Image(rgb_im.astype(np.uint8))

  # camera intrinsic
  cinfo_filename = cinfo_manager.getPackageFileName(
    'package://contactdb_utils/calibrations/boson.yaml')
  cinfo = cinfo_manager.loadCalibrationFile(cinfo_filename, 'boson')
  h_scaling = float(im_shape[0]) / cinfo.height
  w_scaling = float(im_shape[1]) / cinfo.width
  K = np.asarray(cinfo.K)
  K[0] *= w_scaling
  K[2] *= w_scaling
  K[4] *= h_scaling
  K[5] *= h_scaling
  K = K.reshape((3,3))
  intrinsic = open3d.PinholeCameraIntrinsic(im_shape[1], im_shape[0], K[0,0],
    K[1,1], K[0,2], K[1,2])

  if real_depth_maps:  # use registered Kinect depthmaps
    depth_im_dir = osp.join(data_dir, 'depth_images')
    depth_ims = []
    for name in names:
      depth_im_filename = osp.join(depth_im_dir, '{:s}.png'.format(name))
      depth_im = open3d.read_image(depth_im_filename)
      depth_im = np.uint16(fill_holes(depth_im))
      depth_ims.append(depth_im)
  else:  # create depth maps by rendering
    depth_ims = render_depth_maps(mesh_filename, intrinsic, Ts)

  # create RGB-D images
  rgbds = []
  for depth_im, rgb_im, T in zip(depth_ims, rgb_ims, Ts):
    depth_im = open3d.Image(depth_im)
    rgbds.append(open3d.create_rgbd_image_from_color_and_depth(rgb_im,
      depth_im, convert_rgb_to_intensity=False))
    if debug_mode:
      pc = open3d.create_point_cloud_from_rgbd_image(rgbds[-1], intrinsic)
      tmesh = copy(mesh)
      tmesh.transform(T)
      geoms = [pc]
      if show_textured_mesh:
        geoms.append(tmesh)
      open3d.draw_geometries(geoms)

  # create trajectory for texture mapping
  traj = open3d.PinholeCameraTrajectory()
  traj.extrinsic = open3d.Matrix4dVector(np.asarray(Ts))
  traj.intrinsic = intrinsic

  # do texture mapping!
  option = open3d.ColorMapOptmizationOption()
  option.maximum_iteration = 300
  option.depth_threshold_for_visiblity_check = depth_thresh_for_visibility
  option.depth_threshold_for_discontinuity_check = \
      depth_thresh_for_discontinuity
  option.half_dilation_kernel_size_for_discontinuity_map = 0
  option.max_angle_vertex_normal_camera_ray = max_vertex_normal_angle
  open3d.color_map_optimization(mesh, rgbds, traj, option)

  if not debug_mode:  # write result as a PLY file
    mesh_filename = osp.join(data_dir, 'thermal_images',
      '{:s}_textured.ply'.format(object_name))
    open3d.write_triangle_mesh(mesh_filename, mesh)
    print('Written {:s}'.format(mesh_filename))
    if show_textured_mesh:
      show_object_mesh(data_dir)