] print("Running foreground estimation") for index in range(1, 28): name = "GT%02d" % index path = f"{directory}/converted/image/{name}.bmp" image = util.load_image(path) for alpha_method in alpha_methods: path = f"{directory}/alpha/{alpha_method}/{name}.png" alpha = util.load_image(path, "gray") for fg_method, estimate_foreground in fg_methods: foreground = estimate_foreground(image, alpha) print( f'Processing image {name} with foreground estimation ' f'method {fg_method:10} and alpha matte {alpha_method:3}') path = f"{directory}/fg_methods/{fg_method}/{alpha_method}/{name}.bmp" util.save_image(path, foreground) if __name__ == "__main__": estimate_foregrounds(util.find_data_directory())
path = os.path.join(directory, "input_with_gt_fgd/input/GT%02d.tif" % index) lrgb = util.load_image(path) lrgb = transform(lrgb, np.float64(matrices[str(index)]).reshape(3, 3)) lrgb = np.maximum(0, lrgb) srgb = util.lrgb_to_srgb(lrgb, gamma) srgb = np.clip(srgb, 0, 1) image = srgb path = os.path.join(directory, "input_with_gt_fgd/fgd/GT%02d.tif" % index) lrgb = util.load_image(path) lrgb = transform(lrgb, np.float64(matrices[str(index)]).reshape(3, 3)) lrgb = np.maximum(0, lrgb) srgb = util.lrgb_to_srgb(lrgb, gamma) srgb = np.clip(srgb, 0, 1) foreground = srgb path = os.path.join(directory, "converted/image/GT%02d.bmp" % index) util.save_image(path, image) path = os.path.join(directory, "converted/foreground/GT%02d.bmp" % index) util.save_image(path, foreground) if __name__ == "__main__": convert_lrgb_to_srgb(util.find_data_directory())
plt.axis("off") plt.subplot(ny, nx, 2) plt.title("want") plt.imshow(want_srgb, vmin=0, vmax=1) plt.axis("off") plt.subplot(ny, nx, 3) plt.title("clip(10*|difference|, 0, 1)") plt.imshow(np.clip(10 * difference, 0, 1), cmap="gray", vmin=0, vmax=1) plt.axis("off") for channel, name in enumerate(["red", "green", "blue"]): plt.subplot(ny, nx, 4 + channel) plt.title(name + " channel histogram") bins = np.linspace(0, 1, 256) values = want_srgb[:, :, channel].flatten() plt.hist(values, bins=bins, label="want", alpha=0.5) values = have_srgb[:, :, channel].flatten() plt.hist(values, bins=bins, label="have", alpha=0.5) plt.legend() plt.show() with open(output_path, "w") as f: json.dump(matrices, f, indent=4) if __name__ == "__main__": fit_whitepoint_matrices(util.find_data_directory())
f.write(chunk) n_bytes += len(chunk) print(n_bytes * 1e-6, "MB") target_dir = os.path.join(download_directory, os.path.splitext(filename)[0]) if os.path.isdir(target_dir): print("Already unzipped:", target_dir) else: with zipfile.ZipFile(path, "r") as zf: zf.extractall(target_dir) dst_directory = os.path.join(download_directory, "alpha", "gt") src_directory = os.path.join(download_directory, "gt_training_highres") os.makedirs(dst_directory, exist_ok=True) # Copy alpha mattes for filename in os.listdir(src_directory): src = os.path.join(src_directory, filename) dst = os.path.join(dst_directory, filename) if not os.path.isfile(dst): shutil.copyfile(src, dst) if __name__ == "__main__": download(util.find_data_directory())
def print_results(directory): with open(f"{directory}/errors.json") as f: errors = json.load(f) groups = defaultdict(list) exponents = { "SAD": -3, "MSE": 3, "GRAD": -3, } print("") print("| Foreground | Alpha method | Metric | Error |") print("| ---------- | -------------------- | ------ | ------------- |") for fg_method, d0 in errors.items(): for alpha_method, d1 in d0.items(): for error_name, d2 in d1.items(): scale = 10**exponents[error_name] error = scale * np.mean(list(d2.values())) result = f"| {fg_method:10} | {alpha_method:20} | {error_name:6} | {error:5.2f} * 10^{exponents[error_name]:+d} |" print(result) if __name__ == "__main__": print_results(util.find_data_directory())
keys = [fg_method, alpha_method, error_name] d = errors for key in keys: d = d.setdefault(key, {}) d[name] = error # Remove "continue" statement to look at images if you want to continue import matplotlib.pyplot as plt for i, img in enumerate([ estimated_foreground, true_foreground, alpha, is_unknown, ]): plt.subplot(2, 2, 1 + i) plt.imshow(img, cmap="gray") plt.axis("off") plt.show() with open(errors_path, "w") as f: json.dump(errors, f, indent=4) if __name__ == "__main__": compute_errors(util.find_data_directory())