ó
 ‰\c           @   sª   d  d l  m Z d  d l Z d  d l Z d  d l j Z d  d l m	 Z	 d  d l
 m Z d d l m Z d Z d Z d	 „  Z d
 d „ Z d
 e d „ Z d
 e d „ Z d S(   iÿÿÿÿ(   t   reduceN(   t   Polygon(   t   PatchCollectioni   (   t   polygoni   i   c   
      C   s   t  j | d t ƒ} g  |  D] \ } } | ^ q } g  |  D] \ } } | ^ q; } t | | | ƒ \ } }	 | | | |	 f <| S(   Nt   dtype(   t   npt   zerost   intR   (
   t   verticest   shapet   labelt   maskt   xt   yt   prt   pct   rrt   cc(    (    sA   lib/python2.7/site-packages/skimage/future/manual_segmentation.pyt   _mask_from_vertices   s    gš™™™™™Ù?c         C   sJ   t  | d t ƒ} t | g d t d | ƒ} |  j | ƒ } t j ƒ  | S(   Nt   closedt   match_originalt   alpha(   R   t   TrueR   t   add_collectiont   pltt   draw(   t   axR   R   R   t   pt   polygon_object(    (    sA   lib/python2.7/site-packages/skimage/future/manual_segmentation.pyt   _draw_polygon   s
    
c            sm  g  ‰ g  ‰ g  ‰ g  ‰ ˆ j  d k r6 t d ƒ ‚ n  t j ƒ  \ ‰ ‰ ˆ j d d ƒ ˆ j ˆ d d ƒˆ j ƒ  ‡ ‡ ‡ f d †  } ˆ j d	 d
 d d g ƒ ‰ t j	 j
 ˆ d ƒ } | j | ƒ ‡  ‡ ‡ ‡ ‡ ‡ ‡ ‡ f d †  } ˆ j j d | ƒ t j d t ƒ ‡ f d †  t ˆ d d ƒDƒ } | rCt j | ƒ St t j | t j d ˆ j d  ƒ ƒ Sd S(   sl  Return a label image based on polygon selections made with the mouse.

    Parameters
    ----------
    image : (M, N[, 3]) array
        Grayscale or RGB image.

    alpha : float, optional
        Transparency value for polygons drawn over the image.

    return_all : bool, optional
        If True, an array containing each separate polygon drawn is returned.
        (The polygons may overlap.) If False (default), latter polygons
        "overwrite" earlier ones where they overlap.

    Returns
    -------
    labels : array of int, shape ([Q, ]M, N)
        The segmented regions. If mode is `'separate'`, the leading dimension
        of the array corresponds to the number of regions that the user drew.

    Notes
    -----
    Use left click to select the vertices of the polygon
    and right click to confirm the selection once all vertices are selected.

    Examples
    --------
    >>> from skimage import data, future, io
    >>> camera = data.camera()
    >>> mask = future.manual_polygon_segmentation(camera)  # doctest: +SKIP
    >>> io.imshow(mask)  # doctest: +SKIP
    >>> io.show()  # doctest: +SKIP
    i   i   s.   Only 2D grayscale or RGB images are supported.t   bottomgš™™™™™É?t   cmapt   grayc             s:   ˆ r6 ˆ j  ƒ  ˆ j  ƒ  } | j ƒ  ˆ  j j ƒ  n  d  S(   N(   t   popt   removet   canvast	   draw_idle(   t   argst   kwargst	   last_poly(   t   figt   list_of_vertex_listst   polygons_drawn(    sA   lib/python2.7/site-packages/skimage/future/manual_segmentation.pyt   _undoP   s
    

g333333ë?gš™™™™™©?g333333³?u   âŸ²c            s"  |  j  d  k s |  j  ˆ k r" d  Sˆ j j j j d  k	 r> d  S|  j t k r® ˆ j |  j	 |  j
 g ƒ ˆ r… ˆ j ƒ  } | j ƒ  n  t ˆ ˆ d ˆ  d ƒ} ˆ j | ƒ np |  j t k rˆ sÇ d  Sˆ j ˆ ƒ t ˆ ˆ d ˆ  ƒ} ˆ j | ƒ ˆ j ƒ  } | j ƒ  ˆ 2t j ƒ  n  d  S(   NR   gffffffö?(   t   inaxest   NoneR#   t   managert   toolbart   _activet   buttont
   LEFT_CLICKt   appendt   xdatat   ydataR!   R"   R   t   RIGHT_CLICKR   R   (   t   eventt   polyR   R   t   preview_poly(   R   R   R(   R)   R*   t   preview_polygon_drawnt	   temp_listt   undo_pos(    sA   lib/python2.7/site-packages/skimage/future/manual_segmentation.pyt   _extend_polygon]   s*    
t   button_press_eventt   blockc         3   s.   |  ]$ \ } } t  | ˆ  j d   | ƒ Vq d S(   i   N(   R   R	   (   t   .0t   iR   (   t   image(    sA   lib/python2.7/site-packages/skimage/future/manual_segmentation.pys	   <genexpr>†   s   t   starti   i    N(   i   i   (   t   ndimt
   ValueErrorR   t   subplotst   subplots_adjustt   imshowt   set_axis_offt   add_axest
   matplotlibt   widgetst   Buttont
   on_clickedR#   t   mpl_connectt   showR   t	   enumerateR   t   stackR    t   maximumt   broadcast_toR	   (   RB   R   t
   return_allR+   t   undo_buttonR=   t   labels(    (	   R   R   R(   RB   R)   R*   R:   R;   R<   sA   lib/python2.7/site-packages/skimage/future/manual_segmentation.pyt   manual_polygon_segmentation   s,    #
	$%c   	         sW  g  ‰ g  ‰ ˆ j  d k r* t d ƒ ‚ n  t j ƒ  \ ‰ ‰ ˆ j d d ƒ ˆ j ˆ d d ƒˆ j ƒ  ‡ ‡ ‡ f d †  } ˆ j d	 d
 d d g ƒ } t j	 j
 | d ƒ } | j | ƒ ‡  ‡ ‡ ‡ f d †  } t j	 j ˆ | ƒ } t j d t ƒ ‡ f d †  t ˆ d d ƒDƒ } | r-t j | ƒ St t j | t j d ˆ j d  ƒ ƒ Sd S(   s*  Return a label image based on freeform selections made with the mouse.

    Parameters
    ----------
    image : (M, N[, 3]) array
        Grayscale or RGB image.

    alpha : float, optional
        Transparency value for polygons drawn over the image.

    return_all : bool, optional
        If True, an array containing each separate polygon drawn is returned.
        (The polygons may overlap.) If False (default), latter polygons
        "overwrite" earlier ones where they overlap.

    Returns
    -------
    labels : array of int, shape ([Q, ]M, N)
        The segmented regions. If mode is `'separate'`, the leading dimension
        of the array corresponds to the number of regions that the user drew.

    Notes
    -----
    Press and hold the left mouse button to draw around each object.

    Examples
    --------
    >>> from skimage import data, future, io
    >>> camera = data.camera()
    >>> mask = future.manual_lasso_segmentation(camera)  # doctest: +SKIP
    >>> io.imshow(mask)  # doctest: +SKIP
    >>> io.show()  # doctest: +SKIP
    i   i   s.   Only 2D grayscale or RGB images are supported.R   gš™™™™™É?R   R    c             s:   ˆ r6 ˆ j  ƒ  ˆ j  ƒ  } | j ƒ  ˆ  j j ƒ  n  d  S(   N(   R!   R"   R#   R$   (   R%   R&   R'   (   R(   R)   R*   (    sA   lib/python2.7/site-packages/skimage/future/manual_segmentation.pyR+   »   s
    

g333333ë?gš™™™™™©?g333333³?u   âŸ²c            sS   t  |  ƒ d k  r d  Sˆ j |  ƒ t ˆ |  d ˆ  ƒ} ˆ j | ƒ t j ƒ  d  S(   Ni   R   (   t   lenR3   R   R   R   (   R   R   (   R   R   R)   R*   (    sA   lib/python2.7/site-packages/skimage/future/manual_segmentation.pyt   _on_lasso_selectionÈ   s    R?   c         3   s.   |  ]$ \ } } t  | ˆ  j d   | ƒ Vq d S(   i   N(   R   R	   (   R@   RA   R   (   RB   (    sA   lib/python2.7/site-packages/skimage/future/manual_segmentation.pys	   <genexpr>Ô   s   RC   i   i    N(   i   i   (   RD   RE   R   RF   RG   RH   RI   RJ   RK   RL   RM   RN   t   LassoSelectorRP   R   RQ   R   RR   R    RS   RT   R	   (	   RB   R   RU   R+   R<   RV   RZ   t   lassoRW   (    (   R   R   R(   RB   R)   R*   sA   lib/python2.7/site-packages/skimage/future/manual_segmentation.pyt   manual_lasso_segmentationŽ   s(    "
	(   t	   functoolsR    t   numpyR   RK   t   matplotlib.pyplott   pyplotR   t   matplotlib.patchesR   t   matplotlib.collectionsR   R   R   R2   R6   R   R   t   FalseRX   R]   (    (    (    sA   lib/python2.7/site-packages/skimage/future/manual_segmentation.pyt   <module>   s   		o