def __call__(self, vertx, faces, normals): mesh = trimesh.Trimesh(vertices=vertx, faces=faces, vertex_normals=normals) if self.reduction > 0: mesh = mesh.simplify_quadratic_decimation( len(faces) * self.reduction) if self.smoothing > 0: mesh = filter_laplacian(mesh, iterations=self.smoothing) return mesh
def add_uv_texture_to_mesh(mesh, K, image, mask): f, ox, oy = K verts, faces = mesh.get_mesh_verts_faces(0) verts = verts.tolist() mesh = Trimesh(vertices=verts, faces=faces) mesh = filter_laplacian(mesh) verts = mesh.vertices faces = mesh.faces front_view_faces = MeshRCNNModel.get_front_view_faces(mesh) front_view_vertices = np.unique(np.array(front_view_faces)) uv_map = [] for vertex_index in range(len(verts)): if vertex_index in front_view_vertices: x, y, z = verts[vertex_index] i = int(-x * f / z + K[1]) / image.shape[1] j = int(y * f / z + K[2]) / image.shape[0] else: i = 0 j = 0 uv_map.append([i, j]) # Average color calculation inside the mask color_mask = ~np.repeat(mask[:, :, np.newaxis], 3, axis=2) average_color = np.ma.array(image, mask=color_mask).mean(axis=0).mean(axis=0) # Assign average color to pixels outside of the mask image[:, :, 0] = np.where(~mask, average_color[0], image[:, :, 0]) image[:, :, 1] = np.where(~mask, average_color[1], image[:, :, 1]) image[:, :, 2] = np.where(~mask, average_color[2], image[:, :, 2]) image_ = Image.fromarray(image) texture = TextureVisuals(uv=uv_map, image=image_) textured_mesh = Trimesh(vertices=verts, faces=faces, visual=texture) return textured_mesh
def visualize_prediction(self, det_id, image, box, label, score, mask, mesh, K, alpha=0.6, dpi=200): mask_color = np.array(self.colors[label], dtype=np.float32) cat_name = self.cat_names[label] thickness = max([int(np.ceil(0.001 * image.shape[0])), 1]) box_color = (0, 255, 0) # '#00ff00', green text_color = (20, 20, 20) # gray composite = image.copy().astype(np.float32) # overlay mask idx = mask.nonzero() composite[idx[:, 0], idx[:, 1], :] *= 1.0 - alpha composite[idx[:, 0], idx[:, 1], :] += alpha * mask_color # overlay box (x0, y0, x1, y1) = (int(x + 0.5) for x in box) composite = cv2.rectangle(composite, (x0, y0), (x1, y1), color=box_color, thickness=thickness) composite = composite.astype(np.uint8) # overlay text font_scale = 0.002 * image.shape[0] font_thickness = thickness font = cv2.FONT_HERSHEY_TRIPLEX text = cat_name ((text_w, text_h), _) = cv2.getTextSize(text, font, font_scale, font_thickness) # Place text background. if x0 + text_w > composite.shape[1]: x0 = composite.shape[1] - text_w if y0 - int(1.2 * text_h) < 0: y0 = int(1.2 * text_h) back_topleft = x0, y0 - int(1.3 * text_h) back_bottomright = x0 + text_w, y0 cv2.rectangle(composite, back_topleft, back_bottomright, box_color, -1) # Show text text_bottomleft = x0, y0 - int(0.2 * text_h) cv2.putText( composite, text, text_bottomleft, font, font_scale, text_color, thickness=font_thickness, lineType=cv2.LINE_AA, ) save_file = os.path.join( self.output_dir, "%d_mask_%s_%.3f.png" % (det_id, cat_name, score)) cv2.imwrite(save_file, composite[:, :, ::-1]) basic_texturing_mesh = self.add_texture_to_mesh(mesh, K, image) save_file = os.path.join( self.output_dir, "%d_mesh_%s_%.3f.ply" % (det_id, cat_name, score)) basic_texturing_mesh.export( save_file, encoding='binary', vertex_normal=basic_texturing_mesh.vertex_normals.tolist()) uv_texturing_mesh = self.add_uv_texture_to_mesh( mesh, K, image, mask.numpy()) res = FilePathResolver('./output') uv_texturing_mesh = export_obj(uv_texturing_mesh, resolver=res) file_path = './output/uv_textured.obj' with open(file_path, "w") as f: f.write(uv_texturing_mesh) basic_texturing_mesh = filter_laplacian(basic_texturing_mesh) smoothing_file = os.path.join(self.output_dir, "with_smoothing.ply") basic_texturing_mesh.export( smoothing_file, encoding='binary', vertex_normal=basic_texturing_mesh.vertex_normals.tolist()) best_match = self.retrieval.find_closest(basic_texturing_mesh, cat_name) best_match_file = os.path.join(self.output_dir, "retrieval.ply") best_match.export(best_match_file, encoding='binary', vertex_normal=best_match.vertex_normals.tolist())
if __name__ == '__main__': ''' This scripts shows examples of basic functionalities offered by SLAM. Some (most) of these are actually inherited from Trimesh This script does not plot anything, see example_plot.py for that purpose ''' mesh_file = 'data/example_mesh.gii' # loading a mesh stored on the disc as a gifti file, # this is a feature of SLAM mesh = sio.load_mesh(mesh_file) # affine transformations can be applied to mesh objects mesh.apply_transform(mesh.principal_inertia_transform) # laplacian smoothing is available in Trimesh mesh_s = sm.filter_laplacian(mesh, iterations=20) # mesh.fill_holes() is able to fill missing face but do not handle # larger holes # interesting properties / functions of a mesh # see base.py for more details # what's the euler number for the mesh? print('mesh.euler_number=', mesh.euler_number) # access mesh edges mesh.edges # access mesh faces mesh.faces # access mesh vertice mesh.vertices
from trimesh.creation import icosphere mesh1 = icosphere(3, 0.65) mesh2 = icosphere(3, 0.8) elif domain == "cube": from trimesh.creation import box from trimesh.smoothing import filter_laplacian mesh1 = box((1.0, 1.0, 1.0)) mesh2 = box((1.5, 1.5, 1.5)) for i in range(4): mesh1 = mesh1.subdivide() mesh2 = mesh2.subdivide() mesh1 = filter_laplacian(mesh1) mesh2 = filter_laplacian(mesh2, 0.9) elif domain == "combined": from trimesh.creation import icosphere mesh1 = icosphere(4, 0.65) mesh2 = load_example_mesh("cube_fillet") mesh2.vertices -= mesh2.vertices.mean(axis=0) mesh2.vertices *= 0.15 # mesh2 = mesh2.subdivide() coil1 = MeshConductor( mesh_obj=mesh1, N_sph=3, inductance_nchunks=100, fix_normals=False,
import slam.distortion as sdst from trimesh import smoothing as sm import slam.plot as splt import slam.io as sio import numpy as np # import matplotlib.pyplot as plt # import matplotlib # matplotlib.use('TkAgg') if __name__ == '__main__': mesh = sio.load_mesh('data/example_mesh.gii') mesh.apply_transform(mesh.principal_inertia_transform) # mesh.show() mesh_s = sm.filter_laplacian(mesh.copy(), iterations=100) # mesh_s.show() print(mesh.vertices.shape) angle_diff = sdst.angle_difference(mesh, mesh_s) print(angle_diff) face_angle_dist = np.sum(np.abs(angle_diff), 1) print(face_angle_dist) vect_col = np.random.random_integers(0, 255, mesh.faces.shape[0]) print(vect_col.shape) # mesh.visual.vertex_colors = vert_col mesh.visual.faces_colors = vect_col mesh.show()