
    9il                        S SK rSSKJrJr  SSKJr  / SQr\R                  " 5       r	\	R                  S \R                  " 5        5       5        \	R                  SSS	\\   \\R                     S
.5        S rS rS!S jrS rS rS r\R(                  " SS9 S"SS.S jj5       rS rS#S jrS$S jrS%S jrS rS&S jrS rS rS'S jrS(S jrS)S jr     S*S  jr!g)+    N   )dtype_rangedtype_limits)utils)	histogramcumulative_distributionequalize_histrescale_intensityadjust_gamma
adjust_logadjust_sigmoidc              #   B   #    U  H  u  pUR                   U4v   M     g 7fN)__name__).0dlimitss      Y/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/skimage/exposure/exposure.py	<genexpr>r      s     M9LIAAJJ'9Ls   )r   i  )r   i  )r   i?  )uint10uint12uint14boolfloatc                     US:  aj  UnX!-
  n[         R                  " [         R                  " U5      [         R                  " U5      5      nU R                  U:w  a  U R	                  U5      n X-
  n U $ )z:Offset the array to get the lowest value at 0 if negative.r   )nppromote_typesmin_scalar_typedtypeastype)arrlow_boundaryhigh_boundaryoffset	dyn_rangeoffset_dtypes         r   _offset_arrayr'      sj    a!0	''y)2+=+=l+K
 99$**\*ClJ    c                 l   US;  a  [        SU 35      eUS:X  am  [        U R                  5       R                  [        R
                  5      5      n[        U R                  5       R                  [        R
                  5      5      nOUS:X  a  [        U SS9u  p#[        R                  " WWS-   5      nU$ )z1Compute bin centers for bincount-based histogram.imager   -Incorrect value for `source_range` argument: r+   r   Fclip_negative   )	
ValueErrorintminr    r   int64maxr   arange)r+   source_range	image_min	image_maxbin_centerss        r   _bincount_histogram_centersr:   /   s    --HWXXw		**28845			**28845			 +EG	))Iy1}5Kr(   c                     Uc  [        X5      nUS   US   pC[        XU5      n [        R                  " U R	                  5       U[        US5      -
  S-   S9nUS:X  a  [        US5      nXVS nXR4$ )a  
Efficient histogram calculation for an image of integers.

This function is significantly more efficient than np.histogram but
works only on images of integers. It is based on np.bincount.

Parameters
----------
image : array
    Input image.
source_range : string
    'image' determines the range from the input image.
    'dtype' determines the range from the expected range of the images
    of that data type.

Returns
-------
hist : array
    The values of the histogram.
bin_centers : array
    The values at the center of the bins.
Nr   r/   )	minlengthr+   )r:   r'   r   bincountravelr2   r4   )r+   r6   r9   r7   r8   histidxs          r   _bincount_histogramrB   <   s}    . 1%F&q>;r?y%I6E;;u{{}	C	1<M0MPQ0QRDw)QDzr(   c                    Ub]  Uu  p#X#:  a  [        S5      e[        R                  " U5      (       a  [        R                  " U5      (       d  [        SU SU S35      eO|U R                  S:X  a  Su  p#OgU R	                  5       U R                  5       p2[        R                  " U5      (       a  [        R                  " U5      (       d  [        SU SU S35      eX#:X  a
  US-
  nUS-   nX#4$ )	a  Determine the outer bin edges to use for `numpy.histogram`.

These are obtained from either the image or hist_range.

Parameters
----------
image : ndarray
    Image for which the histogram is to be computed.
hist_range: 2-tuple of int or None
    Range of values covered by the histogram bins. If None, the minimum
    and maximum values of `image` are used.

Returns
-------
first_edge, last_edge : int
    The range spanned by the histogram bins.

Notes
-----
This function is adapted from ``np.lib.histograms._get_outer_edges``.
z4max must be larger than min in hist_range parameter.zsupplied hist_range of [z, z] is not finiter   )r   r/   zautodetected hist_range of [      ?)r0   r   isfinitesizer2   r4   )r+   
hist_range
first_edge	last_edges       r   _get_outer_edgesrJ   ^   s    ,  *
!STTJ''BKK	,B,B*:,bCVW  -C 
q $
I %		UYY[IJ''BKK	,B,B.zl"YK H   #%
O	  r(   c                    [        X5      u  p4[        R                  " X4U 5      n[        R                  " U[        R                  5      (       a  [        R                  " U[
        5      n[        R                  " X4US-   SUS9nU$ )a  Computes histogram bins for use with `numpy.histogram`.

Parameters
----------
image : ndarray
    Image for which the histogram is to be computed.
nbins : int
    The number of bins.
hist_range: 2-tuple of int
    Range of values covered by the histogram bins.

Returns
-------
bin_edges : ndarray
    The histogram bin edges.

Notes
-----
This function is a simplified version of
``np.lib.histograms._get_bin_edges`` that only supports uniform bins.
r/   T)endpointr   )rJ   r   result_type
issubdtypeintegerr   linspace)r+   nbinsrG   rH   rI   bin_type	bin_edgess          r   _get_bin_edgesrT      sm    , -U?J ~~jU;H	}}Xrzz**>>(E2 uqy4xI r(   c                 V    US:X  a  S nU$ US:X  a  [        U SS9nU$ [        SU 35      e)Nr+   r   Fr-   r,   )r   r0   )r+   r6   rG   s      r   _get_numpy_hist_rangerV      sJ    w

 	 
	 !%u=
  HWXXr(   F)multichannel_output)channel_axisc                *   U R                   n[        U5      S:X  a"  US   S:  a  Uc  [        R                  " S5        Ub  US   n/ n[        R
                  " U R                  [        R                  5      (       a  [        X5      nO[        X5      n	[        XU	5      n[        U5       H(  n
[        U SU
4   XU5      u  pUR                  U5        M*     [        R                  " W5      n[        R                  " USS9nX}4$ [        XX#5      u  p}X}4$ )a  Return histogram of image.

Unlike `numpy.histogram`, this function returns the centers of bins and
does not rebin integer arrays. For integer arrays, each integer value has
its own bin, which improves speed and intensity-resolution.

If `channel_axis` is not set, the histogram is computed on the flattened
image. For color or multichannel images, set ``channel_axis`` to use a
common binning for all channels. Alternatively, one may apply the function
separately on each channel to obtain a histogram for each color channel
with separate binning.

Parameters
----------
image : array
    Input image.
nbins : int, optional
    Number of bins used to calculate histogram. This value is ignored for
    integer arrays.
source_range : string, optional
    'image' (default) determines the range from the input image.
    'dtype' determines the range from the expected range of the images
    of that data type.
normalize : bool, optional
    If True, normalize the histogram by the sum of its values.
channel_axis : int or None, optional
    If None, the image is assumed to be a grayscale (single channel) image.
    Otherwise, this parameter indicates which axis of the array corresponds
    to channels.

Returns
-------
hist : array
    The values of the histogram. When ``channel_axis`` is not None, hist
    will be a 2D array where the first axis corresponds to channels.
bin_centers : array
    The values at the center of the bins.

See Also
--------
cumulative_distribution

Examples
--------
>>> from skimage import data, exposure, img_as_float
>>> image = img_as_float(data.camera())
>>> np.histogram(image, bins=2)
(array([ 93585, 168559]), array([0. , 0.5, 1. ]))
>>> exposure.histogram(image, nbins=2)
(array([ 93585, 168559]), array([0.25, 0.75]))
   r<      zThis might be a color image. The histogram will be computed on the flattened image. You can instead apply this function to each color channel, or set channel_axis..r   )axis)shapelenr   warnr   rN   r   rO   r:   rV   rT   range
_histogramappendasarraystack)r+   rQ   r6   	normalizerX   shchannelsr@   binsrG   chanhbcr9   s                 r   r   r      s   n 
B
2w!|2
|';

	
 b6 ==bjj11.uCD /uCJ!%
;D(ODuS$Y/YOEAKKN $ jjnxx1%  'u\Mr(   c                    U R                  5       n [        R                  " U R                  [        R                  5      (       a2  [        U[        R                  5      (       a  UOSn[        XU5      u  pTO0[        X5      n[        R                  " XUS9u  pWUSS USS -   S-  nU(       a  U[        R                  " U5      -  nXT4$ )ax  

Parameters
----------
image : ndarray
    Image for which the histogram is to be computed.
bins : int or ndarray
    The number of histogram bins. For images with integer dtype, an array
    containing the bin centers can also be provided. For images with
    floating point dtype, this can be an array of bin_edges for use by
    ``np.histogram``.
source_range : string, optional
    'image' (default) determines the range from the input image.
    'dtype' determines the range from the expected range of the images
    of that data type.
normalize : bool, optional
    If True, normalize the histogram by the sum of its values.
N)rh   r`   r<   r/   g       @)flattenr   rN   r   rO   
isinstancendarrayrB   rV   r   sum)r+   rh   r6   re   r9   r@   rG   rS   s           r   ra   ra     s    ( MMOE	}}U[["**--(rzz::d/[Qk*5?
,,uzJ "~	!"5<bffTl"r(   c                     [        X5      u  p#UR                  5       nU[        US   5      -  n[        R                  " U R
                  5      nUR                  USS9nXC4$ )a  Return cumulative distribution function (cdf) for the given image.

Parameters
----------
image : array
    Image array.
nbins : int, optional
    Number of bins for image histogram.

Returns
-------
img_cdf : array
    Values of cumulative distribution function.
bin_centers : array
    Centers of bins.

See Also
--------
histogram

References
----------
.. [1] https://en.wikipedia.org/wiki/Cumulative_distribution_function

Examples
--------
>>> from skimage import data, exposure, img_as_float
>>> image = img_as_float(data.camera())
>>> hi = exposure.histogram(image)
>>> cdf = exposure.cumulative_distribution(image)
>>> all(cdf[0] == np.cumsum(hi[0])/float(image.size))
True
r<   Fcopy)r   cumsumr   r   _supported_float_typer   r    )r+   rQ   r@   r9   img_cdf	cdf_dtypes         r   r   r   :  sa    D "%/DkkmGgbk**G ++EKK8InnYUn3Gr(   c                 J   Ub*  [         R                  " U[        S9n[        X   U5      u  p4O[        X5      u  p4[         R                  " U R
                  XC5      nUR                  U R                  5      nUR                  [        R                  " U R                  5      SS9$ )a   Return image after histogram equalization.

Parameters
----------
image : array
    Image array.
nbins : int, optional
    Number of bins for image histogram. Note: this argument is
    ignored for integer images, for which each integer is its own
    bin.
mask : ndarray of bools or 0s and 1s, optional
    Array of same shape as `image`. Only points at which mask == True
    are used for the equalization, which is applied to the whole image.

Returns
-------
out : float array
    Image array after histogram equalization.

Notes
-----
This function is adapted from [1]_ with the author's permission.

References
----------
.. [1] http://www.janeriksolem.net/histogram-equalization-with-python-and.html
.. [2] https://en.wikipedia.org/wiki/Histogram_equalization

)r   Frr   )r   arrayr   r   interpflatreshaper]   r    r   ru   r   )r+   rQ   maskcdfr9   outs         r   r	   r	   g  s    < xxD)25;F[25@
))EJJ
1C
++ekk
"C ::e11%++>U:KKr(   c                     US:X  a  U R                   R                  nUS:X  a/  [        R                  " U 5      n[        R                  " U 5      nX44$ U[
        ;   a  [
        U   u  p4U(       a  SnX44$ Uu  p4X44$ )a
  Return image intensity range (min, max) based on desired value type.

Parameters
----------
image : array
    Input image.
range_values : str or 2-tuple, optional
    The image intensity range is configured by this parameter.
    The possible values for this parameter are enumerated below.

    'image'
        Return image min/max as the range.
    'dtype'
        Return min/max of the image's dtype as the range.
    dtype-name
        Return intensity range based on desired `dtype`. Must be valid key
        in `DTYPE_RANGE`. Note: `image` is ignored for this range type.
    2-tuple
        Return `range_values` as min/max intensities. Note that there's no
        reason to use this function if you just want to specify the
        intensity range explicitly. This option is included for functions
        that use `intensity_range` to support all desired range types.

clip_negative : bool, optional
    If True, clip the negative range (i.e. return 0 for min intensity)
    even if the image dtype allows negative values.
r   r+   r   )r   typer   r2   r4   DTYPE_RANGE)r+   range_valuesr.   i_mini_maxs        r   intensity_ranger     s    8 w{{''wuu < 
	$"<0E < $<r(   c                 \   [        U 5      [        [        [        R                  4;   a  [
        R                  " U5      $ [        U 5      [         :X  a  U $ U [        ;   a!   [        R                  " U 5      R                   $ [        SU  S35      e! [         a    [        R                  s $ f = f)a  Determine the output dtype for rescale_intensity.

The dtype is determined according to the following rules:
- if ``dtype_or_range`` is a dtype, that is the output dtype.
- if ``dtype_or_range`` is a dtype string, that is the dtype used, unless
  it is not a NumPy data type (e.g. 'uint12' for 12-bit unsigned integers),
  in which case the data type that can contain it will be used
  (e.g. uint16 in this case).
- if ``dtype_or_range`` is a pair of values, the output data type will be
  ``_supported_float_type(image_dtype)``. This preserves float32 output for
  float32 inputs.

Parameters
----------
dtype_or_range : type, string, or 2-tuple of int/float
    The desired range for the output, expressed as either a NumPy dtype or
    as a (min, max) pair of numbers.
image_dtype : np.dtype
    The input image dtype.

Returns
-------
out_dtype : type
    The data type appropriate for the desired output.
zZIncorrect value for out_range, should be a valid image data type or a pair of values, got .)r   listtupler   ro   r   ru   r   r   	TypeErroruint16r0   )dtype_or_rangeimage_dtypes     r   _output_dtyper     s    4 NeRZZ88**;77Nt#$	88N+000
 --;,<A?
 	
	  	99	s   B B+*B+c           	      R   US;   a+  [        U R                  R                  U R                  5      nO[        X R                  5      n[        [        [        X5      5      u  pE[        [        [        XUS:  S95      u  pg[        R                  " [        R                  " XEXg/5      5      (       a  [        R                  " SSS9  [        R                  " XU5      n XE:w  a"  X-
  XT-
  -  n XU-
  -  U-   R                  U5      $ [        R                  " XU5      R                  U5      $ )a  Return image after stretching or shrinking its intensity levels.

The desired intensity range of the input and output, `in_range` and
`out_range` respectively, are used to stretch or shrink the intensity range
of the input image. See examples below.

Parameters
----------
image : array
    Image array.
in_range, out_range : str or 2-tuple, optional
    Min and max intensity values of input and output image.
    The possible values for this parameter are enumerated below.

    'image'
        Use image min/max as the intensity range.
    'dtype'
        Use min/max of the image's dtype as the intensity range.
    dtype-name
        Use intensity range based on desired `dtype`. Must be valid key
        in `DTYPE_RANGE`.
    2-tuple
        Use `range_values` as explicit min/max intensities.

Returns
-------
out : array
    Image array after rescaling its intensity. This image is the same dtype
    as the input image.

Notes
-----
.. versionchanged:: 0.17
    The dtype of the output array has changed to match the input dtype, or
    float if the output range is specified by a pair of values.

See Also
--------
equalize_hist

Examples
--------
By default, the min/max intensities of the input image are stretched to
the limits allowed by the image's dtype, since `in_range` defaults to
'image' and `out_range` defaults to 'dtype':

>>> image = np.array([51, 102, 153], dtype=np.uint8)
>>> rescale_intensity(image)
array([  0, 127, 255], dtype=uint8)

It's easy to accidentally convert an image dtype from uint8 to float:

>>> 1.0 * image
array([ 51., 102., 153.])

Use `rescale_intensity` to rescale to the proper range for float dtypes:

>>> image_float = 1.0 * image
>>> rescale_intensity(image_float)
array([0. , 0.5, 1. ])

To maintain the low contrast of the original, use the `in_range` parameter:

>>> rescale_intensity(image_float, in_range=(0, 255))
array([0.2, 0.4, 0.6])

If the min/max value of `in_range` is more/less than the min/max image
intensity, then the intensity levels are clipped:

>>> rescale_intensity(image_float, in_range=(0, 102))
array([0.5, 1. , 1. ])

If you have an image with signed integers but want to rescale the image to
just the positive range, use the `out_range` parameter. In that case, the
output dtype will be float:

>>> image = np.array([-10, 0, 10], dtype=np.int8)
>>> rescale_intensity(image, out_range=(0, 127))
array([  0. ,  63.5, 127. ])

To get the desired range with a specific dtype, use ``.astype()``:

>>> rescale_intensity(image, out_range=(0, 127)).astype(np.int8)
array([  0,  63, 127], dtype=int8)

If the input image is constant, the output will be clipped directly to the
output range:
>>> image = np.array([130, 130, 130], dtype=np.int32)
>>> rescale_intensity(image, out_range=(0, 127)).astype(np.int32)
array([127, 127, 127], dtype=int32)
)r   r+   r   r-   zOne or more intensity levels are NaN. Rescaling will broadcast NaN to the full image. Provide intensity levels yourself to avoid this. E.g. with np.nanmin(image), np.nanmax(image).r   )
stacklevel)r   r   r   mapr   r   r   anyisnanr   r_   clipr    )r+   in_range	out_range	out_dtypeiminimaxominomaxs           r   r
   r
     s    x &&!%++"2"2EKK@	!)[[9	UOE<=JDu	KJD 
vvbhhD/011

H 		
 GGE&E|$+.%,44Y??wwuD)00;;r(   c                 V    [         R                  " U S:  5      (       a  [        S5      eg )Nr   zxImage Correction methods work correctly only on images with non-negative values. Use skimage.exposure.rescale_intensity.)r   r   r0   )r+   s    r   _assert_non_negativer   b  s,    	vveai2
 	
 r(   c                     SU-  [         R                  " SSS5      U-  -  n[         R                  " [         R                  " U5      S5      R	                  S5      nX0   $ )z-LUT based implementation of gamma adjustment.   r   r/      uint8)r   rP   minimumrintr    )r+   gammagainluts       r   _adjust_gamma_u8r   k  sM    
*Aq#.%7
8C
**RWWS\3
'
.
.w
7C:r(   c                 4   US:  a  [        S5      eU R                  R                  nU[        R                  L a  [        XU5      nU$ [        U 5        [        [        U S5      S   [        U S5      S   -
  5      nX-  U-  U-  U-  R                  U5      nU$ )aN  Performs Gamma Correction on the input image.

Also known as Power Law Transform.
This function transforms the input image pixelwise according to the
equation ``O = I**gamma`` after scaling each pixel to the range 0 to 1.

Parameters
----------
image : ndarray
    Input image.
gamma : float, optional
    Non negative real number. Default value is 1.
gain : float, optional
    The constant multiplier. Default value is 1.

Returns
-------
out : ndarray
    Gamma corrected output image.

See Also
--------
adjust_log

Notes
-----
For gamma greater than 1, the histogram will shift towards left and
the output image will be darker than the input image.

For gamma less than 1, the histogram will shift towards right and
the output image will be brighter than the input image.

References
----------
.. [1] https://en.wikipedia.org/wiki/Gamma_correction

Examples
--------
>>> from skimage import data, exposure, img_as_float
>>> image = img_as_float(data.moon())
>>> gamma_corrected = exposure.adjust_gamma(image, 2)
>>> # Output is darker for gamma > 1
>>> image.mean() > gamma_corrected.mean()
True
r   z+Gamma should be a non-negative real number.Tr/   )
r0   r   r   r   r   r   r   r   r   r    )r+   r   r   r   r   scales         r   r   r   r  s    \ qyFGGKKEuT2 J 	U#l5$/2\%5Nq5QQR5(E1D8@@GJr(   c                 6   [        U 5        U R                  R                  n[        [	        U S5      S   [	        U S5      S   -
  5      nU(       a  SX-  -  S-
  U-  U-  nU" U5      $ [
        R                  " SX-  -   5      U-  U-  nUR                  U5      $ )a  Performs Logarithmic correction on the input image.

This function transforms the input image pixelwise according to the
equation ``O = gain*log(1 + I)`` after scaling each pixel to the range
0 to 1. For inverse logarithmic correction, the equation is
``O = gain*(2**I - 1)``.

Parameters
----------
image : ndarray
    Input image.
gain : float, optional
    The constant multiplier. Default value is 1.
inv : float, optional
    If True, it performs inverse logarithmic correction,
    else correction will be logarithmic. Defaults to False.

Returns
-------
out : ndarray
    Logarithm corrected output image.

See Also
--------
adjust_gamma

References
----------
.. [1] http://www.ece.ucsb.edu/Faculty/Manjunath/courses/ece178W03/EnhancePart1.pdf

Tr/   r   r   )r   r   r   r   r   r   log2r    )r+   r   invr   r   r   s         r   r   r     s    @ KKE,ud+A.eT1J11MMNE
U]#a'5047Sz
''!em#
$u
,t
3C::er(   c           	      r   [        U 5        U R                  R                  n[        [	        U S5      S   [	        U S5      S   -
  5      nU(       a1  SSS[
        R                  " X!X-  -
  -  5      -   -  -
  U-  nU" U5      $ SS[
        R                  " X!X-  -
  -  5      -   -  U-  nUR                  U5      $ )a  Performs Sigmoid Correction on the input image.

Also known as Contrast Adjustment.
This function transforms the input image pixelwise according to the
equation ``O = 1/(1 + exp*(gain*(cutoff - I)))`` after scaling each pixel
to the range 0 to 1.

Parameters
----------
image : ndarray
    Input image.
cutoff : float, optional
    Cutoff of the sigmoid function that shifts the characteristic curve
    in horizontal direction. Default value is 0.5.
gain : float, optional
    The constant multiplier in exponential's power of sigmoid function.
    Default value is 10.
inv : bool, optional
    If True, returns the negative sigmoid correction. Defaults to False.

Returns
-------
out : ndarray
    Sigmoid corrected output image.

See Also
--------
adjust_gamma

References
----------
.. [1] Gustav J. Braun, "Image Lightness Rescaling Using Sigmoidal Contrast
       Enhancement Functions",
       http://markfairchild.org/PDFs/PAP07.pdf

Tr/   r   )r   r   r   r   r   r   expr    )r+   cutoffr   r   r   r   r   s          r   r   r     s    J KKE,ud+A.eT1J11MMNE
1BFF4EM+A#BCCDDMSzBFF4EM#9:;;<
EC::er(   c                    [         R                  " U 5      n U R                  [        :X  a2  U R	                  5       S:H  =(       a    U R                  5       S:H  (       + $ U R                  S:X  a>  SSKJnJ	n  U R                  S   S:X  a  U" U 5      n U R                  S   S:X  a  U" U 5      n [        U SS9n[         R                  " XU/5      nUS   US   -
  US   US   -
  -  n	X:  $ )	a  Determine if an image is low contrast.

Parameters
----------
image : array-like
    The image under test.
fraction_threshold : float, optional
    The low contrast fraction threshold. An image is considered low-
    contrast when its range of brightness spans less than this
    fraction of its data type's full range. [1]_
lower_percentile : float, optional
    Disregard values below this percentile when computing image contrast.
upper_percentile : float, optional
    Disregard values above this percentile when computing image contrast.
method : str, optional
    The contrast determination method.  Right now the only available
    option is "linear".

Returns
-------
out : bool
    True when the image is determined to be low contrast.

Notes
-----
For boolean images, this function returns False only if all values are
the same (the method, threshold, and percentile arguments are ignored).

References
----------
.. [1] https://scikit-image.org/docs/dev/user_guide/data_types.html

Examples
--------
>>> image = np.linspace(0, 0.04, 100)
>>> is_low_contrast(image)
True
>>> image[-1] = 1
>>> is_low_contrast(image)
True
>>> is_low_contrast(image, upper_percentile=100)
False
r/   r   rZ   r   )rgb2grayrgba2rgbr[   Fr-   )r   
asanyarrayr   r   r4   r2   ndimcolorr   r   r]   r   
percentile)
r+   fraction_thresholdlower_percentileupper_percentilemethodr   r   dlimitsr   ratios
             r   is_low_contrastr     s    d MM% E{{dYY[A%=EIIK1,<>>zzQ.;;q>QUOE;;q>QUOE56G]]55E"FGFAY"wqzGAJ'>?E%%r(   r   )r   r+   F)r   )r   N)r+   Fr*   )r/   r/   )r/   F)rD   
   F)g?r/   c   linear)"numpyr   
util.dtyper   r   _sharedr   __all__rs   r   updateitemsr   float64r'   r:   rB   rJ   rT   rV   channel_as_last_axisr   ra   r   r	   r   r   r
   r   r   r   r   r   r    r(   r   <module>r      s    2      M9J9J9LM M      D!RZZ( 
D.!b"J 66;UMQU 7Up F* Z'LT(V,
^t<n
<~)X.f C&r(   