Ë
    îì�i�(  ã                   ór   — d dl Zd dlZd dlmZ d dlmZ ddlm	Z	m
Z
 dd„Z	 	 	 	 dddœd„Zd	„ Zd
„ Zd„ Zd„ Zy)é    N)Úlinalg)ÚSCIPY_GE_1_17_0_DEV0é   )Ú_ncutÚ_ncut_cyc                 ó¾  — |s|j                  «       }|j                  d¬«      D ���cg c]  \  }}}|d   |k\  sŒ||f‘Œ }}}}|j                  |«       t        j                  |«      }t        j                  | j                  «       dz   | j                  ¬«      }	t        |«      D ](  \  }
}|D ]  }|j                  |   d   D ]  }|
|	|<   Œ	 Œ  Œ* |	|    S c c}}}w )a±  Combine regions separated by weight less than threshold.

    Given an image's labels and its RAG, output new labels by
    combining regions whose nodes are separated by a weight less
    than the given threshold.

    Parameters
    ----------
    labels : ndarray
        The array of labels.
    rag : RAG
        The region adjacency graph.
    thresh : float
        The threshold. Regions connected by edges with smaller weights are
        combined.
    in_place : bool
        If set, modifies `rag` in place. The function will remove the edges
        with weights less that `thresh`. If set to `False` the function
        makes a copy of `rag` before proceeding.

    Returns
    -------
    out : ndarray
        The new labelled array.

    Examples
    --------
    >>> from skimage import data, segmentation, graph
    >>> img = data.astronaut()
    >>> labels = segmentation.slic(img)
    >>> rag = graph.rag_mean_color(img, labels)
    >>> new_labels = graph.cut_threshold(labels, rag, 10)

    References
    ----------
    .. [1] Alain Tremeau and Philippe Colantoni
           "Regions Adjacency Graph Applied To Color Image Segmentation"
           :DOI:`10.1109/83.841950`

    T©ÚdataÚweightr   ©ÚdtypeÚlabels)ÚcopyÚedgesÚremove_edges_fromÚnxÚconnected_componentsÚnpÚarangeÚmaxr   Ú	enumerateÚnodes)r   ÚragÚthreshÚin_placeÚxÚyÚdÚ	to_removeÚcompsÚ	map_arrayÚir   ÚnodeÚlabels                 ú}/var/www/ITT-ServiceAPI-Services/ITT-Document-Scan/mrzscanner/mrzenv/lib/python3.12/site-packages/skimage/graph/_graph_cut.pyÚcut_thresholdr&   	   sê   € ñR Ø�h‰h‹jˆð (+§y¡y°d yÓ';×UÐU™G˜A˜q !¸qÀ¹{ÈfÓ?T�!�Q’ÐU€IÒUØ×Ñ˜)Ô$ä×#Ñ# CÓ(€Eô
 —	‘	˜&Ÿ*™*›,¨Ñ*°&·,±,Ô?€IÜ˜eÓ$ò %‰ˆˆ5Øò 	%ˆDØŸ™ 4™¨Ñ2ò %�Ø#$�	˜%Ò ñ%ñ	%ð%ð
 �VÑÐùô Vs
   ©C»C)Úrngc                óŠ  — t         j                  j                  |«      }|s|j                  «       }|j	                  «       D ]  }|j                  |||¬«       Œ t        ||||«       t        j                  | j                  «       dz   | j                  ¬«      }|j	                  d¬«      D ]  \  }	}
|
d   ||
d   <   Œ ||    S )aƒ  Perform Normalized Graph cut on the Region Adjacency Graph.

    Given an image's labels and its similarity RAG, recursively perform
    a 2-way normalized cut on it. All nodes belonging to a subgraph
    that cannot be cut further are assigned a unique label in the
    output.

    Parameters
    ----------
    labels : ndarray
        The array of labels.
    rag : RAG
        The region adjacency graph.
    thresh : float
        The threshold. A subgraph won't be further subdivided if the
        value of the N-cut exceeds `thresh`.
    num_cuts : int
        The number or N-cuts to perform before determining the optimal one.
    in_place : bool
        If set, modifies `rag` in place. For each node `n` the function will
        set a new attribute ``rag.nodes[n]['ncut label']``.
    max_edge : float, optional
        The maximum possible value of an edge in the RAG. This corresponds to
        an edge between identical regions. This is used to put self
        edges in the RAG.
    rng : {`numpy.random.Generator`, int}, optional
        Pseudo-random number generator.
        By default, a PCG64 generator is used (see :func:`numpy.random.default_rng`).
        If `rng` is an int, it is used to seed the generator.

        The `rng` is used to determine the starting point
        of `scipy.sparse.linalg.eigsh`.

    Returns
    -------
    out : ndarray
        The new labeled array.

    Examples
    --------
    >>> from skimage import data, segmentation, graph
    >>> img = data.astronaut()
    >>> labels = segmentation.slic(img)
    >>> rag = graph.rag_mean_color(img, labels, mode='similarity')
    >>> new_labels = graph.cut_normalized(labels, rag)

    References
    ----------
    .. [1] Shi, J.; Malik, J., "Normalized cuts and image segmentation",
           Pattern Analysis and Machine Intelligence,
           IEEE Transactions on, vol. 22, no. 8, pp. 888-905, August 2000.

    )r   r   r   Tr	   ú
ncut labelr   )
r   ÚrandomÚdefault_rngr   r   Úadd_edgeÚ_ncut_relabelÚzerosr   r   )r   r   r   Únum_cutsr   Úmax_edger'   r#   r!   Únr   s              r%   Úcut_normalizedr2   G   sº   € ô~ �)‰)×
Ñ
 Ó
$€CÙØ�h‰h‹jˆà—	‘	“ò 2ˆØ�‰�T˜4¨ˆÕ1ð2ô �#�v˜x¨Ô-ä—‘˜Ÿ™›¨Ñ)°·±Ô>€Ià—	‘	˜t�	Ó$ò 1‰ˆˆ1Ø!" <¡ˆ	�!�H‘+Òð1ð �VÑÐó    c                 ó*  — t        |j                  «       «      D ��cg c]  \  }}| |   sŒ|‘Œ }}}t        |j                  «       «      D ��cg c]  \  }}| |   rŒ|‘Œ }}}|j                  |«      }|j                  |«      }||fS c c}}w c c}}w )aR  Compute resulting subgraphs from given bi-partition.

    Parameters
    ----------
    cut : array
        A array of booleans. Elements set to `True` belong to one
        set.
    rag : RAG
        The Region Adjacency Graph.

    Returns
    -------
    sub1, sub2 : RAG
        The two resulting subgraphs from the bi-partition.
    )r   r   Úsubgraph)Úcutr   r"   r1   Únodes1Únodes2Úsub1Úsub2s           r%   Úpartition_by_cutr;   —   s‚   € ô4 & c§i¡i£kÓ2×=‘D�A�q°c¸!³fŠaÐ=€FÑ=Ü% c§i¡i£kÓ2×A‘D�A�q¸#¸a»&ŠaÐA€FÑAà�<‰<˜Ó€DØ�<‰<˜Ó€Dà�ˆ:Ðùó >ùÛAs   �B	«B	ÁBÁBc                 ó\  — t         j                  }| j                  «       }| j                  «       }t        j                  | t
        ¬«      }t        j                  ||«      r||fS t        j                  |||d¬«      D ](  }| |kD  }	t        j                  |	||«      }
|
|k  sŒ%|	}|
}Œ* ||fS )aö  Threshold an eigenvector evenly, to determine minimum ncut.

    Parameters
    ----------
    ev : array
        The eigenvector to threshold.
    d : ndarray
        The diagonal matrix of the graph.
    w : ndarray
        The weight matrix of the graph.
    num_cuts : int
        The number of evenly spaced thresholds to check for.

    Returns
    -------
    mask : array
        The array of booleans which denotes the bi-partition.
    mcut : float
        The value of the minimum ncut.
    r   F)Úendpoint)
r   ÚinfÚminr   Ú
zeros_likeÚboolÚallcloseÚlinspacer   Ú	ncut_cost)Úevr   Úwr/   ÚmcutÚmnÚmxÚmin_maskÚtÚmaskÚcosts              r%   Úget_min_ncutrN   º   s¥   € ô* �6‰6€DØ	�‰‹€BØ	�‰‹€Bô
 �}‰}˜R¤tÔ,€HÜ	‡{�{�2�rÔØ˜ˆ~Ðô �[‰[˜˜R °EÔ:ò ˆØ�A‰vˆÜ�‰˜t Q¨Ó*ˆØ�$‹;ØˆHØ‰Dðð �Tˆ>Ðr3   c                 óž   — t        | j                  «       «      }| j                  |   d   d   }| j                  d¬«      D ]
  \  }}|||<   Œ y)aq  Assign a unique integer to the given attribute in the RAG.

    This function assumes that all labels in `rag` are unique. It
    picks up a random label from them and assigns it to the `attr_name`
    attribute of all the nodes.

    rag : RAG
        The Region Adjacency Graph.
    attr_name : string
        The attribute to which a unique integer is assigned.
    r   r   Tr	   N)r?   r   )r   Ú	attr_namer#   Ú	new_labelr1   r   s         r%   Ú
_label_allrR   æ   sR   € ô ˆs�y‰y‹{Ó€DØ—	‘	˜$‘ Ñ)¨!Ñ,€IØ—	‘	˜t�	Ó$ò !‰ˆˆ1Ø ˆˆ)Šñ!r3   c           
      ó,  — t        j                  | «      \  }}|j                  d   }|dkD  �r[||k7  j                  dkD  �rH|j	                  «       }t        j                  t        j                  |j                  |j                  ¬«      |j                  ¬«      |_        |||z
  z  |z  }|j                  |j                  d   «      }	t        rd|ini }
t        j                  |fd|	t        d|dz
  «      dœ|
¤Ž\  }}t        j                  |«      t        j                  |«      }}t        j                   |«      }|dd…|f   }t#        ||||«      \  }}||k  r,t%        || «      \  }}t'        ||||«       t'        ||||«       yt)        | d	«       y)
aÊ  Perform Normalized Graph cut on the Region Adjacency Graph.

    Recursively partition the graph into 2, until further subdivision
    yields a cut greater than `thresh` or such a cut cannot be computed.
    For such a subgraph, indices to labels of all its nodes map to a single
    unique value.

    Parameters
    ----------
    rag : RAG
        The region adjacency graph.
    thresh : float
        The threshold. A subgraph won't be further subdivided if the
        value of the N-cut exceeds `thresh`.
    num_cuts : int
        The number or N-cuts to perform before determining the optimal one.
    random_generator : `numpy.random.Generator`
        Provides initial values for eigenvalue solver.
    r   é   )Úoutr'   ÚSMéd   )ÚwhichÚv0ÚkNr)   )r   ÚDW_matricesÚshapeÚnnzr   r   Ú
reciprocalÚsqrtr
   r*   r   r   Úeigshr?   Úrealr   Úargmin2rN   r;   r-   rR   )r   r   r/   Úrandom_generatorr   rF   ÚmÚd2ÚArY   Úrng_kwÚvalsÚvectorsÚindex2rE   Úcut_maskrG   r9   r:   s                      r%   r-   r-   ø   sp  € ô( ×Ñ˜SÓ!�D€A€qØ	�‰�‰
€Aà	ˆA‹�A˜‘F—<‘< !Ó#ð �V‰V‹Xˆô —-‘-¤§¡¨¯©°R·W±WÔ =À2Ç7Á7ÔKˆŒð �!�a‘%‰L˜2Ñˆà×$Ñ$ Q§W¡W¨Q¡ZÓ0ˆõ /C�%Ð)Ñ*ÈˆÜŸ™ QÐW¨d°r¼SÀÀaÈ!Áe»_ÑWÐPVÑW‰ˆˆgô Ÿ™ ›¤r§w¡w¨wÓ'7ˆgˆÜ×!Ñ! $Ó'ˆØ’Q˜�YÑˆä% b¨!¨Q°Ó9‰ˆ�$Ø�&Š=ô *¨(°CÓ8‰JˆD�$ä˜$ ¨Ð2BÔCÜ˜$ ¨Ð2BÔCØô ˆs�LÕ!r3   )T)gü©ñÒMbP?é
   Tg      ð?)Únetworkxr   Únumpyr   Úscipy.sparser   Úskimage._shared.compatr   Ú r   r   r&   r2   r;   rN   rR   r-   © r3   r%   ú<module>rs      sQ   ðÛ Û Ý å 7ß ó;ðB ØØØðMð 	ôMò` òF)òX!ó$G"r3   