Examples — PyMatting 1.1.13 documentation (original) (raw)
We provide different examples at different levels of abstraction.
Simple Example
This simple example is intended for application-oriented users. All parameters were set beforehand and should work well on most images. The cutout() method employs closed-form alpha matting [LLW07] and multi-level foreground extraction [GUCH20].
from pymatting import cutout cutout( "../data/lemur/lemur.png", "../data/lemur/lemur_trimap.png", "lemur_cutout.png")
Advanced Example
The following example demonstrates the use of the estimate_alpha_cf() method as well as the estimate_foreground_ml() method. Both methods can be easily replaced by other methods from the pymatting.alpha module and the pymatting.foreground module, respectively. Parameters can be tweaked by passing them to the corresponding function calls.
from pymatting import * import numpy as np scale = 1.0 image = load_image("../data/lemur/lemur.png", "RGB", scale, "box") trimap = load_image("../data/lemur/lemur_trimap.png", "GRAY", scale, "nearest") alpha = estimate_alpha_cf(image, trimap) background = np.zeros(image.shape) background[:, :] = [0.5, 0.5, 0.5] foreground = estimate_foreground_ml(image, alpha) new_image = blend(foreground, background, alpha) images = [image, trimap, alpha, new_image] grid = make_grid(images) save_image("lemur_grid.png", grid) cutout = stack_images(foreground, alpha) save_image("lemur_cutout.png", cutout) color_bleeding = blend(image, background, alpha) grid = make_grid([color_bleeding, new_image]) save_image("lemur_color_bleeding.png", grid)
Expert Example
The third example provides an insight how PyMatting is working under-the-hood. The matting Laplacian matrix L and the system of linear equations A x = b are constructed manually. The solution vector x is the flattened alpha matte. The alpha matte alpha is then calculated by solving the linear system using the cg() method. The convergence of the cg() method is accelerated with a preconditioner using the ichol() method. This example is intended for developers and (future) contributors to demonstrate the implementation of the different alpha matting methods.
from pymatting import * import numpy as np import scipy.sparse scale = 1.0 image = load_image("../data/lemur/lemur.png", "RGB", scale, "box") trimap = load_image("../data/lemur/lemur_trimap.png", "GRAY", scale, "nearest") h, w = trimap.shape[:2] L = cf_laplacian(image) is_fg, is_bg, is_known, is_unknown = trimap_split(trimap) lambda_value = 100.0 c = lambda_value * is_known C = scipy.sparse.diags(c) b = lambda_value * is_fg A = L + C A = A.tocsr() A.sum_duplicates() M = ichol(A) x = cg(A, b, M=M) alpha = np.clip(x, 0.0, 1.0).reshape(h, w) save_image("lemur_alpha.png", alpha)