Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
I
image-compression
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Elphel
image-compression
Commits
ac651fcd
Commit
ac651fcd
authored
Mar 10, 2022
by
Nathaniel Callens
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
changes
parent
697f75e8
Changes
3
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
472 additions
and
77 deletions
+472
-77
Error_to_Image-checkpoint.ipynb
.ipynb_checkpoints/Error_to_Image-checkpoint.ipynb
+295
-39
Encoding_Kelly.ipynb
Encoding_Kelly.ipynb
+1
-1
Error_to_Image.ipynb
Error_to_Image.ipynb
+176
-37
No files found.
.ipynb_checkpoints/Error_to_Image-checkpoint.ipynb
View file @
ac651fcd
This diff is collapsed.
Click to expand it.
Encoding_Kelly.ipynb
View file @
ac651fcd
...
...
@@ -423,7 +423,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.
9.
1"
"version": "3.
8.1
1"
}
},
"nbformat": 4,
...
...
Error_to_Image.ipynb
View file @
ac651fcd
...
...
@@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count":
626
,
"execution_count":
33
,
"id": "dbef8759",
"metadata": {
"id": "dbef8759"
...
...
@@ -21,12 +21,13 @@
"from numpy import linalg as la\n",
"from scipy.stats import gaussian_kde\n",
"import seaborn as sns\n",
"import pywt"
"import pywt\n",
"from collections import Counter"
]
},
{
"cell_type": "code",
"execution_count":
654
,
"execution_count":
2
,
"id": "9ed20f84",
"metadata": {
"id": "9ed20f84"
...
...
@@ -99,7 +100,7 @@
},
{
"cell_type": "code",
"execution_count":
647
,
"execution_count":
3
,
"id": "ba2881d9",
"metadata": {},
"outputs": [],
...
...
@@ -111,7 +112,7 @@
},
{
"cell_type": "code",
"execution_count":
655
,
"execution_count":
4
,
"id": "11e95c34",
"metadata": {},
"outputs": [],
...
...
@@ -121,7 +122,7 @@
},
{
"cell_type": "code",
"execution_count": 5
37
,
"execution_count": 5,
"id": "434e4d2f",
"metadata": {},
"outputs": [],
...
...
@@ -159,30 +160,18 @@
},
{
"cell_type": "code",
"execution_count": 6
06
,
"id": "
bf427edd
",
"execution_count": 6,
"id": "
4f4a5a35
",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"-19\n",
"[22286.]\n",
"22285.0\n",
"[22266.]\n",
"22266.999999999996\n"
]
}
],
"outputs": [],
"source": [
"new_error = reconstruct(err, A)"
]
},
{
"cell_type": "code",
"execution_count":
669
,
"id": "
5edcf208
",
"execution_count":
7
,
"id": "
6d95ffce
",
"metadata": {},
"outputs": [],
"source": [
...
...
@@ -205,8 +194,8 @@
},
{
"cell_type": "code",
"execution_count":
673
,
"id": "
953375c5
",
"execution_count":
8
,
"id": "
1c848109
",
"metadata": {},
"outputs": [
{
...
...
@@ -304,22 +293,172 @@
},
{
"cell_type": "code",
"execution_count": 679,
"id": "90387cb9",
"execution_count": 9,
"id": "139938e3",
"metadata": {},
"outputs": [],
"source": [
"class NodeTree(object):\n",
" def __init__(self, left=None, right=None):\n",
" self.left = left\n",
" self.right = right\n",
"\n",
" def children(self):\n",
" return self.left, self.right\n",
"\n",
" def __str__(self):\n",
" return self.left, self.right\n",
"\n",
"\n",
"def huffman_code_tree(node, binString=''):\n",
" '''\n",
" Function to find Huffman Code\n",
" '''\n",
" if type(node) is str:\n",
" return {node: binString}\n",
" (l, r) = node.children()\n",
" d = dict()\n",
" d.update(huffman_code_tree(l, binString + '0'))\n",
" d.update(huffman_code_tree(r, binString + '1'))\n",
" return d\n",
"\n",
"\n",
"def make_tree(nodes):\n",
" '''\n",
" Function to make tree\n",
" :param nodes: Nodes\n",
" :return: Root of the tree\n",
" '''\n",
" while len(nodes) > 1:\n",
" (key1, c1) = nodes[-1]\n",
" (key2, c2) = nodes[-2]\n",
" nodes = nodes[:-2]\n",
" node = NodeTree(key1, key2)\n",
" nodes.append((node, c1 + c2))\n",
" nodes = sorted(nodes, key=lambda x: x[1], reverse=True)\n",
" return nodes[0][0]"
]
},
{
"cell_type": "code",
"execution_count": 263,
"id": "e477d0c8",
"metadata": {},
"outputs": [],
"source": [
"def enc_experiment(images, plot=True):\n",
" origin, predict, diff, error, A = plot_hist(images, 2)\n",
" image = Image.open(images[0]) #Open the image and read it as an Image object\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",
" new_error = np.copy(image)\n",
" #new_error[1:-1,1:-1] = np.reshape(error[1:-1,1:-1],(510, 638))\n",
" new_error[1:-1, 1:-1] = error[1:-1, 1:-1]\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",
" if plot:\n",
" plt.hist(new_error[1:],bins=100)\n",
" plt.show()\n",
" \n",
" #ab_error = np.abs(new_error)\n",
" #string = [str(i) for i in ab_error]\n",
" string = [str(i) for i in new_error]\n",
" #string = [str(i) for i in np.arange(0,5)] + [str(i) for i in np.arange(0,5)] + [str(i) for i in np.arange(0,2)]*2\n",
" freq = dict(Counter(string))\n",
" freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)\n",
" \n",
" node = make_tree(freq)\n",
" encoding_dict = huffman_code_tree(node)\n",
" #encoded = [\"1\"+encoding[str(-i)] if i < 0 else \"0\"+encoding[str(i)] for i in error]\n",
" #print(time.time()-start)\n",
" encoded = new_error.reshape((512,640)).copy().astype(str)\n",
" \n",
" #encoded = np.zeros_like(new_error.reshape((512,640))).astype(str)\n",
" print(encoded)\n",
" for i in range(encoded.shape[0]):\n",
" for j in range(encoded.shape[1]):\n",
" if i == 0 and j == 0:\n",
" encoded[i][j] = encoded[i][j]\n",
" else:\n",
" #print(encoding_dict[encoded[i][j]])\n",
" encoded[i][j] = encoding_dict[encoded[i][j]]\n",
" #print(encoded[i][j])\n",
" \n",
" return encoding_dict, encoded, new_error.reshape((512,640))\n",
" #print(encoding)"
]
},
{
"cell_type": "code",
"execution_count": 264,
"id": "fd8e96a5",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"15"
]
},
"execution_count": 679,
"metadata": {},
"output_type": "execute_result"
"name": "stdout",
"output_type": "stream",
"text": [
"[['22541' '-10' '14' ... '32' '48' '33']\n",
" ['7' '67' '-21' ... '-1' '-1' '77']\n",
" ['7' '-15' '-3' ... '10' '-45' '58']\n",
" ...\n",
" ['49' '82' '-2' ... '-64' '5' '151']\n",
" ['27' '-33' '18' ... '47' '-16' '208']\n",
" ['17' '0' '-5' ... '138' '207' '226']]\n"
]
}
],
"source": []
"source": [
"encode_dict, encoding, error = enc_experiment(images, plot=False)"
]
},
{
"cell_type": "code",
"execution_count": 265,
"id": "7ebd8dd8",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"11110111110\n",
"92\n",
"11110111110001\n"
]
}
],
"source": [
"print(encoding[0][100])\n",
"print(error[0][100])\n",
"print(encode_dict['92'])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a6a579b7",
"metadata": {},
"outputs": [],
"source": [
"def decoder(A, encoded_matrix, encoding_dict):\n",
" \"\"\"\n",
" Function that accecpts the prediction matrix A for the linear system,\n",
" the encoded matrix of error values, and the encoding dicitonary.\n",
" \"\"\"\n",
" error_matrix = encoded_matrix.copy()\n",
" for i in range(error_matrix.shape[0]):\n",
" for j in range(error_matrix.shape[1]):\n",
" if i == 0 and j == 0:\n",
" error_matrix[i][j] = encoded_matrix[i][j]\n",
" else:\n",
" error_matrix[i][j] = encoding_dict."
]
}
],
"metadata": {
...
...
@@ -343,7 +482,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.
9.
1"
"version": "3.
8.1
1"
}
},
"nbformat": 4,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment