B
    \                 @   s~   d dl mZ d dlZd dlZd dlmZ d dlm	Z	 d dl
mZ ddlmZ dZdZd	d
 ZdddZdddZdddZdS )    )reduceN)Polygon)PatchCollection   )polygon      c             C   sJ   t j|td}dd | D }dd | D }t|||\}}||||f< |S )N)Zdtypec             S   s   g | ]\}}|qS  r	   ).0xyr	   r	   Alib/python3.7/site-packages/skimage/future/manual_segmentation.py
<listcomp>   s    z'_mask_from_vertices.<locals>.<listcomp>c             S   s   g | ]\}}|qS r	   r	   )r
   r   r   r	   r	   r   r      s    )npZzerosintr   )verticesshapeZlabelmaskZprZpcZrrZccr	   r	   r   _mask_from_vertices   s    r   皙?c             C   s2   t |dd}t|gd|d}| |}t  |S )NT)closed)Zmatch_originalalpha)r   r   Zadd_collectionpltdraw)axr   r   r   ppolygon_objectr	   r	   r   _draw_polygon   s
    
r   Fc          	      s
  g g g g j dkr"tdt \jdd jdd   fdd}d	d
ddgtj	
d}||  fdd}jd| tjdd fddtddD }|rt|S ttj|tdjdd S dS )al  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
    )r   r   z.Only 2D grayscale or RGB images are supported.g?)bottomgray)cmapc                 s*   r&     }|   j  d S )N)popremovecanvas	draw_idle)argskwargs	last_poly)figlist_of_vertex_listspolygons_drawnr	   r   _undoP   s
    z*manual_polygon_segmentation.<locals>._undog333333?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rz| j| j	g r\
 }|  t d d}| n^| jtkr؈sd S d d   t d}| 
 }|  d d = t  d S )Ngffffff?)r   )Zinaxesr#   ZmanagerZtoolbarZ_activeZbutton
LEFT_CLICKappendZxdataZydatar!   r"   r   RIGHT_CLICKr   r   )ZeventZpolyr   r   Zpreview_poly)r   r   r(   r)   r*   preview_polygon_drawn	temp_listundo_posr	   r   _extend_polygon]   s*    



z4manual_polygon_segmentation.<locals>._extend_polygonZbutton_press_eventT)blockc             3   s(   | ] \}}t | jd d |V  qd S )Nr   )r   r   )r
   ir   )imager	   r   	<genexpr>   s   z.manual_polygon_segmentation.<locals>.<genexpr>r   )startr   Nr   )ndim
ValueErrorr   subplotssubplots_adjustimshowset_axis_offadd_axes
matplotlibwidgetsButton
on_clickedr#   Zmpl_connectshow	enumerater   stackr   maximumbroadcast_tor   )r5   r   
return_allr+   undo_buttonr2   labelsr	   )	r   r   r(   r5   r)   r*   r/   r0   r1   r   manual_polygon_segmentation   s,    #
	
%

rK   c       	   	      s   g g j dkrtdt \jdd jdd   fdd}d	d
ddg}tj	
|d}||  fdd}tj	|}tjdd fddtddD }|rt|S ttj|tdjdd S dS )a*  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
    )r   r   z.Only 2D grayscale or RGB images are supported.g?)r   r   )r    c                 s*   r&     }|   j  d S )N)r!   r"   r#   r$   )r%   r&   r'   )r(   r)   r*   r	   r   r+      s
    z(manual_lasso_segmentation.<locals>._undog333333?g?g333333?u   ⟲c                s>   t | dk rd S |  t|  d}| t  d S )Nr   )r   )lenr-   r   r   r   )r   r   )r   r   r)   r*   r	   r   _on_lasso_selection   s    

z6manual_lasso_segmentation.<locals>._on_lasso_selectionT)r3   c             3   s(   | ] \}}t | jd d |V  qd S )Nr   )r   r   )r
   r4   r   )r5   r	   r   r6      s   z,manual_lasso_segmentation.<locals>.<genexpr>r   )r7   r   Nr   )r8   r9   r   r:   r;   r<   r=   r>   r?   r@   rA   rB   ZLassoSelectorrC   rD   r   rE   r   rF   rG   r   )	r5   r   rH   r+   r1   rI   rM   ZlassorJ   r	   )r   r   r(   r5   r)   r*   r   manual_lasso_segmentation   s(    "
	


rN   )r   )r   F)r   F)	functoolsr   Znumpyr   r?   Zmatplotlib.pyplotZpyplotr   Zmatplotlib.patchesr   Zmatplotlib.collectionsr   r   r   r,   r.   r   r   rK   rN   r	   r	   r	   r   <module>   s   	

o