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

kelly changes

parent c2c824c5
No preview for this file type
This diff is collapsed.
......@@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 3,
"id": "8868bc30",
"metadata": {},
"outputs": [],
......@@ -24,7 +24,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 9,
"id": "76317b02",
"metadata": {},
"outputs": [],
......@@ -33,10 +33,13 @@
" files = os.listdir(dirname)\n",
" scenes = []\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",
"\n",
"def image_extractor(scenes):\n",
"'''def image_extractor(scenes):\n",
" image_folder = []\n",
" for scene in scenes:\n",
" files = os.listdir(scene)\n",
......@@ -51,6 +54,20 @@
" else:\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",
"'''\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",
"def im_distribution(images, num):\n",
" \"\"\"\n",
......@@ -79,7 +96,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 5,
"id": "be1ff8a1",
"metadata": {},
"outputs": [],
......@@ -125,7 +142,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 6,
"id": "8483903e",
"metadata": {},
"outputs": [],
......@@ -171,62 +188,6 @@
" 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",
"id": "c7104fbf",
......@@ -237,21 +198,12 @@
},
{
"cell_type": "code",
"execution_count": 401,
"execution_count": 11,
"id": "a43f3f1c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.444949904726781\n"
]
}
],
"outputs": [],
"source": [
"scenes = file_extractor()\n",
"images = image_extractor(scenes)\n",
"\n",
"def huffman_nb(image):\n",
" origin, predict, diff, error, A = plot_hist(image)\n",
" image = Image.open(image)\n",
......@@ -295,10 +247,7 @@
"\n",
" \n",
" return c_len/o_len\n",
"\n",
"\n",
"encoding, error, image = huffman_nb(images[0])\n",
"print(compress_rate_nb(image, error, encoding))"
"\n"
]
},
{
......@@ -931,7 +880,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.11"
"version": "3.9.1"
}
},
"nbformat": 4,
......
This diff is collapsed.
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