{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Vector field color coding\n=========================\n\nDemonstration of colormap application to a vector field.\n\nPyimof provides the :func:`pyimof.display.plot` that displays a color\nmapping applyed to a dense vector field according to its orientation\nand magnitude. Any circular colormap can be applyed. Matplotlib\nprovides some of them by default: ``hsv``, ``twilight``,\n``twilight_shifted``, ``hsv_r``, ``twilight_r``,\n``twilight_shifted_r``.\n\nIf no colormap is provided to the :func:`pyimof.display.plot`\nfunction, the fector field color coding is made by constructing a HSV\nimage in which the hue is the orientation of the vector flow and the\nvalue is its magnitude. The saturation is set to 1.\n\nPyimof defines the ``middlebury`` matplotlib colormap that is inspired\nby the color coding introduced by the Middlebury optical flow\nevaluation website__ for displaying algorithms results. Its reverse\nversion ``middlebury_r`` is also provided.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\nimport pyimof\n\n# --- Load the Hydrangea sequence\n\nI0, I1 = pyimof.data.hydrangea()\n\n# --- Estimate the optical flow\n\nu, v = pyimof.solvers.ilk(I0, I1)\n\n# --- Display it with different colormaps\n\nfig = plt.figure(figsize=((9, 10)))\nax_arr = fig.subplots(3, 2, True, True)\nfig.tight_layout()\n\nax0, ax1 = ax_arr[0, :]\n\nax0.imshow(I0, cmap='gray')\nax0.set_axis_off()\nax0.set_title(\"Reference image\")\n\npyimof.display.plot(u, v, ax=ax1, cmap=None)\nax1.set_title(\"Vector field to HSV image\")\n\ncmap_list = ['middlebury', 'hsv', 'twilight', 'twilight_shifted']\n\nfor ax, cm in zip(ax_arr[1:, :].ravel(), cmap_list):\n    pyimof.display.plot(u, v, ax=ax, cmap=cm)\n    ax.set_title(f\"'{cm}' colormap\")\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.7.3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}