Commit 829e9198 authored by Kelly Chang's avatar Kelly Chang

kelly push

parent 4a046ba4
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 @@ ...@@ -203,7 +203,6 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"\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",
...@@ -237,15 +236,14 @@ ...@@ -237,15 +236,14 @@
" \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"
] ]
...@@ -410,7 +408,8 @@ ...@@ -410,7 +408,8 @@
" 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",
" image = image.astype(int)\n", " image = image.astype(int)\n",
"\n", " \n",
" bins = np.linspace(min(diff),max(diff),5)[1:-1]\n",
" \n", " \n",
" boundary = np.hstack((image[0,:],image[-1,:],image[1:-1,0],image[1:-1,-1]))\n", " boundary = np.hstack((image[0,:],image[-1,:],image[1:-1,0],image[1:-1,-1]))\n",
" boundary = boundary - image[0,0]\n", " boundary = boundary - image[0,0]\n",
...@@ -423,7 +422,7 @@ ...@@ -423,7 +422,7 @@
" encode1 = huffman_code_tree(node)\n", " encode1 = huffman_code_tree(node)\n",
" \n", " \n",
" \n", " \n",
" mask = diff <= 100\n", " mask = diff <= bins[0]\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",
...@@ -431,20 +430,20 @@ ...@@ -431,20 +430,20 @@
" encode2 = huffman_code_tree(node)\n", " encode2 = huffman_code_tree(node)\n",
"\n", "\n",
" \n", " \n",
" mask = diff > 100\n", " mask = diff > bins[0]\n",
" #new_error = error[mask]\n", " new_error = error[mask]\n",
" #mask2 = diff[mask] <= 200\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 new_error[mask2].astype(int)]\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",
" encode3 = huffman_code_tree(node)\n", " encode3 = huffman_code_tree(node)\n",
" \n", " \n",
"\n", "\n",
" '''mask = diff > 200\n", " mask = diff > bins[1]\n",
" new_error = error[mask]\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", " 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",
...@@ -452,12 +451,13 @@ ...@@ -452,12 +451,13 @@
" encode4 = huffman_code_tree(node)\n", " encode4 = huffman_code_tree(node)\n",
" \n", " \n",
" \n", " \n",
" mask = diff > 300\n", " mask = diff > bins[2]\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",
" encode5 = huffman_code_tree(node)'''\n", " encode5 = huffman_code_tree(node)\n",
" \n",
"\n", "\n",
" \n", " \n",
" \n", " \n",
...@@ -474,10 +474,10 @@ ...@@ -474,10 +474,10 @@
" \n", " \n",
" # return the huffman dictionary\n", " # return the huffman dictionary\n",
" #return encode1, encode2, encode3, encode4, encode5, 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", " return [encode1, encode2, encode3, encode4, encode5], np.ravel(image), error, diff, boundary, bins\n",
"\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, 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", " #original = original.reshape(-1)\n",
" #error = error.reshape(-1)\n", " #error = error.reshape(-1)\n",
" o_len = 0\n", " o_len = 0\n",
...@@ -488,30 +488,23 @@ ...@@ -488,30 +488,23 @@
"\n", "\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", " 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] <= 100:\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", " \n",
" if diff[i] > 100:\n", " if diff[i] <= bins[1] and diff[i] > bins[0]:\n",
" c_len += len(encode3[str(int(error[i]))])\n", " c_len += len(list_dic[2][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] <= 300 and diff[i] > 200:\n", " if diff[i] <= bins[2] and diff[i] > bins[1]:\n",
" c_len += len(encode4[str(int(error[i]))])\n", " c_len += len(list_dic[3][str(int(error[i]))])\n",
" \n", " \n",
" if diff[i] > 300:\n", " if diff[i] > bins[2]:\n",
" c_len += len(encode5[str(int(error[i]))])'''\n", " c_len += len(list_dic[4][str(int(error[i]))])\n",
" \n", " \n",
" return c_len/o_len\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"
] ]
}, },
{ {
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
"cells": [ "cells": [
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 54, "execution_count": 63,
"id": "14f74f21", "id": "14f74f21",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
...@@ -24,7 +24,7 @@ ...@@ -24,7 +24,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 55, "execution_count": 64,
"id": "c16af61f", "id": "c16af61f",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
...@@ -78,7 +78,7 @@ ...@@ -78,7 +78,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 56, "execution_count": 65,
"id": "aceba613", "id": "aceba613",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
...@@ -123,7 +123,7 @@ ...@@ -123,7 +123,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 57, "execution_count": 66,
"id": "6b965751", "id": "6b965751",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
...@@ -171,7 +171,7 @@ ...@@ -171,7 +171,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 58, "execution_count": 67,
"id": "b7561883", "id": "b7561883",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
...@@ -259,7 +259,7 @@ ...@@ -259,7 +259,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 59, "execution_count": 68,
"id": "2eb774d2", "id": "2eb774d2",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
...@@ -287,7 +287,7 @@ ...@@ -287,7 +287,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 60, "execution_count": 69,
"id": "8eeb40d0", "id": "8eeb40d0",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
...@@ -353,7 +353,7 @@ ...@@ -353,7 +353,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 61, "execution_count": 70,
"id": "f959fe93", "id": "f959fe93",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
......
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