def __compute_landmarks_statistic(self, df_row): """ after successful registration load initial nad estimated landmarks afterwords compute various statistic for init, and final alignment :param (int, dict) df_row: tow from iterated table """ idx, dict_row = df_row # load initial landmarks points_ref = tl_io.load_landmarks(dict_row[COL_POINTS_REF]) points_move = tl_io.load_landmarks(dict_row[COL_POINTS_MOVE]) # compute statistic self.__compute_landmarks_inaccuracy(idx, points_ref, points_move, 'init') # load transformed landmarks if COL_POINTS_REF_WARP in dict_row: points_target = points_ref if isinstance(dict_row[COL_POINTS_REF_WARP], str): points_warped = tl_io.load_landmarks(dict_row[COL_POINTS_REF_WARP]) else: points_warped = [] elif COL_POINTS_MOVE_WARP in dict_row: points_target = points_move if isinstance(dict_row[COL_POINTS_MOVE_WARP], str): points_warped = tl_io.load_landmarks(dict_row[COL_POINTS_MOVE_WARP]) else: points_warped = [] else: logging.error('not allowed scenario: no output landmarks') points_target, points_warped = [], [] # compute statistic if len(points_warped) > 0: self.__compute_landmarks_inaccuracy(idx, points_target, points_warped, 'final')
def _visualise_registration(self, df_row): """ visualise the registration results according what landmarks were estimated - in registration or moving frame :param (int, dict) df_row: tow from iterated table """ _, row = df_row path_img_ref, _, path_lnds_ref, path_lnds_move = self._get_paths(row) image_ref = tl_io.load_image(path_img_ref) points_ref = tl_io.load_landmarks(path_lnds_ref) points_move = tl_io.load_landmarks(path_lnds_move) # visualise particular experiment by idx if COL_POINTS_MOVE_WARP in row: fig = self.__visual_image_move_warp_lnds_move_warp( row, image_ref, points_ref, points_move) if fig is None: return None elif COL_POINTS_REF_WARP in row: fig = self.__visual_image_ref_warp_lnds_move_warp( row, image_ref, points_ref, points_move) else: logging.error('Visualisation: no output image or landmarks') fig, _ = tl_visu.create_figure((1, 1)) path_fig = os.path.join(self._get_path_reg_dir(row), NAME_IMAGE_WARPED_VISUAL) tl_visu.export_figure(path_fig, fig) return path_fig
def _visualise_registration(self, df_row): """ visualise the registration results according what landmarks were estimated - in registration or moving frame :param (int, dict) df_row: tow from iterated table """ _, dict_row = df_row image_ref = tl_io.load_image(dict_row[COL_IMAGE_REF]) points_ref = tl_io.load_landmarks(dict_row[COL_POINTS_REF]) points_move = tl_io.load_landmarks(dict_row[COL_POINTS_MOVE]) # visualise particular experiment by idx if COL_POINTS_REF_WARP in dict_row: if COL_IMAGE_REF_WARP not in dict_row \ or not isinstance(dict_row[COL_IMAGE_REF_WARP], str): logging.warning('missing registered image "%s"', COL_IMAGE_REF_WARP) return None if COL_POINTS_REF_WARP not in dict_row \ or not isinstance(dict_row[COL_POINTS_REF_WARP], str): logging.warning('missing registered landmarks "%s"', COL_POINTS_REF_WARP) return None image_warp = tl_io.load_image(dict_row[COL_IMAGE_REF_WARP]) points_warp = tl_io.load_landmarks(dict_row[COL_POINTS_REF_WARP]) # draw image with landmarks image = tl_visu.draw_image_points(image_warp, points_warp) tl_io.save_image(os.path.join(dict_row[COL_REG_DIR], NAME_IMAGE_MOVE_WARP_POINTS), image) # visualise the landmarks move during registration fig = tl_visu.draw_images_warped_landmarks(image_ref, image_warp, points_move, points_ref, points_warp) elif COL_POINTS_MOVE_WARP in dict_row: image_move = tl_io.load_image(dict_row[COL_IMAGE_MOVE]) # image_warp = tl_io.load_image(row['Moving image, Transf.']) points_warp = tl_io.load_landmarks(dict_row[COL_POINTS_MOVE_WARP]) # draw image with landmarks image = tl_visu.draw_image_points(image_move, points_warp) tl_io.save_image(os.path.join(dict_row[COL_REG_DIR], NAME_IMAGE_REF_POINTS_WARP), image) # visualise the landmarks move during registration fig = tl_visu.draw_images_warped_landmarks(image_ref, image_move, points_ref, points_move, points_warp) else: logging.error('not allowed scenario: no output image or landmarks') fig, _ = tl_visu.create_figure((1, 1)) path_fig = os.path.join(dict_row[COL_REG_DIR], NAME_IMAGE_REGIST_VISUAL) tl_visu.export_figure(path_fig, fig) return path_fig
def __visual_image_ref_warp_lnds_move_warp(self, record, image_ref, points_ref, points_move): """ visualise the case with warped reference landmarks to the move frame :param {} record: row with the experiment :param ndarray image_ref: reference image :param ndarray points_ref: landmarks in reference frame :param ndarray points_move: landmarks in moving frame :return obj: """ image_move = tl_io.load_image(self._update_path( record[COL_IMAGE_MOVE])) # image_warp = tl_io.load_image(row['Moving image, Transf.']) path_points_warp = self._update_path(record[COL_POINTS_REF_WARP], 'expt') points_warp = tl_io.load_landmarks(path_points_warp) # draw image with landmarks image = tl_visu.draw_image_points(image_move, points_warp) tl_io.save_image( os.path.join(self._get_path_reg_dir(record), NAME_IMAGE_REF_POINTS_WARP), image) # visualise the landmarks move during registration fig = tl_visu.draw_images_warped_landmarks(image_ref, image_move, points_ref, points_move, points_warp) return fig
def __compute_landmarks_statistic(self, df_row): """ after successful registration load initial nad estimated landmarks afterwords compute various statistic for init, and final alignment :param (int, dict) df_row: tow from iterated table """ idx, row = df_row path_img_ref, _, path_lnds_ref, path_lnds_move = self._get_paths(row) # load initial landmarks points_ref = tl_io.load_landmarks(path_lnds_ref) points_move = tl_io.load_landmarks(path_lnds_move) points_warped = [] img_size = tl_io.load_image(path_img_ref).shape[:2] # compute statistic self.__compute_landmarks_inaccuracy(idx, points_ref, points_move, 'init', img_size) # load transformed landmarks if COL_POINTS_REF_WARP in row: points_target = points_move path_landmarks = self._update_path(row[COL_POINTS_REF_WARP], 'expt') elif COL_POINTS_MOVE_WARP in row: points_target = points_ref path_landmarks = self._update_path(row[COL_POINTS_MOVE_WARP], 'expt') else: logging.error('Statistic: no output landmarks') points_target, path_landmarks = [], None # load landmarks if isinstance(path_landmarks, str): points_warped = tl_io.load_landmarks(path_landmarks) # compute statistic img_size = tl_io.load_image(path_img_ref).shape[:2] if list(points_target) and list(points_warped): self.__compute_landmarks_inaccuracy(idx, points_target, points_warped, 'final', img_size)
def _evaluate_registration(self, dict_row): """ evaluate rests of the experiment and identity the registered image and landmarks when the process finished :param dict_row: {str: value}, dictionary with regist. params :return: {str: value} """ logging.debug('.. warp the registered image and get landmarks') path_dir = dict_row[bm.COL_REG_DIR] path_log = os.path.join(path_dir, bm.NAME_LOG_REGIST) # warp the image path_macro = os.path.join(path_dir, NAME_MACRO_WARP_IMAGE) cmd = '%s -batch %s' % (self.params['path_fiji'], path_macro) tl_expt.run_command_line(cmd, path_logger=path_log) path_img = os.path.join(path_dir, os.path.basename(dict_row[bm.COL_IMAGE_MOVE])) # detect image if os.path.isfile(path_img): dict_row[bm.COL_IMAGE_REF_WARP] = path_img # convert the transform do obtain displacement field path_macro = os.path.join(path_dir, NAME_MACRO_CONVERT_TRANS) cmd = '%s -batch %s' % (self.params['path_fiji'], path_macro) tl_expt.run_command_line(cmd, path_logger=path_log) # load and parse raw transform to detect landmarks path_raw = os.path.join(path_dir, NAME_TXT_TRANSFORM_RAW) # points_ref = tl_io.load_landmarks(dict_row[bm.COL_POINTS_REF]) points_move = tl_io.load_landmarks(dict_row[bm.COL_POINTS_MOVE]) points_warp = load_parse_bunwarpj_displacements_warp_points( path_raw, points_move) if points_warp is None: return dict_row path_lnd = os.path.join(path_dir, os.path.basename(dict_row[bm.COL_POINTS_MOVE])) tl_io.save_landmarks_csv(path_lnd, points_warp) dict_row[bm.COL_POINTS_REF_WARP] = path_lnd return dict_row
def __visual_image_move_warp_lnds_move_warp(self, record, image_ref, points_ref, points_move): """ visualise the case with warped moving image and landmarks to the reference frame so they are simple to overlap :param {} record: row with the experiment :param ndarray image_ref: :param ndarray points_move: :param ndarray points_ref: :return obj | None: """ if COL_IMAGE_MOVE_WARP not in record \ or not isinstance(record[COL_IMAGE_MOVE_WARP], str): logging.warning('Missing registered image "%s"', COL_IMAGE_MOVE_WARP) return None if COL_POINTS_MOVE_WARP not in record \ or not isinstance(record[COL_POINTS_MOVE_WARP], str): logging.warning('Missing registered landmarks "%s"', COL_POINTS_MOVE_WARP) return None path_image_warp = self._update_path(record[COL_IMAGE_MOVE_WARP], 'expt') image_warp = tl_io.load_image(path_image_warp) path_points_warp = self._update_path(record[COL_POINTS_MOVE_WARP], 'expt') points_warp = tl_io.load_landmarks(path_points_warp) # draw image with landmarks image = tl_visu.draw_image_points(image_warp, points_warp) tl_io.save_image( os.path.join(self._get_path_reg_dir(record), NAME_IMAGE_MOVE_WARP_POINTS), image) # visualise the landmarks move during registration fig = tl_visu.draw_images_warped_landmarks(image_ref, image_warp, points_move, points_ref, points_warp) return fig