§
    )Áôg '  ã                   ót   — d dl Zd dlmZ ddlmZ ddlmZm	Z	m
Z
 ddlmZmZ ddlmZ d„ Zdd„Z	 	 	 	 dd„ZdS )é    N)Úndimageé   )Ú_supported_float_type)ÚdilationÚerosionÚfootprint_rectangle)Úimg_as_floatÚview_as_windows)Úgray2rgbc                 ór  — | j         }t          j        | j        ¦  «        j        }t          j        d„ | j        D ¦   «         | j        ¦  «        }t          ddd¦  «        f|z  }| ||<   t          j        |j        t          ¬¦  «        }d||<   |||<   t          t          j        |dd¬¦  «        d	|z  ¦  «        }t          j        |¦  «        }t          j        |j        ¦  «        D ]N}||         rDt          j        ||                              ¦   «         ¦  «        }	t!          |	¦  «        dk    rd
||<   ŒO|S )a#  See ``find_boundaries(..., mode='subpixel')``.

    Notes
    -----
    This function puts in an empty row and column between each *actual*
    row and column of the image, for a corresponding shape of ``2s - 1``
    for every image dimension of size ``s``. These "interstitial" rows
    and columns are filled as ``True`` if they separate two labels in
    `label_img`, ``False`` otherwise.

    I used ``view_as_windows`` to get the neighborhood of each pixel.
    Then I check whether there are two labels or more in that
    neighborhood.
    c                 ó   — g | ]
}d |z  dz
  ‘ŒS ©r   é   © ©Ú.0Úss     úh/var/www/html/bac/bac_backend/python/env/lib/python3.11/site-packages/skimage/segmentation/boundaries.pyú
<listcomp>z-_find_boundaries_subpixel.<locals>.<listcomp>   s    € Ð.Ð.Ð.˜ˆ!ˆa‰%�!‰)Ð.Ð.Ð.ó    Nr   )ÚdtypeFr   Úedge©Úmode)é   T)ÚndimÚnpÚiinfor   ÚmaxÚzerosÚshapeÚsliceÚonesÚboolr
   ÚpadÚ
zeros_likeÚndindexÚuniqueÚravelÚlen)
Ú	label_imgr   Ú	max_labelÚlabel_img_expandedÚpixelsÚedgesÚwindowsÚ
boundariesÚindexÚvaluess
             r   Ú_find_boundaries_subpixelr4   
   s7  € ð Œ>€DÝ”˜œÑ)Ô)Ô-€IåœØ.Ð.˜iœoÐ.Ñ.Ô.°	´ñô Ðõ �D˜$ Ñ"Ô"Ð$ tÑ+€FØ!*Ð�vÑåŒGÐ&Ô,µDÐ9Ñ9Ô9€EØ€Eˆ&�MØ )Ð�uÑÝ�bœfÐ%7¸ÀÐHÑHÔHÈ$ÐQUÉ+ÑVÔV€Gå”˜uÑ%Ô%€JÝ”Ð.Ô4Ñ5Ô5ð )ð )ˆØ�Œ<ð 	)Ý”Y˜w uœ~×3Ò3Ñ5Ô5Ñ6Ô6ˆFÝ�6‰{Œ{˜QŠˆØ$(�
˜5Ñ!øØÐr   r   Úthickc                 ó8  — | j         dk    r|                      t          j        ¦  «        } | j        }t          j        ||¦  «        }|dk    r¾t          | |¦  «        t          | |¦  «        k    }|dk    r| |k    }||z  }nˆ|dk    r‚t          j	        | j         ¦  «        j
        }| |k    }	t          j        ||¦  «        }t          j        | d¬¦  «        }
||
|	<   t          | |¦  «        t          |
|¦  «        k    |	 z  }||	|z  z  }|S t          | ¦  «        }|S )a#  Return bool array where boundaries between labeled regions are True.

    Parameters
    ----------
    label_img : array of int or bool
        An array in which different regions are labeled with either different
        integers or boolean values.
    connectivity : int in {1, ..., `label_img.ndim`}, optional
        A pixel is considered a boundary pixel if any of its neighbors
        has a different label. `connectivity` controls which pixels are
        considered neighbors. A connectivity of 1 (default) means
        pixels sharing an edge (in 2D) or a face (in 3D) will be
        considered neighbors. A connectivity of `label_img.ndim` means
        pixels sharing a corner will be considered neighbors.
    mode : string in {'thick', 'inner', 'outer', 'subpixel'}
        How to mark the boundaries:

        - thick: any pixel not completely surrounded by pixels of the
          same label (defined by `connectivity`) is marked as a boundary.
          This results in boundaries that are 2 pixels thick.
        - inner: outline the pixels *just inside* of objects, leaving
          background pixels untouched.
        - outer: outline pixels in the background around object
          boundaries. When two objects touch, their boundary is also
          marked.
        - subpixel: return a doubled image, with pixels *between* the
          original pixels marked as boundary where appropriate.
    background : int, optional
        For modes 'inner' and 'outer', a definition of a background
        label is required. See `mode` for descriptions of these two.

    Returns
    -------
    boundaries : array of bool, same shape as `label_img`
        A bool image where ``True`` represents a boundary pixel. For
        `mode` equal to 'subpixel', ``boundaries.shape[i]`` is equal
        to ``2 * label_img.shape[i] - 1`` for all ``i`` (a pixel is
        inserted in between all other pairs of pixels).

    Examples
    --------
    >>> labels = np.array([[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, 5, 5, 0, 0],
    ...                    [0, 0, 1, 1, 1, 5, 5, 5, 0, 0],
    ...                    [0, 0, 1, 1, 1, 5, 5, 5, 0, 0],
    ...                    [0, 0, 1, 1, 1, 5, 5, 5, 0, 0],
    ...                    [0, 0, 0, 0, 0, 5, 5, 5, 0, 0],
    ...                    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    ...                    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=np.uint8)
    >>> find_boundaries(labels, mode='thick').astype(np.uint8)
    array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
           [0, 0, 0, 0, 0, 1, 1, 1, 0, 0],
           [0, 0, 1, 1, 1, 1, 1, 1, 1, 0],
           [0, 1, 1, 1, 1, 1, 0, 1, 1, 0],
           [0, 1, 1, 0, 1, 1, 0, 1, 1, 0],
           [0, 1, 1, 1, 1, 1, 0, 1, 1, 0],
           [0, 0, 1, 1, 1, 1, 1, 1, 1, 0],
           [0, 0, 0, 0, 0, 1, 1, 1, 0, 0],
           [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=uint8)
    >>> find_boundaries(labels, mode='inner').astype(np.uint8)
    array([[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, 1, 1, 1, 0, 0],
           [0, 0, 1, 1, 1, 1, 0, 1, 0, 0],
           [0, 0, 1, 0, 1, 1, 0, 1, 0, 0],
           [0, 0, 1, 1, 1, 1, 0, 1, 0, 0],
           [0, 0, 0, 0, 0, 1, 1, 1, 0, 0],
           [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
           [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=uint8)
    >>> find_boundaries(labels, mode='outer').astype(np.uint8)
    array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
           [0, 0, 0, 0, 0, 1, 1, 1, 0, 0],
           [0, 0, 1, 1, 1, 1, 0, 0, 1, 0],
           [0, 1, 0, 0, 1, 1, 0, 0, 1, 0],
           [0, 1, 0, 0, 1, 1, 0, 0, 1, 0],
           [0, 1, 0, 0, 1, 1, 0, 0, 1, 0],
           [0, 0, 1, 1, 1, 1, 0, 0, 1, 0],
           [0, 0, 0, 0, 0, 1, 1, 1, 0, 0],
           [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=uint8)
    >>> labels_small = labels[::2, ::3]
    >>> labels_small
    array([[0, 0, 0, 0],
           [0, 0, 5, 0],
           [0, 1, 5, 0],
           [0, 0, 5, 0],
           [0, 0, 0, 0]], dtype=uint8)
    >>> find_boundaries(labels_small, mode='subpixel').astype(np.uint8)
    array([[0, 0, 0, 0, 0, 0, 0],
           [0, 0, 0, 1, 1, 1, 0],
           [0, 0, 0, 1, 0, 1, 0],
           [0, 1, 1, 1, 0, 1, 0],
           [0, 1, 0, 1, 0, 1, 0],
           [0, 1, 1, 1, 0, 1, 0],
           [0, 0, 0, 1, 0, 1, 0],
           [0, 0, 0, 1, 1, 1, 0],
           [0, 0, 0, 0, 0, 0, 0]], dtype=uint8)
    >>> bool_image = np.array([[False, False, False, False, False],
    ...                        [False, False, False, False, False],
    ...                        [False, False,  True,  True,  True],
    ...                        [False, False,  True,  True,  True],
    ...                        [False, False,  True,  True,  True]],
    ...                       dtype=bool)
    >>> find_boundaries(bool_image)
    array([[False, False, False, False, False],
           [False, False,  True,  True,  True],
           [False,  True,  True,  True,  True],
           [False,  True,  True, False, False],
           [False,  True,  True, False, False]])
    r$   ÚsubpixelÚinnerÚouterT©Úcopy)r   Úastyper   Úuint8r   ÚndiÚgenerate_binary_structurer   r   r   r   Úarrayr4   )r+   Úconnectivityr   Ú
backgroundr   Ú	footprintr1   Úforeground_imager,   Úbackground_imageÚinverted_backgroundÚadjacent_objectss               r   Úfind_boundariesrH   0   s?  € ð^ „˜&Ò Ð Ø×$Ò$¥R¤XÑ.Ô.ˆ	ØŒ>€DÝÔ-¨d°LÑAÔA€IØˆzÒÐÝ˜i¨Ñ3Ô3µw¸yÈ)Ñ7TÔ7TÒTˆ
Ø�7Š?ˆ?Ø(¨JÒ6ÐØÐ*Ñ*ˆJˆJØ�WŠ_ˆ_Ýœ ¤Ñ1Ô1Ô5ˆIØ(¨JÒ6ÐÝÔ5°d¸DÑAÔAˆIÝ"$¤(¨9¸4Ð"@Ñ"@Ô"@ÐØ4=ÐÐ 0Ñ1å˜ IÑ.Ô.ÝÐ.°	Ñ:Ô:ò;à!Ð!ñ "Ðð Ð*Ð-=Ñ=Ñ=ˆJØÐå.¨yÑ9Ô9ˆ
ØÐr   ©r   r   r   r9   c                 ó˜  — t          | j        ¦  «        }t          | d¬¦  «        }|                     |d¬¦  «        }|j        dk    rt          |¦  «        }|dk    r2t          j        |d„ |j        dd	…         D ¦   «         d
gz   d¬¦  «        }t          |||¬¦  «        }|�"t          |t          d¦  «        ¦  «        }	|||	<   |||<   |S )aÙ  Return image with boundaries between labeled regions highlighted.

    Parameters
    ----------
    image : (M, N[, 3]) array
        Grayscale or RGB image.
    label_img : (M, N) array of int
        Label array where regions are marked by different integer values.
    color : length-3 sequence, optional
        RGB color of boundaries in the output image.
    outline_color : length-3 sequence, optional
        RGB color surrounding boundaries in the output image. If None, no
        outline is drawn.
    mode : string in {'thick', 'inner', 'outer', 'subpixel'}, optional
        The mode for finding boundaries.
    background_label : int, optional
        Which label to consider background (this is only useful for
        modes ``inner`` and ``outer``).

    Returns
    -------
    marked : (M, N, 3) array of float
        An image in which the boundaries between labels are
        superimposed on the original image.

    See Also
    --------
    find_boundaries
    T)Ú
force_copyFr:   r   r7   c                 ó   — g | ]
}d d|z  z
  ‘ŒS r   r   r   s     r   r   z#mark_boundaries.<locals>.<listcomp>é   s    € Ð:Ð:Ð: 1�Q˜˜Q™‘YÐ:Ð:Ð:r   Néÿÿÿÿr   Úmirrorr   )r   rB   )r   r   )r   r   r	   r<   r   r   r>   Úzoomr!   rH   r   r   )
Úimager+   ÚcolorÚoutline_colorr   Úbackground_labelÚfloat_dtypeÚmarkedr1   Úoutliness
             r   Úmark_boundariesrW   ¹   sî   € õJ (¨¬Ñ4Ô4€KÝ˜%¨DÐ1Ñ1Ô1€FØ�]Š]˜;¨Uˆ]Ñ3Ô3€FØ„{�aÒÐÝ˜&Ñ!Ô!ˆØˆzÒÐõ
 ”ØÐ:Ð:¨¬°S°b°SÔ(9Ð:Ñ:Ô:¸a¸SÑ@Àxð
ñ 
ô 
ˆõ ! °ÐBRÐSÑSÔS€JØÐ Ý˜JÕ(;¸FÑ(CÔ(CÑDÔDˆØ(ˆˆxÑØ€Fˆ:ÑØ€Mr   )r   r5   r   )rI   Nr9   r   )Únumpyr   Úscipyr   r>   Ú_shared.utilsr   Ú
morphologyr   r   r   Úutilr	   r
   rQ   r   r4   rH   rW   r   r   r   ú<module>r]      sÒ   ðØ Ð Ð Ð Ø  Ð  Ð  Ð  Ð  Ð  à 1Ð 1Ð 1Ð 1Ð 1Ð 1Ø ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?Ø 0Ð 0Ð 0Ð 0Ð 0Ð 0Ð 0Ð 0Ø Ð Ð Ð Ð Ð ð#ð #ð #ðLFð Fð Fð FðX ØØ	Øð7ð 7ð 7ð 7ð 7ð 7r   