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
fcbea69e
Commit
fcbea69e
authored
Mar 24, 2022
by
Nathaniel Callens
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
changes
parent
f7bbe036
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
579 additions
and
40 deletions
+579
-40
Error_to_Image-checkpoint.ipynb
.ipynb_checkpoints/Error_to_Image-checkpoint.ipynb
+285
-15
Encoding_Kelly.ipynb
Encoding_Kelly.ipynb
+9
-10
Error_to_Image.ipynb
Error_to_Image.ipynb
+285
-15
No files found.
.ipynb_checkpoints/Error_to_Image-checkpoint.ipynb
View file @
fcbea69e
...
...
@@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count":
134
,
"execution_count":
2
,
"id": "dbef8759",
"metadata": {
"id": "dbef8759"
...
...
@@ -28,7 +28,7 @@
},
{
"cell_type": "code",
"execution_count":
126
,
"execution_count":
37
,
"id": "9ed20f84",
"metadata": {
"id": "9ed20f84"
...
...
@@ -97,7 +97,7 @@
" \n",
" # calculate the difference\n",
" diff = np.max(neighbor,axis = 1) - np.min(neighbor, axis=1)\n",
" diff = np.pad(diff.reshape(510,638), pad_width=1)\n",
"
#
diff = np.pad(diff.reshape(510,638), pad_width=1)\n",
" \n",
" # flatten the image to a vector\n",
" small_image = image_int[1:-1,1:-1]\n",
...
...
@@ -115,12 +115,12 @@
" error = image_int - prediction\n",
"\n",
" \n",
" return prediction, diff, image_int, error, A"
" return prediction, diff, image_int, error
[1:-1,1:-1]
, A"
]
},
{
"cell_type": "code",
"execution_count":
53
,
"execution_count":
4
,
"id": "ba2881d9",
"metadata": {},
"outputs": [],
...
...
@@ -132,7 +132,7 @@
},
{
"cell_type": "code",
"execution_count": 5
4
,
"execution_count": 5,
"id": "11e95c34",
"metadata": {},
"outputs": [],
...
...
@@ -142,7 +142,7 @@
},
{
"cell_type": "code",
"execution_count":
55
,
"execution_count":
6
,
"id": "434e4d2f",
"metadata": {},
"outputs": [],
...
...
@@ -179,7 +179,7 @@
},
{
"cell_type": "code",
"execution_count":
56
,
"execution_count":
7
,
"id": "3cc609dc",
"metadata": {},
"outputs": [],
...
...
@@ -189,7 +189,7 @@
},
{
"cell_type": "code",
"execution_count":
57
,
"execution_count":
8
,
"id": "5d290a0c",
"metadata": {},
"outputs": [
...
...
@@ -205,7 +205,7 @@
" [ True, True, True, ..., True, True, True]])"
]
},
"execution_count":
57
,
"execution_count":
8
,
"metadata": {},
"output_type": "execute_result"
}
...
...
@@ -216,7 +216,7 @@
},
{
"cell_type": "code",
"execution_count":
60
,
"execution_count":
9
,
"id": "bb11dcd0",
"metadata": {},
"outputs": [],
...
...
@@ -264,7 +264,7 @@
},
{
"cell_type": "code",
"execution_count": 1
27
,
"execution_count": 1
0
,
"id": "c01fda28",
"metadata": {},
"outputs": [],
...
...
@@ -436,20 +436,290 @@
},
{
"cell_type": "code",
"execution_count": 1
39
,
"execution_count": 1
25
,
"id": "4c268907",
"metadata": {},
"outputs": [],
"source": [
"def huffman(image, i):\n",
" pred, diff, origin, error, A = predict(image, i)\n",
" pred = np.ravel(pred[1:-1, 1:-1])\n",
" error = np.ravel(error)\n",
" \n",
" boundary = np.hstack((origin[0,:],origin[-1,:],origin[1:-1,0],origin[1:-1,-1]))\n",
" boundary = boundary - origin[0,0]\n",
" boundary[0] = origin[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 <= 10\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 > 10\n",
" new_error = error[mask]\n",
" mask2 = diff[mask] <= 25\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",
" encode3 = huffman_code_tree(node)\n",
" \n",
"\n",
" mask = diff > 25\n",
" new_error = error[mask]\n",
" mask2 = diff[mask] <= 45\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 > 45\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(origin)\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(origin), error, diff, boundary\n",
"\n",
"\n",
"encode1, encode2, encode3, encode4, encode5, origin, error, diff, boundary = huffman(images, 0)"
]
},
{
"cell_type": "code",
"execution_count": 159,
"id": "e98fc3cf",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"
6.7830123821108295
\n"
"
[22541 -10 14 ... 62 151 208]
\n"
]
}
],
"source": [
"print(np.mean(e))"
"print(boundary)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f5e71acc",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 127,
"id": "642b95a3",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.4427516682942708"
]
},
"execution_count": 127,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def compress_rate(image, error, diff, bound, encode1, encode2, encode3, encode4, encode5):\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] <= 10:\n",
" c_len += len(encode2[str(int(error[i]))])\n",
"\n",
" if diff[i] <= 25 and diff[i] > 10:\n",
" c_len += len(encode3[str(int(error[i]))])\n",
" \n",
" if diff[i] <= 45 and diff[i] > 25:\n",
" c_len += len(encode4[str(int(error[i]))])\n",
" \n",
" if diff[i] > 45:\n",
" c_len += len(encode5[str(int(error[i]))])\n",
" \n",
" return c_len/o_len\n",
"compress_rate(origin, error, diff, boundary, encode1, encode2, encode3, encode4, encode5)\n"
]
},
{
"cell_type": "code",
"execution_count": 209,
"id": "7d507cfb",
"metadata": {},
"outputs": [],
"source": [
"def encode_multiple(error, diff, bound, encode1, encode2, encode3, encode4, encode5):\n",
" #original = original.reshape(-1)\n",
" #error = error.reshape(-1)\n",
" original = len(np.ravel(error))\n",
" error = np.ravel(error)\n",
" encode_error = error.astype(str).astype(object).copy()\n",
" bound_error = bound.astype(str).astype(object).copy()\n",
" \n",
" \n",
" for i in range(0,len(bound_error)):\n",
" bound_error[i] = encode1[bound_error[i]] \n",
" \n",
" for i in range(0, original):\n",
" if diff[i] <= 10:\n",
" encode_error[i] = encode2[encode_error[i]]\n",
"\n",
" if diff[i] <= 25 and diff[i] > 10:\n",
" encode_error[i] = encode3[encode_error[i]]\n",
" \n",
" if diff[i] <= 45 and diff[i] > 25:\n",
" encode_error[i] = encode4[encode_error[i]]\n",
" \n",
" if diff[i] > 45:\n",
" encode_error[i] = encode5[encode_error[i]]\n",
" \n",
" encode_error = np.pad(encode_error.reshape(510,638), pad_width=1)\n",
" encode_error[0] = bound_error[:640]\n",
" encode_error[-1] = bound_error[640:640*2]\n",
" encode_error[1:-1,0] = bound_error[640*2:(640*2)+510]\n",
" encode_error[1:-1,-1] = bound_error[(640*2)+510:]\n",
" \n",
" return encode_error, bound_error\n",
"enc_mat, bound_e = encode_multiple(error, diff, boundary, encode1, encode2, encode3, encode4, encode5)"
]
},
{
"cell_type": "code",
"execution_count": 211,
"id": "2faf5cd9",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 1\n"
]
},
{
"ename": "ValueError",
"evalue": "'101011' is not in list",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_1700/1235154671.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m 42\u001b[0m \u001b[1;32mreturn\u001b[0m \u001b[0merror_matrix\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mastype\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mint\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 43\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 44\u001b[1;33m \u001b[0mdec\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mdecode_multi\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mA\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0menc_mat\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mencode1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mencode2\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mencode3\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mencode4\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mencode5\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdiff\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_1700/1235154671.py\u001b[0m in \u001b[0;36mdecode_multi\u001b[1;34m(A, encoded_matrix, encode1, encode2, encode3, encode4, encode5, diff)\u001b[0m\n\u001b[0;32m 35\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0merror_matrix\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mj\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;34m'101011'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 36\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mj\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 37\u001b[1;33m \u001b[0merror_matrix\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mj\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mthe_keys4\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mthe_values4\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0merror_matrix\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mj\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 38\u001b[0m \u001b[1;32melif\u001b[0m \u001b[0mdiff\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m*\u001b[0m\u001b[1;36m640\u001b[0m \u001b[1;33m+\u001b[0m \u001b[0mj\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m>\u001b[0m \u001b[1;36m45\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 39\u001b[0m \u001b[0merror_matrix\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mj\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mthe_keys5\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mthe_values5\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0merror_matrix\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mj\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mValueError\u001b[0m: '101011' is not in list"
]
}
],
"source": [
"def decode_multi(A, encoded_matrix, encode1, encode2, encode3, encode4, encode5, diff):\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",
" \n",
" \n",
" the_keys1 = list(encode1.keys())\n",
" the_values1 = list(encode1.values())\n",
" the_keys2 = list(encode2.keys())\n",
" the_values2 = list(encode2.values())\n",
" the_keys3 = list(encode3.keys())\n",
" the_values3 = list(encode3.values())\n",
" the_keys4 = list(encode4.keys())\n",
" the_values4 = list(encode4.values())\n",
" the_keys5 = list(encode5.keys())\n",
" the_values5 = list(encode5.values())\n",
" \n",
" error_matrix = encoded_matrix.copy()\n",
" \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] = int(the_keys1[the_values1.index(encoded_matrix[i,j])])\n",
" \n",
" \n",
" elif i == 0 or i == error_matrix.shape[0]-1 or j == 0 or j == error_matrix.shape[1]-1:\n",
" error_matrix[i][j] = int(the_keys1[the_values1.index(error_matrix[i,j])]) + int(error_matrix[0][0])\n",
" else:\n",
" if diff[i*640 + j] <= 10:\n",
" error_matrix[i][j] = int(the_keys2[the_values2.index(error_matrix[i,j])])\n",
" elif diff[i*640 + j] > 10 and diff[i*640 + j] <= 25:\n",
" error_matrix[i,j] = int(the_keys3[the_values3.index(error_matrix[i,j])])\n",
" elif diff[i*640 + j] > 25 and diff[i*640 + j] <= 45:\n",
" if error_matrix[i,j] == '101011':\n",
" print(i,j)\n",
" \n",
" error_matrix[i,j] = int(the_keys4[the_values4.index(error_matrix[i,j])])\n",
" elif diff[i*640 + j] > 45:\n",
" error_matrix[i,j] = int(the_keys5[the_values5.index(error_matrix[i,j])])\n",
" \n",
" \n",
" return error_matrix.astype(int)\n",
"\n",
"dec = decode_multi(A, enc_mat, encode1, encode2, encode3, encode4, encode5, diff)"
]
},
{
"cell_type": "code",
"execution_count": 204,
"id": "64832ca7",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n"
]
}
],
"source": [
"\n",
"print('101011' in enc_mat)"
]
}
],
...
...
Encoding_Kelly.ipynb
View file @
fcbea69e
...
...
@@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count":
2
,
"execution_count":
1
,
"id": "8868bc30",
"metadata": {},
"outputs": [],
...
...
@@ -24,7 +24,7 @@
},
{
"cell_type": "code",
"execution_count":
397
,
"execution_count":
2
,
"id": "76317b02",
"metadata": {},
"outputs": [],
...
...
@@ -79,7 +79,7 @@
},
{
"cell_type": "code",
"execution_count": 3
98
,
"execution_count": 3,
"id": "be1ff8a1",
"metadata": {},
"outputs": [],
...
...
@@ -125,7 +125,7 @@
},
{
"cell_type": "code",
"execution_count":
399
,
"execution_count":
4
,
"id": "8483903e",
"metadata": {},
"outputs": [],
...
...
@@ -173,7 +173,7 @@
},
{
"cell_type": "code",
"execution_count":
400
,
"execution_count":
5
,
"id": "64a3a193",
"metadata": {},
"outputs": [],
...
...
@@ -614,17 +614,17 @@
},
{
"cell_type": "code",
"execution_count":
427
,
"execution_count":
14
,
"id": "15eecad3",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.442
25341796875
"
"0.442
7516682942708
"
]
},
"execution_count":
427
,
"execution_count":
14
,
"metadata": {},
"output_type": "execute_result"
}
...
...
@@ -635,7 +635,6 @@
" 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",
" boundary = np.hstack((image[0,:],image[-1,:],image[1:-1,0],image[1:-1,-1]))\n",
" boundary = boundary - image[0,0]\n",
...
...
@@ -932,7 +931,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 @
fcbea69e
...
...
@@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count":
134
,
"execution_count":
2
,
"id": "dbef8759",
"metadata": {
"id": "dbef8759"
...
...
@@ -28,7 +28,7 @@
},
{
"cell_type": "code",
"execution_count":
126
,
"execution_count":
37
,
"id": "9ed20f84",
"metadata": {
"id": "9ed20f84"
...
...
@@ -97,7 +97,7 @@
" \n",
" # calculate the difference\n",
" diff = np.max(neighbor,axis = 1) - np.min(neighbor, axis=1)\n",
" diff = np.pad(diff.reshape(510,638), pad_width=1)\n",
"
#
diff = np.pad(diff.reshape(510,638), pad_width=1)\n",
" \n",
" # flatten the image to a vector\n",
" small_image = image_int[1:-1,1:-1]\n",
...
...
@@ -115,12 +115,12 @@
" error = image_int - prediction\n",
"\n",
" \n",
" return prediction, diff, image_int, error, A"
" return prediction, diff, image_int, error
[1:-1,1:-1]
, A"
]
},
{
"cell_type": "code",
"execution_count":
53
,
"execution_count":
4
,
"id": "ba2881d9",
"metadata": {},
"outputs": [],
...
...
@@ -132,7 +132,7 @@
},
{
"cell_type": "code",
"execution_count": 5
4
,
"execution_count": 5,
"id": "11e95c34",
"metadata": {},
"outputs": [],
...
...
@@ -142,7 +142,7 @@
},
{
"cell_type": "code",
"execution_count":
55
,
"execution_count":
6
,
"id": "434e4d2f",
"metadata": {},
"outputs": [],
...
...
@@ -179,7 +179,7 @@
},
{
"cell_type": "code",
"execution_count":
56
,
"execution_count":
7
,
"id": "3cc609dc",
"metadata": {},
"outputs": [],
...
...
@@ -189,7 +189,7 @@
},
{
"cell_type": "code",
"execution_count":
57
,
"execution_count":
8
,
"id": "5d290a0c",
"metadata": {},
"outputs": [
...
...
@@ -205,7 +205,7 @@
" [ True, True, True, ..., True, True, True]])"
]
},
"execution_count":
57
,
"execution_count":
8
,
"metadata": {},
"output_type": "execute_result"
}
...
...
@@ -216,7 +216,7 @@
},
{
"cell_type": "code",
"execution_count":
60
,
"execution_count":
9
,
"id": "bb11dcd0",
"metadata": {},
"outputs": [],
...
...
@@ -264,7 +264,7 @@
},
{
"cell_type": "code",
"execution_count": 1
27
,
"execution_count": 1
0
,
"id": "c01fda28",
"metadata": {},
"outputs": [],
...
...
@@ -436,20 +436,290 @@
},
{
"cell_type": "code",
"execution_count": 1
39
,
"execution_count": 1
25
,
"id": "4c268907",
"metadata": {},
"outputs": [],
"source": [
"def huffman(image, i):\n",
" pred, diff, origin, error, A = predict(image, i)\n",
" pred = np.ravel(pred[1:-1, 1:-1])\n",
" error = np.ravel(error)\n",
" \n",
" boundary = np.hstack((origin[0,:],origin[-1,:],origin[1:-1,0],origin[1:-1,-1]))\n",
" boundary = boundary - origin[0,0]\n",
" boundary[0] = origin[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 <= 10\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 > 10\n",
" new_error = error[mask]\n",
" mask2 = diff[mask] <= 25\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",
" encode3 = huffman_code_tree(node)\n",
" \n",
"\n",
" mask = diff > 25\n",
" new_error = error[mask]\n",
" mask2 = diff[mask] <= 45\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 > 45\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(origin)\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(origin), error, diff, boundary\n",
"\n",
"\n",
"encode1, encode2, encode3, encode4, encode5, origin, error, diff, boundary = huffman(images, 0)"
]
},
{
"cell_type": "code",
"execution_count": 159,
"id": "e98fc3cf",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"
6.7830123821108295
\n"
"
[22541 -10 14 ... 62 151 208]
\n"
]
}
],
"source": [
"print(np.mean(e))"
"print(boundary)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f5e71acc",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 127,
"id": "642b95a3",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.4427516682942708"
]
},
"execution_count": 127,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def compress_rate(image, error, diff, bound, encode1, encode2, encode3, encode4, encode5):\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] <= 10:\n",
" c_len += len(encode2[str(int(error[i]))])\n",
"\n",
" if diff[i] <= 25 and diff[i] > 10:\n",
" c_len += len(encode3[str(int(error[i]))])\n",
" \n",
" if diff[i] <= 45 and diff[i] > 25:\n",
" c_len += len(encode4[str(int(error[i]))])\n",
" \n",
" if diff[i] > 45:\n",
" c_len += len(encode5[str(int(error[i]))])\n",
" \n",
" return c_len/o_len\n",
"compress_rate(origin, error, diff, boundary, encode1, encode2, encode3, encode4, encode5)\n"
]
},
{
"cell_type": "code",
"execution_count": 209,
"id": "7d507cfb",
"metadata": {},
"outputs": [],
"source": [
"def encode_multiple(error, diff, bound, encode1, encode2, encode3, encode4, encode5):\n",
" #original = original.reshape(-1)\n",
" #error = error.reshape(-1)\n",
" original = len(np.ravel(error))\n",
" error = np.ravel(error)\n",
" encode_error = error.astype(str).astype(object).copy()\n",
" bound_error = bound.astype(str).astype(object).copy()\n",
" \n",
" \n",
" for i in range(0,len(bound_error)):\n",
" bound_error[i] = encode1[bound_error[i]] \n",
" \n",
" for i in range(0, original):\n",
" if diff[i] <= 10:\n",
" encode_error[i] = encode2[encode_error[i]]\n",
"\n",
" if diff[i] <= 25 and diff[i] > 10:\n",
" encode_error[i] = encode3[encode_error[i]]\n",
" \n",
" if diff[i] <= 45 and diff[i] > 25:\n",
" encode_error[i] = encode4[encode_error[i]]\n",
" \n",
" if diff[i] > 45:\n",
" encode_error[i] = encode5[encode_error[i]]\n",
" \n",
" encode_error = np.pad(encode_error.reshape(510,638), pad_width=1)\n",
" encode_error[0] = bound_error[:640]\n",
" encode_error[-1] = bound_error[640:640*2]\n",
" encode_error[1:-1,0] = bound_error[640*2:(640*2)+510]\n",
" encode_error[1:-1,-1] = bound_error[(640*2)+510:]\n",
" \n",
" return encode_error, bound_error\n",
"enc_mat, bound_e = encode_multiple(error, diff, boundary, encode1, encode2, encode3, encode4, encode5)"
]
},
{
"cell_type": "code",
"execution_count": 211,
"id": "2faf5cd9",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 1\n"
]
},
{
"ename": "ValueError",
"evalue": "'101011' is not in list",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_1700/1235154671.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m 42\u001b[0m \u001b[1;32mreturn\u001b[0m \u001b[0merror_matrix\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mastype\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mint\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 43\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 44\u001b[1;33m \u001b[0mdec\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mdecode_multi\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mA\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0menc_mat\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mencode1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mencode2\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mencode3\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mencode4\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mencode5\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdiff\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_1700/1235154671.py\u001b[0m in \u001b[0;36mdecode_multi\u001b[1;34m(A, encoded_matrix, encode1, encode2, encode3, encode4, encode5, diff)\u001b[0m\n\u001b[0;32m 35\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0merror_matrix\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mj\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;34m'101011'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 36\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mj\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 37\u001b[1;33m \u001b[0merror_matrix\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mj\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mthe_keys4\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mthe_values4\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0merror_matrix\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mj\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 38\u001b[0m \u001b[1;32melif\u001b[0m \u001b[0mdiff\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m*\u001b[0m\u001b[1;36m640\u001b[0m \u001b[1;33m+\u001b[0m \u001b[0mj\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m>\u001b[0m \u001b[1;36m45\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 39\u001b[0m \u001b[0merror_matrix\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mj\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mthe_keys5\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mthe_values5\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0merror_matrix\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mj\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mValueError\u001b[0m: '101011' is not in list"
]
}
],
"source": [
"def decode_multi(A, encoded_matrix, encode1, encode2, encode3, encode4, encode5, diff):\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",
" \n",
" \n",
" the_keys1 = list(encode1.keys())\n",
" the_values1 = list(encode1.values())\n",
" the_keys2 = list(encode2.keys())\n",
" the_values2 = list(encode2.values())\n",
" the_keys3 = list(encode3.keys())\n",
" the_values3 = list(encode3.values())\n",
" the_keys4 = list(encode4.keys())\n",
" the_values4 = list(encode4.values())\n",
" the_keys5 = list(encode5.keys())\n",
" the_values5 = list(encode5.values())\n",
" \n",
" error_matrix = encoded_matrix.copy()\n",
" \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] = int(the_keys1[the_values1.index(encoded_matrix[i,j])])\n",
" \n",
" \n",
" elif i == 0 or i == error_matrix.shape[0]-1 or j == 0 or j == error_matrix.shape[1]-1:\n",
" error_matrix[i][j] = int(the_keys1[the_values1.index(error_matrix[i,j])]) + int(error_matrix[0][0])\n",
" else:\n",
" if diff[i*640 + j] <= 10:\n",
" error_matrix[i][j] = int(the_keys2[the_values2.index(error_matrix[i,j])])\n",
" elif diff[i*640 + j] > 10 and diff[i*640 + j] <= 25:\n",
" error_matrix[i,j] = int(the_keys3[the_values3.index(error_matrix[i,j])])\n",
" elif diff[i*640 + j] > 25 and diff[i*640 + j] <= 45:\n",
" if error_matrix[i,j] == '101011':\n",
" print(i,j)\n",
" \n",
" error_matrix[i,j] = int(the_keys4[the_values4.index(error_matrix[i,j])])\n",
" elif diff[i*640 + j] > 45:\n",
" error_matrix[i,j] = int(the_keys5[the_values5.index(error_matrix[i,j])])\n",
" \n",
" \n",
" return error_matrix.astype(int)\n",
"\n",
"dec = decode_multi(A, enc_mat, encode1, encode2, encode3, encode4, encode5, diff)"
]
},
{
"cell_type": "code",
"execution_count": 204,
"id": "64832ca7",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n"
]
}
],
"source": [
"\n",
"print('101011' in enc_mat)"
]
}
],
...
...
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