Created
April 20, 2016 21:49
-
-
Save nassarofficial/01389ddb88cec4913234e446d467e86b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Horn-Shunk" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Input Image" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<img src=\"http://glimpglobe.com/proj/output_j77EBM-2.gif\">\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Libraries" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"import cv2\n", | |
"import numpy as np\n", | |
"from matplotlib import pyplot as plt\n", | |
"import time" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Display Image" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"def img_display(img, name):\n", | |
" plt.figure()\n", | |
" plt.imshow(img, cmap='gray', interpolation='bicubic')\n", | |
" plt.title(name)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Get Derivatives" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"averageKernel = np.array([[ 0.08333333, 0.16666667, 0.08333333],\n", | |
" [ 0.16666667, 0. , 0.16666667],\n", | |
" [ 0.08333333, 0.16666667, 0.08333333]], dtype=np.float32)\n", | |
"\n", | |
"def average(img):\n", | |
" return cv2.filter2D(img.astype(np.float32), -1, averageKernel)\n", | |
"\n", | |
"\n", | |
"def derivatives(img1, img2):\n", | |
" \n", | |
" # Convolve the images with a kernel\n", | |
" \n", | |
" kernel = 0.25 * np.array(([-1, 1], [-1, 1]))\n", | |
" kernel2 = 0.25 * np.array(([-1, -1], [1, 1]))\n", | |
" kernel = np.fliplr(kernel)\n", | |
"\n", | |
" fx = cv2.filter2D(img1, -1, kernel) + cv2.filter2D(img2, -1, kernel)\n", | |
" fy = cv2.filter2D(img1, -1, kernel2) + cv2.filter2D(img2, -1, kernel2)\n", | |
" ft = cv2.filter2D(img1, -1, 0.25 * np.ones((2, 2))) + \\\n", | |
" cv2.filter2D(img2, -1, -0.25 * np.ones((2, 2)))\n", | |
"\n", | |
" return (fx, fy, ft)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Horn-Shunk" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"def hornShunckFlow(img1, img2, alpha):\n", | |
" img1 = img1.astype(np.float32)\n", | |
" img2 = img2.astype(np.float32)\n", | |
" \n", | |
" [Idx, Idy, Idt] = derivatives(img1, img2)\n", | |
"\n", | |
" u = np.zeros_like(img1)\n", | |
" v = np.zeros_like(img1)\n", | |
"\n", | |
" for iteration in xrange(100):\n", | |
" u0 = np.copy(u)\n", | |
" v0 = np.copy(v)\n", | |
"\n", | |
" u_av = average(u0)\n", | |
" v_av = average(v0)\n", | |
"\n", | |
" u = u_av - 1.0/(alpha**2 + Idx**2 + Idy**2) * Idx * (Idx * u_av + Idy * v_av + Idt)\n", | |
" v = v_av - 1.0/(alpha**2 + Idx**2 + Idy**2) * Idy * (Idx * u_av + Idy * v_av + Idt)\n", | |
" \n", | |
" return u, v" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Load Image" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"img1 = cv2.imread('taxi1.png', cv2.IMREAD_GRAYSCALE)\n", | |
"img2 = cv2.imread('taxi2.png', cv2.IMREAD_GRAYSCALE)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Convert to numpy format" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"img1 = np.float32(img1)\n", | |
"img2 = np.float32(img2)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Calculate Horn-Shunk" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"[u,v] = hornShunckFlow(img1, img2, 0.5)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"x = np.arange(0, img1.shape[1], 1)\n", | |
"y = np.arange(0, img1.shape[0], 1)\n", | |
"x, y = np.meshgrid(x, y)\n", | |
"\n", | |
"img_display(img1, 'Horn-Schunk')\n", | |
"step = 3\n", | |
"plt.quiver(x[::step, ::step], y[::step, ::step],\n", | |
" u[::step, ::step], v[::step, ::step],\n", | |
" color='g', pivot='middle', headwidth=2, headlength=3)\n", | |
"\n", | |
"plt.show()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<img src=\"http://glimpglobe.com/proj/HornSchunk.jpg\">\n", | |
"\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 2", | |
"language": "python", | |
"name": "python2" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 2 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython2", | |
"version": "2.7.11" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment