U
    !?h
                     @   s6   d dl Zd dlmZmZmZ d dlmZ d	ddZdS )
    N)vec_vec_dotmat_vec_dotvec_vec_outer)	boxfilterHz>
   Tc                    sD   tjd d }|| }j\}tdd| }ttdd| }|t||8 }tj||| t|  |  t	 |d| t
| ttd  d  fdd fdd}	|r@td dd}
dtdd }|t	t dd8 }|
t
|7 }
|	|
 fS |	S )	a  Calculates the Laplacian for large kernel matting :cite:`he2010fast`

    Parameters
    ----------
    image: numpy.ndarray
        Image of shape :math:`h\times w \times 3`
    epsilons: float
        Regularization strength, defaults to :math:`10^{-7}`
    radius: int
        Radius of local window size, defaults to :math:`10`, i.e. only adjacent
        pixels are considered. The size of the local window is given as
        :math:`(2 r + 1)^2`, where :math:`r` denotes the radius. A larger radius
        might lead to violated color line constraints, but also favors further
        propagation of information within the image.
    return_diagonal: bool
        Whether to also return the diagonal of the laplacian, defaults to True

    Returns
    -------
    L_matvec: function
        Function that applies the Laplacian matrix to a vector
    diag_L: numpy.ndarray
        Diagonal entries of the matting Laplacian, only returns if
        `return_diagonal` is True
          validmodefullc                    s   |  } t| dd}t| d d d d tjf  dd}|  }|d d d d tjf  t | }|tt|dd7 }| t| }|t|dd8 }| S )Nr
   r   r   )Zreshaper   npZnewaxisr   r   flatten)pZp_sumsZpI_sumsZp_LtempVZVmchimageZmVmradiusw S/var/www/html/venv/lib/python3.8/site-packages/pymatting/laplacian/lkm_laplacian.pyL_matvec3   s    &&zlkm_laplacian.<locals>.L_matvecg      ?)Zastyper   Zfloat64shaper   r   Zlinalginveyer   r   Zonesr   )r   epsilonr   Zreturn_diagonalZwindow_sizeZwindow_areadepthZmeansZcovsr   Zdiag_Lr   r   r   r   lkm_laplacian   s&    "
(r!   )r   r   T)	numpyr   Zpymatting.util.utilr   r   r   Zpymatting.util.boxfilterr   r!   r   r   r   r   <module>   s   