U
    !?h                      @   sJ   d dl mZ d dlmZ d dl mZ d dlmZ d dlZi i fddZ	dS )    )sanity_check_image)lkm_laplacian)trimap_split)cgNc                    s   t |  t| f|\ }t|\}}}}d}	|	| |	| }
d|   fdd}fdd}t||
fd|i|}t|dd	|j}|S )
a  
    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})

    g      Y@g      ?c                    s    | |   S N x)L_matveccr   T/var/www/html/venv/lib/python3.8/site-packages/pymatting/alpha/estimate_alpha_lkm.pyA_matvec6   s    z$estimate_alpha_lkm.<locals>.A_matvecc                    s    |  S r   r   r   )
inv_diag_Ar   r   jacobi9   s    z"estimate_alpha_lkm.<locals>.jacobiMr      )r   r   r   r   npZclipZreshapeshape)imageZtrimapZlaplacian_kwargsZ	cg_kwargsZdiag_LZis_fgZis_bgZis_knownZ
is_unknownZlambda_valuebr   r   r	   alphar   )r
   r   r   r   estimate_alpha_lkm   s    !r   )
Zpymatting.util.utilr   Z!pymatting.laplacian.lkm_laplacianr   r   Zpymatting.solver.cgr   numpyr   r   r   r   r   r   <module>   s
   