コード例 #1
0
ファイル: 2_fusion.py プロジェクト: lsteiner91/mesh-fusion
    def render(self, mesh, Rs):
        """
        Render the given mesh using the generated views.

        :param base_mesh: mesh to render
        :type base_mesh: mesh.Mesh
        :param Rs: rotation matrices
        :type Rs: [numpy.ndarray]
        :return: depth maps
        :rtype: numpy.ndarray
        """

        depthmaps = []
        for i in range(len(Rs)):
            np_vertices = Rs[i].dot(mesh.vertices.astype(np.float64).T)
            np_vertices[2, :] += 1

            np_faces = mesh.faces.astype(np.float64)
            np_faces += 1

            depthmap, mask, img = pyrender.render(np_vertices.copy(),
                                                  np_faces.T.copy(),
                                                  self.render_intrinsics,
                                                  self.znf, self.image_size)

            # This is mainly result of experimenting.
            # The core idea is that the volume of the object is enlarged slightly
            # (by subtracting a constant from the depth map).
            # Dilation additionally enlarges thin structures (e.g. for chairs).
            depthmap -= self.options.depth_offset_factor * self.voxel_size
            depthmap = ndimage.morphology.grey_erosion(depthmap, size=(3, 3))

            depthmaps.append(depthmap)

        return depthmaps
コード例 #2
0
def render(vertices, faces):

    x = 0
    y = math.pi / 4
    z = 0
    R_x = np.array([[1, 0, 0], [0, math.cos(x), -math.sin(x)],
                    [0, math.sin(x), math.cos(x)]])
    R_y = np.array([[math.cos(y), 0, math.sin(y)], [0, 1, 0],
                    [-math.sin(y), 0, math.cos(y)]])
    R_z = np.array([[math.cos(z), -math.sin(z), 0],
                    [math.sin(z), math.cos(z), 0], [0, 0, 1]])
    R = R_z.dot(R_y.dot(R_x))

    np_vertices = np.array(vertices).astype(np.float64)
    np_vertices = R.dot(np_vertices.T).T
    np_vertices[:, 2] += 1.5
    np_faces = np.array(faces).astype(np.float64)
    np_faces += 1

    depthmap, mask, img = pyrender.render(
        np_vertices.T.copy(), np_faces.T.copy(), np.array([fx, fy, cx, cy]),
        np.array([1., 2.]), np.array([img_h, img_w], dtype=np.int32))
    pyplot.imshow(depthmap)
    pyplot.show()
    pyplot.imshow(img)
    pyplot.show()
コード例 #3
0
def render(mesh, Rs):
    """
    Render the given mesh using the generated views.

    :param base_mesh: mesh to render
    :type base_mesh: mesh.Mesh
    :param Rs: rotation matrices
    :type Rs: [numpy.ndarray]
    :return: depth maps
    :rtype: numpy.ndarray
    """

    intrinsics = np.array([
        config['watertight_rendering']['focal_length_x'],
        config['watertight_rendering']['focal_length_y'],
        config['watertight_rendering']['principal_point_x'],
        config['watertight_rendering']['principal_point_x']
    ],
                          dtype=float)
    image_size = np.array([
        config['watertight_rendering']['image_height'],
        config['watertight_rendering']['image_width'],
    ],
                          dtype=np.int32)
    znf = np.array([
        config['watertight_rendering']['mesh_center'][2] - 0.75,
        config['watertight_rendering']['mesh_center'][2] + 0.75
    ],
                   dtype=float)
    depthmaps = []

    for i in range(len(Rs)):
        np_vertices = Rs[i].dot(mesh.vertices.astype(np.float64).T)
        np_vertices[2, :] += config['watertight_rendering']['mesh_center'][2]

        np_faces = mesh.faces.astype(np.float64)
        np_faces += 1

        depthmap, mask, img = pyrender.render(np_vertices.copy(),
                                              np_faces.T.copy(), intrinsics,
                                              znf, image_size)

        # This is mainly the results of experimentation.
        # We first close holes, and then offset the depth map in order to
        # render the car with more volume.
        # The dilation additionally makes sure that thin structures are
        # preserved.
        depthmap = ndimage.morphology.grey_erosion(depthmap, size=(5, 5))
        depthmap = ndimage.morphology.grey_dilation(depthmap, size=(5, 5))
        depthmap -= config['watertight_rendering'][
            'depth_offset_factor'] * config['watertight_fusion']['voxel_size']
        depthmap = ndimage.morphology.grey_erosion(depthmap, size=(3, 3))

        depthmaps.append(depthmap)

    return depthmaps
def render(mesh, Rs):
    """
    Render the given mesh using the generated views.

    :param base_mesh: mesh to render
    :type base_mesh: mesh.Mesh
    :param Rs: rotation matrices
    :type Rs: [numpy.ndarray]
    :return: depth maps
    :rtype: numpy.ndarray
    """

    intrinsics = np.array([
        config['watertight_rendering']['focal_length_x'],
        config['watertight_rendering']['focal_length_y'],
        config['watertight_rendering']['principal_point_x'],
        config['watertight_rendering']['principal_point_x']
    ],
                          dtype=float)
    image_size = np.array([
        config['watertight_rendering']['image_height'],
        config['watertight_rendering']['image_width'],
    ],
                          dtype=np.int32)
    znf = np.array([
        config['watertight_rendering']['mesh_center'][2] - 0.75,
        config['watertight_rendering']['mesh_center'][2] + 0.75
    ],
                   dtype=float)
    depthmaps = []

    for i in range(len(Rs)):
        np_vertices = Rs[i].dot(mesh.vertices.astype(np.float64).T)
        np_vertices[2, :] += config['watertight_rendering']['mesh_center'][2]

        np_faces = mesh.faces.astype(np.float64)
        np_faces += 1

        depthmap, mask, img = pyrender.render(np_vertices.copy(),
                                              np_faces.T.copy(), intrinsics,
                                              znf, image_size)

        # This is mainly result of experimenting.
        # The core idea is that the volume of the object is enlarged slightly
        # (by subtracting a constant from the depth map).
        # Dilation additionally enlarges thin structures (e.g. for chairs).
        depthmap -= config['watertight_rendering'][
            'depth_offset_factor'] * config['watertight_fusion']['voxel_size']
        depthmap = ndimage.morphology.grey_erosion(depthmap, size=(3, 3))

        depthmaps.append(depthmap)

    return depthmaps
コード例 #5
0
	def render(self, verts, pose):
		N = verts.shape[0]
		R = pose[:3, :3]
		trans = pose[:, 3:][:3].T.reshape(3)

		rotated_verts = verts @ R.T
		for i in range(N):
			rotated_verts[i, 0] = rotated_verts[i, 0] + trans[0]
			rotated_verts[i, 1] = rotated_verts[i, 1] + trans[1]
			rotated_verts[i, 2] = rotated_verts[i, 2] + trans[2]

		depth = pyrender.render(rotated_verts, self.faces, self.cam_intr, self.img_size)
		depth = post_process_depth(depth)
		depth_map = convert_depth2depthmap(depth)
		
		return depth, depth_map
コード例 #6
0
def render_depthmaps(verts, faces, calibs):
    # get camera cluster
    cluster = get_view_setting(0, len(calibs))

    # # visualize cluster
    # fig = plt.figure()
    # ax = fig.add_subplot(111, projection='3d')
    # plot_camera_cluster(ax, calibs=get_calibs(), size=0.25, cluster=cluster)
    # axis_equal_3d(ax)
    # plt.show()

    # render depthmaps
    depthmaps = []
    Ks = []
    Rs = []
    Ts = []
    for vidx in cluster:
        K = calibs[vidx].K
        R = calibs[vidx].R
        T = calibs[vidx].T

        render_verts = R.dot(verts) + T.reshape((3, 1))

        # render depth/mask
        depth, mask, img = pyrender.render(render_verts,
                                           faces,
                                           cam_intr,
                                           img_size,
                                           linewidth=0,
                                           colors=None)

        depth[mask != 1] = 10

        Ks.append(K)
        Rs.append(R)
        Ts.append(T)
        depthmaps.append(depth)

    Ks = np.array(Ks, dtype=np.float32)
    Rs = np.array(Rs, dtype=np.float32)
    Ts = np.array(Ts, dtype=np.float32)
    depthmaps = np.array(depthmaps, dtype=np.float32)

    return depthmaps, Ks, Rs * 3, Ts
コード例 #7
0
                rotations[n, k, 0] = (
                    np.random.random() *
                    (config['render_max_x_rotation'] +
                     abs(config['render_min_x_rotation'])) -
                    abs(config['render_min_x_rotation'])) / 180. * math.pi
                rotations[n, k, 1] = (
                    np.random.random() *
                    (config['render_max_y_rotation'] +
                     abs(config['render_min_y_rotation'])) -
                    abs(config['render_min_y_rotation'])) / 180. * math.pi

                mesh.rotate(rotations[n, k])
                mesh.translate(mesh_center)

                np_vertices = mesh.vertices.astype(np.float64)
                np_faces = mesh.faces.astype(np.float64)
                np_faces += 1

                depth_map, mask, img = pyrender.render(np_vertices.T.copy(),
                                                       np_faces.T.copy(),
                                                       intrinsics, znf, size)
                depth_maps[n][k] = depth_map

            print('[Data] rendered %s %d/%d' % (off_files[n],
                                                (n + 1), n_files))

        utils.write_hdf5(angles_file, rotations)
        print('[Data] wrote %s' % angles_file)
        utils.write_hdf5(depth_file, depth_maps)
        print('[Data] wrote %s' % depth_file)
コード例 #8
0
ファイル: test.py プロジェクト: kyanome/pyrenderer
init_trans = np.array([16.800812, 104.01688834, 586.28166456])
init_R = np.array([[1., 0., 0.], [0., -0.93969262, 0.34202021],
                   [0., -0.34202021, -0.93969262]])

rotate_verts = verts @ init_R.T
for i in range(len(verts)):
    rotate_verts[i, 0] = rotate_verts[i, 0] + init_trans[0]
    rotate_verts[i, 1] = rotate_verts[i, 1] + init_trans[1]
    rotate_verts[i, 2] = rotate_verts[i, 2] + init_trans[2]

c_verts = np.copy(rotate_verts)
c_faces = np.copy(faces)
cam_intr = np.array([612., 612., 310., 243.])
img_size = np.array([480, 640], dtype="int32")
depth = pyrender.render(c_verts, c_faces, cam_intr, img_size)

for i in range(depth.shape[0]):
    for k in range(depth.shape[1]):
        if depth[i, k] == -1.:
            depth[i, k] = 0.0
        else:
            print(depth[i, k])

import csv

f = open('some.csv', 'w')

writer = csv.writer(f, lineterminator='\n')
writer.writerows(depth)