This repository has been archived on 2021-02-05. You can view files and clone it, but cannot push or open issues or pull requests.
ipcv/python_notebook/1610473_NguyenGiapPhuongDuy.ipynb
Nguyễn Anh Khoa a43837539e init
2019-05-14 22:37:19 +07:00

1 line
5.7 KiB
Plaintext

{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"name":"1610473_NguyenGiapPhuongDuy.ipynb","version":"0.3.2","provenance":[],"collapsed_sections":[]},"kernelspec":{"name":"python3","display_name":"Python 3"}},"cells":[{"metadata":{"id":"sBYcZdp9ntC7","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":353},"outputId":"a58211f6-957e-429f-df2e-284919cdab36","executionInfo":{"status":"ok","timestamp":1556182367870,"user_tz":-420,"elapsed":1115,"user":{"displayName":"Phương Duy Nguyễn Giáp","photoUrl":"","userId":"16644867781124154449"}}},"cell_type":"code","source":["import numpy as np\n","import scipy.ndimage as ndi\n","np.set_printoptions(suppress=True)\n","\n","I1 = np.array([\n"," [5, 0, 0, 1, 2],\n"," [2, 1, 5, 1, 2],\n"," [7, 1, 5, 1, 2],\n"," [7, 4, 5, 4, 3],\n"," [7, 1, 6, 1, 3]\n","])\n","\n","Hx = np.array([\n"," [-1, 0, 1],\n"," [-2, 0, 2],\n"," [-1, 0, 1]\n","])\n","\n","Hy = np.array([\n"," [-1, -2, -1],\n"," [ 0, 0, 0],\n"," [ 1, 2, 1]\n","])\n","\n","\n","\n","print(\"1.a.\")\n","\n","sobelx = ndi.convolve(I1, Hx)\n","sobely = ndi.convolve(I1, Hy)\n","\n","x, y = 0, 0\n","print(\"Gradient vector at ({0}, {1}):\".format(x, y), np.array([sobelx[x, y], sobely[x, y]]))\n","x, y = 1, 1\n","print(\"Gradient vector at ({0}, {1}):\".format(x, y), np.array([sobelx[x, y], sobely[x, y]]))\n","x, y = 0, 3\n","print(\"Gradient vector at ({0}, {1}):\".format(x, y), np.array([sobelx[x, y], sobely[x, y]]))\n","\n","\n","\n","print(\"\\n\\n1.b.\")\n","H = np.zeros(8)\n","for i in I1.flatten():\n"," H[i] += 1\n","H = H/H.sum()\n","print(\"Normalized histogram:\")\n","print(H)\n","\n","\n","\n","print(\"\\n\\n1.c.\")\n","\n","hist_c = np.cumsum(H)\n","hist_c = hist_c/hist_c.max()\n","target = np.cumsum(np.ones(8))\n","target = target/target.max()\n","\n","light = hist_c.copy()\n","for x in range(len(hist_c)):\n"," l = np.where(np.abs(target - hist_c[x]) < 1/16)[0]\n"," if len(l) > 0:\n"," light[x] = l[0]\n","I2 = I1.copy()\n","for r in range(I1.shape[0]):\n"," for c in range(I1.shape[1]):\n"," I2[r, c] = light[I2[r, c]]\n","\n","print(\"I2:\")\n","print(I2)\n","\n","newH = np.zeros(8)\n","for i in I2.flatten():\n"," newH[i] += 1\n","newH = newH/newH.sum()\n","print(\"New histogram:\")\n","print(newH)"],"execution_count":21,"outputs":[{"output_type":"stream","text":["1.a.\n","Gradient vector at (0, 0): [16 8]\n","Gradient vector at (1, 1): [ 1 -9]\n","Gradient vector at (0, 3): [-3 -5]\n","\n","\n","1.b.\n","Normalized histogram:\n","[0.08 0.28 0.16 0.08 0.08 0.16 0.04 0.12]\n","\n","\n","1.c.\n","I2:\n","[[6 0 0 2 3]\n"," [3 2 6 2 3]\n"," [7 2 6 2 3]\n"," [7 4 6 4 4]\n"," [7 2 6 2 4]]\n","New histogram:\n","[0.08 0. 0.28 0.16 0.16 0. 0.2 0.12]\n"],"name":"stdout"}]},{"metadata":{"id":"NJOPvJYDnx5J","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":319},"outputId":"e7488bc0-5d52-4d23-8ee0-fbcddf337b32","executionInfo":{"status":"ok","timestamp":1556182381830,"user_tz":-420,"elapsed":884,"user":{"displayName":"Phương Duy Nguyễn Giáp","photoUrl":"","userId":"16644867781124154449"}}},"cell_type":"code","source":["print(\"2.a.\")\n","N = 2\n","x = np.tile(np.arange(0, N), (N, 1))\n","WDFT = np.exp(-2j*np.pi/N*x*x.T) # u = x.T\n","print(\"DFT kernel for 2 points:\")\n","print(WDFT, end = \"\\n\\n\")\n","fx = [1, 3]\n","res = np.matmul(WDFT, fx)\n","print(\"DFT result for fx =\", fx)\n","print(res)\n","\n","print(\"\\n\\n2.b.\")\n","N = 4\n","x = np.tile(np.arange(0, N), (N, 1))\n","WDCT = np.cos((x + 0.5)*x.T*np.pi/N)*np.sqrt(2/N)\n","WDCT[0] = WDCT[0]/np.sqrt(1/2)\n","print(\"DCT kernel for 4 points:\")\n","print(WDCT, end = \"\\n\\n\")\n","fx = [1, 0, 1]\n","res = np.matmul(WDCT, fx + [0])\n","print(\"DCT result for fx =\", fx)\n","print(res[:-1])\n"],"execution_count":22,"outputs":[{"output_type":"stream","text":["2.a.\n","DFT kernel for 2 points:\n","[[ 1.+0.j 1.+0.j]\n"," [ 1.+0.j -1.-0.j]]\n","\n","DFT result for fx = [1, 3]\n","[ 4.+0.j -2.-0.j]\n","\n","\n","2.b.\n","DCT kernel for 4 points:\n","[[ 1. 1. 1. 1. ]\n"," [ 0.65328148 0.27059805 -0.27059805 -0.65328148]\n"," [ 0.5 -0.5 -0.5 0.5 ]\n"," [ 0.27059805 -0.65328148 0.65328148 -0.27059805]]\n","\n","DCT result for fx = [1, 0, 1]\n","[2. 0.38268343 0. ]\n"],"name":"stdout"}]},{"metadata":{"id":"O6zB3Chtn2H0","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":185},"outputId":"7bc34eae-9cf5-452b-8deb-32657685401b","executionInfo":{"status":"ok","timestamp":1556182392871,"user_tz":-420,"elapsed":1197,"user":{"displayName":"Phương Duy Nguyễn Giáp","photoUrl":"","userId":"16644867781124154449"}}},"cell_type":"code","source":["print(\"3.a.\")\n","translate = np.array([\n"," [1, 0, -3],\n"," [0, 1, -3],\n"," [0, 0, 1]\n","])\n","\n","t = np.radians(45)\n","rotate = np.array([\n"," [np.cos(t), -np.sin(t), 0],\n"," [np.sin(t), np.cos(t), 0],\n"," [0, 0, 1]\n","])\n","\n","itranslate = np.array([\n"," [1, 0, 3],\n"," [0, 1, 3],\n"," [0, 0, 1]\n","])\n","\n","resulta = np.matmul(np.matmul(itranslate, rotate), translate)\n","print(\"Rotate matrix:\")\n","print(resulta)\n","\n","print(\"\\n\\n3.b.\")\n","import numpy.linalg\n","x, y = 2, 3\n","resultb = np.matmul(np.linalg.inv(resulta), [y, x, 1])\n","resultb = np.round(resultb) # zero-order\n","print(\"Source of I({0}, {1}) is\".format(x, y))\n","print(resultb[:-1])"],"execution_count":23,"outputs":[{"output_type":"stream","text":["3.a.\n","Rotate matrix:\n","[[ 0.70710678 -0.70710678 3. ]\n"," [ 0.70710678 0.70710678 -1.24264069]\n"," [ 0. 0. 1. ]]\n","\n","\n","3.b.\n","Source of I(2, 3) is\n","[2. 2.]\n"],"name":"stdout"}]}]}