from PIL import Image, ImageFont, ImageDraw from pyquaternion import Quaternion import open3d as o3d def text_3d(text, pos, direction=None, degree=0, font=None, font_size=300): """ 7 ==> ....... ....... ... ... Generate a 3D text point cloud used for visualization. :param text: content of the text :param pos: 3D xyz position of the text upper left corner :param direction: 3D normalized direction of where the text faces :param degree: in plane rotation of text :param font: Name of the font - change it according to your system :param font_size: size of the font :return: o3d.geoemtry.PointCloud object """ if direction is None: direction = (0., 0., 1.) font_obj = ImageFont.truetype(font, font_size) font_dim = font_obj.getsize(text) img = Image.new('RGB', font_dim, color=(255, 255, 255)) draw = ImageDraw.Draw(img) draw.text((0, 0), text, font=font_obj, fill=(0, 0, 0)) img = np.asarray(img) img_mask = img[:, :, 0] < 128 indices = np.indices([*img.shape[0:2], 1])[:, img_mask, 0].reshape(3, -1).T pcd = o3d.geometry.PointCloud() pcd.colors = o3d.utility.Vector3dVector(img[img_mask, :].astype(float) / 255.0) pcd.points = o3d.utility.Vector3dVector(indices / 100.0) raxis = np.cross([0.0, 0.0, 1.0], direction) if np.linalg.norm(raxis) < 1e-6: raxis = (0.0, 0.0, 1.0) trans = (Quaternion(axis=raxis, radians=np.arccos(direction[2])) * Quaternion(axis=direction, degrees=degree)).transformation_matrix trans[0:3, 3] = np.asarray(pos) pcd.transform(trans) return pcd if __name__ == '__main__': # 调用方式 text_point_cloud = text_3d('text', font='./fonts/casy_cz.ttf', pos=[0, 0, 0], direction=[0., 0., 1.0], degree=-90)
2021-05-26
最新推荐文章于 2024-03-29 15:45:04 发布