Created
April 7, 2016 11:19
-
-
Save nassarofficial/69dbfdf54f7b08637140c782dee6e7a6 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": "code", | |
"execution_count": 6, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"import numpy as np\n", | |
"import scipy.ndimage.filters as filter\n", | |
"import matplotlib.pyplot as plt\n", | |
"import numpy as np\n", | |
"import matplotlib.image as mpimg\n", | |
"import scipy\n", | |
"from scipy.signal import sepfir2d\n", | |
"from scipy.signal import cspline2d\n", | |
"import numpy.matlib\n", | |
"from numpy.linalg import inv\n", | |
"import math" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Implementation of convmtx2" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"def my_convmtx2(kernel, patch_size):\n", | |
" s = kernel.shape\n", | |
" m = patch_size - s[0] + 1\n", | |
" n = patch_size - s[1] + 1\n", | |
" mtx = np.zeros((m * n, patch_size ** 2))\n", | |
"\n", | |
" k = 0\n", | |
" for i in xrange(0, m):\n", | |
" for j in xrange(0, n):\n", | |
" for p in xrange(0, s[0]):\n", | |
" l1 = (i + p) * patch_size + j\n", | |
" l2 = l1 + s[1]\n", | |
" mtx[k, l1:l2] = kernel[p, :]\n", | |
" k += 1\n", | |
" return mtx\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"f = mpimg.imread('photo3.png')[:,:,0].astype(np.float32)\n", | |
"shape = f.shape\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<img src=\"https://dl.dropboxusercontent.com/u/46105572/ucim/orig.png\">" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"kernel = np.array([[1.,1.,1.],[1.,1.,1.],[1.,1.,1.]], dtype=np.float)\n", | |
"\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"H = my_convmtx2(kernel, shape[0])\n", | |
"\n", | |
"# Flattened Image to Vector\n", | |
"f_vec = f.flatten()\n", | |
"\n", | |
"# Size of Image vector\n", | |
"shqrt = math.sqrt(f_vec.size)\n", | |
"\n", | |
"\n", | |
"# THE BLURRING OPERATION\n", | |
"hf = np.dot(H,f_vec)\n", | |
"\n", | |
"# Putting the blurered vec into a matrix\n", | |
"hf = np.reshape(hf, (shqrt-2, shqrt-2))\n", | |
"\n", | |
"\n", | |
"# Adding noise to get G\n", | |
"g = hf + 0.04*hf.std()*np.random.random(hf.shape)\n", | |
"z = g\n", | |
"# Putting G into a vector\n", | |
"g_vec = g.flatten()\n", | |
"\n", | |
"# Pseudo Inverse\n", | |
"inver = np.linalg.pinv(H)\n", | |
"\n", | |
"# Inverse * g\n", | |
"hG = np.dot(inver,g_vec)\n", | |
"\n", | |
"# Putting the restored vector into a matrix\n", | |
"res = np.reshape(hG, (shqrt, shqrt))\n", | |
"\n", | |
"print res.shape\n", | |
"\n", | |
"plt.subplot(121),plt.imshow(z, cmap = 'gray')\n", | |
"plt.subplot(122),plt.imshow(res, cmap = 'gray')\n", | |
"plt.title('Output'), plt.xticks([]), plt.yticks([])\n", | |
"plt.savefig('f0.png')\n", | |
"plt.show()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Distorted Image" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<img src=\"https://dl.dropboxusercontent.com/u/46105572/ucim/g.png\">" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Restored Image" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<img src=\"https://dl.dropboxusercontent.com/u/46105572/ucim/res.png\">" | |
] | |
}, | |
{ | |
"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