def _visual_image_ref_warp_lnds_move_warp(record, path_dataset=None, path_experiment=None): """ visualise the case with warped reference landmarks to the move frame :param {} record: row with the experiment :param str|None path_dataset: path to the dataset folder :param str|None path_experiment: path to the experiment folder :return obj|None: """ assert COL_POINTS_REF_WARP in record and isinstance(record[COL_POINTS_REF_WARP], str), \ 'Missing registered image "%s"' % COL_POINTS_REF_WARP path_points_warp = update_path_(record[COL_POINTS_REF_WARP], path_experiment) if not os.path.isfile(path_points_warp): logging.warning('missing warped landmarks for: %r', dict(record)) return points_ref, points_move, path_img_ref = _load_landmarks(record, path_dataset) points_warp = load_landmarks(path_points_warp) if not list(points_warp): return # draw image with landmarks image_move = load_image(update_path_(record[COL_IMAGE_MOVE], path_dataset)) # image_warp = tl_io.load_image(row['Moving image, Transf.']) image = draw_image_points(image_move, points_warp) save_image(os.path.join(update_path_(record[COL_REG_DIR], path_experiment), NAME_IMAGE_REF_POINTS_WARP), image) del image # visualise the landmarks move during registration image_ref = load_image(path_img_ref) fig = draw_images_warped_landmarks(image_ref, image_move, points_ref, points_move, points_warp) del image_ref, image_move return fig
def __images_preprocessing(self, item): """ create some pre-process images, convert to gray scale and histogram matching :param dict item: the input record :return dict: updated item with optionally added pre-process images """ path_dir = self._get_path_reg_dir(item) def __path_img(path_img, pproc): img_name, img_ext = os.path.splitext(os.path.basename(path_img)) return os.path.join(path_dir, img_name + '_' + pproc + img_ext) def __save_img(col, path_img_new, img): col_temp = col + self.COL_IMAGE_EXT_TEMP if isinstance(item.get(col_temp), str): path_img = self._absolute_path(item[col_temp], destination='expt') os.remove(path_img) save_image(path_img_new, img) return self._relativize_path(path_img_new, destination='path_exp'), col def __convert_gray(path_img_col): path_img, col = path_img_col path_img_new = __path_img(path_img, 'gray') __save_img(col, path_img_new, rgb2gray(load_image(path_img))) return self._relativize_path(path_img_new, destination='path_exp'), col for pproc in self.params.get('preprocessing', []): path_img_ref, path_img_move, _, _ = self._get_paths( item, prefer_pproc=True) if pproc.startswith('match'): color_space = pproc.split('-')[-1] path_img_new = __path_img(path_img_move, pproc) img = image_histogram_matching( load_image(path_img_move), load_image(path_img_ref), use_color=color_space, ) path_img_new, col = __save_img(self.COL_IMAGE_MOVE, path_img_new, img) item[col + self.COL_IMAGE_EXT_TEMP] = path_img_new elif pproc in ('gray', 'grey'): argv_params = [(path_img_ref, self.COL_IMAGE_REF), (path_img_move, self.COL_IMAGE_MOVE)] # IDEA: find a way how to convert images in parallel inside mproc pool # problem is in calling class method inside the pool which is ot static for path_img, col in iterate_mproc_map(__convert_gray, argv_params, nb_workers=1, desc=None): item[col + self.COL_IMAGE_EXT_TEMP] = path_img else: logging.warning('unrecognized pre-processing: %s', pproc) return item
def _visual_image_move_warp_lnds_ref_warp(cls, item, path_dataset=None, path_experiment=None): """ visualise the case with warped reference landmarks to the move frame :param dict item: row with the experiment :param str|None path_dataset: path to the dataset folder :param str|None path_experiment: path to the experiment folder :return obj|None: """ assert isinstance(item.get(cls.COL_POINTS_REF_WARP, None), str), \ 'Missing registered points in "%s"' % cls.COL_POINTS_REF_WARP path_points_warp = update_path(item[cls.COL_POINTS_REF_WARP], pre_path=path_experiment) if not os.path.isfile(path_points_warp): logging.warning('missing warped landmarks for: %r', dict(item)) return points_ref, points_move, path_img_ref = cls._load_landmarks( item, path_dataset) points_warp = load_landmarks(path_points_warp) if not list(points_warp): return # draw image with landmarks image_move = load_image( update_path(item[cls.COL_IMAGE_MOVE], pre_path=path_dataset)) image = draw_image_points(image_move, points_warp) save_image( os.path.join( update_path(item[cls.COL_REG_DIR], pre_path=path_experiment), cls.NAME_IMAGE_REF_POINTS_WARP), image) del image image_ref = load_image(path_img_ref) image_warp = cls._load_warped_image(item, path_experiment) image = overlap_two_images(image_ref, image_warp) save_image( os.path.join( update_path(item[cls.COL_REG_DIR], pre_path=path_experiment), cls.NAME_IMAGE_REF_WARP), image) del image, image_warp # visualise the landmarks move during registration fig = draw_images_warped_landmarks(image_ref, image_move, points_ref, points_move, points_warp) del image_ref, image_move return fig
def _load_warped_image(cls, item, path_experiment=None): """load the wapted image if it exists :param dict item: row with the experiment :param str|None path_experiment: path to the experiment folder :return ndarray: """ name_img = item.get(cls.COL_IMAGE_MOVE_WARP, None) if not isinstance(name_img, str): logging.warning('Missing registered image in "%s"', cls.COL_IMAGE_MOVE_WARP) image_warp = None else: path_img_warp = update_path(name_img, pre_path=path_experiment) if os.path.isfile(path_img_warp): image_warp = load_image(path_img_warp) else: logging.warning('Define image is missing: %s', path_img_warp) image_warp = None return image_warp
def _visual_image_move_warp_lnds_move_warp(cls, item, path_dataset=None, path_experiment=None): """ visualise the case with warped moving image and landmarks to the reference frame so they are simple to overlap :param dict item: row with the experiment :param str|None path_dataset: path to the dataset folder :param str|None path_experiment: path to the experiment folder :return obj|None: """ if not isinstance(item.get(cls.COL_POINTS_MOVE_WARP), str): raise ValueError('Missing registered points in "%s"' % cls.COL_POINTS_MOVE_WARP) path_points_warp = update_path(item[cls.COL_POINTS_MOVE_WARP], pre_path=path_experiment) if not os.path.isfile(path_points_warp): logging.warning('missing warped landmarks for: %r', dict(item)) return points_ref, points_move, path_img_ref = cls._load_landmarks( item, path_dataset) image_warp = cls._load_warped_image(item, path_experiment) points_warp = load_landmarks(path_points_warp) if not list(points_warp): return # draw image with landmarks image = draw_image_points(image_warp, points_warp) _path = update_path(item[cls.COL_REG_DIR], pre_path=path_experiment) save_image(os.path.join(_path, cls.NAME_IMAGE_MOVE_WARP_POINTS), image) del image # visualise the landmarks move during registration image_ref = load_image(path_img_ref) fig = draw_images_warped_landmarks(image_ref, image_warp, points_move, points_ref, points_warp) return fig
def __convert_gray(path_img_col): path_img, col = path_img_col path_img_new = __path_img(path_img, 'gray') __save_img(col, path_img_new, rgb2gray(load_image(path_img))) return self._relativize_path(path_img_new, destination='path_exp'), col
def setUpClass(cls): cls.img_ref = load_image(PATH_IMAGE_REF) cls.img_src = load_image(PATH_IMAGE_SRC)