def test_match_method(self, method=None, save=False): # skip if no method present if method is None: self.skipTest('Type \'None\' was passed and skipped') # load images plain = load_img_file(os.path.join(self.dat_path, 'scotland_plain.png')) house = load_img_file(os.path.join(self.dat_path, 'scotland_house.png')) refer = load_img_file(os.path.join(self.dat_path, 'scotland_pitie.png')) # create color match object match = ColorMatcher(src=house, ref=plain, method=method).main() # assess quality refer_val = self.avg_hist_dist(plain, refer) match_val = self.avg_hist_dist(plain, match) print('\nAvg. histogram distance of original %s vs. %s %s' % (round(refer_val, 3), method, round(match_val, 3))) obj = ColorMatcher(src=house, ref=plain, method='mvgd') mu_house, mu_plain, cov_house, cov_plain = obj.mu_r, obj.mu_z, obj.cov_r, obj.cov_z obj = ColorMatcher(src=house, ref=match, method='mvgd') mu_match, cov_match = obj.mu_z, obj.cov_z refer_w2 = ColorMatcher.w2_dist(mu_a=mu_house, mu_b=mu_plain, cov_a=cov_house, cov_b=cov_plain) match_w2 = ColorMatcher.w2_dist(mu_a=mu_match, mu_b=mu_plain, cov_a=cov_match, cov_b=cov_plain) print('Wasserstein-2 distance of original %s vs. %s %s' % (round(refer_w2, 3), method, round(match_w2, 3))) # assertion self.assertEqual(True, refer_val > match_val) # write images to tests data directory (if option set) if save: save_img_file(match, file_path=os.path.join(self.dat_path, 'scotland_'+method), file_type='png')
def test_img_dims(self, src_img, img_ref, exp_val): try: obj = ColorMatcher(src=src_img, ref=img_ref) res = obj.main() ret = res.mean().astype('bool') msg = '' except BaseException as e: ret = False msg = e self.assertEqual(exp_val, ret, msg=msg)
def test_match_method_imageio(self): # choose method method = METHODS[0] # get tests data from imageio lib fn_img1 = 'chelsea' fn_img2 = 'astronaut' img1 = imageio.imread('imageio:' + fn_img1 + '.png') img2 = imageio.imread('imageio:' + fn_img2 + '.png') # create color match object (without using keyword arguments) match = ColorMatcher(img1, img2, method=method).main() # assess quality refer_val = self.avg_hist_dist(img1, img2) match_val = self.avg_hist_dist(img1, match) print('\nAvg. histogram distance of original %s vs. %s' % (round(refer_val, 3), round(match_val, 3))) # save result output_filename = os.path.join( self.dat_path, fn_img1.split('.')[0] + '_from_' + fn_img2 + '_' + method) save_img_file(img1, file_path=os.path.join(self.dat_path, fn_img1)) save_img_file(img2, file_path=os.path.join(self.dat_path, fn_img2)) save_img_file(match, file_path=output_filename) # assertion self.assertEqual(True, refer_val > match_val)
def test_hist_matcher(self): # get test data from imageio lib import imageio fn_img1 = 'chelsea' fn_img2 = 'coffee' img1 = imageio.imread('imageio:' + fn_img1 + '.png') img2 = imageio.imread('imageio:' + fn_img2 + '.png') # create color match object match = ColorMatcher(img1, img2, method='hm').main() # assess quality match_val = self.avg_hist_dist(match, img2) print('Avg. histogram distances %s vs %s' % (float('inf'), match_val)) # save result loc_path = './test/data' output_filename = os.path.join( loc_path, fn_img1.split('.')[0] + '_from_' + fn_img2) save_img_file(img1, file_path=os.path.join(loc_path, fn_img1)) save_img_file(img2, file_path=os.path.join(loc_path, fn_img2)) save_img_file(match, file_path=output_filename) # assertion self.assertEqual(True, float('inf') > match_val)
def test_kodak_images(self): # prepare data url = 'https://www.math.purdue.edu/~lucier/PHOTO_CD/BMP_IMAGES/' self.fnames = ['IMG' + str(i + 1).zfill(4) + '.bmp' for i in range(24)] loc_path = os.path.join(self.dat_path, 'kodak') try: os.makedirs(loc_path, 0o755) os.makedirs(os.path.join(loc_path, 'results'), 0o755) except OSError: pass if not os.path.exists(loc_path): download_stack(url, loc_path) for fn_img1 in self.fnames: for fn_img2 in self.fnames: # load images img1 = load_img_file(os.path.join(loc_path, fn_img1)) img2 = load_img_file(os.path.join(loc_path, fn_img2)) # create color match object res = ColorMatcher(img1, img2, method='hm-mkl-hm').main() # assess quality val = MatchMethodTester.avg_hist_dist(res, img2) print('Avg. histogram distance %s' % val) # save result output_filename = os.path.join(loc_path, 'results', fn_img1.split('.')[0] + '_from_' + fn_img2) save_img_file(res, file_path=output_filename) # assertion self.assertEqual(True, val != 0)
def main(): # program info print("\ncolor-matcher v%s \n" % __version__) # parse options cfg = parse_options(sys.argv[1:]) # select files from window (if option set) if cfg['win']: cfg['src_path'] = select_file('.', 'Select source image') cfg['ref_path'] = select_file(cfg['src_path'], 'Select reference image') # cancel if file paths not provided if not cfg['src_path'] or not cfg['ref_path']: usage() print('Canceled due to missing image file path\n') sys.exit() # select light field image(s) considering provided folder or file if os.path.isdir(cfg['src_path']): filenames = [ f for f in os.listdir(cfg['src_path']) if f.lower().endswith(FILE_EXTS) ] elif not os.path.isfile(cfg['src_path']) or not os.path.isfile( cfg['ref_path']): print('File(s) not found \n') sys.exit() else: filenames = [cfg['src_path']] # method handling cfg['method'] = cfg['method'] if cfg['method'] in METHODS else 'mvgd' # file handling ref = load_img_file(cfg['ref_path']) output_path = os.path.dirname(cfg['src_path']) # process the images for f in filenames: src = load_img_file(f) res = ColorMatcher(src=src, ref=ref, method=cfg['method']).main() filename = os.path.splitext(os.path.basename( cfg['src_path']))[0] + '_' + cfg['method'] file_ext = os.path.splitext(cfg['src_path'])[-1] save_img_file(res, file_path=os.path.join(output_path, filename), file_type=file_ext[1:]) return True
def test_mvgd_matcher(self): # load images plain = load_img_file(os.path.join(self.dat_path, 'scotland_house.png')) house = load_img_file(os.path.join(self.dat_path, 'scotland_plain.png')) refer = load_img_file(os.path.join(self.dat_path, 'scotland_pitie.png')) # create color match object match = ColorMatcher(plain, house, method='mvgd').main() # assess quality refer_val = self.avg_hist_dist(refer, house) match_val = self.avg_hist_dist(match, house) print('Avg. histogram distances %s vs %s' % (refer_val, match_val)) # assertion self.assertEqual(True, refer_val > match_val)