ó
 \c           @   s˘   d  d l  m Z d  d l 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
 d d  Z d d d d d  Z d S(   i˙˙˙˙(   t   divisionN(   t   ndimagei   (   t   dilationt   erosiont   square(   t   img_as_floatt   view_as_windows(   t   gray2rgbc         C   s@  |  j  } t j |  j  j } t j g  |  j D] } d | d ^ q. |  j  } t d	 d	 d  f | } |  | | <t j	 | j d t
 } t | | <| | | <t t j | d d d d d d
 |  } t j |  } x_ t j | j  D]K }	 | |	 rí t j | |	 j    }
 t |
  d k r8t | |	 <q8qí qí W| S(   s  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.
    i   i   t   dtypet   modet   constantt   constant_valuesi    i   N(   i   (   t   ndimt   npt   iinfoR   t   maxt   zerost   shapet   slicet   Nonet   onest   boolt   FalseR   t   padt
   zeros_liket   ndindext   uniquet   ravelt   lent   True(   t	   label_imgR   t	   max_labelt   st   label_img_expandedt   pixelst   edgest   windowst
   boundariest   indext   values(    (    s>   lib/python2.7/site-packages/skimage/segmentation/boundaries.pyt   _find_boundaries_subpixel
   s&    	'



i   t   thicki    c         C   s8  |  j  d k r$ |  j t j  }  n  |  j } t j | |  } | d k r$t |  |  t |  |  k } | d k r |  | k } | | M} n | d k r t j	 |  j   j
 } |  | k }	 t j | |  } t j |  d t }
 | |
 |	 <t |  |  t |
 |  k |	 @} | |	 | BM} n  | St |   } | Sd S(   s  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=np.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]], dtype=bool)
    R   t   subpixelt   innert   outert   copyN(   R   t   astypeR   t   uint8R   t   ndit   generate_binary_structureR   R   R   R   t   arrayR   R(   (   R   t   connectivityR	   t
   backgroundR   t   selemR%   t   foreground_imageR   t   background_imaget   inverted_backgroundt   adjacent_objects(    (    s>   lib/python2.7/site-packages/skimage/segmentation/boundaries.pyt   find_boundaries1   s,    n	
R,   c   
      C   sÔ   t  |  d t } | j d k r0 t |  } n  | d k r t j | g  | j d  D] } d d | ^ qS d g d d } n  t | d | d | } | d
 k	 rĆ t	 | t
 d	   }	 | | |	 <n  | | | <| S(   sŮ  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_copyi   R*   i˙˙˙˙i   R	   t   reflectR4   i   N(   R   R   R   R   R0   t   zoomR   R:   R   R   R   (
   t   imageR   t   colort   outline_colorR	   t   background_labelt   markedR    R%   t   outlines(    (    s>   lib/python2.7/site-packages/skimage/segmentation/boundaries.pyt   mark_boundaries¸   s    8	
(   i   i   i    (   t
   __future__R    t   numpyR   t   scipyR   R0   t
   morphologyR   R   R   t   utilR   R   R?   R   R(   R:   R   RD   (    (    (    s>   lib/python2.7/site-packages/skimage/segmentation/boundaries.pyt   <module>   s   	'