{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# GLCM Texture Features\n\nThis example illustrates texture classification using gray level\nco-occurrence matrices (GLCMs) [1]_. A GLCM is a histogram of co-occurring\ngrayscale values at a given offset over an image.\n\nIn this example, samples of two different textures are extracted from\nan image: grassy areas and sky areas. For each patch, a GLCM with\na horizontal offset of 5 (`distance=[5]` and `angles=[0]`) is computed.\nNext, two features of the GLCM matrices are computed: dissimilarity and\ncorrelation. These are plotted to illustrate that the classes form\nclusters in feature space.\nIn a typical classification problem, the final step (not included in\nthis example) would be to train a classifier, such as logistic\nregression, to label image patches from new images.\n\n.. versionchanged:: 0.19\n           `greymatrix` was renamed to `graymatrix` in 0.19.\n.. versionchanged:: 0.19\n           `greycoprops` was renamed to `graycoprops` in 0.19.\n\n## References\n.. [1] Haralick, RM.; Shanmugam, K.,\n       \"Textural features for image classification\"\n       IEEE Transactions on systems, man, and cybernetics 6 (1973): 610-621.\n       :DOI:`10.1109/TSMC.1973.4309314`\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\n\nfrom skimage.feature import graycomatrix, graycoprops\nfrom skimage import data\n\n\nPATCH_SIZE = 21\n\n# open the camera image\nimage = data.camera()\n\n# select some patches from grassy areas of the image\ngrass_locations = [(280, 454), (342, 223), (444, 192), (455, 455)]\ngrass_patches = []\nfor loc in grass_locations:\n    grass_patches.append(\n        image[loc[0] : loc[0] + PATCH_SIZE, loc[1] : loc[1] + PATCH_SIZE]\n    )\n\n# select some patches from sky areas of the image\nsky_locations = [(38, 34), (139, 28), (37, 437), (145, 379)]\nsky_patches = []\nfor loc in sky_locations:\n    sky_patches.append(\n        image[loc[0] : loc[0] + PATCH_SIZE, loc[1] : loc[1] + PATCH_SIZE]\n    )\n\n# compute some GLCM properties each patch\nxs = []\nys = []\nfor patch in grass_patches + sky_patches:\n    glcm = graycomatrix(\n        patch, distances=[5], angles=[0], levels=256, symmetric=True, normed=True\n    )\n    xs.append(graycoprops(glcm, 'dissimilarity')[0, 0])\n    ys.append(graycoprops(glcm, 'correlation')[0, 0])\n\n# create the figure\nfig = plt.figure(figsize=(8, 8))\n\n# display original image with locations of patches\nax = fig.add_subplot(3, 2, 1)\nax.imshow(image, cmap=plt.cm.gray, vmin=0, vmax=255)\nfor y, x in grass_locations:\n    ax.plot(x + PATCH_SIZE / 2, y + PATCH_SIZE / 2, 'gs')\nfor y, x in sky_locations:\n    ax.plot(x + PATCH_SIZE / 2, y + PATCH_SIZE / 2, 'bs')\nax.set_xlabel('Original Image')\nax.set_xticks([])\nax.set_yticks([])\nax.axis('image')\n\n# for each patch, plot (dissimilarity, correlation)\nax = fig.add_subplot(3, 2, 2)\nax.plot(xs[: len(grass_patches)], ys[: len(grass_patches)], 'go', label='Grass')\nax.plot(xs[len(grass_patches) :], ys[len(grass_patches) :], 'bo', label='Sky')\nax.set_xlabel('GLCM Dissimilarity')\nax.set_ylabel('GLCM Correlation')\nax.legend()\n\n# display the image patches\nfor i, patch in enumerate(grass_patches):\n    ax = fig.add_subplot(3, len(grass_patches), len(grass_patches) * 1 + i + 1)\n    ax.imshow(patch, cmap=plt.cm.gray, vmin=0, vmax=255)\n    ax.set_xlabel(f\"Grass {i + 1}\")\n\nfor i, patch in enumerate(sky_patches):\n    ax = fig.add_subplot(3, len(sky_patches), len(sky_patches) * 2 + i + 1)\n    ax.imshow(patch, cmap=plt.cm.gray, vmin=0, vmax=255)\n    ax.set_xlabel(f\"Sky {i + 1}\")\n\n\n# display the patches and plot\nfig.suptitle('Grey level co-occurrence matrix features', fontsize=14, y=1.05)\nplt.tight_layout()\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
}