.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        Click :ref:`here <sphx_glr_download_auto_examples_edges_plot_random_shapes.py>`     to download the full example code or to run this example in your browser via Binder
    .. rst-class:: sphx-glr-example-title

    .. _sphx_glr_auto_examples_edges_plot_random_shapes.py:


=============
Random Shapes
=============

Example of generating random shapes with particular properties.



.. image:: /auto_examples/edges/images/sphx_glr_plot_random_shapes_001.png
    :class: sphx-glr-single-img


.. rst-class:: sphx-glr-script-out

 Out:

 .. code-block:: none

    Image shape: (128, 128)
    Labels: [('rectangle', ((117, 119), (47, 116)))]






|


.. code-block:: default


    import matplotlib.pyplot as plt

    from skimage.draw import random_shapes

    # Let's start simple and generate a 128x128 image
    # with a single grayscale rectangle.
    result = random_shapes((128, 128), max_shapes=1, shape='rectangle',
                           multichannel=False, random_seed=0)

    # We get back a tuple consisting of (1) the image with the generated shapes
    # and (2) a list of label tuples with the kind of shape (e.g. circle,
    # rectangle) and ((r0, r1), (c0, c1)) coordinates.
    image, labels = result
    print(f"Image shape: {image.shape}\nLabels: {labels}")

    # We can visualize the images.
    fig, axes = plt.subplots(nrows=2, ncols=3)
    ax = axes.ravel()
    ax[0].imshow(image, cmap='gray')
    ax[0].set_title('Grayscale shape')

    # The generated images can be much more complex. For example, let's try many
    # shapes of any color. If we want the colors to be particularly light, we can
    # set the `intensity_range` to an upper subrange of (0,255).
    image1, _ = random_shapes((128, 128), max_shapes=10,
                              intensity_range=((100, 255),))

    # Moar :)
    image2, _ = random_shapes((128, 128), max_shapes=10,
                              intensity_range=((200, 255),))
    image3, _ = random_shapes((128, 128), max_shapes=10,
                              intensity_range=((50, 255),))
    image4, _ = random_shapes((128, 128), max_shapes=10,
                              intensity_range=((0, 255),))

    for i, image in enumerate([image1, image2, image3, image4], 1):
        ax[i].imshow(image)
        ax[i].set_title(f"Colored shapes, #{i-1}")

    # These shapes are well suited to test segmentation algorithms. Often, we
    # want shapes to overlap to test the algorithm. This is also possible:
    image, _ = random_shapes((128, 128), min_shapes=5, max_shapes=10,
                             min_size=20, allow_overlap=True)
    ax[5].imshow(image)
    ax[5].set_title('Overlapping shapes')

    for a in ax:
        a.set_xticklabels([])
        a.set_yticklabels([])

    plt.show()


.. rst-class:: sphx-glr-timing

   **Total running time of the script:** ( 0 minutes  0.159 seconds)


.. _sphx_glr_download_auto_examples_edges_plot_random_shapes.py:


.. only :: html

 .. container:: sphx-glr-footer
    :class: sphx-glr-footer-example


  .. container:: binder-badge

    .. image:: https://mybinder.org/badge_logo.svg
      :target: https://mybinder.org/v2/gh/scikit-image/scikit-image/v0.17.x?filepath=notebooks/auto_examples/edges/plot_random_shapes.ipynb
      :width: 150 px


  .. container:: sphx-glr-download sphx-glr-download-python

     :download:`Download Python source code: plot_random_shapes.py <plot_random_shapes.py>`



  .. container:: sphx-glr-download sphx-glr-download-jupyter

     :download:`Download Jupyter notebook: plot_random_shapes.ipynb <plot_random_shapes.ipynb>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
