Ë
    ÿ[;j'J  ã                   óž   — d dl Z d dlZddlmZmZ ddlmZ ddlm	Z	 dd„Z
dd„Zddd	œd
„Zddd	œd„Zdd„Zd„ Zdd	œd„Zddd	œd„Zddd	œd„Zy)é    Né   )Ú_supported_float_typeÚcheck_nDé   )Ú_moments_cy)Úmoments_raw_to_centralc                 ó   — t        | d|¬«      S )av  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.float64)
    >>> M = moments_coords(coords)
    >>> centroid = (M[1, 0] / M[0, 0], M[0, 1] / M[0, 0])
    >>> centroid
    (14.5, 15.5)
    r   )Úorder)Úmoments_coords_central)Úcoordsr
   s     úaG:\00. PROJECTS\API\Inventory\templateJSON\kerjaOCR\Lib\site-packages\skimage/measure/_moments.pyÚmoments_coordsr   
   s   € ôL " &¨!°5Ô9Ð9ó    c                 óx  — t        | t        «      rt        j                  | d¬«      } t	        | d«       | j
                  d   }t        | j                  «      }|€t        j                  | dt        ¬«      }| j                  |d¬	«      |z
  } t        j                  t        |dz   «      D �cg c]  }| |z  ‘Œ	 c}d¬«      } | j                  | j
                  d
|dz
  z  z   «      } d}t        |«      D ]*  }| dd…|f   }t        j                  |dd|z   «      }||z  }Œ, t        j                  |d¬«      }	|	S c c}w )a±  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),
    ...          decimals=2)  # doctest: +NORMALIZE_WHITESPACE
    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
    éÿÿÿÿ)Úaxisr   r   Nr   )r   ÚdtypeF©Úcopy)r   )Ú
isinstanceÚtupleÚnpÚstackr   Úshaper   r   ÚmeanÚfloatÚastypeÚrangeÚreshapeÚmoveaxisÚsum)
r   Úcenterr
   ÚndimÚ
float_typeÚcÚcalcr   Úisolated_axisÚMcs
             r   r   r   3   s(  € ô~ �&œ%Ô ô —‘˜& rÔ*ˆÜˆV�QÔØ�<‰<˜‰?€Dä& v§|¡|Ó4€JØ€~Ü—‘˜ a¬uÔ5ˆð �]‰]˜:¨Eˆ]Ó2°VÑ;€Fô �X‰X¬%°¸±	Ô*:Ó;Ñ*: Q�v˜q“yÐ*:Ñ;À"ÔE€Fð �^‰^˜FŸL™L¨4°4¸!±8Ñ+<Ñ<Ó=€Fà€Dä�d–ˆàšq $˜w™ˆô Ÿ™ M°1°a¸$±hÓ?ˆð �mÑ#‰ð ô 
�‰�˜1Ô	€Bà€Iùò) <s   Â+D7©Úspacingc                ó:   — t        | d| j                  z  ||¬«      S )u   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 : (N[, ...]) double or uint8 array
        Rasterized shape as image.
    order : int, optional
        Maximum order of moments. Default is 3.
    spacing : tuple of float, shape (ndim,)
        The pixel spacing along each axis of the image.

    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] https://en.wikipedia.org/wiki/Image_moment

    Examples
    --------
    >>> image = np.zeros((20, 20), dtype=np.float64)
    >>> image[13:17, 13:17] = 1
    >>> M = moments(image)
    >>> centroid = (M[1, 0] / M[0, 0], M[0, 1] / M[0, 0])
    >>> centroid
    (14.5, 14.5)
    ©r   ©r
   r*   )Úmoments_centralr#   )Úimager
   r*   s      r   Úmomentsr0   š   s   € ôX ˜5 $¨¯©Ñ"3¸5È'ÔRÐRr   c                ó’  — |€t        | ||¬«      }t        |«      S t        | j                  «      }|€!t	        j
                  | j                  |¬«      }| j                  |d¬«      }t        t        | j                  «      «      }| j                  }	|	dz   }
t	        j                  |dz   |¬«      }t        | j                  «      D ]  \  }}t	        j                  ||¬«      ||   z  ||   z
  }|dd…t        j                  f   |z  }t	        j                  ||d| |	gz   ||dz   d z   ||	|
g|d| |
gz   ||dz   d z   d¬«      }Œ� |S )	u¢  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 : (N[, ...]) 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.
    spacing : tuple of float, shape (ndim,)
        The pixel spacing along each axis of the 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] https://en.wikipedia.org/wiki/Image_moment

    Examples
    --------
    >>> image = np.zeros((20, 20), dtype=np.float64)
    >>> image[13:17, 13:17] = 1
    >>> M = moments(image)
    >>> centroid = (M[1, 0] / M[0, 0], M[0, 1] / M[0, 0])
    >>> moments_central(image, centroid)
    array([[16.,  0., 20.,  0.],
           [ 0.,  0.,  0.,  0.],
           [20.,  0., 25.,  0.],
           [ 0.,  0.,  0.,  0.]])
    Nr-   ©r   Fr   r   Úgreedy)Úoptimize)r0   r   r   r   r   Úonesr#   r   Úlistr   ÚarangeÚ	enumerater   ÚnewaxisÚeinsum)r/   r"   r
   r*   ÚkwargsÚmoments_rawÚfloat_dtyper&   ÚLÚ	sum_labelÚorder_labelÚordersÚdimÚ
dim_lengthÚdeltaÚpowers_of_deltas                   r   r.   r.   É   sN  € ðb €~ô ˜e¨5¸'ÔBˆÜ% kÓ2Ð2Ü'¨¯©Ó4€KØ€Ü—'‘'˜%Ÿ*™*¨KÔ8ˆØ�<‰<˜¨%ˆ<Ó0€DÜŒU�5—:‘:ÓÓ€AØ—
‘
€IØ˜a‘-€KÜ�Y‰Y�u˜q‘y¨Ô4€FÜ$ U§[¡[Ö1‰ˆˆZÜ—	‘	˜*¨KÔ8¸7À3¹<ÑGÈ&ÐQTÉ+ÑUˆØ¢¤2§:¡: Ñ.°&Ñ8ˆô �y‰yØØˆdˆsˆG�y�kÑ! A c¨A¡g i LÑ0ØØ˜Ð$ØˆdˆsˆG�{�mÑ# a¨¨a©¨	 lÑ2Øô
‰ð 2ð €Kr   c                 óB  — t        j                  t        j                  | j                  «      |k  «      rt	        d«      ‚|€t        j
                  | j                  «      }t        j                  | «      }| j                  «       d   }t        |«      }t        j                  t        |dz   «      | j                  ¬«      D ]Z  }t        |«      dk  rt         j                  ||<   Œ%| |   |t        |«      z  z  |t        |«      |j                  z  dz   z  z  ||<   Œ\ |S )u}  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.
    spacing : tuple of float, shape (ndim,)
        The pixel spacing along each axis of the image.

    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] https://en.wikipedia.org/wiki/Image_moment

    Examples
    --------
    >>> image = np.zeros((20, 20), dtype=np.float64)
    >>> image[13:17, 13:17] = 1
    >>> m = moments(image)
    >>> centroid = (m[0, 1] / m[0, 0], m[1, 0] / m[0, 0])
    >>> mu = moments_central(image, centroid)
    >>> moments_normalized(mu)
    array([[       nan,        nan, 0.078125  , 0.        ],
           [       nan, 0.        , 0.        , 0.        ],
           [0.078125  , 0.        , 0.00610352, 0.        ],
           [0.        , 0.        , 0.        , 0.        ]])
    z)Shape of image moments must be >= `order`r   r   )Úrepeatr   )r   ÚanyÚarrayr   Ú
ValueErrorr5   r#   Ú
zeros_likeÚravelÚminÚ	itertoolsÚproductr   r!   Únan)Úmur
   r*   ÚnuÚmu0ÚscaleÚpowerss          r   Úmoments_normalizedrV     sê   € ôZ 
‡v�vŒb�h‰h�r—x‘xÓ  EÑ)Ô*ÜÐDÓEÐEØ€Ü—'‘'˜"Ÿ'™'Ó"ˆÜ	�‰�rÓ	€BØ
�(‰(‹*�Q‰-€CÜ�‹L€EÜ×#Ñ#¤E¨%°!©)Ó$4¸R¿W¹W×EˆÜˆv‹;˜Š?ÜŸ™ˆBˆvŠJà˜V™* u´°F³Ñ';Ñ;Øœ˜F› b§g¡gÑ-°Ñ1Ñ2ñˆBˆvŠJð	 Fð €Ir   c                 ó¬   — | j                   dk(  rt        j                  nt        j                  }t	        j
                  | j                  |d¬«      «      S )u=  Calculate Hu's set of image moments (2D-only).

    Note that this set of moments is proved 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] https://en.wikipedia.org/wiki/Image_moment

    Examples
    --------
    >>> image = np.zeros((20, 20), dtype=np.float64)
    >>> image[13:17, 13:17] = 0.5
    >>> image[10:12, 10:12] = 1
    >>> mu = moments_central(image)
    >>> nu = moments_normalized(mu)
    >>> np.round(moments_hu(nu), 4)  # doctest: +FLOAT_CMP
    array([0.7454, 0.3512, 0.104 , 0.0406, 0.0026, 0.0241, 0.    ])
    Úfloat32Fr   )r   r   rX   Úfloat64r   Ú
moments_hur   )rR   r   s     r   rZ   rZ   V  s=   € ðN Ÿ(™( iÒ/ŒB�JŠJ´R·Z±Z€EÜ×!Ñ! "§)¡)¨E¸ )Ó">Ó?Ð?r   c                óÆ   — t        | d| j                  z  d|¬«      }|t        t        j                  | j                  t
        ¬«      «         |d| j                  z     z  }|S )a5  Return the (weighted) centroid of an image.

    Parameters
    ----------
    image : array
        The input image.
    spacing : tuple of float, shape (ndim,)
        The pixel spacing along each axis of the image.

    Returns
    -------
    center : tuple of float, length ``image.ndim``
        The centroid of the (nonzero) pixels in ``image``.

    Examples
    --------
    >>> image = np.zeros((20, 20), dtype=np.float64)
    >>> image[13:17, 13:17] = 0.5
    >>> image[10:12, 10:12] = 1
    >>> centroid(image)
    array([13.16666667, 13.16666667])
    r,   r   )r"   r
   r*   r2   )r.   r#   r   r   ÚeyeÚint)r/   r*   ÚMr"   s       r   Úcentroidr_   �  s[   € ô. 	˜ d¨U¯Z©ZÑ&7¸qÈ'ÔR€Aà	Œ%”—‘�u—z‘z¬Ô-Ó
.Ñ/à
ˆD�5—:‘:ÑÑ
ñ	ð ð
 €Mr   c                óØ  — |€t        | d|¬«      }|d| j                  z     }t        j                  | j                  | j                  f|j                  ¬«      }t        dt        j                  | j                  t        ¬«      z  «      }t        j                  |«      }d|j                  _
        t        j                  ||   «      ||   z
  |z  |dd t        j                  t        | j                  «      d«      D ]i  }t        j                  | j                  t        ¬«      }d|t        |«      <   |t        |«          |z  ||<   |t        |«          |z  |j                   |<   Œk |S )uU  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.
    spacing : tuple of float, shape (ndim,)
        The pixel spacing along each axis of the image.

    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.
    Nr   r-   r,   r2   Tr   )r.   r#   r   Úzerosr   r   r\   r]   ÚdiagÚflagsÚ	writeabler!   rN   Úcombinationsr   r6   ÚT)	r/   rQ   r*   rS   ÚresultÚcorners2ÚdÚdimsÚmu_indexs	            r   Úinertia_tensorrl   ¡  s0  € ð: 
€zÜØ˜ Gô
ˆð ˆT�E—J‘JÑÑ
€CÜ�X‰X�u—z‘z 5§:¡:Ð.°b·h±hÔ?€Fô �QœŸ™ §
¡
´#Ô6Ñ6Ó7€HÜ
�‰�‹€AØ€A‡G�GÔô �F‰F�2�h‘<Ó  2 h¡<Ñ/°3Ñ6€A�a€Dä×&Ñ&¤u¨U¯Z©ZÓ'8¸!Ö<ˆÜ—8‘8˜EŸJ™J¬cÔ2ˆØ ˆ”�d“ÑØœ5 ›?Ñ+Ð+¨cÑ1ˆˆt‰ØœU 8›_Ñ-Ð-°Ñ3ˆ�‰�Šð	 =ð
 €Mr   c                ó¬   — |€t        | ||¬«      }t        j                  j                  |«      }t        j                  |dd|¬«      }t        |d¬«      S )al  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.
    spacing : tuple of float, shape (ndim,)
        The pixel spacing along each axis of the image.

    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.
    Nr)   r   )ÚoutT)Úreverse)rl   r   ÚlinalgÚeigvalshÚclipÚsorted)r/   rQ   rf   r*   Úeigvalss        r   Úinertia_tensor_eigvalsru   Ù  sO   € ð@ 	€yÜ˜5 "¨gÔ6ˆÜ�i‰i× Ñ  Ó#€Gô
 �g‰g�g˜q $¨GÔ4€GÜ�' 4Ô(Ð(r   )é   )Nrv   )rv   N)N)NN)rN   Únumpyr   Ú_shared.utilsr   r   Ú r   Ú_moments_analyticalr   r   r   r0   r.   rV   rZ   r_   rl   ru   © r   r   Ú<module>r|      sp   ðÛ ã ç ;Ý Ý 7ó&:óRdðN,S tô ,Sð^L¸Dô Ló^;ò|(@ðV  $ô ð@5¨dô 5ðp()¸dõ ()r   