
    1-Ph8              	           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dS )    )warnN   )measure)ensure_spacingc                 X   t          j        |          }| |         }t          j        | d          }t          j        |          |         }t          j        |          rt          |          }nd}|dk    rt          ||||          }t          |          |k    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	            T/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/skimage/feature/peak.py_get_high_intensity_peaksr   
   s    
 JtE,K*k\999KL,E	{9 i..a<
 
 
 5zzIjyj!L    c                 X   |j         dk    s| j         dk    r| |k    S t          j        | |d          }| |k    }|t          j        |          nt          j        ||                   }|r5d|dd<   |,t          j        |t          j        |                    }d||<   || |k    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     ~ejAooy  "5IINNNI
9
C '+lrvc{{{s4y8I8I $AAA.s/A$/G/GHHK#C59CJr   c                     t          |          D ]]\  }}|dk    rd| t          d          f|z  t          d|          fz   <   d| t          d          f|z  t          | d          fz   <   ^| S )zSet label border values to 0.r   N)	enumerateslice)labelborder_widthiwidths       r   _exclude_borderr6   =   s     l++ ? ?5A::<=uT{{nq E$$6$6#889=>uT{{nq E5&$$7$7#99::Lr   c                     ||n|                                  }|%t          |||                                 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   sB    
 "/!:		I 	=599;;#>??	r   c                    t          |t                    r|r|ndf| j        z  }nt          |t                    r!|dk     rt	          d          |f| j        z  }nt          |t
                    rht          |          | j        k    rt	          d          |D ];}t          |t                    st	          d          |dk     rt	          d          <|}nt          d          |S )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	NC	(	( 
AJKKK&(5:5	NE	*	* 
~%*,,/   & 	Q 	QGgs++  $   {{ !OPPP %9
 
 	

 r   r
   Tc
                 f   ||j         dk    r|dk     rt          dt          d           t          | ||          }
t	          | ||          }|-d|z  dz   }t          j        |f| j        z  t                    }nt          j	        |          }|6t          | ||          }t          ||
          }t          | ||||	          }nt          |                    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    }| |                                         }||t          j        |          <   t          ||||          }t          |||||	          }t+          |          D ]\  }}|dd|fxx         |j        z  cc<   |                    |           |rt          j        |          }nt          j        d	t                    }t=          |          |k    rEt          j        | t                    }d
|tA          |j!                  <   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   s   n 	Y^q00lQ6F6FF		
 	
 	
 	
 .e\>RRLum]CCI<!#GTGej0===		Jy))	~eY	::t\22 04L&
 

 "&--V-"D"DlSS=bk22 	/Xek**.FFXek**.F '(8(A(ABB 	2 	2NIs{  	A5Js**J5;Jr~j112!*iJOOD3D"5|V K
 $C.. / /QAAAsF###qw.####$$[1111 	6)$566KK(6555K{i''-T222C(,Ckm$$%3sI|V K r   c                    |                                  }|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  }||k    }t          j        |          }t          j        ||
          }t          |d 	          ddd
         }t          j
        d |D             t                    }g }g }g }t          j        | |dz   | |dz   f         \  }}|D ]\  }}|
||f         }||k    r||z   }||z   }t          j        |dk    ||k               }||         }||         }|dk     }|||         z
  ||<   ||xx         |z  cc<   ||k    }|||         z
  ||<   ||xx         |z  cc<   d|
||f<   |                    |           |                    |           |                    |           t          j
        |          }t          j
        |          }t          j
        |          }|t          |          k     r=t          j        |          ddd
         d|         }||         }||         }||         }|||fS )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   )keyc                 @    g | ]}t          j        |j                  S  )r   roundcentroid).0ps     r   
<listcomp>z$_prominent_peaks.<locals>.<listcomp>q  s$    ;;;rx
++;;;r   rI   )rV   shaper   r9   r$   maximum_filter1dr   r2   regionpropssortedarrayr   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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JD$"&++%	}$q(L}$q(L",QZa  G "l!  G '>D4KC)OEe$$I	733E 5778882>EX;;U;;;3GGGFIMM  "x	**]N]Q=N,NN K %+ . . [[019${2J${2J 
Q
T8IJJJ#J/J#J/J
 %q.K&*Z-D&DJ{#{###t+###%-L'+j.F'FJ|$|$$$,$$$ /0GJ
*+ U###  ---  ---##IH]++MH]++M3y>>!!j++DDbD1*9*=k*	%k2%k2m]22r   rp   )warningsr   numpyr   scipy.ndimagendimager$    r   _shared.coordr   r   r.   r6   r<   rF   infrj   r   rw   r   r   <module>r      s                         * * * * * *  6   0  
 
 
  F f6C C C CN !trvh3 h3 h3 h3 h3 h3r   