Commit 271201a3 authored by Kelly Chang's avatar Kelly Chang

kelly changes

parent c2c824c5
No preview for this file type
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
"cells": [ "cells": [
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 1, "execution_count": 6,
"id": "8868bc30", "id": "8868bc30",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
...@@ -24,8 +24,8 @@ ...@@ -24,8 +24,8 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 2, "execution_count": 7,
"id": "76317b02", "id": "0f944705",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
...@@ -33,7 +33,10 @@ ...@@ -33,7 +33,10 @@
" files = os.listdir(dirname)\n", " files = os.listdir(dirname)\n",
" scenes = []\n", " scenes = []\n",
" for file in files:\n", " for file in files:\n",
" scenes.append(os.path.join(dirname, file))\n", " if file == '.DS_Store':\n",
" continue\n",
" else:\n",
" scenes.append(os.path.join(dirname, file))\n",
" return scenes\n", " return scenes\n",
"\n", "\n",
"def image_extractor(scenes):\n", "def image_extractor(scenes):\n",
...@@ -41,7 +44,12 @@ ...@@ -41,7 +44,12 @@
" for scene in scenes:\n", " for scene in scenes:\n",
" files = os.listdir(scene)\n", " files = os.listdir(scene)\n",
" for file in files:\n", " for file in files:\n",
" image_folder.append(os.path.join(scene, file))\n", " #if file[-4:] == \".jp4\" or file[-7:] == \"_6.tiff\":\n",
" if file[-5:] != \".tiff\" or file[-7:] == \"_6.tiff\":\n",
" continue\n",
" else:\n",
" image_folder.append(os.path.join(scene, file))\n",
" '''print(image_folder)\n",
" images = []\n", " images = []\n",
" for folder in image_folder:\n", " for folder in image_folder:\n",
" ims = os.listdir(folder)\n", " ims = os.listdir(folder)\n",
...@@ -49,8 +57,8 @@ ...@@ -49,8 +57,8 @@
" if im[-4:] == \".jp4\" or im[-7:] == \"_6.tiff\":\n", " if im[-4:] == \".jp4\" or im[-7:] == \"_6.tiff\":\n",
" continue\n", " continue\n",
" else:\n", " else:\n",
" images.append(os.path.join(folder, im))\n", " images.append(os.path.join(folder, im))'''\n",
" return images #returns a list of file paths to .tiff files in the specified directory given in file_extractor\n", " return image_folder #returns a list of file paths to .tiff files in the specified directory given in file_extractor\n",
"\n", "\n",
"def im_distribution(images, num):\n", "def im_distribution(images, num):\n",
" \"\"\"\n", " \"\"\"\n",
...@@ -79,8 +87,8 @@ ...@@ -79,8 +87,8 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 3, "execution_count": 8,
"id": "be1ff8a1", "id": "b18d5e38",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
...@@ -126,8 +134,8 @@ ...@@ -126,8 +134,8 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 4, "execution_count": 9,
"id": "8483903e", "id": "35d4f6a0",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
...@@ -182,13 +190,11 @@ ...@@ -182,13 +190,11 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 7, "execution_count": 18,
"id": "a43f3f1c", "id": "c50169ed",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"scenes = file_extractor()\n",
"images = image_extractor(scenes)\n",
"def huffman_nb(image):\n", "def huffman_nb(image):\n",
" origin, predict, diff, error, A = plot_hist(image)\n", " origin, predict, diff, error, A = plot_hist(image)\n",
" image = Image.open(image)\n", " image = Image.open(image)\n",
...@@ -222,195 +228,32 @@ ...@@ -222,195 +228,32 @@
" \n", " \n",
" \n", " \n",
"def compress_rate_nb(image, error, encoding):\n", "def compress_rate_nb(image, error, encoding):\n",
" #original = original.reshape(-1)\n", " original = image.reshape(-1)\n",
" #error = error.reshape(-1)\n", " error = error.reshape(-1)\n",
" o_len = 0\n", " o_len = 0\n",
" c_len = 0\n", " c_len = 0\n",
" for i in range(0, len(original)):\n", " for i in range(0, len(original)):\n",
" o_len += len(bin(original[i])[2:])\n", " o_len += len(bin(original[i])[2:])\n",
" c_len += len(encoding[str(int(error[i]))])\n", " c_len += len(encoding[str(int(error[i]))])\n",
"\n", "\n",
" \n",
" return c_len/o_len\n", " return c_len/o_len\n",
"\n" "\n"
] ]
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"id": "eac2f456", "id": "e34201fd",
"metadata": {},
"source": [
"### Huffman with dividing into non-uniform bins"
]
},
{
"cell_type": "markdown",
"id": "3a3f06a5",
"metadata": {}, "metadata": {},
"source": [ "source": [
"### Huffman with dividing into uniform bins" "### Huffman dividing into bins"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 57, "execution_count": 11,
"id": "14075c94", "id": "205c4731",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"325380\n",
"325380\n"
]
},
{
"data": {
"text/plain": [
"0.4432273356119792"
]
},
"execution_count": 57,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def huffman_u(image):\n",
" origin, predict, diff, error, A = plot_hist(image)\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",
" print(len(diff))\n",
" \n",
" boundary = np.hstack((image[0,:],image[-1,:],image[1:-1,0],image[1:-1,-1]))\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 <= 100\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 > 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",
" 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",
" new_error = error[mask]\n",
" mask2 = diff[mask] <= 300\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",
" encode4 = huffman_code_tree(node)\n",
" \n",
" \n",
" mask = diff > 300\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",
" \n",
" \n",
"\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",
" new_error[0,:] = new_error[0,:] - keep\n",
" new_error[-1,:] = new_error[-1,:] - keep\n",
" new_error[1:-1,0] = new_error[1:-1,0] - keep\n",
" new_error[1:-1,-1] = new_error[1:-1,-1] - keep\n",
" new_error[0,0] = keep\n",
" new_error = np.ravel(new_error)\n",
" \n",
" # return the huffman dictionary\n",
" #return encode1, encode2, encode3, encode4, encode5, np.ravel(image), error, diff, boundary\n",
" print(len(diff))\n",
" return encode1, encode2, encode3, np.ravel(image), error, diff, boundary\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",
" #original = original.reshape(-1)\n",
" #error = error.reshape(-1)\n",
" o_len = 0\n",
" c_len = 0\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",
"\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",
" \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",
" \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",
" \n",
" '''if diff[i] <= 300 and diff[i] > 200:\n",
" c_len += len(encode4[str(int(error[i]))])\n",
" \n",
" if diff[i] > 300:\n",
" c_len += len(encode5[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"
]
},
{
"cell_type": "code",
"execution_count": 55,
"id": "207b0bd2",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"325380\n"
]
},
{
"data": {
"text/plain": [
"0.44205322265625"
]
},
"execution_count": 55,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"def huffman(image):\n", "def huffman(image):\n",
" origin, predict, diff, error, A = plot_hist(image)\n", " origin, predict, diff, error, A = plot_hist(image)\n",
...@@ -479,13 +322,11 @@ ...@@ -479,13 +322,11 @@
" #new_error = np.ravel(new_error)\n", " #new_error = np.ravel(new_error)\n",
" \n", " \n",
" bins = [25,40,70]\n", " bins = [25,40,70]\n",
" \n", " list_dic = [encode1, encode2, encode3, encode4, encode5]\n",
" # return the huffman dictionary\n", " # return the huffman dictionary\n",
" return encode1, encode2, encode3, encode4, encode5, np.ravel(image), error, new_error, diff, boundary, bins, predict\n", " return list_dic, np.ravel(image), error, new_error, diff, boundary, bins, predict\n",
" \n",
"\n",
"\n", "\n",
"def compress_rate(image, error, diff, bound, encode1, encode2, encode3, encode4, encode5):\n", "def compress_rate(image, error, diff, bound, list_dic, bins):\n",
" #original = original.reshape(-1)\n", " #original = original.reshape(-1)\n",
" #error = error.reshape(-1)\n", " #error = error.reshape(-1)\n",
"\n", "\n",
...@@ -494,59 +335,47 @@ ...@@ -494,59 +335,47 @@
" im = np.reshape(image,(512, 640))\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", " 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", " original = im[1:-1,1:-1].reshape(-1)\n",
"\n", " diff = diff.reshape(-1)\n",
" \n",
" # calculate the bit for boundary\n",
" for i in range(0,len(bound)):\n", " for i in range(0,len(bound)):\n",
" o_len += len(bin(real_b[i])[2:])\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", " \n",
" for i in range(0, len(original)):\n", " \n",
" for i in range(0,len(original)):\n",
" o_len += len(bin(original[i])[2:])\n", " o_len += len(bin(original[i])[2:])\n",
" if diff[i] <= 25:\n", " if diff[i] <= bins[0]:\n",
" c_len += len(encode2[str(int(error[i]))])\n", " c_len += len(list_dic[1][str(int(error[i]))])\n",
"\n",
" if diff[i] <= 40 and diff[i] > 25:\n",
" c_len += len(encode3[str(int(error[i]))])\n",
" \n",
" if diff[i] <= 70 and diff[i] > 40:\n",
" c_len += len(encode4[str(int(error[i]))])\n",
" \n", " \n",
" if diff[i] > 70:\n", " elif diff[i] <= bins[1] and diff[i] > bins[0]:\n",
" c_len += len(encode5[str(int(error[i]))])\n", " c_len += len(list_dic[2][str(int(error[i]))])\n",
" \n", " \n",
" return c_len/o_len\n", " elif diff[i] <= bins[2] and diff[i] > bins[1]:\n",
"scenes = file_extractor()\n", " c_len += len(list_dic[3][str(int(error[i]))])\n",
"images = image_extractor(scenes)\n", " else: \n",
"encode1, encode2, encode3, encode4, encode5, image, error, diff, boundary, bins = huffman(images[0])\n", " c_len += len(list_dic[4][str(int(error[i]))])\n",
"compress_rate(image, error, diff, boundary, encode1, encode2, encode3, encode4, encode5)\n" "\n",
"\n",
" return c_len/o_len\n"
] ]
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"id": "816764c9", "id": "2e84c206",
"metadata": {}, "metadata": {},
"source": [ "source": [
"## Huffman Divide into 6 bins" "### Huffman dividing into bins"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 430, "execution_count": null,
"id": "15eecad3", "id": "18e44483",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [],
{
"data": {
"text/plain": [
"0.4421759033203125"
]
},
"execution_count": 430,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"def huffman6(image):\n", "def huffman_u(image):\n",
" origin, predict, diff, error, A = plot_hist(image)\n", " origin, predict, diff, error, A = plot_hist(image)\n",
" image = Image.open(image)\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 = np.array(image)[1:,:] #Convert to an array, leaving out the first row because the first row is just housekeeping data\n",
...@@ -564,7 +393,7 @@ ...@@ -564,7 +393,7 @@
" encode1 = huffman_code_tree(node)\n", " encode1 = huffman_code_tree(node)\n",
" \n", " \n",
" \n", " \n",
" mask = diff <= 5\n", " mask = diff <= 100\n",
" string = [str(i) for i in error[mask].astype(int)]\n", " string = [str(i) for i in error[mask].astype(int)]\n",
" freq = dict(Counter(string))\n", " freq = dict(Counter(string))\n",
" freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)\n", " freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)\n",
...@@ -572,19 +401,20 @@ ...@@ -572,19 +401,20 @@
" encode2 = huffman_code_tree(node)\n", " encode2 = huffman_code_tree(node)\n",
"\n", "\n",
" \n", " \n",
" mask = diff > 5\n", " mask = diff > 100\n",
" new_error = error[mask]\n", " #new_error = error[mask]\n",
" mask2 = diff[mask] <= 15\n", " #mask2 = diff[mask] <= 200\n",
" string = [str(i) for i in new_error[mask2].astype(int)]\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 = dict(Counter(string))\n",
" freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)\n", " freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)\n",
" node = make_tree(freq)\n", " node = make_tree(freq)\n",
" encode3 = huffman_code_tree(node)\n", " encode3 = huffman_code_tree(node)\n",
" \n", " \n",
"\n", "\n",
" mask = diff > 15\n", " '''mask = diff > 200\n",
" new_error = error[mask]\n", " new_error = error[mask]\n",
" mask2 = diff[mask] <= 30\n", " mask2 = diff[mask] <= 300\n",
" string = [str(i) for i in new_error[mask2].astype(int)]\n", " string = [str(i) for i in new_error[mask2].astype(int)]\n",
" freq = dict(Counter(string))\n", " freq = dict(Counter(string))\n",
" freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)\n", " freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)\n",
...@@ -592,22 +422,12 @@ ...@@ -592,22 +422,12 @@
" encode4 = huffman_code_tree(node)\n", " encode4 = huffman_code_tree(node)\n",
" \n", " \n",
" \n", " \n",
" mask = diff > 30\n", " mask = diff > 300\n",
" new_error = error[mask]\n",
" mask2 = diff[mask] <= 50\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",
" encode5 = huffman_code_tree(node)\n",
" \n",
" \n",
" mask = diff > 50\n",
" string = [str(i) for i in error[mask].astype(int)]\n", " string = [str(i) for i in error[mask].astype(int)]\n",
" freq = dict(Counter(string))\n", " freq = dict(Counter(string))\n",
" freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)\n", " freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)\n",
" node = make_tree(freq)\n", " node = make_tree(freq)\n",
" encode6 = huffman_code_tree(node)\n", " encode5 = huffman_code_tree(node)'''\n",
"\n", "\n",
" \n", " \n",
" \n", " \n",
...@@ -623,9 +443,11 @@ ...@@ -623,9 +443,11 @@
" new_error = np.ravel(new_error)\n", " new_error = np.ravel(new_error)\n",
" \n", " \n",
" # return the huffman dictionary\n", " # return the huffman dictionary\n",
" return encode1, encode2, encode3, encode4, encode5, encode6, np.ravel(image), error, diff, boundary\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",
"\n", "\n",
"def compress_rate6(image, error, diff, bound, encode1, encode2, encode3, encode4, encode5, encode6):\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",
" #original = original.reshape(-1)\n", " #original = original.reshape(-1)\n",
" #error = error.reshape(-1)\n", " #error = error.reshape(-1)\n",
" o_len = 0\n", " o_len = 0\n",
...@@ -640,106 +462,79 @@ ...@@ -640,106 +462,79 @@
" \n", " \n",
" for i in range(0, len(original)):\n", " for i in range(0, len(original)):\n",
" o_len += len(bin(original[i])[2:])\n", " o_len += len(bin(original[i])[2:])\n",
" if diff[i] <= 5:\n", " if diff[i] <= 100:\n",
" c_len += len(encode2[str(int(error[i]))])\n", " c_len += len(encode2[str(int(error[i]))])\n",
"\n", " \n",
" if diff[i] <= 15 and diff[i] > 5:\n", " if diff[i] > 100:\n",
" c_len += len(encode3[str(int(error[i]))])\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",
" \n", " \n",
" if diff[i] <= 30 and diff[i] > 15:\n", " '''if diff[i] <= 300 and diff[i] > 200:\n",
" c_len += len(encode4[str(int(error[i]))])\n", " c_len += len(encode4[str(int(error[i]))])\n",
" \n", " \n",
" if diff[i] <= 50 and diff[i] > 30:\n", " if diff[i] > 300:\n",
" c_len += len(encode5[str(int(error[i]))])\n", " c_len += len(encode5[str(int(error[i]))])'''\n",
" \n",
" if diff[i] > 50:\n",
" c_len += len(encode6[str(int(error[i]))])\n",
" \n", " \n",
" return c_len/o_len\n", " return c_len/o_len\n",
"scenes = file_extractor()\n", "scenes = file_extractor()\n",
"images = image_extractor(scenes)\n", "images = image_extractor(scenes)\n",
"encode1, encode2, encode3, encode4, encode5, encode6, image, error, diff, boundary = huffman(images[0])\n", "encode1, encode2, encode3, image, error, diff, boundary = huffman_u(images[0])\n",
"compress_rate(image, error, diff, boundary, encode1, encode2, encode3, encode4, encode5, encode6)\n" "compress_rate_u(image, error, diff, boundary, encode1, encode2, encode3)\n"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 431, "execution_count": 19,
"id": "f8a8c717", "id": "e1ce9912",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"Compression rate of huffman with different bins: 0.448723882039388\n" "Compression rate of huffman with different bins: 0.40459069242931545\n",
"Compression rate of huffman without bins: 0.410545890687004\n"
] ]
} }
], ],
"source": [ "source": [
"scenes = file_extractor()\n", "scenes = file_extractor()\n",
"images = image_extractor(scenes)\n", "images = image_extractor(scenes)\n",
"num_images = im_distribution(images, \"_9\")\n",
"rate = []\n",
"\n",
"for i in range(len(num_images)):\n",
" encode1, encode2, encode3, encode4, encode5, encode6, image, error, diff, bound = huffman6(num_images[i])\n",
" r = compress_rate6(image, error, diff, bound, encode1, encode2, encode3, encode4, encode5, encode6)\n",
" rate.append(r)\n",
" \n",
" \n",
"print(f\"Compression rate of huffman with different bins: {np.mean(rate)}\")\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "6abed5da",
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'file_extractor' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m/var/folders/z2/plvrsqjs023g1cmx7k19mhzr0000gn/T/ipykernel_3263/2742763429.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mnum_images\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mfile_extractor\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'im'\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 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mrate\u001b[0m \u001b[0;34m=\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 4\u001b[0m \u001b[0mrate_nb\u001b[0m \u001b[0;34m=\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 5\u001b[0m \u001b[0mrate_u\u001b[0m \u001b[0;34m=\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;31mNameError\u001b[0m: name 'file_extractor' is not defined"
]
}
],
"source": [
"num_images = file_extractor('im')\n",
"\n", "\n",
"rate = []\n", "rate = []\n",
"rate_nb = []\n", "rate_nb = []\n",
"rate_u = []\n", "rate_u = []\n",
"for i in range(len(num_images)):\n", "for i in range(len(images)):\n",
" encode1, encode2, encode3, encode4, encode5, image, error, diff, bound = huffman(num_images[i])\n", " list_dic, image, error, new_error, diff, bound, bins, predict = huffman(images[i])\n",
" r = compress_rate(image, error, diff, bound, encode1, encode2, encode3, encode4, encode5)\n", " r = compress_rate(image, error, diff, bound, list_dic, bins)\n",
" rate.append(r)\n", " rate.append(r)\n",
" encoding, error, image = huffman_nb(num_images[i])\n", " \n",
" encoding, error, image = huffman_nb(images[i])\n",
" \n",
" r = compress_rate_nb(image, error, encoding)\n", " r = compress_rate_nb(image, error, encoding)\n",
" rate_nb.append(r)\n", " rate_nb.append(r)\n",
" encode1, encode2, encode3, image, error, diff, bound = huffman_u(num_images[i])\n",
" r = compress_rate_u(image, error, diff, bound, encode1, encode2, encode3)\n",
" rate_u.append(r)\n",
" \n", " \n",
"print(f\"Compression rate of huffman with different bins: {np.mean(rate)}\")\n", "print(f\"Compression rate of huffman with different bins: {np.mean(rate)}\")\n",
"print(f\"Compression rate of huffman without bins: {np.mean(rate_nb)}\")\n", "print(f\"Compression rate of huffman without bins: {np.mean(rate_nb)}\")\n"
"print(f\"Compression rate of huffman with uniform bins: {np.mean(rate_u)}\")"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 238, "execution_count": null,
"id": "992dd8bb", "id": "7a9a31f6",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": []
"origin, predict, diff, error, A = plot_hist(images[0])" },
] {
"cell_type": "code",
"execution_count": null,
"id": "c71591f2",
"metadata": {},
"outputs": [],
"source": []
}, },
{ {
"cell_type": "code", "cell_type": "code",
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
"cells": [ "cells": [
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 1, "execution_count": 3,
"id": "8868bc30", "id": "8868bc30",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
...@@ -24,7 +24,7 @@ ...@@ -24,7 +24,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 2, "execution_count": 9,
"id": "76317b02", "id": "76317b02",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
...@@ -33,10 +33,13 @@ ...@@ -33,10 +33,13 @@
" files = os.listdir(dirname)\n", " files = os.listdir(dirname)\n",
" scenes = []\n", " scenes = []\n",
" for file in files:\n", " for file in files:\n",
" scenes.append(os.path.join(dirname, file))\n", " if file == '.DS_Store':\n",
" continue\n",
" else:\n",
" scenes.append(os.path.join(dirname, file))\n",
" return scenes\n", " return scenes\n",
"\n", "\n",
"def image_extractor(scenes):\n", "'''def image_extractor(scenes):\n",
" image_folder = []\n", " image_folder = []\n",
" for scene in scenes:\n", " for scene in scenes:\n",
" files = os.listdir(scene)\n", " files = os.listdir(scene)\n",
...@@ -51,6 +54,20 @@ ...@@ -51,6 +54,20 @@
" else:\n", " else:\n",
" images.append(os.path.join(folder, im))\n", " images.append(os.path.join(folder, im))\n",
" return images #returns a list of file paths to .tiff files in the specified directory given in file_extractor\n", " return images #returns a list of file paths to .tiff files in the specified directory given in file_extractor\n",
"'''\n",
"\n",
"def image_extractor(scenes):\n",
" image_folder = []\n",
" for scene in scenes:\n",
" files = os.listdir(scene)\n",
" for file in files:\n",
" #if file[-4:] == \".jp4\" or file[-7:] == \"_6.tiff\":\n",
" if file[-5:] != \".tiff\" or file[-7:] == \"_6.tiff\":\n",
" print(file)\n",
" continue\n",
" else:\n",
" image_folder.append(os.path.join(scene, file))\n",
" return image_folder\n",
"\n", "\n",
"def im_distribution(images, num):\n", "def im_distribution(images, num):\n",
" \"\"\"\n", " \"\"\"\n",
...@@ -79,7 +96,7 @@ ...@@ -79,7 +96,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 3, "execution_count": 5,
"id": "be1ff8a1", "id": "be1ff8a1",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
...@@ -125,7 +142,7 @@ ...@@ -125,7 +142,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 4, "execution_count": 6,
"id": "8483903e", "id": "8483903e",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
...@@ -171,62 +188,6 @@ ...@@ -171,62 +188,6 @@
" return nodes[0][0]" " return nodes[0][0]"
] ]
}, },
{
"cell_type": "code",
"execution_count": 5,
"id": "64a3a193",
"metadata": {},
"outputs": [],
"source": [
"def reconstruct(error, A):\n",
" \"\"\"\n",
" Function that reconstructs the original image\n",
" from the error matrix and using the predictive\n",
" algorithm developed in the encoding.\n",
" \n",
" Parameters:\n",
" error (array): matrix of errors computed in encoding. Same \n",
" shape as the original image (512, 640) in this case\n",
" A (array): Matrix used for the system of equations to create predictions\n",
" Returns: \n",
" image (array): The reconstructed image\n",
" \"\"\"\n",
" new_e = error.copy()\n",
" rows, columns = new_e.shape\n",
"\n",
" for r in range(1, rows-1):\n",
" for c in range(1, columns-1):\n",
" z0, z1, z2, z3 = new_e[r-1][c-1], new_e[r-1][c], new_e[r-1][c+1], new_e[r][c-1]\n",
" y = np.vstack((-z0+z2-z3, z0+z1+z2, -z0-z1-z2-z3))\n",
"\n",
" '''if r == 345 and c == 421:\n",
" print(new_e[r][c])\n",
" print(np.linalg.solve(A,y)[-1])\n",
" print(new_e[r][c] + np.linalg.solve(A,y)[-1])\n",
" print(np.ceil(new_e[r][c]) + np.floor(np.linalg.solve(A,y)[-1]))\n",
" \n",
" y0 = np.ravel(-z0+z2-z3)\n",
" y1 = np.ravel(z0+z1+z2)\n",
" y2 = np.ravel(-z0-z1-z2-z3)\n",
" y = np.vstack((y0,y1,y2))\n",
" # use numpy solver to solve the system of equations all at once\n",
" predict = np.floor(np.linalg.solve(A,y)[-1])\n",
" # flatten the neighbor pixlels and stack them together\n",
" z0 = np.ravel(z0)\n",
" z1 = np.ravel(z1)\n",
" z2 = np.ravel(z2)\n",
" z3 = np.ravel(z3)\n",
" neighbor = np.vstack((z0,z1,z2,z3)).T'''\n",
" \n",
" #Real solution that works, DO NOT DELETE\n",
" print(new_e[r][c]+ np.floor(np.linalg.solve(A,y)[-1]))\n",
" new_e[r][c] = new_e[r][c] + np.floor(np.linalg.solve(A,y)[-1])\n",
" print(new_e[r][c])\n",
" #new_e[r][c] = np.ceil(new_e[r][c]) + np.floor(np.linalg.solve(A,y)[-1])\n",
" \n",
" return new_e"
]
},
{ {
"cell_type": "markdown", "cell_type": "markdown",
"id": "c7104fbf", "id": "c7104fbf",
...@@ -237,21 +198,12 @@ ...@@ -237,21 +198,12 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 401, "execution_count": 11,
"id": "a43f3f1c", "id": "a43f3f1c",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.444949904726781\n"
]
}
],
"source": [ "source": [
"scenes = file_extractor()\n", "\n",
"images = image_extractor(scenes)\n",
"def huffman_nb(image):\n", "def huffman_nb(image):\n",
" origin, predict, diff, error, A = plot_hist(image)\n", " origin, predict, diff, error, A = plot_hist(image)\n",
" image = Image.open(image)\n", " image = Image.open(image)\n",
...@@ -295,10 +247,7 @@ ...@@ -295,10 +247,7 @@
"\n", "\n",
" \n", " \n",
" return c_len/o_len\n", " return c_len/o_len\n",
"\n", "\n"
"\n",
"encoding, error, image = huffman_nb(images[0])\n",
"print(compress_rate_nb(image, error, encoding))"
] ]
}, },
{ {
...@@ -931,7 +880,7 @@ ...@@ -931,7 +880,7 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.8.11" "version": "3.9.1"
} }
}, },
"nbformat": 4, "nbformat": 4,
......
This source diff could not be displayed because it is too large. You can view the blob instead.
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