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/1611743_NguyenQuangHoangLam.ipynb
Nguyễn Anh Khoa a43837539e init
2019-05-14 22:37:19 +07:00

1 line
9.7 KiB
Plaintext

{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"name":"1611743_NguyenQuangHoangLam.ipynb","version":"0.3.2","provenance":[],"collapsed_sections":[]},"kernelspec":{"name":"python3","display_name":"Python 3"}},"cells":[{"metadata":{"id":"xU5SBsbeXfHq","colab_type":"code","outputId":"79b2b3cb-49b0-49f3-955d-42a3c53cdfaf","executionInfo":{"status":"ok","timestamp":1556184069784,"user_tz":-420,"elapsed":1154,"user":{"displayName":"LÂM NGUYỄN QUANG HOÀNG","photoUrl":"","userId":"12690077016043803462"}},"colab":{"base_uri":"https://localhost:8080/","height":1187}},"cell_type":"code","source":["import numpy as np\n","from scipy import signal\n","from scipy import fftpack\n","import matplotlib.pyplot as plt \n","\n","\n","print(\"Question 1: \")\n","### Question 1\n","print(\"a/\")\n","##a \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","\n","H_sobel_x = np.array([[-1,0,1],[-2,0,2],[-1,0,1]])\n","H_sobel_y = np.array([[-1,-2,-1],[0,0,0],[1,2,1]])\n","\n","grad_x = signal.convolve2d(I1,H_sobel_x,boundary='symm',mode ='same')\n","grad_y = signal.convolve2d(I1,H_sobel_y,boundary='symm',mode = 'same')\n","\n","\n","print(f\"Gradient of (0,0) according to x and y: {grad_x[0,0]} {grad_y[0,0]}\")\n","print(f\"Gradient of (1,1) according to x and y: {grad_x[1,1]} {grad_y[1,1]}\")\n","print(f\"Gradient of (0,3) according to x and y: {grad_x[0,3]} {grad_y[0,3]}\")\n","\n","##b\n","print(\"b/\")\n","n = I1.size\n","hist1 = np.bincount(I1.flatten())/n\n","print(\"Normalized Histogram of I1: \")\n","print(hist1)\n","\n","##c\n","print(\"c/\")\n","hist2 = np.array([1/8 for i in range(8)])\n","cdf1 = np.cumsum(hist1)\n","cdf2 = np.cumsum(hist2)\n","\n","## equalize \n","print(\"I2: \")\n","I2 = np.zeros((N,N))\n","N = I1.shape[0]\n","for i in range(N):\n"," for j in range(N):\n"," x = I1[i,j]\n"," c_x = cdf1[x]\n"," for k in range(len(cdf2)):\n"," if cdf2[k] == c_x:\n"," I2[i,j] = k\n"," \n","print(I2.astype(np.int))\n","\n","print(\"Histogram of I2: \")\n","plt.hist(I2.flatten(),8,[0,8])\n","plt.show()\n","\n"," \n","### Question 2 \n","print(\"Question 2: \")\n","#a\n","print(\"a/\")\n","f = np.array([1,2])\n","\n","A = np.arange(2).reshape((1,2))\n","\n","M = A.reshape((2,1)).dot(A)\n","\n","M_dft = np.round(np.exp(1)**(-2j*(np.pi/2)*M))\n","\n","print(\"Based vectors after DFT: \")\n","i=0\n","for e in M_dft:\n"," print(f\"e{i}: {e}\")\n"," i+=1\n"," \n","#b \n","print(\"b/\")\n","M = 4\n","u = np.arange(M).reshape((M,1))\n","x = 2*u+1\n","W_dct = np.dot(u,x.T)\n","W_dct = np.cos(np.pi*W_dct/(2*M))\n","for _ in range(M):\n"," if _ == 0:\n"," W_dct[_,:]*= np.sqrt(1/M)\n"," else:\n"," W_dct[_,:]*= np.sqrt(2/M)\n","\n","print(\"Based vectors after DCT for 4 dims: \")\n","i=0\n","for e in W_dct:\n"," print(f\"e{i}: {e}\")\n"," i+=1\n"," \n","IW = np.round(W_dct.dot(W_dct.T)).astype(np.int32)\n","print(f\"W_dct.dot(W_dct.T): \\n {IW}\")\n","\n","if(np.array_equal(IW,np.eye(4))):\n"," print(\"W is orthor\")\n","\n","\n","fx = np.array([1,0,1])\n","\n","fx_dct = fftpack.dct(fx)\n","\n","print(f\"dct of fx: {fx_dct}\")\n","\n","### Question 3:\n","\n","print(\"Question 3: \")\n","print(\"a/\")\n","phi = np.pi/4\n","H2 = np.array([[np.cos(phi),-np.sin(phi),0],[np.sin(phi),np.cos(phi),0],[0,0,1]])\n","H1 = np.array([[1,0,-3],[0,1,-3],[0,0,1]])\n","H3 = np.array([[1,0,3],[0,1,3],[0,0,1]])\n","\n","print(f\"Translate -3: \\n{H1}\")\n","print(f\"Rotate : \\n {H2}\")\n","print(f\"Translate 3: \\n{H3}\")\n","H = H3.dot(H2).dot(H1)\n","\n","print(f\"Matrix to rotate the image around (3,3): \\n {H}\")\n","\n","print(\"b/\")\n","\n","Io = np.array((2,3,1)).reshape((3,1))\n","I = np.linalg.pinv(H).dot(Io)\n","\n","x,y=I[0:2,:].flatten()\n","print(f\"x, y after zero order: {np.round(x).astype(np.int32)},{np.round(y).astype(np.int32)}\")\n","\n"],"execution_count":84,"outputs":[{"output_type":"stream","text":["Question 1: \n","a/\n","Gradient of (0,0) according to x and y: 16 8\n","Gradient of (1,1) according to x and y: 1 -9\n","Gradient of (0,3) according to x and y: -3 -5\n","b/\n","Normalized Histogram of I1: \n","[0.08 0.28 0.16 0.08 0.08 0.16 0.04 0.12]\n","c/\n","I2: \n","[[0 0 0 0 0]\n"," [0 0 0 0 0]\n"," [7 0 0 0 0]\n"," [7 0 0 0 0]\n"," [7 0 0 0 0]]\n","Histogram of I2: \n"],"name":"stdout"},{"output_type":"display_data","data":{"image/png":"iVBORw0KGgoAAAANSUhEUgAAAXQAAAD8CAYAAABn919SAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4zLCBo\ndHRwOi8vbWF0cGxvdGxpYi5vcmcvnQurowAAC99JREFUeJzt3W+MZfVdx/H3pyymhVZLs+Nm5Y/T\nGEJCTIRmgtU2DUppqDQFn5iS2BDTZPugNaAmBvuk+gwTrT4xTVZA1khpkD8psaRCKAk2UewsRVn+\nVCpCu+vCDiEKGJMK/fpgztZxs9OZ+2f23P3m/Uomc++5Z+Z8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Qa7gHck\n2QWcBfz7Th3odAi6bzEwpSTLwKXAY+NOsm5Y1ngCOAY8VFULMRfwp8DvAj8Ye5ATFPBgkoPDFdeL\n4L3AGvAXwxLVLUnOHnuoE3wCuHPsIQCq6gjwR8B3gaPAf1bVgzt1vNMh6JpCkncC9wA3VtVrY88D\nUFVvVdUlrF9VfFmS0ZeqknwMOFZVB8ee5SQ+WFXvY/0dTT8zLPONbRfwPuCLVXUp8F/AQjyvBTAs\nAX0c+OuxZwFIcg7rKwrvBX4KODvJr+/U8U6HoG/rLQb0f4Y16nuAO6rq3rHnOdHwT/RHgKvGngX4\nAPDxYb36y8AvJ/mrcUdaN5zdUVXHgPtYX34c22Hg8IZ/Xd3NeuAXxUeBx6vq5bEHGXwY+LeqWquq\n/wHuBX5xpw52OgTdtxiYwPDk463AM1X1hbHnOS7JUpJ3D7ffwfqT3M+OOxVU1e9V1XlVtcz679bX\nq2rHzqC2K8nZw5PaDEsaHwFGf0VVVb0EfC/JRcOmK4BRn3A/wXUsyHLL4LvA+5OcNfzZvIL157V2\nxI6/2+KsFvUtBpLcCVwO7E5yGPh8Vd067lTA+hnnJ4Enh/VqgM8NV/WOaS9wYHgFwtuAu6pqYV4i\nuID2APetN4BdwJeq6mvjjvRDvwncMZxgPQ/8xsjzAD/8i+9K4NNjz3JcVT2W5G7gceBN4Fvs4BWj\nC/+yRUnS9pwOSy6SpG0w6JLUhEGXpCYMuiQ1YdAlqQmDLklNGHRJasKgS1IT/wuDycEaNT65agAA\nAABJRU5ErkJggg==\n","text/plain":["<Figure size 432x288 with 1 Axes>"]},"metadata":{"tags":[]}},{"output_type":"stream","text":["Question 2: \n","a/\n","Based vectors after DFT: \n","e0: [1.+0.j 1.+0.j]\n","e1: [ 1.+0.j -1.-0.j]\n","b/\n","Based vectors after DCT for 4 dims: \n","e0: [0.5 0.5 0.5 0.5]\n","e1: [ 0.65328148 0.27059805 -0.27059805 -0.65328148]\n","e2: [ 0.5 -0.5 -0.5 0.5]\n","e3: [ 0.27059805 -0.65328148 0.65328148 -0.27059805]\n","W_dct.dot(W_dct.T): \n"," [[1 0 0 0]\n"," [0 1 0 0]\n"," [0 0 1 0]\n"," [0 0 0 1]]\n","W is orthor\n","dct of fx: [4. 0. 2.]\n","Question 3: \n","a/\n","Translate -3: \n","[[ 1 0 -3]\n"," [ 0 1 -3]\n"," [ 0 0 1]]\n","Rotate : \n"," [[ 0.70710678 -0.70710678 0. ]\n"," [ 0.70710678 0.70710678 0. ]\n"," [ 0. 0. 1. ]]\n","Translate 3: \n","[[1 0 3]\n"," [0 1 3]\n"," [0 0 1]]\n","Matrix to rotate the image around (3,3): \n"," [[ 0.70710678 -0.70710678 3. ]\n"," [ 0.70710678 0.70710678 -1.24264069]\n"," [ 0. 0. 1. ]]\n","b/\n","x, y after zero order: 2,4\n"],"name":"stdout"}]}]}