
    0-Ph>                         d dl Z d dlZddlmZmZmZ	 ddl
mZ d Zd Zd Zd	 Z eeeee
          Z ee                                          Zd Z	 	 	 	 	 	 	 	 	 ddddZdS )    N   )polygondiskellipse   )warnc                    t          |d         | d         z
  |d                   |d         z
  }t          |d         | d         z
  |d                   |d         z
  }|d         |                    t          d|                    z   dz
  }|d         |                    t          d|                    z   dz
  }t          | d         | d         |z   | d         |z   | d         g| d         | d         | d         |z   | d         |z   g          }d| d         | d         |z   dz   f| d         | d         |z   dz   fff}	||	fS )a  Generate a mask for a filled rectangle shape.

    The height and width of the rectangle are generated randomly.

    Parameters
    ----------
    point : tuple
        The row and column of the top left corner of the rectangle.
    image : tuple
        The height, width and depth of the image into which the shape
        is placed.
    shape : tuple
        The minimum and maximum size of the shape to fit.
    random : `numpy.random.Generator`

        The random state to use for random sampling.

    Raises
    ------
    ArithmeticError
        When a shape cannot be fit into the image with the given starting
        coordinates. This usually means the image dimensions are too small or
        shape dimensions too large.

    Returns
    -------
    label : tuple
        A (category, ((r0, r1), (c0, c1))) tuple specifying the category and
        bounding box coordinates of the shape.
    indices : 2-D array
        A mask of indices that the shape fills.

    r   r   	rectangle)minintegersmaxdraw_polygon)
pointimageshaperandomavailable_widthavailable_heightrcr
   labels
             [/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/skimage/draw/_random_shapes.py_generate_rectangle_maskr   	   sY   D %(U1X-uQx8858CO58eAh.a99E!HD 	a6??3q*:#;#;<<<q@Aa6??3q/#:#:;;;a?A!H!HqL!HqL!H		
 !H!H!HqL!HqL		
 I E!HeAhlQ&67%(E!HqLSTDT9UVWEe    c           	      H   |d         dk    s|d         dk    rt          d          |d         dz  }|d         dz  }| d         }|d         | d         z
  }| d         }|d         | d         z
  }	t          ||||	|          |z
  }
|
dk     rt          d          t          ||                    t          d|
                    z             }t          | d         | d         f|          }d| d         |z
  dz   | d         |z   f| d         |z
  dz   | d         |z   fff}||fS )a  Generate a mask for a filled circle shape.

    The radius of the circle is generated randomly.

    Parameters
    ----------
    point : tuple
        The row and column of the top left corner of the rectangle.
    image : tuple
        The height, width and depth of the image into which the shape is placed.
    shape : tuple
        The minimum and maximum size and color of the shape to fit.
    random : `numpy.random.Generator`
        The random state to use for random sampling.

    Raises
    ------
    ArithmeticError
        When a shape cannot be fit into the image with the given starting
        coordinates. This usually means the image dimensions are too small or
        shape dimensions too large.

    Returns
    -------
    label : tuple
        A (category, ((r0, r1), (c0, c1))) tuple specifying the category and
        bounding box coordinates of the shape.
    indices : 2-D array
        A mask of indices that the shape fills.
    r   r   zsize must be > 1 for circles       @cannot fit shape to imagecircle)
ValueErrorr   ArithmeticErrorintr   r   	draw_disk)r   r   r   r   
min_radius
max_radiusleftrighttopbottomavailable_radiusradiusr   r   s                 r   _generate_circle_maskr+   D   sS   > Qx1}}aA7888qSJqSJ8D!HuQxE
(C1Xa F4VZ@@:M!9:::fooc!5E.F.FGGGHHF eAha)622D 	1X"E!Hv$561X"E!Hv$56	
E ;r   c                 t   |d         dk    s|d         dk    rt          d          t          |d         | d         z
  | d         |d                   |d         z
  }|d         |                    t          d|                    z   dz
  }t	          t          j        t          j        d          |z                      }t          | d         | d         |z
  | d         g| d         | d         |dz  z   | d         |z   g          }d| d         |z
  | d         dz   f| d         | d         |z   dz   fff}||fS )a  Generate a mask for a filled equilateral triangle shape.

    The length of the sides of the triangle is generated randomly.

    Parameters
    ----------
    point : tuple
        The row and column of the top left corner of a up-pointing triangle.
    image : tuple
        The height, width and depth of the image into which the shape
        is placed.
    shape : tuple
        The minimum and maximum size and color of the shape to fit.
    random : `numpy.random.Generator`
        The random state to use for random sampling.

    Raises
    ------
    ArithmeticError
        When a shape cannot be fit into the image with the given starting
        coordinates. This usually means the image dimensions are too small or
        shape dimensions too large.

    Returns
    -------
    label : tuple
        A (category, ((r0, r1), (c0, c1))) tuple specifying the category and
        bounding box coordinates of the shape.
    indices : 2-D array
        A mask of indices that the shape fills.

    r   r   z#dimension must be > 1 for trianglesg      ?r   triangle)	r   r   r   r   r!   npceilsqrtr   )	r   r   r   r   available_sidesidetriangle_heightr-   r   s	            r   _generate_triangle_maskr4      sO   B Qx1}}aA>???qE!H,eAhaAAE!HLN8fooc!^&<&<===AD"'"''"2"2T"9::;;O!H!H&!H	
 !H!Htqy !HtO	
 H 	
(_
$eAhl	3eAha4RS@S5TUE
 U?r   c           	      x   |d         dk    s|d         dk    rt          d          |d         dz  }|d         dz  }| d         }|d         | d         z
  }| d         }|d         | d         z
  }	t          ||||	|          }
|
|k     rt          d          |                    ||
dz             }|                    ||
dz             }|                    t          j         t          j                  }t          | d         | d         |||dd         |          }t          j        t          ||                    }t	          j        |d                   }t	          j	        |d                   dz   }t	          j        |d                   }t	          j	        |d                   dz   }d	||f||fff}||fS )
a  Generate a mask for a filled ellipse shape.

    The rotation, major and minor semi-axes of the ellipse are generated
    randomly.

    Parameters
    ----------
    point : tuple
        The row and column of the top left corner of the rectangle.
    image : tuple
        The height, width and depth of the image into which the shape is
        placed.
    shape : tuple
        The minimum and maximum size and color of the shape to fit.
    random : `numpy.random.Generator`
        The random state to use for random sampling.

    Raises
    ------
    ArithmeticError
        When a shape cannot be fit into the image with the given starting
        coordinates. This usually means the image dimensions are too small or
        shape dimensions too large.

    Returns
    -------
    label : tuple
        A (category, ((r0, r1), (c0, c1))) tuple specifying the category and
        bounding box coordinates of the shape.
    indices : 2-D array
        A mask of indices that the shape fills.
    r   r   zsize must be > 1 for ellipsesr   r   Nr   )r   rotationr   )
r   r   r    uniformr.   pidraw_ellipsemathr/   r   )r   r   r   r   r#   r$   r%   r&   r'   r(   r)   r_radiusc_radiusr6   r   min_xmax_xmin_ymax_yr   s                       r   _generate_ellipse_maskrA      s   B Qx1}}aA8999qCJqCJ8D!HuQxE
(C1Xa F4VZ@@*$$9:::
 ~~j*:Q*>??H~~j*:Q*>??H~~rufbe,,HaaBQBi  G 3x2233JF71:EF71:"EF71:EF71:"E%%89EE>r   )r
   r   r-   r   c                      |dk    r|f}nt          |          dk    r||z  } fd|D             }t          j        |          S )a  Generate an array of random colors.

    Parameters
    ----------
    num_colors : int
        Number of colors to generate.
    num_channels : int
        Number of elements representing color.
    intensity_range : {tuple of tuples of ints, tuple of ints}, optional
        The range of values to sample pixel values from. For grayscale images
        the format is (min, max). For multichannel - ((min, max),) if the
        ranges are equal across the channels, and
        ((min_0, max_0), ... (min_N, max_N)) if they differ.
    random : `numpy.random.Generator`
        The random state to use for random sampling.

    Raises
    ------
    ValueError
        When the `intensity_range` is not in the interval (0, 255).

    Returns
    -------
    colors : array
        An array of shape (num_colors, num_channels), where the values for
        each channel are drawn from the corresponding `intensity_range`.

    r   c                 ^    g | ])}                     |d          |d         dz             *S )r   r   )size)r   ).0r   
num_colorsr   s     r   
<listcomp>z+_generate_random_colors.<locals>.<listcomp>,  s7    XXX1fooadAaD1H:o>>XXXr   )lenr.   	transpose)rF   num_channelsintensity_ranger   colorss   `  ` r   _generate_random_colorsrM     sd    : q*,	_			"	")L8XXXXXXXXF<r      Fd   )channel_axisc          	         || d         k    s|| d         k    rt          d          |pt          | d         | d                   }|d}||dk    rdnd}n5|dk    r|fn|}|D ]'}|D ]"}d|cxk    rdk    sn d}t          |          #(t          j                            |
          }
|}| d         | d         |f} t          j        | dt          j        	          }t          j        | t          	          }g }|
	                    ||dz             }t          ||||
          }||f}t          |          D ]}||
                    t                    }nt          |         }t          |	          D ]}|
	                    t          d| d         |z
                      }|
	                    t          d| d         |z
                      }||f}	  ||| ||
          \  }}n# t          $ r g }Y w xY w|s||                                         s'||         ||<   d
||<   |                    |            nt%          d           |t          j        |d          }nt          j        |d|          }||fS )a  Generate an image with random shapes, labeled with bounding boxes.

    The image is populated with random shapes with random sizes, random
    locations, and random colors, with or without overlap.

    Shapes have random (row, col) starting coordinates and random sizes bounded
    by `min_size` and `max_size`. It can occur that a randomly generated shape
    will not fit the image at all. In that case, the algorithm will try again
    with new starting coordinates a certain number of times. However, it also
    means that some shapes may be skipped altogether. In that case, this
    function will generate fewer shapes than requested.

    Parameters
    ----------
    image_shape : tuple
        The number of rows and columns of the image to generate.
    max_shapes : int
        The maximum number of shapes to (attempt to) fit into the shape.
    min_shapes : int, optional
        The minimum number of shapes to (attempt to) fit into the shape.
    min_size : int, optional
        The minimum dimension of each shape to fit into the image.
    max_size : int, optional
        The maximum dimension of each shape to fit into the image.
    num_channels : int, optional
        Number of channels in the generated image. If 1, generate monochrome
        images, else color images with multiple channels. Ignored if
        ``multichannel`` is set to False.
    shape : {rectangle, circle, triangle, ellipse, None} str, optional
        The name of the shape to generate or `None` to pick random ones.
    intensity_range : {tuple of tuples of uint8, tuple of uint8}, optional
        The range of values to sample pixel values from. For grayscale
        images the format is (min, max). For multichannel - ((min, max),)
        if the ranges are equal across the channels, and
        ((min_0, max_0), ... (min_N, max_N)) if they differ. As the
        function supports generation of uint8 arrays only, the maximum
        range is (0, 255). If None, set to (0, 254) for each channel
        reserving color of intensity = 255 for background.
    allow_overlap : bool, optional
        If `True`, allow shapes to overlap.
    num_trials : int, optional
        How often to attempt to fit a shape into the image before skipping it.
    rng : {`numpy.random.Generator`, int}, optional
        Pseudo-random number generator.
        By default, a PCG64 generator is used (see :func:`numpy.random.default_rng`).
        If `rng` is an int, it is used to seed the generator.
    channel_axis : int or None, optional
        If None, the image is assumed to be a grayscale (single channel) image.
        Otherwise, this parameter indicates which axis of the array corresponds
        to channels.

        .. versionadded:: 0.19
           ``channel_axis`` was added in 0.19.

    Returns
    -------
    image : uint8 array
        An image with the fitted shapes.
    labels : list
        A list of labels, one per shape in the image. Each label is a
        (category, ((r0, r1), (c0, c1))) tuple specifying the category and
        bounding box coordinates of the shape.

    Examples
    --------
    >>> import skimage.draw
    >>> image, labels = skimage.draw.random_shapes((32, 32), max_shapes=3)
    >>> image # doctest: +SKIP
    array([
       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]]], dtype=uint8)
    >>> labels # doctest: +SKIP
    [('circle', ((22, 18), (25, 21))),
     ('triangle', ((5, 6), (13, 13)))]
    r   r   z3Minimum dimension must be less than ncols and nrowsNr      )rS      z1Intensity range must lie within (0, 255) interval)dtypeTzJCould not fit any shapes to image, consider reducing the minimum dimensionr   )axisrP   )r   r   r.   r   default_rngfulluint8zerosboolr   rM   rangechoiceSHAPE_CHOICESSHAPE_GENERATORSr    anyappendr   squeezemoveaxis)image_shape
max_shapes
min_shapesmin_sizemax_sizerJ   r   rK   allow_overlap
num_trialsrngrQ   tmpintensity_pair	intensitymsg
user_shaper   filledlabels
num_shapesrL   	shape_idxshape_generator_columnrowr   indicesr   s                                 r   random_shapesr{   0  s   ~ +a.  H{1~$=$=NOOO>3{1~{1~>>H&2a&7&7(([$0A$5$5  ?! 	* 	*N+ * *	Y----#----MC$S//) .*
 )


$
$CJq>;q><@KGKBH555EXk...FFj*q.99J$ZPSTTFx E:&&  	!jj77OO.z:Oz"" 	 	A\\#aQ()B"C"CDDF,,s1k!nx&?@@AAC&ME!0UC!P!P"   
  F7O$7$7$9$9 !'	!2g"&we$$$	 :  
 
5q)))E2|44&=s   G''G65G6)	r   r   NrN   NNFrO   N)r:   numpyr.   drawr   r   r   r"   r   r9   _shared.utilsr   r   r+   r4   rA   dictr`   listvaluesr_   rM   r{    r   r   <module>r      s2        U U U U U U U U U U            8 8 8v; ; ;|7 7 7tB B BL 4& $"	    %,,..//"  "  " P 
[ [ [ [ [ [ [ [r   