
    1-Ph$                     r    d Z ddlZddlmZ ddlmZ ddlm	Z	 ddl
mZmZ ddlmZ d	 Z	 	 	 	 	 dddddZdS )z
canny.py - Canny Edge detector

Reference: Canny, J., A Computational Approach To Edge Detection, IEEE Trans.
    Pattern Analysis and Machine Intelligence, 8:679-714, 1986
    N   )dtype_limits)gaussian)_supported_float_typecheck_nD   ) _nonmaximum_suppression_bilinearc                    t          |||d          }|dk    p|du}t          | j                  }|l|rt          j        | j        |          }| }t          j        | j        t                    }	d|	ddddf<   d|	ddddf<   d|	ddddf<   d|	ddddf<   ng|                    t          d	          }t          j        |           }| |         ||<   t          j
        d
d
          }
t          j        ||
d          }	|r<t          |                    |d	          fi |t          j        |          j        z   }t          |fi |}|r||z  }||	fS )a  Generate a smoothed image and an eroded mask.

    The image is smoothed using a gaussian filter ignoring masked
    pixels and the mask is eroded.

    Parameters
    ----------
    image : array
        Image to be smoothed.
    mask : array
        Mask with 1's for significant pixels, 0's for masked pixels.
    sigma : scalar or sequence of scalars
        Standard deviation for Gaussian kernel. The standard
        deviations of the Gaussian filter are given for each axis as a
        sequence, or as a single number, in which case it is equal for
        all axes.
    mode : str, {'reflect', 'constant', 'nearest', 'mirror', 'wrap'}
        The ``mode`` parameter determines how the array borders are
        handled, where ``cval`` is the value when mode is equal to
        'constant'.
    cval : float, optional
        Value to fill past edges of input if `mode` is 'constant'.

    Returns
    -------
    smoothed_image : ndarray
        The smoothed array
    eroded_mask : ndarray
        The eroded mask.

    Notes
    -----
    This function calculates the fractional contribution of masked pixels
    by applying the function to the mask (which gets you the fraction of
    the pixel data that's due to significant points). We then mask the image
    and apply the function. The resulting values will be lower by the
    bleed-over fraction, so you can recalibrate by dividing by the function
    on the mask to recover the effect of smoothing from just the significant
    pixels.
    F)sigmamodecvalpreserve_rangeconstantN)dtyper   r   )copyr   )border_value)dictr   r   nponesshapeboolastype
zeros_likendigenerate_binary_structurebinary_erosionr   finfoeps)imagemaskr   r   r   gaussian_kwargscompute_bleedover
float_typemasked_imageeroded_masks
bleed_oversmoothed_images                V/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/skimage/feature/_canny.py_preprocessr+      s   R TUSSSO
*>d$.>&u{33J| 	:75;j999Dgek666BQBEBCCFAAArrEAAArssF {{4e{,,}U++"4[T )!Q//(qqAAA 

 T[[%[88LLOLLhz""&' 	 l>>o>>N
  %*$;&&          ?Fr           )r   r   c                (   t          j        | j        t           j                  s$t          j        | j        t           j                  rt          d          t          | d           t          | d          d         }|d}n&|rd|cxk    rd	k    sn t          d
          n||z  }|d}n&|rd|cxk    rd	k    sn t          d
          n||z  }||k     rt          d          t          | ||||          \  }	}
t          j
        |	d          }t          j
        |	d          }||z  }|||z  z  }t          j        ||           |r t          j        |d|z  d|z  g          \  }}t          ||||
|          }|dk    }t          j        dt                    }t          j        ||          \  }}|dk    r|S |||k    z  }t          j        ||                   }t          j        |dz   ft                    }d||<   ||         }|S )a  Edge filter an image using the Canny algorithm.

    Parameters
    ----------
    image : 2D array
        Grayscale input image to detect edges on; can be of any dtype.
    sigma : float, optional
        Standard deviation of the Gaussian filter.
    low_threshold : float, optional
        Lower bound for hysteresis thresholding (linking edges).
        If None, low_threshold is set to 10% of dtype's max.
    high_threshold : float, optional
        Upper bound for hysteresis thresholding (linking edges).
        If None, high_threshold is set to 20% of dtype's max.
    mask : array, dtype=bool, optional
        Mask to limit the application of Canny to a certain area.
    use_quantiles : bool, optional
        If ``True`` then treat low_threshold and high_threshold as
        quantiles of the edge magnitude image, rather than absolute
        edge magnitude values. If ``True`` then the thresholds must be
        in the range [0, 1].
    mode : str, {'reflect', 'constant', 'nearest', 'mirror', 'wrap'}
        The ``mode`` parameter determines how the array borders are
        handled during Gaussian filtering, where ``cval`` is the value when
        mode is equal to 'constant'.
    cval : float, optional
        Value to fill past edges of input if `mode` is 'constant'.

    Returns
    -------
    output : 2D array (image)
        The binary edge map.

    See also
    --------
    skimage.filters.sobel

    Notes
    -----
    The steps of the algorithm are as follows:

    * Smooth the image using a Gaussian with ``sigma`` width.

    * Apply the horizontal and vertical Sobel operators to get the gradients
      within the image. The edge strength is the norm of the gradient.

    * Thin potential edges to 1-pixel wide curves. First, find the normal
      to the edge at each point. This is done by looking at the
      signs and the relative magnitude of the X-Sobel and Y-Sobel
      to sort the points into 4 categories: horizontal, vertical,
      diagonal and antidiagonal. Then look in the normal and reverse
      directions to see if the values in either of those directions are
      greater than the point in question. Use interpolation to get a mix of
      points instead of picking the one that's the closest to the normal.

    * Perform a hysteresis thresholding: first label all points above the
      high threshold as edges. Then recursively label any point above the
      low threshold that is 8-connected to a labeled point as an edge.

    References
    ----------
    .. [1] Canny, J., A Computational Approach To Edge Detection, IEEE Trans.
           Pattern Analysis and Machine Intelligence, 8:679-714, 1986
           :DOI:`10.1109/TPAMI.1986.4767851`
    .. [2] William Green's Canny tutorial
           https://en.wikipedia.org/wiki/Canny_edge_detector

    Examples
    --------
    >>> from skimage import feature
    >>> rng = np.random.default_rng()
    >>> # Generate noisy image of a square
    >>> im = np.zeros((256, 256))
    >>> im[64:-64, 64:-64] = 1
    >>> im += 0.2 * rng.random(im.shape)
    >>> # First trial with the Canny filter, with the default smoothing
    >>> edges1 = feature.canny(im)
    >>> # Increase the smoothing for better results
    >>> edges2 = feature.canny(im, sigma=3)

    z'64-bit integer images are not supportedr   F)clip_negativer   Ng?r.   r-   z,Quantile thresholds must be between 0 and 1.g?z1low_threshold should be lower then high_threshold)axisr   )outg      Y@)   r3   T)r   
issubdtyper   int64uint64
ValueErrorr   r   r+   r   sobelsqrt
percentiler	   r   r   labeluniquezeros)r    r   low_thresholdhigh_thresholdr!   use_quantilesr   r   	dtype_maxsmoothedr&   jsobelisobel	magnitude
low_maskedlow_maskstrellabelscount	high_masknonzero_sums
good_labeloutput_masks                          r*   cannyrO   g   s   D 
}U["(++ Dr}U[")/T/T DBCCCUAU%888;I	 #}++++++++KLLL , 	"	 $~,,,,,,,,KLLL - 	)#%%LMMM (tUD$GGHk Yxa(((FYxa(((FI& IGI9%%%% 
(*-u~/EF)
 )
%~
 2	; J A~HGFD!!EIh..MFEzzJ.89I9VI.//L519,--J#J|V$Kr,   )r-   NNNF)__doc__numpyr   scipy.ndimagendimager   
util.dtyper   _shared.filtersr   _shared.utilsr   r   	_canny_cyr	   r+   rO    r,   r*   <module>rY      s               % % % % % % & & & & & & ; ; ; ; ; ; ; ; 7 7 7 7 7 7S' S' S'p 	_ 
	_ _ _ _ _ _ _r,   