B
    \i                 @   s0   d dl Zd dlZddlmZ dd Zdd ZdS )    N   )warnc             C   s(   | }xt | jD ]}|j|d}qW |S )a0  Integral image / summed area table.

    The integral image contains the sum of all elements above and to the
    left of it, i.e.:

    .. math::

       S[m, n] = \sum_{i \leq m} \sum_{j \leq n} X[i, j]

    Parameters
    ----------
    image : ndarray
        Input image.

    Returns
    -------
    S : ndarray
        Integral image/summed area table of same shape as input image.

    References
    ----------
    .. [1] F.C. Crow, "Summed-area tables for texture mapping,"
           ACM SIGGRAPH Computer Graphics, vol. 18, 1984, pp. 207-212.

    )Zaxis)rangendimZcumsum)ZimageSi r   9lib/python3.7/site-packages/skimage/transform/integral.pyintegral_image   s    r
   c                sZ  t t t t |}jd }j}t ||dg}dk }|dk }| | |   || | ||   }t | dk rtdt |}dj }t	t
|d dd }	xt|D ]}
t
|
dd |	}dd |D dt fd	dt|D  |t  d   | fd
dt|D 7 }qW |S )a  Use an integral image to integrate over a given window.

    Parameters
    ----------
    ii : ndarray
        Integral image.
    start : List of tuples, each tuple of length equal to dimension of `ii`
        Coordinates of top left corner of window(s).
        Each tuple in the list contains the starting row, col, ... index
        i.e `[(row_win1, col_win1, ...), (row_win2, col_win2,...), ...]`.
    end : List of tuples, each tuple of length equal to dimension of `ii`
        Coordinates of bottom right corner of window(s).
        Each tuple in the list containing the end row, col, ... index i.e
        `[(row_win1, col_win1, ...), (row_win2, col_win2, ...), ...]`.

    Returns
    -------
    S : scalar or ndarray
        Integral (sum) over the given window(s).


    Examples
    --------
    >>> arr = np.ones((5, 6), dtype=np.float)
    >>> ii = integral_image(arr)
    >>> integrate(ii, (1, 0), (1, 2))  # sum from (1, 0) to (1, 2)
    array([ 3.])
    >>> integrate(ii, [(3, 3)], [(4, 5)])  # sum from (3, 3) to (4, 5)
    array([ 6.])
    >>> # sum from (1, 0) to (1, 2) and from (3, 3) to (4, 5)
    >>> integrate(ii, [(1, 0), (3, 3)], [(1, 2), (4, 5)])
    array([ 3.,  6.])
    r      z1end coordinates must be greater or equal to startr   Nc             S   s   g | ]}|d kqS )1r   ).0bitr   r   r	   
<listcomp>w   s    zintegrate.<locals>.<listcomp>c                s&   g | ]}t | d    dk qS )r   r   )npany)r   r)	bool_maskstartr   r	   r   {   s   c                s,   g | ]$} | s$t |   nd qS )r   )tuple)r   r   )badcorner_pointsiisignr   r	   r      s   )r   Z
atleast_2dZarrayshapeZtiler   
IndexErrorZzerosr   lenbinr   zfillsuminvert)r   r   endZrowsZtotal_shapeZstart_negativesZend_negativesr   Zbit_permwidthr   Zbinaryr   )r   r   r   r   r   r   r	   	integrate'   s6    "





r$   )Znumpyr   collectionsZ_shared.utilsr   r
   r$   r   r   r   r	   <module>   s    