U
    h                     @   s|   d dl mZ d dlZddlmZ ddlmZ dZdZ	dd	 Z
ed
ddddZed
ddddZed
ddddZdS )    )reduceN   )polygon)require      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   F/tmp/pip-unpacked-wheel-gtraehzf/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      s     )npzerosintr   )verticesshapelabelmaskprZpcrrccr   r   r   _mask_from_vertices   s    r   
matplotlibz>=3.3皙?c           	      C   sV   ddl m} ddlm} dd lm} ||dd}||gd|d}| |}|  |S )Nr   )Polygon)PatchCollectionT)closed)Zmatch_originalalpha)Zmatplotlib.patchesr   Zmatplotlib.collectionsr   matplotlib.pyplotpyplotZadd_collectiondraw)	axr   r   r   r   pltr   ppolygon_objectr   r   r   _draw_polygon   s    
r'   Fc              	      s  ddl }ddlm g g g g jdkr6td \jdd jdd   fd	d
}	ddddg	|j
	d}||  	fdd}jd| 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   Nr   r   .Only 2D grayscale or RGB images are supported.皙?ZbottomgrayZcmapc                     s*   r&     }|   j  d S NpopremovecanvasZ	draw_idleargskwargsZ	last_polyfiglist_of_vertex_listspolygons_drawnr   r   _undoV   s
    z*manual_polygon_segmentation.<locals>._undo333333?皙?333333?   ⟲c                    s   | j d ks| j krd S  r$d S | jtkrr| j| jg rT }|  t	 d d}| n^| jt
krЈsd S d d   t	 d}|  }|  d d =   d S )Ngffffff?r   )ZinaxesZget_navigate_modebutton
LEFT_CLICKappendZxdataZydatar0   r1   r'   RIGHT_CLICKr"   )eventZpolyr   r&   Zpreview_poly)r   r#   r8   r$   r9   preview_polygon_drawn	temp_listundo_posr   r   _extend_polygonc   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   )r   r    r!   ndim
ValueErrorsubplotssubplots_adjustimshowset_axis_offadd_axeswidgetsButton
on_clickedr2   Zmpl_connectshow	enumerater   stackr   maximumbroadcast_tor   )rP   r   
return_allr   r:   undo_buttonrH   labelsr   )
r   r#   r7   rP   r8   r$   r9   rE   rF   rG   r   manual_polygon_segmentation!   s2    $
	
%


rf   c           	   	      s  ddl }ddlm g g jdkr.td \jdd jdd   fd	d
}	ddddg}|j
|d}||  fdd}|j
| 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   Nr(   r)   r*   r+   r,   r-   c                     s*   r&     }|   j  d S r.   r/   r3   r6   r   r   r:      s
    z(manual_lasso_segmentation.<locals>._undor;   r<   r=   r>   c                    s>   t | dk rd S |  t|  d}|   d S )Nr   r?   )lenrB   r'   r"   )r   r&   )r   r#   r8   r$   r9   r   r   _on_lasso_selection   s    

z6manual_lasso_segmentation.<locals>._on_lasso_selectionTrI   c                 3   s(   | ] \}}t | jd d |V  qd S rK   rL   rM   rO   r   r   rQ      s   z,manual_lasso_segmentation.<locals>.<genexpr>r   rR   r   )r   r    r!   rT   rU   rV   rW   rX   rY   rZ   r[   r\   r]   ZLassoSelectorr^   r_   r   r`   r   ra   rb   r   )	rP   r   rc   r   r:   rG   rd   rh   re   r   )r   r#   r7   rP   r8   r$   r9   r   manual_lasso_segmentation   s.    #
	



ri   )r   )r   F)r   F)	functoolsr   Znumpyr   r"   r   Z_shared.version_requirementsr   rA   rC   r   r'   rf   ri   r   r   r   r   <module>   s   	r