1 line
9.7 KiB
Plaintext
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) accordin
|