def estimate_alpha_lkm(image, trimap, laplacian_kwargs={}, cg_kwargs={}): """ Estimate alpha from an input image and an input trimap as described in Fast Matting Using Large Kernel Matting Laplacian Matrices by :cite:`he2010fast`. Parameters ---------- image: numpy.ndarray Image with shape :math:`h \\times w \\times d` for which the alpha matte should be estimated trimap: numpy.ndarray Trimap with shape :math:`h \\times w` of the image laplacian_kwargs: dictionary Arguments passed to the :code:`lkm_laplacian` function cg_kwargs: dictionary Arguments passed to the :code:`cg` solver Returns ------- alpha: numpy.ndarray Estimated alpha matte Example ------- >>> from pymatting import * >>> image = load_image("data/lemur/lemur.png", "RGB") >>> trimap = load_image("data/lemur/lemur_trimap.png", "GRAY") >>> alpha = estimate_alpha_lkm( ... image, ... trimap, ... laplacian_kwargs={"epsilon": 1e-6, "radius": 15}, ... cg_kwargs={"maxiter":2000}) """ sanity_check_image(image) L_matvec, diag_L = lkm_laplacian(image, **laplacian_kwargs) is_fg, is_bg, is_known, is_unknown = trimap_split(trimap) lambda_value = 100.0 c = lambda_value * is_known b = lambda_value * is_fg inv_diag_A = 1.0 / (diag_L + c) def A_matvec(x): return L_matvec(x) + c * x def jacobi(x): return inv_diag_A * x x = cg(A_matvec, b, M=jacobi, **cg_kwargs) alpha = np.clip(x, 0, 1).reshape(trimap.shape) return alpha
def estimate_alpha_lbdm(image, trimap, preconditioner=None, laplacian_kwargs={}, cg_kwargs={}): """ Estimate alpha from an input image and an input trimap using Learning Based Digital Matting as proposed by :cite:`zheng2009learning`. Parameters ---------- image: numpy.ndarray Image with shape :math:`h \\times w \\times d` for which the alpha matte should be estimated trimap: numpy.ndarray Trimap with shape :math:`h \\times w` of the image preconditioner: function or scipy.sparse.linalg.LinearOperator Function or sparse matrix that applies the preconditioner to a vector (default: ichol) laplacian_kwargs: dictionary Arguments passed to the :code:`lbdm_laplacian` function cg_kwargs: dictionary Arguments passed to the :code:`cg` solver Returns ------- alpha: numpy.ndarray Estimated alpha matte Example ------- >>> from pymatting import * >>> image = load_image("data/lemur/lemur.png", "RGB") >>> trimap = load_image("data/lemur/lemur_trimap.png", "GRAY") >>> alpha = estimate_alpha_lbdm( ... image, ... trimap, ... laplacian_kwargs={"epsilon": 1e-6}, ... cg_kwargs={"maxiter":2000}) """ if preconditioner is None: preconditioner = ichol sanity_check_image(image) A, b = make_linear_system(lbdm_laplacian(image, **laplacian_kwargs), trimap) x = cg(A, b, M=preconditioner(A), **cg_kwargs) alpha = np.clip(x, 0, 1).reshape(trimap.shape) return alpha
def estimate_alpha_knn(image, trimap, preconditioner=None, laplacian_kwargs={}, cg_kwargs={}): """ Estimate alpha from an input image and an input trimap using KNN Matting similar to :cite:`chen2013knn`. See `knn_laplacian` for more details. Parameters ---------- image: numpy.ndarray Image with shape :math:`h \\times w \\times d` for which the alpha matte should be estimated trimap: numpy.ndarray Trimap with shape :math:`h \\times w` of the image preconditioner: function or scipy.sparse.linalg.LinearOperator Function or sparse matrix that applies the preconditioner to a vector (default: jacobi) laplacian_kwargs: dictionary Arguments passed to the :code:`knn_laplacian` function cg_kwargs: dictionary Arguments passed to the :code:`cg` solver Returns ------- alpha: numpy.ndarray Estimated alpha matte Example ------- >>> from pymatting import * >>> image = load_image("data/lemur/lemur.png", "RGB") >>> trimap = load_image("data/lemur/lemur_trimap.png", "GRAY") >>> alpha = estimate_alpha_knn( ... image, ... trimap, ... laplacian_kwargs={"n_neighbors": [15, 10]}, ... cg_kwargs={"maxiter":2000}) """ if preconditioner is None: preconditioner = jacobi sanity_check_image(image) A, b = make_linear_system(knn_laplacian(image, **laplacian_kwargs), trimap) x = cg(A, b, M=preconditioner(A), **cg_kwargs) alpha = np.clip(x, 0, 1).reshape(trimap.shape) return alpha
def estimate_alpha_cf(image, trimap, preconditioner=None, laplacian_kwargs={}, cg_kwargs={}): """ Estimate alpha from an input image and an input trimap using Closed-Form Alpha Matting as proposed by :cite:`levin2007closed`. Parameters ---------- image: numpy.ndarray Image with shape :math:`h \\times w \\times d` for which the alpha matte should be estimated trimap: numpy.ndarray Trimap with shape :math:`h \\times w` of the image preconditioner: function or scipy.sparse.linalg.LinearOperator Function or sparse matrix that applies the preconditioner to a vector (default: ichol) laplacian_kwargs: dictionary Arguments passed to the :code:`cf_laplacian` function cg_kwargs: dictionary Arguments passed to the :code:`cg` solver is_known: numpy.ndarray Binary mask of pixels for which to compute the laplacian matrix. Providing this parameter might improve performance if few pixels are unknown. Returns ------- alpha: numpy.ndarray Estimated alpha matte Example ------- >>> from pymatting import * >>> image = load_image("data/lemur/lemur.png", "RGB") >>> trimap = load_image("data/lemur/lemur_trimap.png", "GRAY") >>> alpha = estimate_alpha_cf( ... image, ... trimap, ... laplacian_kwargs={"epsilon": 1e-6}, ... cg_kwargs={"maxiter":2000}) """ if preconditioner is None: preconditioner = ichol sanity_check_image(image) h, w = trimap.shape[:2] is_fg, is_bg, is_known, is_unknown = trimap_split(trimap) L = cf_laplacian(image, **laplacian_kwargs, is_known=is_known) # Split Laplacian matrix L into # # [L_U R ] # [R^T L_K] # # and then solve L_U x_U = -R is_fg_K for x where K (is_known) corresponds to # fixed pixels and U (is_unknown) corresponds to unknown pixels. For reference, see # Grady, Leo, et al. "Random walks for interactive alpha-matting." Proceedings of VIIP. Vol. 2005. 2005. L_U = L[is_unknown, :][:, is_unknown] R = L[is_unknown, :][:, is_known] m = is_fg[is_known] x = trimap.copy().ravel() x[is_unknown] = cg(L_U, -R.dot(m), M=preconditioner(L_U), **cg_kwargs) alpha = np.clip(x, 0, 1).reshape(trimap.shape) return alpha