# Convert depth so it is in the same units as the real test images depth /= par['cam']['depth_scale'] # Render RGB image rgb = renderer.render(model, im_size_rgb, K_rgb, view['R'], view['t'], clip_near, clip_far, texture=model_texture, ambient_weight=ambient_weight, shading=shading, mode='rgb') # The OpenCV function was used for rendering of the training images # provided for the SIXD Challenge 2017. rgb = cv2.resize(rgb, par['cam']['im_size'], interpolation=cv2.INTER_AREA) #rgb = scipy.misc.imresize(rgb, par['cam']['im_size'][::-1], 'bicubic') # Save the rendered images inout.save_im(out_rgb_mpath.format(obj_id, im_id), rgb) inout.save_depth(out_depth_mpath.format(obj_id, im_id), depth) # Get 2D bounding box of the object model at the ground truth pose ys, xs = np.nonzero(depth > 0) obj_bb = misc.calc_2d_bbox(xs, ys, par['cam']['im_size']) obj_info[im_id] = { 'cam_K': par['cam']['K'].flatten().tolist(), 'view_level': int(views_level[view_id]), #'sphere_radius': float(radius) } obj_gt[im_id] = [{ 'cam_R_m2c': view['R'].flatten().tolist(), 'cam_t_m2c': view['t'].flatten().tolist(),
R_model = R_z.dot(R_model) R_model_inv = np.linalg.inv(R_model) for im_id in im_ids: if im_id % 10 == 0: print('scene,view: ' + str(scene_id) + ',' + str(im_id)) # Load the RGB and depth image rgb = inout.load_im(rgb_in_mpath.format(scene_id, im_id)) depth = load_hinter_depth(depth_in_mpath.format(scene_id, im_id)) depth *= 10.0 # Convert depth map to [100um] # Save the RGB and depth image inout.save_im(rgb_out_mpath.format(scene_id, im_id), rgb) inout.save_depth(depth_out_mpath.format(scene_id, im_id), depth) # Load the GT pose R_m2c = load_hinter_mat(rot_mpath.format(scene_id, im_id)) t_m2c = load_hinter_mat(tra_mpath.format(scene_id, im_id)) t_m2c *= 10 # Convert to [mm] # Transfom the GT pose (to compensate transformation of the models) R_m2c = R_m2c.dot(R_model_inv) t_m2c = t_m2c + R_m2c.dot(R_model.dot(t_model)) # Get 2D bounding box of the object model at the ground truth pose obj_bb = misc.calc_pose_2d_bbox(model, par['cam']['im_size'], par['cam']['K'], R_m2c, t_m2c)
# }) if do_vis: # Visibility mask depth_im_vis = misc.norm_depth(depth_im, 0.2, 1.0) depth_im_vis = np.dstack([depth_im_vis] * 3) visib_gt_vis = visib_gt.astype(np.float) zero_ch = np.zeros(visib_gt_vis.shape) visib_gt_vis = np.dstack([zero_ch, visib_gt_vis, zero_ch]) vis = 0.5 * depth_im_vis + 0.5 * visib_gt_vis vis[vis > 1] = 1 vis_path = vis_mpath.format(dataset, delta, data_id, im_id, gt_id) inout.save_im(vis_path, vis) # Mask of depth differences below delta # mask_below_delta_vis = np.dstack([mask_below_delta, # zero_ch, zero_ch]) # vis_delta = 0.5 * depth_im_vis + 0.5 * mask_below_delta_vis # vis_delta[vis_delta > 1] = 1 # vis_delta_path = vis_delta_mpath.format( # dataset, delta, data_id, im_id, gt_id, delta) # inout.save_im(vis_delta_path, vis_delta) res_path = dp[gt_stats_mpath_key].format(data_id, delta) misc.ensure_dir(os.path.dirname(res_path)) inout.save_yaml(res_path, gt_stats) # visib_to_below_delta_fracs = sorted(visib_to_below_delta_fracs,
txt_offset = 0 # txt_offset = 5 p_id = np.argmin(ys) tex_loc = (xs[p_id], ys[p_id] - 5) # tex_loc = (bbox[0], bbox[1]) cv2.putText(ren_rgb_info, txt, tex_loc, cv2.FONT_HERSHEY_PLAIN, 0.9, color_uint8, 1) # Save RGB visualization if vis_rgb: vis_im_rgb = 0.5 * rgb.astype(np.float32) +\ 0.5 * ren_rgb + \ 1.0 * ren_rgb_info vis_im_rgb[vis_im_rgb > 255] = 255 inout.save_im(vis_rgb_mpath.format(dataset, scene_id, im_id), vis_im_rgb.astype(np.uint8)) # Save image of depth differences if vis_depth: # Calculate the depth difference at pixels where both depth maps # are valid valid_mask = (depth > 0) * (ren_depth > 0) depth_diff = valid_mask * (depth - ren_depth.astype(np.float32)) f, ax = plt.subplots(1, 1) cax = ax.matshow(depth_diff) ax.axis('off') ax.set_title('measured - GT depth [mm]') f.colorbar(cax, fraction=0.03, pad=0.01) f.tight_layout(pad=0) plt.savefig(vis_depth_mpath.format(dataset, scene_id, im_id),
img_obj[y, x, 2] = pt3d[2] #################### 计算imgObj ################## black_out_mask[mask] = white_mask[mask].astype(black_out_mask.dtype) #################### save imgs################## if im_id > 300: now_test = True # print "out" if(not now_test): inout.save_im(out_rgb_mpath.format(dataset, scene_id, im_id), rgb.astype(np.uint8)) inout.save_depth(out_depth_mpath.format(dataset,obj_id, im_id), depth) from numpngw import write_png write_png(out_obj_mpath.format(dataset, scene_id, im_id), img_obj.astype(np.uint16)) inout.save_im(out_seg_mpath.format(dataset, scene_id, im_id), black_out_mask.astype(np.uint8)) R_str = [[str(num) for num in item] for item in R.tolist() ] R_str = [" ".join(item) for item in R_str] R_str = [item+'\n' for item in R_str] t_str = [str(item/1000) for item in t.squeeze().tolist()] t_str = " ".join(t_str)
# }) if do_vis: # Visibility mask depth_im_vis = misc.norm_depth(depth_im, 0.2, 1.0) depth_im_vis = np.dstack([depth_im_vis] * 3) visib_gt_vis = visib_gt.astype(np.float) zero_ch = np.zeros(visib_gt_vis.shape) visib_gt_vis = np.dstack([zero_ch, visib_gt_vis, zero_ch]) vis = 0.5 * depth_im_vis + 0.5 * visib_gt_vis vis[vis > 1] = 1 vis_path = vis_mpath.format( dataset, delta, data_id, im_id, gt_id) inout.save_im(vis_path, vis) # Mask of depth differences below delta # mask_below_delta_vis = np.dstack([mask_below_delta, # zero_ch, zero_ch]) # vis_delta = 0.5 * depth_im_vis + 0.5 * mask_below_delta_vis # vis_delta[vis_delta > 1] = 1 # vis_delta_path = vis_delta_mpath.format( # dataset, delta, data_id, im_id, gt_id, delta) # inout.save_im(vis_delta_path, vis_delta) res_path = dp[gt_stats_mpath_key].format(data_id, delta) misc.ensure_dir(os.path.dirname(res_path)) inout.save_yaml(res_path, gt_stats) # visib_to_below_delta_fracs = sorted(visib_to_below_delta_fracs,
# tex_loc = (bbox[0], bbox[1]) cv2.putText(ren_rgb_info, txt, tex_loc, cv2.FONT_HERSHEY_PLAIN, 0.9, color_uint8, 1) # Save RGB visualization if vis_rgb: vis_im_rgb = 0.5 * rgb.astype(np.float32) + \ 0.5 * ren_rgb + \ 1.0 * ren_rgb_info vis_im_rgb[vis_im_rgb > 255] = 255 vis_rgb_path = vis_rgb_mpath.format( result_path=result_path, result_name=result_name, scene_id=scene_id, im_id=im_id, obj_id=obj_id) misc.ensure_dir(os.path.dirname(vis_rgb_path)) inout.save_im(vis_rgb_path, vis_im_rgb.astype(np.uint8)) # Save image of depth differences if vis_depth: # Calculate the depth difference at pixels where both depth maps # are valid valid_mask = (depth > 0) * (ren_depth > 0) depth_diff = valid_mask * (depth - ren_depth.astype(np.float32)) f, ax = plt.subplots(1, 1) cax = ax.matshow(depth_diff) ax.axis('off') ax.set_title('measured - GT depth [mm]') f.colorbar(cax, fraction=0.03, pad=0.01) f.tight_layout(pad=0) vis_depth_path = vis_depth_mpath.format(
# tex_loc = (bbox[0], bbox[1]) cv2.putText(ren_rgb_info, txt, tex_loc, cv2.FONT_HERSHEY_PLAIN, 0.9, color_uint8, 1) # Save RGB visualization if vis_rgb: vis_im_rgb = 0.5 * rgb.astype(np.float32) + \ 0.5 * ren_rgb + \ 1.0 * ren_rgb_info vis_im_rgb[vis_im_rgb > 255] = 255 vis_rgb_path = vis_rgb_mpath.format( result_path=result_path, result_name=result_name, scene_id=scene_id, im_id=im_id, obj_id=obj_id) misc.ensure_dir(os.path.dirname(vis_rgb_path)) inout.save_im(vis_rgb_path, vis_im_rgb.astype(np.uint8)) # Save image of depth differences if vis_depth: # Calculate the depth difference at pixels where both depth maps # are valid valid_mask = (depth > 0) * (ren_depth > 0) depth_diff = valid_mask * (depth - ren_depth.astype(np.float32)) f, ax = plt.subplots(1, 1) cax = ax.matshow(depth_diff) ax.axis('off') ax.set_title('measured - GT depth [mm]') f.colorbar(cax, fraction=0.03, pad=0.01) f.tight_layout(pad=0) vis_depth_path = vis_depth_mpath.format(
# Convert depth so it is in the same units as the real test images depth /= p['cam']['depth_scale'] # Render RGB image rgb = renderer.render(model, im_size_rgb, K_rgb, view['R'], view['t'], clip_near, clip_far, texture=model_texture, ambient_weight=ambient_weight, shading=shading, mode='rgb') # The OpenCV function was used for rendering of the training images # provided for the SIXD Challenge 2017. rgb = cv2.resize(rgb, p['cam']['im_size'], interpolation=cv2.INTER_AREA) # rgb = scipy.misc.imresize(rgb, par['cam']['im_size'][::-1], 'bicubic') # Save the rendered images inout.save_im(out_rgb_mpath.format(obj_id, im_id), rgb) inout.save_depth(out_depth_mpath.format(obj_id, im_id), depth) # Get 2D bounding box of the object model at the ground truth pose ys, xs = np.nonzero(depth > 0) obj_bb = misc.calc_2d_bbox(xs, ys, p['cam']['im_size']) obj_info[im_id] = { 'cam_K': p['cam']['K'].flatten().tolist(), 'view_level': int(views_level[view_id]), # 'sphere_radius': float(radius) } obj_gt[im_id] = [{ 'cam_R_m2c': view['R'].flatten().tolist(), 'cam_t_m2c': view['t'].flatten().tolist(),