def run_fuse(self): """ Run fusion. """ assert os.path.exists(self.options.depth_dir) common.makedir(self.options.out_dir) files = self.read_directory(self.options.depth_dir) timer = common.Timer() Rs = self.get_views() for filepath in files: # As rendering might be slower, we wait for rendering to finish. # This allows to run rendering and fusing in parallel (more or less). depths = common.read_hdf5(filepath) timer.reset() tsdf = self.fusion(depths, Rs) tsdf = tsdf[0] vertices, triangles = libmcubes.marching_cubes(-tsdf, 0) vertices /= self.options.resolution vertices -= 0.5 tsdf_file = os.path.join(self.options.tsdf_dir, '0.hf5') off_file = os.path.join(self.options.out_dir, ntpath.basename(filepath)[:-3]) common.write_hdf5(tsdf_file, tsdf) libmcubes.export_off(vertices, triangles, off_file) print('[Data] wrote %s (%f seconds)' % (off_file, timer.elapsed()))
def run_render(self, filepath): """ Run rendering. """ timer = common.Timer() Rs = self.get_views() timer.reset() mesh = common.Mesh.from_off(filepath) depths = self.render(mesh, Rs) depth_file = self.get_outpath(filepath) common.write_hdf5(depth_file, np.array(depths)) print('[Data] wrote %s (%f seconds)' % (depth_file, timer.elapsed()))
def run_render(self): """ Run rendering. """ assert os.path.exists(self.options.in_dir) common.makedir(self.options.depth_dir) files = self.read_directory(self.options.in_dir) timer = common.WallTimer() Rs = self.get_views() for filepath in files: timer.reset() mesh = common.Mesh.from_off(filepath) depths = self.render(mesh, Rs) depth_file = os.path.join(self.options.depth_dir, os.path.basename(filepath) + '.h5') common.write_hdf5(depth_file, np.array(depths)) print('[Data] wrote %s (%f seconds)' % (depth_file, timer.elapsed()))
def create_dataset(self, training=True): # Root of the data data_dir = self.training if training else self.testing # Subdirectories for images, ground truth and masks imgs_dir = join(data_dir, 'images') ground_truth_dir = join(data_dir, '1st_manual') mask_dir = join(data_dir, 'mask') # Initialise empty data arrays imgs_arr = np.empty((self.n_imgs, self.height, self.width, self.channels)) ground_truth_arr = np.empty((self.n_imgs, self.height, self.width)) masks_arr = np.empty((self.n_imgs, self.height, self.width)) # Loop through all files in images directory for i, file_ in enumerate(sorted(listdir(imgs_dir))): print "Loading '{}'".format(file_) # Add the image to an ndarray img = Image.open(join(imgs_dir, file_)) imgs_arr[i] = np.asarray(img) # Find the ground truth gt_file = file_[0:6] + "_manual1.gif" g_truth = Image.open(join(ground_truth_dir, gt_file)) ground_truth_arr[i] = np.asarray(g_truth).astype(np.uint8) * 255 # Find the mask ext = "_test_mask.gif" mask_file = file_[0:6] + ext b_mask = Image.open(join(mask_dir, mask_file)) masks_arr[i] = np.asarray(b_mask).astype(np.uint8) * 255 # Value assertions print "imgs max: {}".format(np.max(imgs_arr)) print "imgs min: {}".format(np.min(imgs_arr)) assert(np.max(ground_truth_arr) == 255 and np.max(masks_arr) == 255) assert(np.min(ground_truth_arr) == 0 and np.min(masks_arr) == 0) print "Ground truth and border masks are correctly within pixel value range 0 - 255 (black - white)" # Reshaping imgs_arr = np.transpose(imgs_arr,(0,3,1,2)) assert(imgs_arr.shape == (self.n_imgs, self.channels, self.height, self.width)) ground_truth_arr = np.reshape(ground_truth_arr,(self.n_imgs, 1, self.height, self.width)) assert(ground_truth_arr.shape == (self.n_imgs, 1, self.height, self.width)) masks_arr = np.reshape(masks_arr, (self.n_imgs, 1, self.height, self.width)) assert(masks_arr.shape == (self.n_imgs, 1, self.height, self.width)) # Write the data to disk type_ = 'train' if training else 'test' print "Saving dataset to '{}'".format(self.out_dir) write_hdf5(imgs_arr, join(self.out_dir, "200image_dataset_imgs_{}.hdf5".format(type_))) write_hdf5(ground_truth_arr, join(self.out_dir, "200image_dataset_groundTruth_{}.hdf5".format(type_))) write_hdf5(masks_arr, join(self.out_dir, "200image_dataset_borderMasks_{}.hdf5".format(type_))) return imgs_arr, ground_truth_arr, masks_arr
# May also switch axes if necessary. mesh.switch_axes(0, 2) scaled_off_path = os.path.join(options.scale_dir, os.path.basename(filepath)) mesh.to_off(scaled_off_path) """ Run rendering. """ timer.reset() mesh = common.Mesh.from_off(scaled_off_path) depths = fusion_tools.render(mesh, Rs) depth_file_path = os.path.join(options.depth_dir, os.path.basename(filepath) + '.h5') common.write_hdf5(depth_file_path, np.array(depths)) print('----- [Depth] wrote %s (%f seconds)' % (depth_file_path, timer.elapsed())) """ Performs TSDF fusion. As rendering might be slower, we wait for rendering to finish. This allows to run rendering and fusing in parallel (more or less). """ depths = common.read_hdf5(depth_file_path) timer.reset() tsdf = fusion_tools.fusion(depths, Rs) tsdf = tsdf[0] vertices, triangles = libmcubes.marching_cubes(-tsdf, 0) vertices /= options.resolution