{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Random Shapes\n\nExample of generating random shapes with particular properties.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\n\nfrom skimage.draw import random_shapes\n\n# Let's start simple and generate a 128x128 image\n# with a single grayscale rectangle.\nresult = random_shapes(\n    (128, 128), max_shapes=1, shape='rectangle', channel_axis=None, rng=0\n)\n\n# We get back a tuple consisting of (1) the image with the generated shapes\n# and (2) a list of label tuples with the kind of shape (e.g. circle,\n# rectangle) and ((r0, r1), (c0, c1)) coordinates.\nimage, labels = result\nprint(f'Image shape: {image.shape}\\nLabels: {labels}')\n\n# We can visualize the images.\nfig, axes = plt.subplots(nrows=2, ncols=3)\nax = axes.ravel()\nax[0].imshow(image, cmap='gray')\nax[0].set_title('Grayscale shape')\n\n# The generated images can be much more complex. For example, let's try many\n# shapes of any color. If we want the colors to be particularly light, we can\n# set the `intensity_range` to an upper subrange of (0,255).\nimage1, _ = random_shapes((128, 128), max_shapes=10, intensity_range=((100, 255),))\n\n# Moar :)\nimage2, _ = random_shapes((128, 128), max_shapes=10, intensity_range=((200, 255),))\nimage3, _ = random_shapes((128, 128), max_shapes=10, intensity_range=((50, 255),))\nimage4, _ = random_shapes((128, 128), max_shapes=10, intensity_range=((0, 255),))\n\nfor i, image in enumerate([image1, image2, image3, image4], 1):\n    ax[i].imshow(image)\n    ax[i].set_title(f'Colored shapes, #{i-1}')\n\n# These shapes are well suited to test segmentation algorithms. Often, we\n# want shapes to overlap to test the algorithm. This is also possible:\nimage, _ = random_shapes(\n    (128, 128), min_shapes=5, max_shapes=10, min_size=20, allow_overlap=True\n)\nax[5].imshow(image)\nax[5].set_title('Overlapping shapes')\n\nfor a in ax:\n    a.set_xticklabels([])\n    a.set_yticklabels([])\n\nplt.show()"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.12.12"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}