Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Trying to understand the Python wrapper for OpenCV 3's EMD function, whose C++ documentation is [here](https://docs.opencv.org/3.4.3/d6/dc7/group__imgproc__hist.html#ga902b8e60cc7075c8947345489221e0e0). (Fun fact, OpenCV's Python bindings are [automatically generated](https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_bindings/py_bindings_basics/py_bindings_basics.html), so Python documentation isn't guaranteed.)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import cv2\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The EMD function expects \"signatures\" rather than images/matrices. For an N-dimensional matrix with a total of M elements, the signature is an M x (N+1) array. Each of the M rows corresponds to a single pixel/element in the original image/matrix. Within each row, the first value is the pixel/element value that occurs in the source image/matrix, and the remaining N values are the coordinates along each dimension of that pixel/element. In short, it’s a list of pixel values and their corresponding coordinates.\n",
"\n",
"We can generate this signature by iterating through the values in our source image and filling in the signature’s rows one-by-one. The order we go through the source image pixels doesn’t matter."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def img_to_sig(arr):\n",
" \"\"\"Convert a 2D array to a signature for cv2.EMD\"\"\"\n",
" \n",
" # cv2.EMD requires single-precision, floating-point input\n",
" sig = np.empty((arr.size, 3), dtype=np.float32)\n",
" count = 0\n",
" for i in range(arr.shape[0]):\n",
" for j in range(arr.shape[1]):\n",
" sig[count] = np.array([arr[i,j], i, j])\n",
" count += 1\n",
" return sig"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[2. 0. 0.]\n",
" [0. 0. 1.]\n",
" [0. 0. 2.]\n",
" [2. 1. 0.]\n",
" [0. 1. 1.]\n",
" [0. 1. 2.]\n",
" [0. 2. 0.]\n",
" [0. 2. 1.]\n",
" [2. 2. 2.]]\n",
"[[0. 0. 0.]\n",
" [1. 0. 1.]\n",
" [1. 0. 2.]\n",
" [2. 1. 0.]\n",
" [0. 1. 1.]\n",
" [0. 1. 2.]\n",
" [0. 2. 0.]\n",
" [2. 2. 1.]\n",
" [0. 2. 2.]]\n"
]
}
],
"source": [
"arr1 = np.array([[2, 0, 0],\n",
" [2, 0, 0],\n",
" [0, 0, 2]])\n",
"\n",
"arr2 = np.array([[0, 1, 1],\n",
" [2, 0, 0],\n",
" [0, 2, 0]])\n",
"\n",
"sig1 = img_to_sig(arr1)\n",
"sig2 = img_to_sig(arr2)\n",
"\n",
"print(sig1)\n",
"print(sig2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The output of `cv2.EMD` has three values. The first is the distance between the two signatures. This appears to be normalized in some way---adding non-moving elements will reduce the distance, and doubling all pixel values doesn't affect the distance. I'm not sure what the second element---a `None`---is. The third value is the \"flow matrix\", telling you what was moved where. Per the documentation, if the input arrays (before being converted to signatures) have `size1` and `size2` elements each, the flow is a \"`𝚜𝚒𝚣𝚎𝟷×𝚜𝚒𝚣𝚎𝟸` flow matrix: `𝚏𝚕𝚘𝚠_i,j` is a flow from i-th point of signature1 to j-th point of signature2 .\""
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.8333333134651184\n",
"None\n",
"[[0. 1. 1. 0. 0. 0. 0. 0. 0.]\n",
" [0. 0. 0. 0. 0. 0. 0. 0. 0.]\n",
" [0. 0. 0. 0. 0. 0. 0. 0. 0.]\n",
" [0. 0. 0. 2. 0. 0. 0. 0. 0.]\n",
" [0. 0. 0. 0. 0. 0. 0. 0. 0.]\n",
" [0. 0. 0. 0. 0. 0. 0. 0. 0.]\n",
" [0. 0. 0. 0. 0. 0. 0. 0. 0.]\n",
" [0. 0. 0. 0. 0. 0. 0. 0. 0.]\n",
" [0. 0. 0. 0. 0. 0. 0. 2. 0.]]\n"
]
}
],
"source": [
"dist, _, flow = cv2.EMD(sig1, sig2, cv2.DIST_L2)\n",
"\n",
"print(dist)\n",
"print(_)\n",
"print(flow)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"So that 1 in row 1, column 2 is saying that one unit of \"earth\" was moved from the coordinates in row 1 of the first signature, or (0, 0), to the coordinates in row 2 of the second signature, or (0, 1). Note that unmoved earth shows up in this flow matrix---along the diagonal in this case, since the coordinates are in the same order in the two signatures."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now to visualize this."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"def plot_flow(sig1, sig2, flow, arrow_width_scale=3):\n",
" \"\"\"Plots the flow computed by cv2.EMD\n",
" \n",
" The source images are retrieved from the signatures and\n",
" plotted in a combined image, with the first image in the\n",
" red channel and the second in the green. Arrows are\n",
" overplotted to show moved earth, with arrow thickness\n",
" indicating the amount of moved earth.\"\"\"\n",
" \n",
" img1 = sig_to_img(sig1)\n",
" img2 = sig_to_img(sig2)\n",
" combined = np.dstack((img1, img2, 0*img2))\n",
" # RGB values should be between 0 and 1\n",
" combined /= combined.max()\n",
" print('Red channel is \"before\"; green channel is \"after\"; yellow means \"unchanged\"')\n",
" plt.imshow(combined)\n",
"\n",
" flows = np.transpose(np.nonzero(flow))\n",
" for src, dest in flows:\n",
" # Skip the pixel value in the first element, grab the\n",
" # coordinates. It'll be useful later to transpose x/y.\n",
" start = sig1[src, 1:][::-1]\n",
" end = sig2[dest, 1:][::-1]\n",
" if np.all(start == end):\n",
" # Unmoved earth shows up as a \"flow\" from a pixel\n",
" # to that same exact pixel---don't plot mini arrows\n",
" # for those pixels\n",
" continue\n",
" \n",
" # Add a random shift to arrow positions to reduce overlap.\n",
" shift = np.random.random(1) * .3 - .15\n",
" start = start + shift\n",
" end = end + shift\n",
" \n",
" mag = flow[src, dest] * arrow_width_scale\n",
" plt.quiver(*start, *(end - start), angles='xy',\n",
" scale_units='xy', scale=1, color='white',\n",
" edgecolor='black', linewidth=mag/3,\n",
" width=mag, units='dots',\n",
" headlength=5,\n",
" headwidth=3,\n",
" headaxislength=4.5)\n",
" \n",
" plt.title(\"Earth moved from img1 to img2\")\n",
" \n",
"def sig_to_img(sig):\n",
" \"\"\"Convert a signature back to a 2D image\"\"\"\n",
" intsig = sig.astype(int)\n",
" img = np.empty((intsig[:, 1].max()+1, intsig[:, 2].max()+1), dtype=float)\n",
" for i in range(sig.shape[0]):\n",
" img[intsig[i, 1], intsig[i, 2]] = sig[i, 0]\n",
" return img"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAX8AAADHCAYAAADxqlPLAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvNQv5yAAAFcZJREFUeJzt3Xu4HVV5x/HvzySA3NFEDLkQeIwCUm8cwVttWsQCgvGxVIPKrWLUlgoWqxQFAa1SSrFSUAyCXEQuosVgsYgPUEABOaHcQgqmKOY0EUJCQsI98PaPWYfs7Oxz3bMvZ9bv8zz7OXv2rL3Wmj3vvGdmzew9igjMzCwvL+t0B8zMrP2c/M3MMuTkb2aWISd/M7MMOfmbmWXIyd/MLENO/iWS9FFJP+9g++dIOqGkuqZLWitpXJq+UdKRZdSd6vuZpMPKqs+aJ+kkSd9PzzdY/x3oS6nbkqSFkmal5y8tZ0l1Hy/pu2XV1y6VTf6S3iXpV5JWS1op6ZeS3prmHS7plibrnyEpJI3vfy0iLomI9zbb9wHa+52kpyWtkbQqLdunJL20DiPiUxHxlWHW9Z7BykTE7yNiy4h4oYS+b7SxRcR+EXFhs3XnriYu1tY8zmq23jLXfz1JF0h6LsXyGkn3Sfq6pG1q2h/Wtp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LMcZjuys+w5J4WVqglDF/MzMbW/zzDmZmGco2+UvaV9IDkhZLOq7T/RktSedLelTSfZ3uS7OG83MKNjTHdnfp1rjOcthH0jjgQWAfoA+4Azi4wdf2u56kd1NcN35RROze6f40Q9JkYHLtzykAHxiL66VTHNvdp1vjOtc9/z2BxRHxUEQ8B1wGzO5wn0YlIm4CVna6H2XwzymUwrHdZbo1rnNN/lOAJTXTfXTByrD1hvg5BRuYY7uLdVNc55r8G12+l9/4V5ca4ucUbHCO7S7VbXGda/LvA6bVTE8FlnaoL1Yj/ZzCj4BLIuLHne7PGOTY7kLdGNe5Jv87gJmSdpK0CTAHmN/hPmVvOD+nYENybHeZbo3rLJN/RKwDjgKupTj5ckVELOxsr0ZH0qXArcDrJPVJ+nin+9SE/p9T+LOaO2Pt3+lOjSWO7a7UlXGd5aWeZma5y3LP38wsd07+ZmYZcvI3M8uQk7+ZWYac/M3MMuTkb2aWISd/M7MMOfmbmWXo/wG8A7DePaSMyAAAAABJRU5ErkJggg==\n",
"text/plain": [
"<matplotlib.figure.Figure at 0x7fe9b5b81c18>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Red channel is \"before\"; green channel is \"after\"; yellow means \"unchanged\"\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAQ8AAAEICAYAAABBKnGGAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvNQv5yAAAGeFJREFUeJzt3XmUXGWd//H3h6wEkbBpFkIACYgKcwwRcVzIEJiBQIBx/M0PRAUBm6AgztFRjnhMwijgnBkHGR0zMMOPZTAgeyL7FmBkkU6EhEVCAkjahDUQEhKC3fn+/rg3SdGp7up+6tbSnc8r556uqvvUfb51q+pT9z51b0oRgZlZb23V6ALMrG9yeJ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ASUVORK5CYII=\n",
"text/plain": [
"<matplotlib.figure.Figure at 0x7fe9b5a505c0>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"fig, (ax1, ax2) = plt.subplots(1, 2)\n",
"ax1.imshow(arr1, cmap='gray')\n",
"ax1.set_title(\"Starting Distribution\")\n",
"ax2.imshow(arr2, cmap='gray')\n",
"ax2.set_title(\"Ending Distribution\")\n",
"plt.show()\n",
"\n",
"plot_flow(sig1, sig2, flow)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"So we see for these two distributions, 2 units of earth are distributed from the upper-left corner to the other top-row cells, another two units move along the bottom row, and a final two units in the middle row are unmoved."
]
}
],
"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.6.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.