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   T/var/www/html/venv/lib/python3.8/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      s     )npZzerosintr   )verticesshapelabelmaskprZpcZ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.皙?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_modeZbutton
LEFT_CLICKappendZxdataZydatar/   r0   r%   RIGHT_CLICKr    )eventZpolyr   r$   Zpreview_poly)r   r!   r7   r"   r8   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_clickedr1   Zmpl_connectshow	enumerater   stackr   maximumbroadcast_tor   )rN   r   
return_allr   r9   undo_buttonrF   labelsr   )
r   r!   r6   rN   r7   r"   r8   rC   rD   rE   r   manual_polygon_segmentation!   s2    $
	
%


rd   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.   r2   r5   r   r   r9      s
    z(manual_lasso_segmentation.<locals>._undor:   r;   r<   r=   c                    s>   t | dk rd S |  t|  d}|   d S )Nr   r>   )lenr@   r%   r    )r   r$   )r   r!   r7   r"   r8   r   r   _on_lasso_selection   s    

z6manual_lasso_segmentation.<locals>._on_lasso_selectionTrG   c                 3   s(   | ] \}}t | jd d |V  qd S rI   rJ   rK   rM   r   r   rO      s   z,manual_lasso_segmentation.<locals>.<genexpr>r   rP   r   )r   r   r   rR   rS   rT   rU   rV   rW   rX   rY   rZ   r[   ZLassoSelectorr\   r]   r   r^   r   r_   r`   r   )	rN   r   ra   r   r9   rE   rb   rf   rc   r   )r   r!   r6   rN   r7   r"   r8   r   manual_lasso_segmentation   s.    #
	



rg   )r   )r   F)r   F)	functoolsr   numpyr   r    r   Z_shared.version_requirementsr   r?   rA   r   r%   rd   rg   r   r   r   r   <module>   s   	r