Commit b428fbbd authored by Nathaniel Callens's avatar Nathaniel Callens

basic fixes

parents 093ce109 829e9198
No preview for this file type
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -203,7 +203,6 @@
"metadata": {},
"outputs": [],
"source": [
"\n",
"def huffman_nb(image):\n",
" origin, predict, diff, error, A = plot_hist(image)\n",
" image = Image.open(image)\n",
......@@ -237,15 +236,14 @@
" \n",
" \n",
"def compress_rate_nb(image, error, encoding):\n",
" #original = original.reshape(-1)\n",
" #error = error.reshape(-1)\n",
" original = image.reshape(-1)\n",
" error = error.reshape(-1)\n",
" o_len = 0\n",
" c_len = 0\n",
" for i in range(0, len(original)):\n",
" o_len += len(bin(original[i])[2:])\n",
" c_len += len(encoding[str(int(error[i]))])\n",
"\n",
" \n",
" return c_len/o_len\n",
"\n"
]
......@@ -410,7 +408,8 @@
" image = Image.open(image)\n",
" image = np.array(image)[1:,:] #Convert to an array, leaving out the first row because the first row is just housekeeping data\n",
" image = image.astype(int)\n",
"\n",
" \n",
" bins = np.linspace(min(diff),max(diff),5)[1:-1]\n",
" \n",
" boundary = np.hstack((image[0,:],image[-1,:],image[1:-1,0],image[1:-1,-1]))\n",
" boundary = boundary - image[0,0]\n",
......@@ -423,7 +422,7 @@
" encode1 = huffman_code_tree(node)\n",
" \n",
" \n",
" mask = diff <= 100\n",
" mask = diff <= bins[0]\n",
" string = [str(i) for i in error[mask].astype(int)]\n",
" freq = dict(Counter(string))\n",
" freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)\n",
......@@ -431,20 +430,20 @@
" encode2 = huffman_code_tree(node)\n",
"\n",
" \n",
" mask = diff > 100\n",
" #new_error = error[mask]\n",
" #mask2 = diff[mask] <= 200\n",
" #string = [str(i) for i in new_error[mask2].astype(int)]\n",
" string = [str(i) for i in error[mask].astype(int)]\n",
" mask = diff > bins[0]\n",
" new_error = error[mask]\n",
" mask2 = diff[mask] <= bins[1]\n",
" string = [str(i) for i in new_error[mask2].astype(int)]\n",
" #string = [str(i) for i in error[mask].astype(int)]\n",
" freq = dict(Counter(string))\n",
" freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)\n",
" node = make_tree(freq)\n",
" encode3 = huffman_code_tree(node)\n",
" \n",
"\n",
" '''mask = diff > 200\n",
" mask = diff > bins[1]\n",
" new_error = error[mask]\n",
" mask2 = diff[mask] <= 300\n",
" mask2 = diff[mask] <= bins[2]\n",
" string = [str(i) for i in new_error[mask2].astype(int)]\n",
" freq = dict(Counter(string))\n",
" freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)\n",
......@@ -452,12 +451,13 @@
" encode4 = huffman_code_tree(node)\n",
" \n",
" \n",
" mask = diff > 300\n",
" mask = diff > bins[2]\n",
" string = [str(i) for i in error[mask].astype(int)]\n",
" freq = dict(Counter(string))\n",
" freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)\n",
" node = make_tree(freq)\n",
" encode5 = huffman_code_tree(node)'''\n",
" encode5 = huffman_code_tree(node)\n",
" \n",
"\n",
" \n",
" \n",
......@@ -474,10 +474,10 @@
" \n",
" # return the huffman dictionary\n",
" #return encode1, encode2, encode3, encode4, encode5, np.ravel(image), error, diff, boundary\n",
" return encode1, encode2, encode3, np.ravel(image), error, diff, boundary\n",
" return [encode1, encode2, encode3, encode4, encode5], np.ravel(image), error, diff, boundary, bins\n",
"\n",
"#def compress_rate_u(image, error, diff, bound, encode1, encode2, encode3, encode4, encode5):\n",
"def compress_rate_u(image, error, diff, bound, encode1, encode2, encode3):\n",
"def compress_rate_u(image, error, diff, bound, list_dic, bins):\n",
" #original = original.reshape(-1)\n",
" #error = error.reshape(-1)\n",
" o_len = 0\n",
......@@ -488,30 +488,23 @@
"\n",
" for i in range(0,len(bound)):\n",
" o_len += len(bin(real_b[i])[2:])\n",
" c_len += len(encode1[str(bound[i])])\n",
" c_len += len(list_dic[0][str(bound[i])])\n",
" \n",
" for i in range(0, len(original)):\n",
" o_len += len(bin(original[i])[2:])\n",
" if diff[i] <= 100:\n",
" c_len += len(encode2[str(int(error[i]))])\n",
" if diff[i] <= bins[0]:\n",
" c_len += len(list_dic[1][str(int(error[i]))])\n",
" \n",
" if diff[i] > 100:\n",
" c_len += len(encode3[str(int(error[i]))])\n",
"\n",
" '''if diff[i] <= 200 and diff[i] > 100:\n",
" c_len += len(encode3[str(int(error[i]))])'''\n",
" if diff[i] <= bins[1] and diff[i] > bins[0]:\n",
" c_len += len(list_dic[2][str(int(error[i]))])\n",
" \n",
" '''if diff[i] <= 300 and diff[i] > 200:\n",
" c_len += len(encode4[str(int(error[i]))])\n",
" if diff[i] <= bins[2] and diff[i] > bins[1]:\n",
" c_len += len(list_dic[3][str(int(error[i]))])\n",
" \n",
" if diff[i] > 300:\n",
" c_len += len(encode5[str(int(error[i]))])'''\n",
" if diff[i] > bins[2]:\n",
" c_len += len(list_dic[4][str(int(error[i]))])\n",
" \n",
" return c_len/o_len\n",
"scenes = file_extractor()\n",
"images = image_extractor(scenes)\n",
"encode1, encode2, encode3, image, error, diff, boundary = huffman_u(images[0])\n",
"compress_rate_u(image, error, diff, boundary, encode1, encode2, encode3)\n"
" return c_len/o_len\n"
]
},
{
......
......@@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 29,
"execution_count": 63,
"id": "14f74f21",
"metadata": {},
"outputs": [],
......@@ -24,7 +24,7 @@
},
{
"cell_type": "code",
"execution_count": 80,
"execution_count": 64,
"id": "c16af61f",
"metadata": {},
"outputs": [],
......@@ -49,15 +49,6 @@
" continue\n",
" else:\n",
" image_folder.append(os.path.join(scene, file))\n",
" '''print(image_folder)\n",
" images = []\n",
" for folder in image_folder:\n",
" ims = os.listdir(folder)\n",
" for im in ims:\n",
" if im[-4:] == \".jp4\" or im[-7:] == \"_6.tiff\":\n",
" continue\n",
" else:\n",
" images.append(os.path.join(folder, im))'''\n",
" return image_folder #returns a list of file paths to .tiff files in the specified directory given in file_extractor\n",
"\n",
"def im_distribution(images, num):\n",
......@@ -87,7 +78,7 @@
},
{
"cell_type": "code",
"execution_count": 31,
"execution_count": 65,
"id": "aceba613",
"metadata": {},
"outputs": [],
......@@ -124,17 +115,15 @@
" neighbor = np.vstack((z0,z1,z2,z3)).T\n",
" # calculate the difference\n",
" diff = np.max(neighbor,axis = 1) - np.min(neighbor, axis=1)\n",
" \n",
" # flatten the image to a vector\n",
" image = np.ravel(image[1:-1,1:-1])\n",
" error = image-predict\n",
" # calculate the error\n",
" error = np.ravel(image[1:-1,1:-1])-predict\n",
" \n",
" return image, predict, diff, error, A"
]
},
{
"cell_type": "code",
"execution_count": 32,
"execution_count": 66,
"id": "6b965751",
"metadata": {},
"outputs": [],
......@@ -182,65 +171,78 @@
},
{
"cell_type": "code",
"execution_count": 111,
"execution_count": 67,
"id": "b7561883",
"metadata": {},
"outputs": [],
"source": [
"def huffman(image, bins):\n",
" origin, predict, diff, error, A = plot_hist(image)\n",
"def huffman(image, num_bins):\n",
" # get the prediction error and difference\n",
" image, predict, diff, error, A = plot_hist(image)\n",
" \n",
" image = Image.open(image)\n",
" image = np.array(image)[1:,:] #Convert to an array, leaving out the first row because the first row is just housekeeping data\n",
" image = image.astype(int)\n",
" # get the number of points in each bins\n",
" data_points_per_bin = len(diff) // num_bins\n",
" \n",
" # sort the difference and create the bins\n",
" sorted_diff = diff.copy()\n",
" sorted_diff.sort()\n",
" bins = [sorted_diff[i*data_points_per_bin] for i in range(1,num_bins)]\n",
" \n",
" # get the boundary \n",
" boundary = np.hstack((image[0,:],image[-1,:],image[1:-1,0],image[1:-1,-1]))\n",
" \n",
" # take the difference of the boundary with the very first pixel\n",
" boundary = boundary - image[0,0]\n",
" boundary[0] = image[0,0]\n",
"\n",
" string = [str(i) for i in boundary]\n",
" freq = dict(Counter(string))\n",
" freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)\n",
" node = make_tree(freq)\n",
" encode1 = huffman_code_tree(node)\n",
" \n",
" \n",
" mask = diff <= bins[0]\n",
" string = [str(i) for i in error[mask].astype(int)]\n",
" freq = dict(Counter(string))\n",
" freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)\n",
" node = make_tree(freq)\n",
" encode2 = huffman_code_tree(node)\n",
"\n",
" \n",
" mask = diff > bins[0]\n",
" new_error = error[mask]\n",
" mask2 = diff[mask] <= bins[1]\n",
" string = [str(i) for i in new_error[mask2].astype(int)]\n",
" freq = dict(Counter(string))\n",
" freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)\n",
" node = make_tree(freq)\n",
" encode3 = huffman_code_tree(node)\n",
" \n",
"\n",
" mask = diff > bins[1]\n",
" new_error = error[mask]\n",
" mask2 = diff[mask] <= bins[2]\n",
" string = [str(i) for i in new_error[mask2].astype(int)]\n",
" # huffman encode the boundary\n",
" string = [str(i) for i in boundary]\n",
" freq = dict(Counter(string))\n",
" freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)\n",
" node = make_tree(freq)\n",
" encode4 = huffman_code_tree(node)\n",
" encode = huffman_code_tree(node)\n",
" \n",
" # create a list of huffman table\n",
" list_dic = [encode]\n",
" n = len(bins)\n",
" \n",
" mask = diff > bins[2]\n",
" # loop through different bins\n",
" for i in range (0,n):\n",
" # the fisrt bin\n",
" if i == 0 :\n",
" # get the point within the bin and huffman encode\n",
" mask = diff <= bins[i]\n",
" string = [str(i) for i in error[mask].astype(int)]\n",
" freq = dict(Counter(string))\n",
" freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)\n",
" node = make_tree(freq)\n",
" encode = huffman_code_tree(node)\n",
" list_dic.append(encode)\n",
" \n",
" # the middle bins\n",
" else:\n",
" # get the point within the bin and huffman encode\n",
" mask = diff > bins[i-1]\n",
" new_error = error[mask]\n",
" mask2 = diff[mask] <= bins[i]\n",
" string = [str(i) for i in new_error[mask2].astype(int)]\n",
" freq = dict(Counter(string))\n",
" freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)\n",
" node = make_tree(freq)\n",
" encode = huffman_code_tree(node)\n",
" list_dic.append(encode)\n",
" \n",
" # the last bin \n",
" # get the point within the bin and huffman encode\n",
" mask = diff > bins[-1]\n",
" string = [str(i) for i in error[mask].astype(int)]\n",
" freq = dict(Counter(string))\n",
" freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)\n",
" node = make_tree(freq)\n",
" encode5 = huffman_code_tree(node)\n",
"\n",
" encode = huffman_code_tree(node)\n",
" list_dic.append(encode)\n",
"\n",
" # create a error matrix that includes the boundary (used in encoding matrix)\n",
" new_error = np.copy(image)\n",
" new_error[1:-1,1:-1] = np.reshape(error,(510, 638))\n",
" keep = new_error[0,0]\n",
......@@ -250,28 +252,23 @@
" new_error[1:-1,-1] = new_error[1:-1,-1] - keep\n",
" new_error[0,0] = keep\n",
" \n",
" \n",
" #new_error = np.ravel(new_error)\n",
" \n",
" \n",
" list_dic = [encode1, encode2, encode3, encode4, encode5]\n",
" # return the huffman dictionary\n",
" return list_dic, np.ravel(image), error, new_error, diff, boundary, predict\n",
" return list_dic, np.ravel(image), error, new_error, diff, boundary, predict, bins\n",
" \n"
]
},
{
"cell_type": "code",
"execution_count": 112,
"execution_count": 68,
"id": "2eb774d2",
"metadata": {},
"outputs": [],
"source": [
"def encoder(error, list_dic, diff, bound, bins):\n",
" # copy the error matrix (including the boundary)\n",
" encoded = np.copy(error).astype(int).astype(str).astype(object)\n",
" \n",
" diff = np.reshape(diff,(510,638))\n",
" \n",
" # loop through all the pixel to encode\n",
" for i in range(encoded.shape[0]):\n",
" for j in range(encoded.shape[1]):\n",
" if i == 0 or i == encoded.shape[0]-1 or j == 0 or j == encoded.shape[1]-1:\n",
......@@ -285,13 +282,12 @@
" else: \n",
" encoded[i][j] = list_dic[4][encoded[i][j]]\n",
"\n",
" \n",
" return encoded"
]
},
{
"cell_type": "code",
"execution_count": 113,
"execution_count": 69,
"id": "8eeb40d0",
"metadata": {},
"outputs": [],
......@@ -301,7 +297,7 @@
" Function that accecpts the prediction matrix A for the linear system,\n",
" the encoded matrix of error values, and the encoding dicitonary.\n",
" \"\"\"\n",
"\n",
" # change the dictionary back to list\n",
" the_keys0 = list(list_dic[0].keys())\n",
" the_values0 = list(list_dic[0].values())\n",
" \n",
......@@ -318,15 +314,18 @@
" the_values4 = list(list_dic[4].values())\n",
" \n",
" error_matrix = np.zeros((512,640))\n",
" \n",
" # loop through all the element in the matrix\n",
" for i in range(error_matrix.shape[0]):\n",
" for j in range(error_matrix.shape[1]):\n",
" # if it's the very first pixel on the image\n",
" if i == 0 and j == 0:\n",
" error_matrix[i][j] = int(the_keys0[the_values0.index(encoded_matrix[i,j])])\n",
" \n",
" # if it's on the boundary\n",
" elif i == 0 or i == error_matrix.shape[0]-1 or j == 0 or j == error_matrix.shape[1]-1:\n",
" error_matrix[i][j] = int(the_keys0[the_values0.index(encoded_matrix[i,j])]) + error_matrix[0][0]\n",
" # if not the boundary\n",
" else:\n",
" # predict the image with the known pixel value\n",
" z0 = error_matrix[i-1][j-1]\n",
" z1 = error_matrix[i-1][j]\n",
" z2 = error_matrix[i-1][j+1]\n",
......@@ -337,7 +336,8 @@
" y = np.vstack((y0,y1,y2))\n",
" difference = max(z0,z1,z2,z3) - min(z0,z1,z2,z3)\n",
" predict = np.round(np.round(np.linalg.solve(A,y)[-1][0],1))\n",
"\n",
" \n",
" # add on the difference by searching the dictionary\n",
" if difference <= bins[0]:\n",
" error_matrix[i][j] = int(the_keys1[the_values1.index(encoded_matrix[i,j])]) + int(predict)\n",
" elif difference <= bins[1] and difference > bins[0]:\n",
......@@ -353,17 +353,17 @@
},
{
"cell_type": "code",
"execution_count": 114,
"execution_count": 70,
"id": "f959fe93",
"metadata": {},
"outputs": [],
"source": [
"def compress_rate(image, error, diff, bound, list_dic, bins):\n",
" #original = original.reshape(-1)\n",
" #error = error.reshape(-1)\n",
"\n",
" # the bits for the original image\n",
" o_len = 0\n",
" # the bits for the compressed image\n",
" c_len = 0\n",
" # initializing the varible \n",
" im = np.reshape(image,(512, 640))\n",
" real_b = np.hstack((im[0,:],im[-1,:],im[1:-1,0],im[1:-1,-1]))\n",
" original = im[1:-1,1:-1].reshape(-1)\n",
......@@ -374,9 +374,12 @@
" o_len += len(bin(real_b[i])[2:])\n",
" c_len += len(list_dic[0][str(bound[i])])\n",
" \n",
" \n",
" # calculate the bit for the pixels inside the boundary\n",
" for i in range(0,len(original)):\n",
" # for the original image\n",
" o_len += len(bin(original[i])[2:])\n",
" \n",
" # check the difference and find the coresponding huffman table\n",
" if diff[i] <= bins[0]:\n",
" c_len += len(list_dic[1][str(int(error[i]))])\n",
" \n",
......@@ -385,247 +388,68 @@
" \n",
" elif diff[i] <= bins[2] and diff[i] > bins[1]:\n",
" c_len += len(list_dic[3][str(int(error[i]))])\n",
" else: \n",
" c_len += len(list_dic[4][str(int(error[i]))])\n",
"\n",
" else: \n",
" c_len += len(list_dic[5][str(int(error[i]))])\n",
"\n",
" return c_len/o_len"
]
},
{
"cell_type": "code",
"execution_count": 121,
"execution_count": null,
"id": "3e0e9742",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n"
]
},
{
"ename": "KeyboardInterrupt",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m/var/folders/z2/plvrsqjs023g1cmx7k19mhzr0000gn/T/ipykernel_3109/243266254.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0mlist_dic\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mimage\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0merror\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnew_error\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdiff\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mbound\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mpredict\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mhuffman\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mimage\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mbins\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0mencoded_matrix\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mencoder\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreshape\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnew_error\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m512\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m640\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlist_dic\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdiff\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mbound\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mbins\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 8\u001b[0;31m \u001b[0mreconstruct_image\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdecoder\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mA\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mencoded_matrix\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlist_dic\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mbins\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 9\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mallclose\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mimage\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreshape\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m512\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m640\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mreconstruct_image\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/var/folders/z2/plvrsqjs023g1cmx7k19mhzr0000gn/T/ipykernel_3109/3881165701.py\u001b[0m in \u001b[0;36mdecoder\u001b[0;34m(A, encoded_matrix, list_dic, bins)\u001b[0m\n\u001b[1;32m 39\u001b[0m \u001b[0my\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mvstack\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0my0\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0my1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0my2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 40\u001b[0m \u001b[0mdifference\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmax\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mz0\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mz1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mz2\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mz3\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0mmin\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mz0\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mz1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mz2\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mz3\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 41\u001b[0;31m \u001b[0mpredict\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mround\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mround\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlinalg\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msolve\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mA\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0my\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 42\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 43\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mdifference\u001b[0m \u001b[0;34m<=\u001b[0m \u001b[0mbins\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m<__array_function__ internals>\u001b[0m in \u001b[0;36mround_\u001b[0;34m(*args, **kwargs)\u001b[0m\n",
"\u001b[0;32m/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/numpy/core/fromnumeric.py\u001b[0m in \u001b[0;36mround_\u001b[0;34m(a, decimals, out)\u001b[0m\n\u001b[1;32m 3635\u001b[0m \u001b[0maround\u001b[0m \u001b[0;34m:\u001b[0m \u001b[0mequivalent\u001b[0m \u001b[0mfunction\u001b[0m\u001b[0;34m;\u001b[0m \u001b[0msee\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mdetails\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3636\u001b[0m \"\"\"\n\u001b[0;32m-> 3637\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0maround\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdecimals\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mdecimals\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mout\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mout\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3638\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3639\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m<__array_function__ internals>\u001b[0m in \u001b[0;36maround\u001b[0;34m(*args, **kwargs)\u001b[0m\n",
"\u001b[0;32m/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/numpy/core/fromnumeric.py\u001b[0m in \u001b[0;36maround\u001b[0;34m(a, decimals, out)\u001b[0m\n\u001b[1;32m 3260\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3261\u001b[0m \"\"\"\n\u001b[0;32m-> 3262\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0m_wrapfunc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'round'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdecimals\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mdecimals\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mout\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mout\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3263\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3264\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/numpy/core/fromnumeric.py\u001b[0m in \u001b[0;36m_wrapfunc\u001b[0;34m(obj, method, *args, **kwds)\u001b[0m\n\u001b[1;32m 56\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 57\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 58\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mbound\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwds\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 59\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mTypeError\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 60\u001b[0m \u001b[0;31m# A TypeError occurs if the object does have such a method in its\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mKeyboardInterrupt\u001b[0m: "
]
}
],
"outputs": [],
"source": [
"scenes = file_extractor()\n",
"images = image_extractor(scenes)\n",
"bins = [25,40,70]\n",
"#bins = [25,40,70]\n",
"A = np.array([[3,0,-1],[0,3,3],[1,-3,-4]])\n",
"for image in images:\n",
" list_dic, image, error, new_error, diff, bound, predict = huffman(image, bins)\n",
" encoded_matrix = encoder(np.reshape(new_error,(512,640)), list_dic, diff, bound, bins)\n",
" reconstruct_image = decoder(A, encoded_matrix, list_dic, bins)\n",
" print(np.allclose(image.reshape(512,640), reconstruct_image))\n"
"list_dic, image, error, new_error, diff, bound, predict, bins = huffman(images[0], 4)\n",
"encoded_matrix = encoder(np.reshape(new_error,(512,640)), list_dic, diff, bound, bins)\n",
"reconstruct_image = decoder(A, encoded_matrix, list_dic, bins)\n",
"print(np.allclose(image.reshape(512,640), reconstruct_image))\n",
"print(len(list_dic))"
]
},
{
"cell_type": "code",
"execution_count": 119,
"id": "ceb0b957",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 120,
"id": "60297ad0",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 120,
"metadata": {},
"output_type": "execute_result"
}
],
"source": []
},
{
"cell_type": "code",
"execution_count": 79,
"execution_count": null,
"id": "f0948ab2",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1626032871_679543_6.tiff\n",
"1640843156_813147\n",
"1640843156_729797\n",
"1640843156_846487\n",
"1640843156_829817\n",
"1640843156_763137\n",
"1640843156_779807\n",
"1640843156_796477\n",
"1640843156_746467\n",
"1626032832_004943_6.tiff\n",
"1626032832_004943_18.jp4\n",
"1626032832_004943_19.jp4\n",
"1626032832_004943_17.jp4\n",
"1626032832_004943_16.jp4\n",
"1626033289_579773_6.tiff\n",
"1626033496_437803_16.jp4\n",
"1626033496_437803_17.jp4\n",
"1626033496_437803_19.jp4\n",
"1626033496_437803_18.jp4\n",
"1626033496_437803_6.tiff\n",
"1626032610_393963_6.tiff\n",
"1626033067_752083_19.jp4\n",
"1626033067_752083_18.jp4\n",
"1626033067_752083_6.tiff\n",
"1626033067_752083_16.jp4\n",
"1626033067_752083_17.jp4\n",
"1626032738_736293_6.tiff\n",
"Compression rate of huffman with different bins: 0.40459069242931545\n"
]
}
],
"outputs": [],
"source": [
"scenes = file_extractor()\n",
"images = image_extractor(scenes)\n",
"rate = []\n",
"\n",
"\n",
"rate1 = []\n",
"rate2 = []\n",
"rate3 = []\n",
"bins1 = [25,40,70]\n",
"bins2 = [50,100,150]\n",
"bins3 = [30,50,100]\n",
"B = [bins1, bins2, bins3]\n",
"for i in range(len(images)):\n",
" list_dic, image, error, new_error, diff, bound, bins, predict = huffman(images[i])\n",
" r = compress_rate(image, error, diff, bound, list_dic, bins)\n",
" rate.append(r)\n",
" \n",
" \n",
"print(f\"Compression rate of huffman with different bins: {np.mean(rate)}\")\n"
" for j, bins in enumerate(B):\n",
" list_dic, image, error, new_error, diff, bound, predict = huffman(images[i], bins)\n",
" r = compress_rate(image, error, diff, bound, list_dic, bins)\n",
" if j == 0:\n",
" rate1.append(r)\n",
" elif j == 1:\n",
" rate2.append(r)\n",
" else:\n",
" rate3.append(r)\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 27,
"id": "792b7936",
"execution_count": null,
"id": "7d615dcd",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0.41151123046875, 0.4466471354166667, 0.38627176920572914, 0.41287577311197915, 0.3977305094401042, 0.41076253255208334]\n"
]
}
],
"outputs": [],
"source": [
"print(rate)"
"print(f\"Compression rate of huffman with bins {bins1}: {np.mean(rate1)}\")\n",
"print(f\"Compression rate of huffman with bins {bins2}: {np.mean(rate2)}\")\n",
"print(f\"Compression rate of huffman with bins {bins3}: {np.mean(rate3)}\")\n"
]
},
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment