skimage2.draw.random_shapes#
- skimage2.draw.random_shapes(image_shape, max_shapes, min_shapes=1, min_size=2, max_size=None, num_channels=3, shape=None, intensity_range=None, allow_overlap=False, num_trials=100, rng=None, *, channel_axis=-1)[source]#
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_sizeandmax_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_shapetuple
The number of rows and columns of the image to generate.
- max_shapesint
The maximum number of shapes to (attempt to) fit into the shape.
- min_shapesint, optional
The minimum number of shapes to (attempt to) fit into the shape.
- min_sizeint, optional
The minimum dimension of each shape to fit into the image.
- max_sizeint, optional
The maximum dimension of each shape to fit into the image.
- num_channelsint, optional
Number of channels in the generated image. If 1, generate monochrome images, else color images with multiple channels. Ignored if
multichannelis set to False.- shape{“rectangle”, “circle”, “triangle”, “ellipse”, None}, optional
The name of the shape to generate or
Noneto 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_overlapbool, optional
If
True, allow shapes to overlap.- num_trialsint, 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
numpy.random.default_rng()). Ifrngis an int, it is used to seed the generator.- channel_axisint 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.
Added in version 0.19:
channel_axiswas added in 0.19.
- Returns:
- imagendarray of dtype uint8
An image with the fitted shapes.
- labelslist
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 _skimage2.draw >>> image, labels = _skimage2.draw.random_shapes((32, 32), max_shapes=3) >>> image array([ [[255, 255, 255], [255, 255, 255], [255, 255, 255], ..., [255, 255, 255], [255, 255, 255], [255, 255, 255]]], dtype=uint8) >>> labels [('circle', ((22, 18), (25, 21))), ('triangle', ((5, 6), (13, 13)))]