Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 142 additions & 0 deletions Class15/.ipynb_checkpoints/Apply_Kernel-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def applyKernel(mat, kernel):\n",
" rows = mat.shape[0] - kernel.shape[0] + 1\n",
" cols = mat.shape[1] - kernel.shape[0] + 1\n",
" ker_rows = kernel.shape[0]\n",
" ker_cols = kernel.shape[1]\n",
" res = np.zeros((rows, cols))\n",
" \n",
" for ix in range(rows):\n",
" for iy in range(cols):\n",
" neighborhood = mat[ix : ix + ker_rows, iy : iy + ker_cols]\n",
" product = neighborhood * kernel\n",
" res[ix, iy] = product.sum()\n",
" return res"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Matrix : \n",
"[[ 56 29 144 227 32 1 59]\n",
" [ 72 22 134 11 35 18 98]\n",
" [ 63 161 177 108 0 11 45]\n",
" [232 95 37 126 79 119 3]\n",
" [102 132 86 39 142 8 51]\n",
" [ 15 152 188 154 163 241 179]\n",
" [170 74 65 160 132 216 107]]\n",
"Kernel : \n",
"[[-1 -4 0]\n",
" [ 4 -4 4]\n",
" [-3 4 0]]\n"
]
}
],
"source": [
"matrix = np.random.randint(0, 255, size=(7,7))\n",
"print(\"Matrix : \")\n",
"print(matrix)\n",
"\n",
"kernel = np.random.randint(-5, 5, size=(3,3))\n",
"print(\"Kernel : \")\n",
"print(kernel)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Result : \n",
"[[1019. -784. -519. -703. 468.]\n",
" [-160. -327. 491. 263. 268.]\n",
" [ 211. -185. -751. 1007. -586.]\n",
" [ 175. 393. 267. -632. 660.]\n",
" [-640. 34. 991. 369. 698.]]\n"
]
}
],
"source": [
"result = applyKernel(matrix, kernel)\n",
"print(\"Result : \")\n",
"print(result)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'\\nAuthor : Prashant Singh\\n'"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'''\n",
"Author : Prashant Singh\n",
"'''"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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
}
142 changes: 142 additions & 0 deletions Class15/Apply_Kernel.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def applyKernel(mat, kernel):\n",
" rows = mat.shape[0] - kernel.shape[0] + 1\n",
" cols = mat.shape[1] - kernel.shape[0] + 1\n",
" ker_rows = kernel.shape[0]\n",
" ker_cols = kernel.shape[1]\n",
" res = np.zeros((rows, cols))\n",
" \n",
" for ix in range(rows):\n",
" for iy in range(cols):\n",
" neighborhood = mat[ix : ix + ker_rows, iy : iy + ker_cols]\n",
" product = neighborhood * kernel\n",
" res[ix, iy] = product.sum()\n",
" return res"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Matrix : \n",
"[[ 56 29 144 227 32 1 59]\n",
" [ 72 22 134 11 35 18 98]\n",
" [ 63 161 177 108 0 11 45]\n",
" [232 95 37 126 79 119 3]\n",
" [102 132 86 39 142 8 51]\n",
" [ 15 152 188 154 163 241 179]\n",
" [170 74 65 160 132 216 107]]\n",
"Kernel : \n",
"[[-1 -4 0]\n",
" [ 4 -4 4]\n",
" [-3 4 0]]\n"
]
}
],
"source": [
"matrix = np.random.randint(0, 255, size=(7,7))\n",
"print(\"Matrix : \")\n",
"print(matrix)\n",
"\n",
"kernel = np.random.randint(-5, 5, size=(3,3))\n",
"print(\"Kernel : \")\n",
"print(kernel)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Result : \n",
"[[1019. -784. -519. -703. 468.]\n",
" [-160. -327. 491. 263. 268.]\n",
" [ 211. -185. -751. 1007. -586.]\n",
" [ 175. 393. 267. -632. 660.]\n",
" [-640. 34. 991. 369. 698.]]\n"
]
}
],
"source": [
"result = applyKernel(matrix, kernel)\n",
"print(\"Result : \")\n",
"print(result)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'\\nAuthor : Prashant Singh\\n'"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'''\n",
"Author : Prashant Singh\n",
"'''"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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
}