def render_color(mesh, f, Rt, img_size=(224, 224), bg_img=None): # apply Rt verts = mesh.points().copy() for i in range(len(verts)): verts[i] = np.dot(Rt[:3, :3], verts[i]) + Rt[:, 3] rn = ColoredRenderer() if bg_img is not None: rn.background_image = bg_img.astype( np.float) / 300. if bg_img.max() > 1 else bg_img rn.camera = ProjectPoints(v=verts, rt=np.zeros(3), t=np.zeros(3), f=np.array([f, f]), c=np.array([img_size[1], img_size[0]]) / 2., k=np.zeros(5)) rn.frustum = { 'near': .5, 'far': 10., 'width': img_size[1], 'height': img_size[0] } rn.v = verts rn.f = mesh.face_vertex_indices() rn.bgcolor = np.zeros(3) rn.vc = vc = mesh.vertex_colors()[:, :3] return rn.r
def test_teapot(): # load teapot and sphere reference = read_and_process_mesh( "example_data/pointclouds/teapot_mesh.obj", trans=ch.array([0, 0, 0]), rotation=ch.array([np.pi, 0, 0])) target = read_and_process_mesh( "example_data/pointclouds/sphere_normal_2K_mesh.obj", trans=ch.array([0, 0, 0]), rotation=ch.array([0, 0, 0])) # reference V_ref = ch.array(reference.v) vc_ref = ch.array(reference.vc) A_ref = LambertianPointLight(v=V_ref, f=reference.f, num_verts=len( V_ref), light_pos=ch.array([-1000, -1000, -1000]), vc=vc_ref, light_color=ch.array([0.9, 0, 0])) +\ LambertianPointLight(v=V_ref, f=reference.f, num_verts=len( V_ref), light_pos=ch.array([1000, -1000, -1000]), vc=vc_ref, light_color=ch.array([0.0, 0.9, 0])) +\ LambertianPointLight(v=V_ref, f=reference.f, num_verts=len( V_ref), light_pos=ch.array([-1000, 1000, -1000]), vc=vc_ref, light_color=ch.array([0.0, 0.0, 0.9])) U_ref = ProjectPoints(v=V_ref, f=[w, w], c=[w / 2., h / 2.], k=ch.zeros(5), t=ch.zeros(3), rt=ch.zeros(3)) f_ref = ColoredRenderer(vc=A_ref, camera=U_ref, f=reference.f, bgcolor=[1.0, 1.0, 1.0], frustum={ 'width': w, 'height': h, 'near': 1, 'far': 20 }) # target V_tgt = ch.array(target.v) vc_tgt = ch.array(target.vc) A_tgt = LambertianPointLight(v=V_tgt, f=target.f, num_verts=len( V_tgt), light_pos=ch.array([-1000, -1000, -1000]), vc=vc_tgt, light_color=ch.array([0.9, 0, 0])) +\ LambertianPointLight(v=V_tgt, f=target.f, num_verts=len( V_tgt), light_pos=ch.array([1000, -1000, -1000]), vc=vc_tgt, light_color=ch.array([0.0, 0.9, 0])) +\ LambertianPointLight(v=V_tgt, f=target.f, num_verts=len( V_tgt), light_pos=ch.array([-1000, 1000, -1000]), vc=vc_tgt, light_color=ch.array([0.0, 0.0, 0.9])) U_tgt = ProjectPoints(v=V_tgt, f=[w, w], c=[w / 2., h / 2.], k=ch.zeros(5), t=ch.zeros(3), rt=ch.zeros(3)) f_tgt = ColoredRenderer(vc=A_tgt, camera=U_tgt, f=target.f, bgcolor=[1.0, 1.0, 1.0], frustum={ 'width': w, 'height': h, 'near': 1, 'far': 20 }) # offset = ch.zeros(V_tgt.shape) translation, rotation = ch.array([0, 0, 6]), ch.zeros(3) f_tgt.v = translation + V_tgt.dot(Rodrigues(rotation)) f_ref.v = translation + V_ref.dot(Rodrigues(rotation)) op_mesh_target = om.read_trimesh( "example_data/pointclouds/sphere_normal_2K_mesh.obj") n_rotations = 144 # camera positions for index in range(n_rotations): rotation[:] = np.random.rand(3) * np.pi * 2 np.save(os.path.join(save_dir, "rot_v{:03d}".format(index)), rotation) img_ref = f_ref.r Image.fromarray((img_ref * 255).astype(np.uint8)).save( os.path.join(save_dir, "reference_v{:03d}.png".format(index))) img_tgt = f_tgt.r Image.fromarray((img_tgt * 255).astype(np.uint8)).save( os.path.join(save_dir, "target_v{:03d}.png".format(index))) E_raw = f_tgt - img_ref # E_pyr = gaussian_pyramid(E_raw, n_levels=6, normalization='size') free_variables = [V_tgt] # dogleg # Newton-CG # SLSQP # BFGS # trust-ncg method = "trust-ncg" maxiter = 30 ch.minimize({'pyr': E_raw}, x0=free_variables, method=method, options=dict(maxiter=30), callback=create_callback(f_tgt, step=index * maxiter)) ch.minimize({'pyr': E_raw}, x0=free_variables, method=method, options=dict(maxiter=30), callback=create_callback(f_tgt, step=index * maxiter)) # is not the same? target.v = f_tgt.v.r.copy() # save mesh # mesh = pymesh.form_mesh(f_tgt.v.r, f_tgt.f) # pymesh.save_mesh(os.path.join( # save_dir, "target_v{:03d}.obj".format(index)), mesh) point_array = op_mesh_target.points() point_array[:] = target.v np.copyto(op_mesh_target.points(), f_tgt.v.r) om.write_mesh( os.path.join(save_dir, "target_v{:03d}.obj".format(index)), op_mesh_target)
mesh = read_and_process_mesh("example_data/pointclouds/teapot_mesh.obj", trans=ch.array([0, 0, 0]), rotation=ch.array([np.pi, 0, 0])) V_ref = ch.array(mesh.v) # reference A_ref = LambertianPointLight(v=V_ref, f=mesh.f, num_verts=len(V_ref), light_pos=ch.array([-1000, -1000, -1000]), vc=mesh.vc, light_color=ch.array([0.9, 0, 0])) +\ LambertianPointLight(v=V_ref, f=mesh.f, num_verts=len(V_ref), light_pos=ch.array([1000, -1000, -1000]), vc=mesh.vc, light_color=ch.array([0.0, 0.9, 0])) +\ LambertianPointLight(v=V_ref, f=mesh.f, num_verts=len(V_ref), light_pos=ch.array( [-1000, 1000, -1000]), vc=mesh.vc, light_color=ch.array([0.0, 0.0, 0.9])) U_ref = ProjectPoints(v=V_ref, f=[w, w], c=[w / 2., h / 2.], k=ch.zeros(5), t=ch.zeros(3), rt=ch.zeros(3)) f_ref = ColoredRenderer(vc=A_ref, camera=U_ref, f=mesh.f, bgcolor=[1.0, 1.0, 1.0], frustum={ 'width': w, 'height': h, 'near': 1, 'far': 20 }) f_ref.v = translation + V_ref.dot(Rodrigues(rot)) Image.fromarray((f_ref.r * 255).astype(np.uint8)).save( os.path.join(save_dir, "opendr_ref.png"))