Ë
    ÿ[;j2:  ã            	       óÖ   — d dl mZ d dlZd dlmZ ddlmZ ddl	m
Z
 d„ Zdd„Zd„ Zd	„ Zd
„ Zddddej                   ddej                   ej                   f	d„Zdddej                   fd„Zy)é    )ÚwarnNé   )Úmeasure)Úensure_spacingc                 ó.  — t        j                  |«      }| |   }t        j                  | d¬«      }t        j                  |«      |   }t        j                  |«      rt        |«      }nd}|dkD  rt        ||||¬«      }t        |«      |kD  r|d| }|S )z8
    Return the highest intensity peak coordinates.
    Ústable)ÚkindNé   )ÚspacingÚp_normÚmax_out)ÚnpÚnonzeroÚargsortÚ	transposeÚisfiniteÚintr   Úlen)	ÚimageÚmaskÚ	num_peaksÚmin_distancer   ÚcoordÚintensitiesÚidx_maxsortr   s	            ú]G:\00. PROJECTS\API\Inventory\templateJSON\kerjaOCR\Lib\site-packages\skimage/feature/peak.pyÚ_get_high_intensity_peaksr   
   s”   € ô
 �J‰J�tÓ€EØ˜‘,€Kä—*‘*˜k˜\°Ô9€KÜ�L‰L˜Ó Ñ,€Eä	‡{�{�9ÔÜ�i“.‰àˆà�aÒÜØ˜<°Àô
ˆô ˆ5ƒz�IÒØ�j�yÐ!ˆà€Ló    c                 ób  — |j                   dk(  s| j                   dk(  r| |kD  S t        j                  | |d¬«      }| |k(  }|€t        j                  |«      nt        j                  ||   «      }|r5d|dd |�.t        j
                  |t        j                  |«      «      }d||<   || |kD  z  }|S )zJ
    Return the mask containing all peak candidates above thresholds.
    r
   Únearest)Ú	footprintÚmodeNFT)ÚsizeÚndiÚmaximum_filterr   ÚallÚlogical_xorÚbinary_opening)r   r!   Ú	thresholdr   Ú	image_maxÚoutÚimage_is_trivialÚisolated_pxs           r   Ú_get_peak_maskr.   %   s±   € ð ‡~�~˜Ò˜eŸj™j¨AšoØ�yÑ Ð ä×"Ñ" 5°IÀIÔN€Ià
�9Ñ
€Cð '+ l”r—v‘v˜c”{¼¿¹¸sÀ4¹yÓ8IÐÙØˆ‰AˆØÐäŸ.™.¨¬s×/AÑ/AÀ$Ó/GÓHˆKØ#ˆC�Ñàˆ5�9ÑÑ€CØ€Jr   c                 óº   — t        |«      D ]L  \  }}|dk(  rŒd| t        d«      f|z  t        d|«      fz   <   d| t        d«      f|z  t        | d«      fz   <   ŒN | S )zSet label border values to 0.r   N)Ú	enumerateÚslice)ÚlabelÚborder_widthÚiÚwidths       r   Ú_exclude_borderr6   =   sr   € ô ˜lÖ+‰ˆˆ5Ø�AŠ:ØØ<=ˆŒu�T‹{ˆn˜qÑ ¤E¨$°Ó$6Ð#8Ñ8Ñ9Ø=>ˆŒu�T‹{ˆn˜qÑ ¤E¨5¨&°$Ó$7Ð#9Ñ9Ò:ð	 ,ð
 €Lr   c                 ól   — |�|n| j                  «       }|�t        ||| j                  «       z  «      }|S )zSReturn the threshold value according to an absolute and a relative
    value.

    )ÚminÚmax)r   Úthreshold_absÚthreshold_relr)   s       r   Ú_get_thresholdr<   H   s9   € ð
 "/Ð!:‘ÀÇ	Á	Ã€IàÐ Ü˜	 =°5·9±9³;Ñ#>Ó?ˆ	àÐr   c                 óš  — t        |t        «      r|r|ndf| j                  z  }|S t        |t        «      r"|dk  rt	        d«      ‚|f| j                  z  }|S t        |t
        «      rYt        |«      | j                  k7  rt	        d«      ‚|D ]-  }t        |t        «      st	        d«      ‚|dk  sŒ$t	        d«      ‚ |}|S t        d«      ‚)zCReturn border_width values relative to a min_distance if requested.r   z+`exclude_border` cannot be a negative valuezP`exclude_border` should have the same length as the dimensionality of the image.zD`exclude_border`, when expressed as a tuple, must only contain ints.z,`exclude_border` can not be a negative valueze`exclude_border` must be bool, int, or tuple with the same length as the dimensionality of the image.)Ú
isinstanceÚboolÚndimr   Ú
ValueErrorÚtupler   Ú	TypeError)r   r   Úexclude_borderr3   Úexcludes        r   Ú_get_excluded_border_widthrF   U   só   € ô �.¤$Ô'Ù(6™¸AÐ?À%Ç*Á*ÑLˆð4 Ðô3 
�N¤CÔ	(Ø˜AÒÜÐJÓKÐKØ&Ð(¨5¯:©:Ñ5ˆð, Ðô+ 
�N¤EÔ	*Üˆ~Ó %§*¡*Ò,Üð/óð ó &ˆGÜ˜g¤sÔ+Ü ð$óð ð ˜‹{Ü Ð!OÓPÐPð &ð &ˆð Ðô ð9ó
ð 	
r   r
   Tc
                 ó8  — |�|j                   dk(  r|dk  rt        dt        d¬«       t        | ||«      }
t	        | ||«      }|€2d|z  dz   }t        j                  |f| j                  z  t        ¬«      }nt        j                  |«      }|€*t        | ||«      }t        ||
«      }t        | ||||	«      }|S t        |j                  t        d¬«      |
«      }t        j                  | j                   t
        j"                  «      r*t        j$                  | j                   «      j&                  }n)t        j(                  | j                   «      j&                  }g }t+        t-        j.                  |«      «      D ]š  \  }}|€Œ	||   |dz   k(  }| |   j1                  «       }||t        j2                  |«      <   t        ||||«      }t        |||||	«      }t+        |«      D ]   \  }}|dd…|fxx   |j4                  z  cc<   Œ" |j7                  |«       Œœ |rt        j8                  |«      }nt        j:                  d	t        ¬«      }t=        |«      |kD  rBt        j>                  | t        ¬«      }d
|tA        |jB                  «      <   t        | ||||	«      }|S )a\  Find peaks in an image as coordinate list.

    Peaks are the local maxima in a region of `2 * min_distance + 1`
    (i.e. peaks are separated by at least `min_distance`).

    If both `threshold_abs` and `threshold_rel` are provided, the maximum
    of the two is chosen as the minimum intensity threshold of peaks.

    .. versionchanged:: 0.18
        Prior to version 0.18, peaks of the same height within a radius of
        `min_distance` were all returned, but this could cause unexpected
        behaviour. From 0.18 onwards, an arbitrary peak within the region is
        returned. See issue gh-2592.

    Parameters
    ----------
    image : ndarray
        Input image.
    min_distance : int, optional
        The minimal allowed distance separating peaks. To find the
        maximum number of peaks, use `min_distance=1`.
    threshold_abs : float or None, optional
        Minimum intensity of peaks. By default, the absolute threshold is
        the minimum intensity of the image.
    threshold_rel : float or None, optional
        Minimum intensity of peaks, calculated as
        ``max(image) * threshold_rel``.
    exclude_border : int, tuple of ints, or bool, optional
        If positive integer, `exclude_border` excludes peaks from within
        `exclude_border`-pixels of the border of the image.
        If tuple of non-negative ints, the length of the tuple must match the
        input array's dimensionality.  Each element of the tuple will exclude
        peaks from within `exclude_border`-pixels of the border of the image
        along that dimension.
        If True, takes the `min_distance` parameter as value.
        If zero or False, peaks are identified regardless of their distance
        from the border.
    num_peaks : int, optional
        Maximum number of peaks. When the number of peaks exceeds `num_peaks`,
        return `num_peaks` peaks based on highest peak intensity.
    footprint : ndarray of bools, optional
        If provided, `footprint == 1` represents the local region within which
        to search for peaks at every point in `image`.
    labels : ndarray of ints, optional
        If provided, each unique region `labels == value` represents a unique
        region to search for peaks. Zero is reserved for background.
    num_peaks_per_label : int, optional
        Maximum number of peaks for each label.
    p_norm : float
        Which Minkowski p-norm to use. Should be in the range [1, inf].
        A finite large p may cause a ValueError if overflow can occur.
        ``inf`` corresponds to the Chebyshev distance and 2 to the
        Euclidean distance.

    Returns
    -------
    output : ndarray
        The coordinates of the peaks.

    Notes
    -----
    The peak local maximum function returns the coordinates of local peaks
    (maxima) in an image. Internally, a maximum filter is used for finding
    local maxima. This operation dilates the original image. After comparison
    of the dilated and original images, this function returns the coordinates
    of the peaks where the dilated image equals the original image.

    See also
    --------
    skimage.feature.corner_peaks

    Examples
    --------
    >>> img1 = np.zeros((7, 7))
    >>> img1[3, 4] = 1
    >>> img1[3, 2] = 1.5
    >>> img1
    array([[0. , 0. , 0. , 0. , 0. , 0. , 0. ],
           [0. , 0. , 0. , 0. , 0. , 0. , 0. ],
           [0. , 0. , 0. , 0. , 0. , 0. , 0. ],
           [0. , 0. , 1.5, 0. , 1. , 0. , 0. ],
           [0. , 0. , 0. , 0. , 0. , 0. , 0. ],
           [0. , 0. , 0. , 0. , 0. , 0. , 0. ],
           [0. , 0. , 0. , 0. , 0. , 0. , 0. ]])

    >>> peak_local_max(img1, min_distance=1)
    array([[3, 2],
           [3, 4]])

    >>> peak_local_max(img1, min_distance=2)
    array([[3, 2]])

    >>> img2 = np.zeros((20, 20, 20))
    >>> img2[10, 10, 10] = 1
    >>> img2[15, 15, 15] = 1
    >>> peak_idx = peak_local_max(img2, exclude_border=0)
    >>> peak_idx
    array([[10, 10, 10],
           [15, 15, 15]])

    >>> peak_mask = np.zeros_like(img2, dtype=bool)
    >>> peak_mask[tuple(peak_idx.T)] = True
    >>> np.argwhere(peak_mask)
    array([[10, 10, 10],
           [15, 15, 15]])

    Nr
   zmWhen min_distance < 1, peak_local_max acts as finding image > max(threshold_abs, threshold_rel * max(image)).r   )Ú
stacklevel©ÚdtypeÚsafe)Úcasting)r   r   T)"r#   r   ÚRuntimeWarningrF   r<   r   Úonesr@   r?   Úasarrayr.   r6   r   Úastyper   Ú
issubdtyperJ   ÚfloatingÚfinfor8   Úiinfor0   r$   Úfind_objectsÚcopyÚlogical_notÚstartÚappendÚvstackÚemptyr   Ú
zeros_likerB   ÚT)r   r   r:   r;   rD   r   r!   ÚlabelsÚnum_peaks_per_labelr   r3   r)   r#   r   ÚcoordinatesÚ_labelsÚbg_valÚlabels_peak_coordÚ	label_idxÚroiÚ
label_maskÚ
img_objectÚidxÚsr+   s                            r   Úpeak_local_maxrj   v   sg  € ðn 	Ð˜YŸ^™^¨qÒ0°lÀQÒ6FÜðFäØõ		
ô .¨e°\À>ÓR€Lä˜u m°]ÓC€IàÐØ�<Ñ !Ñ#ˆÜ—G‘G˜T˜G e§j¡jÑ0¼Ô=‰	ä—J‘J˜yÓ)ˆ	à€~ä˜e Y°	Ó:ˆä˜t \Ó2ˆô 0Ø�4˜ L°&ó
ˆðf Ðô] " &§-¡-´¸V -Ó"DÀlÓSˆä�=‰=˜Ÿ™¤b§k¡kÔ2Ü—X‘X˜eŸk™kÓ*×.Ñ.‰Fä—X‘X˜eŸk™kÓ*×.Ñ.ˆFð Ðä'¬×(8Ñ(8¸Ó(AÖB‰NˆI�sØˆ{Øð   ™¨	°A©Ñ5ˆJà˜s™Ÿ™Ó*ˆJà5;ˆJ”r—~‘~ jÓ1Ñ2ä! *¨i¸ÀJÓOˆDä3Ø˜DÐ"5°|ÀVóˆKô
 $ Cž.‘��QØšA˜s˜FÓ# q§w¡wÑ.Ô#ð )ð ×$Ñ$ [Õ1ð+ Cñ. ÜŸ)™)Ð$5Ó6‰KäŸ(™( 6´Ô5ˆKäˆ{Ó˜iÒ'Ü—-‘- ¬TÔ2ˆCØ(,ˆC”�k—m‘mÓ$Ñ%Ü3Ø�s˜I |°VóˆKð Ðr   c                 óö  — | j                  «       }|j                  \  }}|€dt        j                  |«      z  }d|z  dz   }d|z  dz   }	t	        j
                  ||ddd¬«      }
t	        j
                  |
|	ddd¬«      }
||
k(  }||z  }||kD  }t        j                  |«      }t        j                  ||
«      }t        |d„ ¬	«      ddd
…   }t        j                  |D �cg c]!  }t        j                  |j                  «      ‘Œ# c}t        ¬«      }g }g }g }t        j                  | |dz   …| |dz   …f   \  }}|D ]¶  \  }}|
||f   }||kD  sŒ||z   }||z   }t        j                  |dkD  ||k  «      }||   }||   }|dk  }|||   z
  ||<   ||xx   |z  cc<   ||k\  }|||   z
  ||<   ||xx   |z  cc<   d|
||f<   |j!                  |«       |j!                  |«       |j!                  |«       Œ¸ t        j                  |«      }t        j                  |«      }t        j                  |«      }|t#        |«      k  r-t        j$                  |«      ddd
…   d| }||   }||   }||   }|||fS c c}w )a{  Return peaks with non-maximum suppression.

    Identifies most prominent features separated by certain distances.
    Non-maximum suppression with different sizes is applied separately
    in the first and second dimension of the image to identify peaks.

    Parameters
    ----------
    image : (M, N) ndarray
        Input image.
    min_xdistance : int
        Minimum distance separating features in the x dimension.
    min_ydistance : int
        Minimum distance separating features in the y dimension.
    threshold : float
        Minimum intensity of peaks. Default is `0.5 * max(image)`.
    num_peaks : int
        Maximum number of peaks. When the number of peaks exceeds `num_peaks`,
        return `num_peaks` coordinates based on peak intensity.

    Returns
    -------
    intensity, xcoords, ycoords : tuple of array
        Peak intensity values, x and y indices.
    Ng      à?r   r
   r   Úconstant)r#   Úaxisr"   Úcvalc                 ó   — | j                   S ©N)Úintensity_max)Úxs    r   Ú<lambda>z"_prominent_peaks.<locals>.<lambda>p  s   € ¨¯ªr   )ÚkeyéÿÿÿÿrI   )rV   Úshaper   r9   r$   Úmaximum_filter1dr   r2   ÚregionpropsÚsortedÚarrayÚroundÚcentroidr   ÚmgridÚlogical_andrY   r   r   )r   Úmin_xdistanceÚmin_ydistancer)   r   ÚimgÚrowsÚcolsÚycoords_sizeÚxcoords_sizeÚimg_maxr   Úimg_tÚ	label_imgÚpropsÚpÚcoordsÚ	img_peaksÚycoords_peaksÚxcoords_peaksÚycoords_extÚxcoords_extÚycoords_idxÚxcoords_idxÚaccumÚ
ycoords_nhÚ
xcoords_nhÚ
ycoords_inÚxcoords_lowÚxcoords_highr   s                                  r   Ú_prominent_peaksr™   <  sÛ  € ð: �*‰*‹,€CØ—‘�J€Dˆ$àÐØœ"Ÿ&™& ›+Ñ%ˆ	à�}Ñ$ qÑ(€LØ�}Ñ$ qÑ(€LÜ×"Ñ"Ø�, Q¨Z¸aô€Gô ×"Ñ"Ø�l¨°À!ô€Gð �'‰>€DØˆ4�K€CØ�)‰O€Eä—‘˜eÓ$€IÜ×Ñ 	¨7Ó3€Eô �5Ñ7Ô8¹¸2¸Ñ>€EÜ�X‰X±UÓ;±U°”r—x‘x §
¡
Õ+°UÑ;Ä3ÔG€Fà€IØ€MØ€Mô  "Ÿx™xØ	ˆ˜¨Ñ*Ð*¨]¨N¸]ÈQÑ=NÐ,NÐNñ Ñ€K�ó %+Ñ ˆ�[Ø˜ [Ð0Ñ1ˆØ�9Óà$ {Ñ2ˆJØ$ {Ñ2ˆJô Ÿ™¨
°Q©¸
ÀTÑ8IÓJˆJØ# JÑ/ˆJØ# JÑ/ˆJð
 % q™.ˆKØ&*¨Z¸Ñ-DÑ&DˆJ�{Ñ#Ø�{Ó# tÑ+Ó#Ø%¨Ñ-ˆLØ'+¨j¸Ñ.FÑ'FˆJ�|Ñ$Ø�|Ó$¨Ñ,Ó$ð /0ˆG�J 
Ð*Ñ+ð ×Ñ˜UÔ#Ø× Ñ  Ô-Ø× Ñ  Õ-ð9 %+ô< —‘˜Ó#€IÜ—H‘H˜]Ó+€MÜ—H‘H˜]Ó+€Mà”3�y“>Ò!Ü—j‘j Ó+©D¨b¨DÑ1°*°9Ð=ˆØ˜kÑ*ˆ	Ø% kÑ2ˆØ% kÑ2ˆà�m ]Ð2Ð2ùòg <s   Ã&I6rp   )Úwarningsr   Únumpyr   Úscipy.ndimageÚndimager$   Ú r   Ú_shared.coordr   r   r.   r6   r<   rF   Úinfrj   r™   © r   r   Ú<module>r¢      ss   ðÝ ã Ý å Ý *òó6ò0ò
òðF ØØØØ�f‰fØØØŸ™Ø�6‰6óCðN ¨!°tÀrÇvÁvôh3r   