o
    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)dtypec                 S   s   g | ]\}}|qS  r	   .0xyr	   r	   ]/var/www/html/scripts/venv/lib/python3.10/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   )npzerosintr   )verticesshapelabelmaskpr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)match_originalalpha)matplotlib.patchesr   matplotlib.collectionsr    matplotlib.pyplotpyplot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cmapc                     .   r     }|   j  d S d S Npopremovecanvas	draw_idleargskwargs	last_polyfiglist_of_vertex_listspolygons_drawnr	   r   _undoV      z*manual_polygon_segmentation.<locals>._undog333333?g?333333?rI      ⟲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#   )inaxesget_navigate_modebutton
LEFT_CLICKappendxdataydatar:   r;   r.   RIGHT_CLICKr)   )eventpolyr   r-   preview_poly)r#   r*   rD   r+   rE   preview_polygon_drawn	temp_listundo_posr	   r   _extend_polygonc   s,   



z4manual_polygon_segmentation.<locals>._extend_polygon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_clickedr<   mpl_connectshow	enumerater   stackr   maximumbroadcast_tor   )rd   r#   
return_allr   rF   undo_buttonrZ   labelsr	   )
r#   r*   rC   rd   rD   r+   rE   rW   rX   rY   r   manual_polygon_segmentation!   s2   $
	
%


 r|   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/   r0   r1   r2   r4   r5   c                     r7   r8   r9   r>   rB   r	   r   rF      rG   z(manual_lasso_segmentation.<locals>._undorH   rJ   c                    s>   t | dk rd S |  t|  d}|   d S )Nr   rK   )lenrP   r.   r)   )r   r-   )r#   r*   rD   r+   rE   r	   r   _on_lasso_selection   s   

z6manual_lasso_segmentation.<locals>._on_lasso_selectionTr\   c                 3   r^   r_   r`   ra   rc   r	   r   re      rf   z,manual_lasso_segmentation.<locals>.<genexpr>r   rg   r   )r   r&   r'   ri   rj   rk   rl   rm   rn   ro   rp   rq   rr   LassoSelectorrt   ru   r   rv   r   rw   rx   r   )	rd   r#   ry   r   rF   rY   rz   r~   r{   r	   )r#   r*   rC   rd   rD   r+   rE   r   manual_lasso_segmentation   s.   #
	



 r   )r   )r   F)	functoolsr   numpyr   r)   r   _shared.version_requirementsr   rO   rS   r   r.   r|   r   r	   r	   r	   r   <module>   s    	r