o
    e                     @   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   RD:\Projects\ConvertPro\env\Lib\site-packages\skimage/future/manual_segmentation.py
<listcomp>       z'_mask_from_vertices.<locals>.<listcomp>c                 S   s   g | ]\}}|qS r   r   r	   r   r   r   r      r   )npZ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vrtd \jdd jdd   fd	d
}	g d	|j
	d}||  	fdd}jd| jdd fddtddD }|r{t|S ttj|tdjd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                     .   r     }|   j  d S d S NpopremovecanvasZ	draw_idleargskwargsZ	last_polyfiglist_of_vertex_listspolygons_drawnr   r   _undoV      z*manual_polygon_segmentation.<locals>._undog333333?g?333333?r?      ⟲c                    s   | j d u s
| j u rd S  rd S | jtkr:| j| jg r* }|  t	 d d}| d S | jt
krksCd S d d   t	 d}|  }|  d d =   d S d S )Ngffffff?r   )ZinaxesZget_navigate_modeZbutton
LEFT_CLICKappendZxdataZydatar2   r3   r'   RIGHT_CLICKr"   )eventZpolyr   r&   Zpreview_poly)r   r#   r:   r$   r;   preview_polygon_drawn	temp_listundo_posr   r   _extend_polygonc   s,   



z4manual_polygon_segmentation.<locals>._extend_polygonZbutton_press_eventTblockc                 3   *    | ]\}}t | jd d |V  qd S Nr   r   r   r
   ir   imager   r   	<genexpr>       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_clickedr4   Zmpl_connectshow	enumerater   stackr   maximumbroadcast_tor   )rR   r   
return_allr   r<   undo_buttonrI   labelsr   )
r   r#   r9   rR   r:   r$   r;   rF   rG   rH   r   manual_polygon_segmentation!   s2   $
	
%


 ri   c           	   	      s  ddl }ddlm g g jdvrtd \jdd jdd   fd	d
}	g d}|j
|d}||  fdd}|j
| jdd fddtddD }|rtt|S ttj|tdjd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                     r/   r0   r1   r5   r8   r   r   r<      r=   z(manual_lasso_segmentation.<locals>._undor>   r@   c                    s>   t | dk rd S |  t|  d}|   d S )Nr   rA   )lenrC   r'   r"   )r   r&   )r   r#   r:   r$   r;   r   r   _on_lasso_selection   s   

z6manual_lasso_segmentation.<locals>._on_lasso_selectionTrJ   c                 3   rL   rM   rN   rO   rQ   r   r   rS      rT   z,manual_lasso_segmentation.<locals>.<genexpr>r   rU   r   )r   r    r!   rW   rX   rY   rZ   r[   r\   r]   r^   r_   r`   ZLassoSelectorra   rb   r   rc   r   rd   re   r   )	rR   r   rf   r   r<   rH   rg   rk   rh   r   )r   r#   r9   rR   r:   r$   r;   r   manual_lasso_segmentation   s.   #
	



 rl   )r   )r   F)	functoolsr   numpyr   r"   r   Z_shared.version_requirementsr   rB   rD   r   r'   ri   rl   r   r   r   r   <module>   s    	r