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 test_hist_matching(self, clr_space): """ test run in parallel with failing experiment """ img = image_histogram_matching(self.img_src, self.img_ref, use_color=clr_space) self.assertAlmostEqual(self.img_src.shape, img.shape)