Project 'Elphel/master' was moved to 'Elphel/image-compression'. Please update any links and bookmarks that may still have the old path.
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 @@
"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": 54,
"execution_count": 63,
"id": "14f74f21",
"metadata": {},
"outputs": [],
......@@ -24,7 +24,7 @@
},
{
"cell_type": "code",
"execution_count": 55,
"execution_count": 64,
"id": "c16af61f",
"metadata": {},
"outputs": [],
......@@ -78,7 +78,7 @@
},
{
"cell_type": "code",
"execution_count": 56,
"execution_count": 65,
"id": "aceba613",
"metadata": {},
"outputs": [],
......@@ -123,7 +123,7 @@
},
{
"cell_type": "code",
"execution_count": 57,
"execution_count": 66,
"id": "6b965751",
"metadata": {},
"outputs": [],
......@@ -171,7 +171,7 @@
},
{
"cell_type": "code",
"execution_count": 58,
"execution_count": 67,
"id": "b7561883",
"metadata": {},
"outputs": [],
......@@ -259,7 +259,7 @@
},
{
"cell_type": "code",
"execution_count": 59,
"execution_count": 68,
"id": "2eb774d2",
"metadata": {},
"outputs": [],
......@@ -287,7 +287,7 @@
},
{
"cell_type": "code",
"execution_count": 60,
"execution_count": 69,
"id": "8eeb40d0",
"metadata": {},
"outputs": [],
......@@ -353,7 +353,7 @@
},
{
"cell_type": "code",
"execution_count": 61,
"execution_count": 70,
"id": "f959fe93",
"metadata": {},
"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