
    9i$                     n    S SK rS SKJr  S SKJr  SSKJr  SSKJ	r	  SSK
Jr  S rSSS	SS
S.S jrSS jrg)    N)sparse)csgraph   )_raveled_offsets_and_distances)	map_array)_safe_downcast_indicesc                 8    [         R                  " X-
  5      U-  $ )a>  A default edge function for complete image graphs.

A pixel graph on an image with no edge values and no mask is a very
boring regular lattice, so we define a default edge weight to be the
absolute difference between values *weighted* by the distance
between them.

Parameters
----------
values0 : array
    The pixel values for each node.
values1 : array
    The pixel values for each neighbor.
distances : array
    The distance between each node and its neighbor.

Returns
-------
edge_values : array of float
    The computed values: abs(values0 - values1) * distances.
)npabs)values0values1	distancess      T/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/skimage/graph/_graph.py_weighted_abs_diffr   	   s    , 66'#$y00       matrix)maskedge_functionconnectivityspacingsparse_typec                   Uc0  U R                   [        :X  a  U nO[        R                  " U [        S9nUc  U R                   [        :X  a  S nO[        n[        R
                  " USSSS9n[        R                  " U5      n[        UR                  X4S9u  pUSS2[        R                  4   U-   n
[        R                  " XR                  5      n[        R                  " U5      n[        R                  " UR                  5      n[        XU5      nUR                  S	5      U
   n[        R                  " USS
9n[        R                   " UU5      n[        R                   " UU5      nX   nX   n[        UX5      nU R                  S	5      nU" UU   UU   U5      nUR                  n["        R$                  " UUU44UU4S9nUS:X  a  ["        R&                  " U5      nUU4$ US:w  a  SU 3n[)        U5      eUU4$ )a4  Create an adjacency graph of pixels in an image.

Pixels where the mask is True are nodes in the returned graph, and they are
connected by edges to their neighbors according to the connectivity
parameter. By default, the *value* of an edge when a mask is given, or when
the image is itself the mask, is the Euclidean distance between the pixels.

However, if an int- or float-valued image is given with no mask, the value
of the edges is the absolute difference in intensity between adjacent
pixels, weighted by the Euclidean distance.

Parameters
----------
image : array
    The input image. If the image is of type bool, it will be used as the
    mask as well.
mask : array of bool
    Which pixels to use. If None, the graph for the whole image is used.
edge_function : callable
    A function taking an array of pixel values, and an array of neighbor
    pixel values, and an array of distances, and returning a value for the
    edge. If no function is given, the value of an edge is just the
    distance.
connectivity : int
    The square connectivity of the pixel neighborhood: the number of
    orthogonal steps allowed to consider a pixel a neighbor. See
    `scipy.ndimage.generate_binary_structure` for details.
spacing : tuple of float
    The spacing between pixels along each axis.
sparse_type : {"matrix", "array"}, optional
    The return type of `graph`, either `scipy.sparse.csr_array` or
    `scipy.sparse.csr_matrix` (default).

Returns
-------
graph : scipy.sparse.csr_matrix or scipy.sparse.csr_array
    A sparse adjacency matrix in which entry (i, j) is 1 if nodes i and j
    are neighbors, 0 otherwise. Depending on `sparse_type`, this can be
    returned as a `scipy.sparse.csr_array`.
nodes : array of int
    The nodes of the graph. These correspond to the raveled indices of the
    nonzero pixels in the mask.
N)dtypec                     U$ )N )xyr   s      r   r   "pixel_graph.<locals>.edge_function_   s      r   r   constantF)modeconstant_values)r   r   axis)shaper   arrayz/`sparse_type` must be 'array' or 'matrix', got )r   boolr
   	ones_liker   padflatnonzeror   r&   newaxisbroadcast_toarangesizer   reshapesumrepeatr   	csr_array
csr_matrix
ValueError)imager   r   r   r   r   paddednodes_paddedneighbor_offsets_paddeddistances_paddedneighbors_paddedneighbor_distances_fullnodesnodes_sequential	neighborsneighbors_masknum_neighborsindicesindices_sequentialneighbor_indicesneighbor_distancesneighbor_indices_sequentialimage_rdatamgraphmsgs                              r   pixel_graphrL   "   s   h |;;$D<<T2D;;$! /M2 VVD!*eDF>>&)L0N<1- $ArzzM25LL oo.>@V@VWNN4 Eyy, *%@I^^B'(89NFF>2Mii}-G#3]C 00@"+,<e"VmmBG'"235GD 	A	"$?@A!QE h!!%(
 %<	 
	?}Mo%<r   c                    Uc#  [         R                  " U R                  S   5      nUc  SnO[        SU R                  S   U-  5      n[	        U [         R
                  S5      u  U l        U l        [         R                  " U R                  S   5      n/ n[         R                  " XT5       HS  n[        R                  " U SUS9n[         R                  " U5      n	UR                  [         R                  " U	SS95        MU     [         R                  " U5      n
[         R                  " U
5      n[         R                   " X   5      nXU      nUb  [         R"                  " X5      nX4$ UnX4$ )a4  Find the pixel with the highest closeness centrality.

Closeness centrality is the inverse of the total sum of shortest distances
from a node to every other node.

Parameters
----------
graph : scipy.sparse.csr_array or scipy.sparse.csr_matrix
    The sparse representation of the graph.
nodes : array of int
    The raveled index of each node in graph in the image. If not provided,
    the returned value will be the index in the input graph.
shape : tuple of int
    The shape of the image in which the nodes are embedded. If provided,
    the returned coordinates are a NumPy multi-index of the same
    dimensionality as the input shape. Otherwise, the returned coordinate
    is the raveled index provided in `nodes`.
partition_size : int
    This function computes the shortest path distance between every pair
    of nodes in the graph. This can result in a very large (N*N) matrix.
    As a simple performance tweak, the distance values are computed in
    lots of `partition_size`, resulting in a memory requirement of only
    partition_size*N.

Returns
-------
position : int or tuple of int
    If shape is given, the coordinate of the central pixel in the image.
    Otherwise, the raveled index of that pixel.
distances : array of float
    The total sum of distances from each node to each other reachable
    node.
r   r   r   z"index values too large for csgraphF)directedrB   r$   )r
   r.   r&   maxr   int32rB   indptrarray_splitr   shortest_path
nan_to_numappendr1   concatenater+   argminunravel_index)rJ   r=   r&   partition_size
num_splitsidxstotal_shortest_path_len_list	partitionshortest_pathsshortest_paths_no_inftotal_shortest_path_lennonzeromin_spraveled_indexcentrals                  r   central_pixelre      sA   D }		%++a.)
EKKNn<=
"8rxx=#EM5< 99U[[^$D#% ^^D5	 ..uuiX "n =$++BFF3Hq,QR 6 !nn-IJnn45GYY.78F&/*M""=8 ++  ++r   )NNd   )numpyr
   scipyr   scipy.sparser   morphology._utilr   util._map_arrayr   'segmentation.random_walker_segmentationr   r   rL   re   r   r   r   <module>rm      s9        = ' L18 
~B9,r   