Skip to content

Instantly share code, notes, and snippets.

@nassarofficial
Created April 20, 2016 21:51
Show Gist options
  • Save nassarofficial/8a5092f983d8a4d92ad9619480e3ee54 to your computer and use it in GitHub Desktop.
Save nassarofficial/8a5092f983d8a4d92ad9619480e3ee54 to your computer and use it in GitHub Desktop.
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Lucas Kanade"
]
},
{
"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": 8,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import cv2\n",
"import numpy as np\n",
"from matplotlib import pyplot as plt"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Displaying Image"
]
},
{
"cell_type": "code",
"execution_count": 9,
"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": 10,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"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": [
"### Lucas Kanade Function"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def lucas_kanade(img1, img2,window):\n",
" \n",
" ## Get derivateives\n",
" Idx, Idy, Idt = derivatives(img1, img2)\n",
"\n",
" # It\n",
" It = cv2.filter2D(Idx**2, -1, window)*cv2.filter2D(Idy**2, -1, window) - \\\n",
" cv2.filter2D((Idx*Idy), -1, window)**2\n",
" \n",
" It[It == 0] = np.inf\n",
"\n",
" ##A(1,1) + Ix(m,n)*Ix(m,n)\n",
" \n",
" u = (-cv2.filter2D(Idy**2, -1, window) * cv2.filter2D(Idx*Idt, -1, window) +\n",
" cv2.filter2D(Idx*Idy, -1, window) * cv2.filter2D(Idy*Idt, -1, window)) / \\\n",
" It\n",
" \n",
" v = (cv2.filter2D(Idx*Idt, -1, window) * cv2.filter2D(Idx*Idy, -1, window) -\n",
" cv2.filter2D(Idx**2, -1, window) * cv2.filter2D(Idy*Idt, -1, window)) / \\\n",
" It\n",
"\n",
" return (u, v)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Loading the image"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"img1 = cv2.imread('car1.png', cv2.IMREAD_GRAYSCALE)\n",
"img2 = cv2.imread('car2.png', cv2.IMREAD_GRAYSCALE)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Convert to numpy format"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"img1 = np.float32(img1)\n",
"img2 = np.float32(img2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Calculate Lucas Kanade"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"u, v = lucas_kanade(img1, img2, np.ones((3, 3)))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Display "
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": false
},
"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, 'Lucas Kanade')\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/LucasKanade.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