
    1-Ph                     4    d dl ZddlmZmZ d	defdZd
dZdS )    N   )	map_arrayArrayMapFreturn_mappingc                 
   | j         |j         k    rt          d| j          d|j                    t          |           \  }}}t          |          \  }}}|                                t	          j        d          z   }||z  |z   }	t          |	          \  }
}}|s|
S t	          j        |	          }t	          j        ||          \  }}t          |j	        ||                   }t          |j	        ||                   }|
||fS )a  Return the join of the two input segmentations.

    The join J of S1 and S2 is defined as the segmentation in which two
    voxels are in the same segment if and only if they are in the same
    segment in *both* S1 and S2.

    Parameters
    ----------
    s1, s2 : numpy arrays
        s1 and s2 are label fields of the same shape.
    return_mapping : bool, optional
        If true, return mappings for joined segmentation labels to the original labels.

    Returns
    -------
    j : numpy array
        The join segmentation of s1 and s2.
    map_j_to_s1 : ArrayMap, optional
        Mapping from labels of the joined segmentation j to labels of s1.
    map_j_to_s2 : ArrayMap, optional
        Mapping from labels of the joined segmentation j to labels of s2.

    Examples
    --------
    >>> from skimage.segmentation import join_segmentations
    >>> s1 = np.array([[0, 0, 1, 1],
    ...                [0, 2, 1, 1],
    ...                [2, 2, 2, 1]])
    >>> s2 = np.array([[0, 1, 1, 0],
    ...                [0, 1, 1, 0],
    ...                [0, 1, 1, 1]])
    >>> join_segmentations(s1, s2)
    array([[0, 1, 3, 2],
           [0, 5, 3, 2],
           [4, 5, 5, 3]])
    >>> j, m1, m2 = join_segmentations(s1, s2, return_mapping=True)
    >>> m1
    ArrayMap(array([0, 1, 2, 3, 4, 5]), array([0, 0, 1, 1, 2, 2]))
    >>> np.all(m1[j] == s1)
    True
    >>> np.all(m2[j] == s2)
    True
    z8Cannot join segmentations of different shape. s1.shape: z, s2.shape:    )
shape
ValueErrorrelabel_sequentialmaxnpuint8uniquedivmodr   	in_values)s1s2r   s1_relabeled_backward_map1s2_relabeledbackward_map2factor	j_initialjmap_j_to_j_initiallabels_jlabels_s1_relabeledlabels_s2_relabeledmap_j_to_s1map_j_to_s2s                    Z/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/skimage/segmentation/_join.pyjoin_segmentationsr#      s0   X 
x28:: :/1x: :
 
 	

 &8%;%;"L!]%7%;%;"L!]VVXX#F%4I1)<<Aq
 y##H/1y6/J/J,,$m4G&H K $m4G&H K k;&&    r   c           
      V   |dk    rt          d          t          j        |           dk     rt          d          t          |          }t          j        |           }|d         dk    r>t          j        dgt          j        ||t          |          z   dz
            g          }n%t          j        ||t          |          z             }| j        }|j	        dvrt          d          t          j        |d                   }|j        |j        k     r|}n(|d         t          j        |          j        k     r|}n|}t          j        | j        |          }|                    |          }t%          | |||	           t'          ||          }t'          ||          }	|||	fS )
u
  Relabel arbitrary labels to {`offset`, ... `offset` + number_of_labels}.

    This function also returns the forward map (mapping the original labels to
    the reduced labels) and the inverse map (mapping the reduced labels back
    to the original ones).

    Parameters
    ----------
    label_field : numpy array of int, arbitrary shape
        An array of labels, which must be non-negative integers.
    offset : int, optional
        The return labels will start at `offset`, which should be
        strictly positive.

    Returns
    -------
    relabeled : numpy array of int, same shape as `label_field`
        The input label field with labels mapped to
        {offset, ..., number_of_labels + offset - 1}.
        The data type will be the same as `label_field`, except when
        offset + number_of_labels causes overflow of the current data type.
    forward_map : ArrayMap
        The map from the original label space to the returned label
        space. Can be used to re-apply the same mapping. See examples
        for usage. The output data type will be the same as `relabeled`.
    inverse_map : ArrayMap
        The map from the new label space to the original space. This
        can be used to reconstruct the original label field from the
        relabeled one. The output data type will be the same as `label_field`.

    Notes
    -----
    The label 0 is assumed to denote the background and is never remapped.

    The forward map can be extremely big for some inputs, since its
    length is given by the maximum of the label field. However, in most
    situations, ``label_field.max()`` is much smaller than
    ``label_field.size``, and in these cases the forward map is
    guaranteed to be smaller than either the input or output images.

    Examples
    --------
    >>> from skimage.segmentation import relabel_sequential
    >>> label_field = np.array([1, 1, 5, 5, 8, 99, 42])
    >>> relab, fw, inv = relabel_sequential(label_field)
    >>> relab
    array([1, 1, 2, 2, 3, 5, 4])
    >>> print(fw)
    ArrayMap:
      1 → 1
      5 → 2
      8 → 3
      42 → 4
      99 → 5
    >>> np.array(fw)
    array([0, 1, 0, 0, 0, 2, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
           0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0,
           0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
           0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
           0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5])
    >>> np.array(inv)
    array([ 0,  1,  5,  8, 42, 99])
    >>> (fw[label_field] == relab).all()
    True
    >>> (inv[relab] == label_field).all()
    True
    >>> relab, fw, inv = relabel_sequential(label_field, offset=5)
    >>> relab
    array([5, 5, 6, 6, 7, 9, 8])
    r   z!Offset must be strictly positive.z3Cannot relabel array that contains negative values.r   iuz&label_field must have an integer dtype)dtype)out)r
   r   minintr   concatenatearangelenr(   kind	TypeErrormin_scalar_typeitemsizeiinfor   emptyr	   astyper   r   )
label_fieldoffsetin_valsout_vals
input_typerequired_typeoutput_type	out_arrayfw_mapinv_maps
             r"   r   r   L   s   N {{<===	vkQNOOO[[Fi$$GqzQ>A3	&&3w<<:ORS:S(T(T"UVV9VVc'll%:;;"Jd""@AAA &x|44M]333#B<"(:..222$KK'K*+>>>I{++Hk7H)<<<<gx((Fx))Gfg%%r$   )F)r   )numpyr   util._map_arrayr   r   boolr#   r    r$   r"   <module>rD      st        1 1 1 1 1 1 1 1C' C't C' C' C' C'Ll& l& l& l& l& l&r$   