
    9i$                     :    S SK rSSKJr  SSKJr  SSKJr  SS jrg)	    N   )_supported_float_type)
rank_order   )reconstruction_loopc           
      J   [        U R                  5      [        UR                  5      :X  d   eUS:X  a(  [        R                  " X:  5      (       a  [	        S5      eUS:X  a(  [        R                  " X:  5      (       a  [	        S5      eUc(  [        R
                  " S/U R                  -  [        S9nOUR                  [        SS	9nUco  [        UR                   Vs/ s H
  oUS
-  S:H  PM     sn5      (       d  [	        S5      e[        R                  " UR                   Vs/ s H  oUS
-  PM	     sn5      nO{UR                  UR                  :w  a  [	        S5      e[        [        XCR                  5       VVs/ s H  u  peSUs=:*  =(       a    U:  Os  PM     snn5      (       d  [	        S5      eSU[        S U 5       5      '   [        R                  " U R                  S-   [        S9n[        R                  " U R                  5      [        R                  " UR                  5      S-
  -   USS& S
US'   [        S [        X@R                  5       5       5      nUS:X  a  [        R                  " U 5      n	O,US:X  a  [        R                  " U 5      n	O[	        SU S35      e[!        UR"                  5      n
[        R$                  " XyU
S9nXS/UQ7'   XS/UQ7'   UR&                  n[        R(                  " [        R*                  " U* 5      [        R,                  5      n[        R"                  " UR.                  R1                  5       5      n[        R                  " UR2                  SS 5      UR"                  R4                  -  nUR2                  S   UR"                  R4                  -  n[        R6                  [        UR                  U5       VVs/ s H  u  pV[9        U* XV-
  5      PM     snn   nUSS2U4   R;                  5       n[        R                  " U Vs/ s H  n[        R<                  " UU-  5      PM     snU5      nUR?                  S5      n[        R@                  " U5      R                  USS	9nUS:X  a  USSS2   n[        R$                  " USU5      n[        R$                  " USU5      nUSS UUSS '   USS UUSS '   US:X  a  [C        U5      u  nnOUS:X  a  [C        U* 5      u  nnU* nUS   nWR                  USS	9n[E        UUUUUU5        WUSU    n[        R                  " U R                  5      [        R                  " UR                  5      S-
  -   Ul        UU   $ s  snf s  snf s  snnf s  snnf s  snf )ae  Perform a morphological reconstruction of an image.

Morphological reconstruction by dilation is similar to basic morphological
dilation: high-intensity values will replace nearby low-intensity values.
The basic dilation operator, however, uses a footprint to
determine how far a value in the input image can spread. In contrast,
reconstruction uses two images: a "seed" image, which specifies the values
that spread, and a "mask" image, which gives the maximum allowed value at
each pixel. The mask image, like the footprint, limits the spread
of high-intensity values. Reconstruction by erosion is simply the inverse:
low-intensity values spread from the seed image and are limited by the mask
image, which represents the minimum allowed value.

Alternatively, you can think of reconstruction as a way to isolate the
connected regions of an image. For dilation, reconstruction connects
regions marked by local maxima in the seed image: neighboring pixels
less-than-or-equal-to those seeds are connected to the seeded region.
Local maxima with values larger than the seed image will get truncated to
the seed value.

Parameters
----------
seed : ndarray
    The seed image (a.k.a. marker image), which specifies the values that
    are dilated or eroded.
mask : ndarray
    The maximum (dilation) / minimum (erosion) allowed value at each pixel.
method : {'dilation'|'erosion'}, optional
    Perform reconstruction by dilation or erosion. In dilation (or
    erosion), the seed image is dilated (or eroded) until limited by the
    mask image. For dilation, each seed value must be less than or equal
    to the corresponding mask value; for erosion, the reverse is true.
    Default is 'dilation'.
footprint : ndarray, optional
    The neighborhood expressed as an n-D array of 1's and 0's.
    Default is the n-D square of radius equal to 1 (i.e. a 3x3 square
    for 2D images, a 3x3x3 cube for 3D images, etc.)
offset : ndarray, optional
    The coordinates of the center of the footprint.
    Default is located on the geometrical center of the footprint, in that
    case footprint dimensions must be odd.

Returns
-------
reconstructed : ndarray
    The result of morphological reconstruction.

Examples
--------
>>> import numpy as np
>>> from skimage.morphology import reconstruction

First, we create a sinusoidal mask image with peaks at middle and ends.

>>> x = np.linspace(0, 4 * np.pi)
>>> y_mask = np.cos(x)

Then, we create a seed image initialized to the minimum mask value (for
reconstruction by dilation, min-intensity values don't spread) and add
"seeds" to the left and right peak, but at a fraction of peak value (1).

>>> y_seed = y_mask.min() * np.ones_like(x)
>>> y_seed[0] = 0.5
>>> y_seed[-1] = 0
>>> y_rec = reconstruction(y_seed, y_mask)

The reconstructed image (or curve, in this case) is exactly the same as the
mask image, except that the peaks are truncated to 0.5 and 0. The middle
peak disappears completely: Since there were no seed values in this peak
region, its reconstructed value is truncated to the surrounding value (-1).

As a more practical example, we try to extract the bright features of an
image by subtracting a background image created by reconstruction.

>>> y, x = np.mgrid[:20:0.5, :20:0.5]
>>> bumps = np.sin(x) + np.sin(y)

To create the background image, set the mask image to the original image,
and the seed image to the original image with an intensity offset, `h`.

>>> h = 0.3
>>> seed = bumps - h
>>> background = reconstruction(seed, bumps)

The resulting reconstructed image looks exactly like the original image,
but with the peaks of the bumps cut off. Subtracting this reconstructed
image from the original image leaves just the peaks of the bumps

>>> hdome = bumps - background

This operation is known as the h-dome of the image and leaves features
of height `h` in the subtracted image.

Notes
-----
The algorithm is taken from [1]_. Applications for grayscale reconstruction
are discussed in [2]_ and [3]_.

References
----------
.. [1] Robinson, "Efficient morphological reconstruction: a downhill
       filter", Pattern Recognition Letters 25 (2004) 1759-1767.
.. [2] Vincent, L., "Morphological Grayscale Reconstruction in Image
       Analysis: Applications and Efficient Algorithms", IEEE Transactions
       on Image Processing (1993)
.. [3] Soille, P., "Morphological Image Analysis: Principles and
       Applications", Chapter 6, 2nd edition (2003), ISBN 3540429883.
dilationz`Intensity of seed image must be less than that of the mask image for reconstruction by dilation.erosionzbIntensity of seed image must be greater than that of the mask image for reconstruction by erosion.N   )dtypeT)copyr   r   z$Footprint dimensions must all be oddz)Offset and footprint ndims must be equal.r   z(Offset must be included inside footprintFc              3   >   #    U  H  n[        XS -   5      v   M     g7f)r   Nslice).0ds     b/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/skimage/morphology/grayreconstruct.py	<genexpr>!reconstruction.<locals>.<genexpr>   s     4VE!UOOVs   c              3   B   #    U  H  u  p[        XU-   5      v   M     g 7f)Nr   )r   oss      r   r   r      s     N6Mda%q5//6Ms   zBReconstruction method can be one of 'erosion' or 'dilation'. Got 'z'.)#tupleshapenpany
ValueErroronesndimboolastypeallarrayzipzerosintminmaxr   r   fullsizeresult_typemin_scalar_typeint32charupperstridesitemsizemgridr   	transposesumreshapeargsortr   r   )seedmaskmethod	footprintoffsetr   r   dimsinside_slices	pad_valuefloat_dtypeimagesisizesigned_int_dtypeunsigned_int_dtypevalue_strideimage_stridefootprint_mgridfootprint_offsetsfootprint_offset
nb_stridesindex_sortedprevnext
value_rank	value_mapstartrec_imgs                               r   reconstructionrR      s   Z djj 1111t{ 3 3@
 	
 
9	!4!4?
 	

 GGQC$))O48	$$T$5	~	81EQJ899CDD9??;?a6?;<;;)..(HIIS-IJ-ITQQ!ZZaZ-IJKKGHH 9>Ie4V445 88DIIM-Dxx

#rxx	'@1'DEDHDGNc&**6MNNM FF4L		9	FF4L	##)(".
 	
 (

3KWWTK8F"&A"&A KKE~~b&8&8%&@"((K"2"7"7"="="?@ 88FNN12./6<<3H3HHL>>!$(=(==Lhh&))//6&BC&Bdar15	&BCO (95??A %6	
$5  FF<"223$5	
 	J ^^BF ::f%,,-=E,JL#DbD) 775"./D775"./D)#2.Dab	*12.Dcr	  *6 2
I	9	 *F7 3
IJ	OE""#5E"BJ
D$
E<P 
=L12GHHTZZ(BHHY__,E,IJGM=!!e 9; KN 	D	
s    V
&V>!V
"V.#V )r	   NN)	numpyr   _shared.utilsr   filters._rank_orderr   _grayreconstructr   rR        r   <module>rY      s     1 , 1Q"rX   