ó
 \c           @   sÎ   d  d l  m Z d  d l Z d d l m Z d d l m Z d  d l Z d  d l	 m
 Z
 d d	  Z d d d
  Z d d  Z d d d d  Z d d  Z d   Z d   Z d d  Z d d d  Z d S(   i˙˙˙˙(   t   divisionNi   (   t	   assert_nDi   (   t   _moments_cy(   t   warni   c         C   s   t  |  d d | S(   s  Calculate all raw image moments up to a certain order.

    The following properties can be calculated from raw image moments:
     * Area as: ``M[0, 0]``.
     * Centroid as: {``M[1, 0] / M[0, 0]``, ``M[0, 1] / M[0, 0]``}.

    Note that raw moments are neither translation, scale nor rotation
    invariant.

    Parameters
    ----------
    coords : (N, D) double or uint8 array
        Array of N points that describe an image of D dimensionality in
        Cartesian space.
    order : int, optional
        Maximum order of moments. Default is 3.

    Returns
    -------
    M : (``order + 1``, ``order + 1``, ...) array
        Raw image moments. (D dimensions)

    References
    ----------
    .. [1] Johannes Kilian. Simple Image Analysis By Moments. Durham
           University, version 0.2, Durham, 2001.

    Examples
    --------
    >>> coords = np.array([[row, col]
    ...                    for row in range(13, 17)
    ...                    for col in range(14, 18)], dtype=np.double)
    >>> M = moments_coords(coords)
    >>> centroid_row = M[1, 0] / M[0, 0]
    >>> centroid_col = M[0, 1] / M[0, 0]
    >>> centroid_row, centroid_col
    (14.5, 15.5)
    i    t   order(   t   moments_coords_central(   t   coordsR   (    (    s7   lib/python2.7/site-packages/skimage/measure/_moments.pyt   moments_coords
   s    'c         C   s#  t  |  t  r! t j |   }  n  t |  d  |  j d } | d k r_ t j |  d d } n  |  j t	  | }  |  d t j
 f t j | d  }  |  j |  j d | d  }  d } xM t |  D]? } |  d d  | f } t j | d d |  } | | } qÇ Wt j | d d } | S(   s  Calculate all central image moments up to a certain order.

    The following properties can be calculated from raw image moments:
     * Area as: ``M[0, 0]``.
     * Centroid as: {``M[1, 0] / M[0, 0]``, ``M[0, 1] / M[0, 0]``}.

    Note that raw moments are neither translation, scale nor rotation
    invariant.

    Parameters
    ----------
    coords : (N, D) double or uint8 array
        Array of N points that describe an image of D dimensionality in
        Cartesian space. A tuple of coordinates as returned by
        ``np.nonzero`` is also accepted as input.
    center : tuple of float, optional
        Coordinates of the image centroid. This will be computed if it
        is not provided.
    order : int, optional
        Maximum order of moments. Default is 3.

    Returns
    -------
    Mc : (``order + 1``, ``order + 1``, ...) array
        Central image moments. (D dimensions)

    References
    ----------
    .. [1] Johannes Kilian. Simple Image Analysis By Moments. Durham
           University, version 0.2, Durham, 2001.

    Examples
    --------
    >>> coords = np.array([[row, col]
    ...                    for row in range(13, 17)
    ...                    for col in range(14, 18)])
    >>> moments_coords_central(coords)
    array([[ 16.,   0.,  20.,   0.],
           [  0.,   0.,   0.,   0.],
           [ 20.,   0.,  25.,   0.],
           [  0.,   0.,   0.,   0.]])

    As seen above, for symmetric objects, odd-order moments (columns 1 and 3,
    rows 1 and 3) are zero when centered on the centroid, or center of mass,
    of the object (the default). If we break the symmetry by adding a new
    point, this no longer holds:

    >>> coords2 = np.concatenate((coords, [[17, 17]]), axis=0)
    >>> np.round(moments_coords_central(coords2), 2)
    array([[ 17.  ,   0.  ,  22.12,  -2.49],
           [  0.  ,   3.53,   1.73,   7.4 ],
           [ 25.88,   6.02,  36.63,   8.83],
           [  4.15,  19.17,  14.8 ,  39.6 ]])

    Image moments and central image moments are equivalent (by definition)
    when the center is (0, 0):

    >>> np.allclose(moments_coords(coords),
    ...             moments_coords_central(coords, (0, 0)))
    True
    i   i   t   axisi    .N(   i   (   t
   isinstancet   tuplet   npt	   transposeR   t   shapet   Nonet   meant   astypet   floatt   newaxist   aranget   reshapet   ranget   moveaxist   sum(   R   t   centerR   t   ndimt   calcR   t   isolated_axist   Mc(    (    s7   lib/python2.7/site-packages/skimage/measure/_moments.pyR   4   s     >$c         C   s   t  |  d |  j d | S(   s4  Calculate all raw image moments up to a certain order.

    The following properties can be calculated from raw image moments:
     * Area as: ``M[0, 0]``.
     * Centroid as: {``M[1, 0] / M[0, 0]``, ``M[0, 1] / M[0, 0]``}.

    Note that raw moments are neither translation, scale nor rotation
    invariant.

    Parameters
    ----------
    image : nD double or uint8 array
        Rasterized shape as image.
    order : int, optional
        Maximum order of moments. Default is 3.

    Returns
    -------
    m : (``order + 1``, ``order + 1``) array
        Raw image moments.

    References
    ----------
    .. [1] Wilhelm Burger, Mark Burge. Principles of Digital Image Processing:
           Core Algorithms. Springer-Verlag, London, 2009.
    .. [2] B. JĂ¤hne. Digital Image Processing. Springer-Verlag,
           Berlin-Heidelberg, 6. edition, 2005.
    .. [3] T. H. Reiss. Recognizing Planar Objects Using Invariant Image
           Features, from Lecture notes in computer science, p. 676. Springer,
           Berlin, 1993.
    .. [4] http://en.wikipedia.org/wiki/Image_moment

    Examples
    --------
    >>> image = np.zeros((20, 20), dtype=np.double)
    >>> image[13:17, 13:17] = 1
    >>> M = moments(image)
    >>> cr = M[1, 0] / M[0, 0]
    >>> cc = M[0, 1] / M[0, 0]
    >>> cr, cc
    (14.5, 14.5)
    i    R   (   i    (   t   moments_centralR   (   t   imageR   (    (    s7   lib/python2.7/site-packages/skimage/measure/_moments.pyt   moments   s    +c         K   s@  | d k	 rl d } t |  d | k rG | d k rG | d | f } n | | f } t |  d | d | j S| d k r t |   } n  |  j t  } xŁ t |  j  D] \ } } t	 j
 | d t | | }	 |	 d d  t	 j f t	 j
 | d  }
 t	 j | | |  j  } t	 j | |
  } t	 j | d |  } qŚ W| S(	   s  Calculate all central image moments up to a certain order.

    The center coordinates (cr, cc) can be calculated from the raw moments as:
    {``M[1, 0] / M[0, 0]``, ``M[0, 1] / M[0, 0]``}.

    Note that central moments are translation invariant but not scale and
    rotation invariant.

    Parameters
    ----------
    image : nD double or uint8 array
        Rasterized shape as image.
    center : tuple of float, optional
        Coordinates of the image centroid. This will be computed if it
        is not provided.
    order : int, optional
        The maximum order of moments computed.

    Other Parameters
    ----------------
    cr : double
        DEPRECATED: Center row coordinate for 2D image.
    cc : double
        DEPRECATED: Center column coordinate for 2D image.

    Returns
    -------
    mu : (``order + 1``, ``order + 1``) array
        Central image moments.

    References
    ----------
    .. [1] Wilhelm Burger, Mark Burge. Principles of Digital Image Processing:
           Core Algorithms. Springer-Verlag, London, 2009.
    .. [2] B. JĂ¤hne. Digital Image Processing. Springer-Verlag,
           Berlin-Heidelberg, 6. edition, 2005.
    .. [3] T. H. Reiss. Recognizing Planar Objects Using Invariant Image
           Features, from Lecture notes in computer science, p. 676. Springer,
           Berlin, 1993.
    .. [4] http://en.wikipedia.org/wiki/Image_moment

    Examples
    --------
    >>> image = np.zeros((20, 20), dtype=np.double)
    >>> image[13:17, 13:17] = 1
    >>> M = moments(image)
    >>> cr = M[1, 0] / M[0, 0]
    >>> cc = M[0, 1] / M[0, 0]
    >>> moments_central(image, (cr, cc))
    array([[ 16.,   0.,  20.,   0.],
           [  0.,   0.,   0.,   0.],
           [ 20.,   0.,  25.,   0.],
           [  0.,   0.,   0.,   0.]])
    s°   Using deprecated 2D-only, xy-coordinate interface to moments_central. This interface will be removed in scikit-image 0.16. Use moments_central(image, center=(cr, cc), order=3).t   crR   R   t   dtypeNi   i˙˙˙˙(   R   R   R   t   Tt   centroidR   R   t	   enumerateR   R   R   R   t   rollaxisR   t   dot(   R   R   t   ccR   t   kwargst   messageR   t   dimt
   dim_lengtht   deltat   powers_of_delta(    (    s7   lib/python2.7/site-packages/skimage/measure/_moments.pyR   Ć   s"    7
*c         C   sÉ   t  j t  j |  j  | k  r0 t d   n  t  j |   } |  j   d } xs t j t	 | d  d |  j
 D]O } t |  d k  r t  j | | <qr |  | | t |  | j
 d | | <qr W| S(   s*  Calculate all normalized central image moments up to a certain order.

    Note that normalized central moments are translation and scale invariant
    but not rotation invariant.

    Parameters
    ----------
    mu : (M,[ ...,] M) array
        Central image moments, where M must be greater than or equal
        to ``order``.
    order : int, optional
        Maximum order of moments. Default is 3.

    Returns
    -------
    nu : (``order + 1``,[ ...,] ``order + 1``) array
        Normalized central image moments.

    References
    ----------
    .. [1] Wilhelm Burger, Mark Burge. Principles of Digital Image Processing:
           Core Algorithms. Springer-Verlag, London, 2009.
    .. [2] B. JĂ¤hne. Digital Image Processing. Springer-Verlag,
           Berlin-Heidelberg, 6. edition, 2005.
    .. [3] T. H. Reiss. Recognizing Planar Objects Using Invariant Image
           Features, from Lecture notes in computer science, p. 676. Springer,
           Berlin, 1993.
    .. [4] http://en.wikipedia.org/wiki/Image_moment

    Examples
    --------
    >>> image = np.zeros((20, 20), dtype=np.double)
    >>> image[13:17, 13:17] = 1
    >>> m = moments(image)
    >>> cr = m[0, 1] / m[0, 0]
    >>> cc = m[1, 0] / m[0, 0]
    >>> mu = moments_central(image, cr, cc)
    >>> moments_normalized(mu)
    array([[        nan,         nan,  0.078125  ,  0.        ],
           [        nan,  0.        ,  0.        ,  0.        ],
           [ 0.078125  ,  0.        ,  0.00610352,  0.        ],
           [ 0.        ,  0.        ,  0.        ,  0.        ]])

    s)   Shape of image moments must be >= `order`i    i   t   repeati   (   R   t   anyt   arrayR   t
   ValueErrort
   zeros_liket   ravelt	   itertoolst   productR   R   R   t   nan(   t   muR   t   nut   mu0t   powers(    (    s7   lib/python2.7/site-packages/skimage/measure/_moments.pyt   moments_normalized  s    -!)+c         C   s   t  j |  j t j   S(   sĺ  Calculate Hu's set of image moments (2D-only).

    Note that this set of moments is proofed to be translation, scale and
    rotation invariant.

    Parameters
    ----------
    nu : (M, M) array
        Normalized central image moments, where M must be > 4.

    Returns
    -------
    nu : (7,) array
        Hu's set of image moments.

    References
    ----------
    .. [1] M. K. Hu, "Visual Pattern Recognition by Moment Invariants",
           IRE Trans. Info. Theory, vol. IT-8, pp. 179-187, 1962
    .. [2] Wilhelm Burger, Mark Burge. Principles of Digital Image Processing:
           Core Algorithms. Springer-Verlag, London, 2009.
    .. [3] B. JĂ¤hne. Digital Image Processing. Springer-Verlag,
           Berlin-Heidelberg, 6. edition, 2005.
    .. [4] T. H. Reiss. Recognizing Planar Objects Using Invariant Image
           Features, from Lecture notes in computer science, p. 676. Springer,
           Berlin, 1993.
    .. [5] http://en.wikipedia.org/wiki/Image_moment


    (   R   t
   moments_huR   R   t   double(   R8   (    (    s7   lib/python2.7/site-packages/skimage/measure/_moments.pyR<   M  s    c         C   sT   t  |  d d |  j d d } | t t j |  j d t  | d |  j } | S(   s  Return the (weighted) centroid of an image.

    Parameters
    ----------
    image : array
        The input image.

    Returns
    -------
    center : tuple of float, length ``image.ndim``
        The centroid of the (nonzero) pixels in ``image``.
    R   i    R   i   R!   (   i    (   i    (   R   R   R
   R   t   eyet   int(   R   t   MR   (    (    s7   lib/python2.7/site-packages/skimage/measure/_moments.pyR#   o  s    c         C   s1  | d k r! t |  d d } n  | d |  j } t j |  j |  j f  } t d t j |  j d t  } t j |  } t	 | j
 _ t j | |  | | | | (x t j t |  j  d  D]c } t j |  j d t } d | t |  <| t |  | | | <| t |  | | j | <qĆ W| S(   sń  Compute the inertia tensor of the input image.

    Parameters
    ----------
    image : array
        The input image.
    mu : array, optional
        The pre-computed central moments of ``image``. The inertia tensor
        computation requires the central moments of the image. If an
        application requires both the central moments and the inertia tensor
        (for example, `skimage.measure.regionprops`), then it is more
        efficient to pre-compute them and pass them to the inertia tensor
        call.

    Returns
    -------
    T : array, shape ``(image.ndim, image.ndim)``
        The inertia tensor of the input image. :math:`T_{i, j}` contains
        the covariance of image intensity along axes :math:`i` and :math:`j`.

    References
    ----------
    .. [1] https://en.wikipedia.org/wiki/Moment_of_inertia#Inertia_tensor
    .. [2] Bernd JĂ¤hne. Spatio-Temporal Image Processing: Theory and
           Scientific Applications. (Chapter 8: Tensor Methods) Springer, 1993.
    R   i   i    R!   i   N(   i    (   R   R   R   R   t   zerosR
   R>   R?   t   diagt   Truet   flagst	   writeableR   R4   t   combinationsR   t   listR"   (   R   R7   R9   t   resultt   corners2t   dt   dimst   mu_index(    (    s7   lib/python2.7/site-packages/skimage/measure/_moments.pyt   inertia_tensor  s    " " c         C   s@   | d k r t |  |  } n  t j j |  } t | d t S(   s  Compute the eigenvalues of the inertia tensor of the image.

    The inertia tensor measures covariance of the image intensity along
    the image axes. (See `inertia_tensor`.) The relative magnitude of the
    eigenvalues of the tensor is thus a measure of the elongation of a
    (bright) object in the image.

    Parameters
    ----------
    image : array
        The input image.
    mu : array, optional
        The pre-computed central moments of ``image``.
    T : array, shape ``(image.ndim, image.ndim)``
        The pre-computed inertia tensor. If ``T`` is given, ``mu`` and
        ``image`` are ignored.

    Returns
    -------
    eigvals : list of float, length ``image.ndim``
        The eigenvalues of the inertia tensor of ``image``, in descending
        order.

    Notes
    -----
    Computing the eigenvalues requires the inertia tensor of the input image.
    This is much faster if the central moments (``mu``) are provided, or,
    alternatively, one can provide the inertia tensor (``T``) directly.
    t   reverseN(   R   RM   R   t   linalgt   eigvalsht   sortedRC   (   R   R7   R"   t   eigvals(    (    s7   lib/python2.7/site-packages/skimage/measure/_moments.pyt   inertia_tensor_eigvalsˇ  s    (   t
   __future__R    t   numpyR   t   _shared.utilsR   t    R   R4   t   warningsR   R   R   R   R   R   R;   R<   R#   RM   RS   (    (    (    s7   lib/python2.7/site-packages/skimage/measure/_moments.pyt   <module>   s   *d.N9	"	4