Commit 6d853115 authored by Nathaniel Callens's avatar Nathaniel Callens

Merge branch 'master' of git.elphel.com:Elphel/master

parents 174904aa 88251d36
No preview for this file type
This source diff could not be displayed because it is too large. You can view the blob instead.
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
"cells": [ "cells": [
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 12, "execution_count": 34,
"id": "14f74f21", "id": "14f74f21",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
...@@ -24,7 +24,7 @@ ...@@ -24,7 +24,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 13, "execution_count": 35,
"id": "c16af61f", "id": "c16af61f",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
...@@ -78,7 +78,7 @@ ...@@ -78,7 +78,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 14, "execution_count": 36,
"id": "aceba613", "id": "aceba613",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
...@@ -139,7 +139,7 @@ ...@@ -139,7 +139,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 15, "execution_count": 37,
"id": "6b965751", "id": "6b965751",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
...@@ -192,7 +192,7 @@ ...@@ -192,7 +192,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 24, "execution_count": 38,
"id": "b7561883", "id": "b7561883",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
...@@ -300,14 +300,23 @@ ...@@ -300,14 +300,23 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 17, "execution_count": 39,
"id": "2eb774d2", "id": "2eb774d2",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"def encoder(error, list_dic, diff, bound, bins):\n", "def encoder(error, list_dic, diff, bound, bins):\n",
" \"\"\"\n", " \"\"\"\n",
" This function en\n", " This function encode the matrix with huffman coding tables\n",
" \n",
" Input:\n",
" error (512, 640): a matrix with all the errors\n",
" list_dic (num_dic + 1,): a list of huffman coding table \n",
" bound (2300,): the boundary values after subtracting the very first pixel value\n",
" bins (num_bins - 1,): a list of threshold to cut the bins\n",
" \n",
" Return:\n",
" encoded (512, 640): encoded matrix\n",
" \"\"\"\n", " \"\"\"\n",
" # copy the error matrix (including the boundary)\n", " # copy the error matrix (including the boundary)\n",
" encoded = np.copy(error).astype(int).astype(str).astype(object)\n", " encoded = np.copy(error).astype(int).astype(str).astype(object)\n",
...@@ -331,15 +340,21 @@ ...@@ -331,15 +340,21 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 23, "execution_count": 40,
"id": "8eeb40d0", "id": "8eeb40d0",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"def decoder(A, encoded_matrix, list_dic, bins):\n", "def decoder(A, encoded_matrix, list_dic, bins):\n",
" \"\"\"\n", " \"\"\"\n",
" Function that accecpts the prediction matrix A for the linear system,\n", " This function decodes the encoded_matrix.\n",
" the encoded matrix of error values, and the encoding dicitonary.\n", " Input:\n",
" A (3 X 3): system of equation\n",
" list_dic (num_dic + 1,): a list of huffman coding table \n",
" encoded_matrix (512, 640): encoded matrix\n",
" bins (num_bins - 1,): a list of threshold to cut the bins\n",
" Return:\n",
" decode_matrix (512, 640): decoded matrix\n",
" \"\"\"\n", " \"\"\"\n",
" # change the dictionary back to list\n", " # change the dictionary back to list\n",
" # !!!!!WARNING!!!! has to change this part, eveytime you change the number of bins\n", " # !!!!!WARNING!!!! has to change this part, eveytime you change the number of bins\n",
...@@ -358,23 +373,23 @@ ...@@ -358,23 +373,23 @@
" the_keys4 = list(list_dic[4].keys())\n", " the_keys4 = list(list_dic[4].keys())\n",
" the_values4 = list(list_dic[4].values())\n", " the_values4 = list(list_dic[4].values())\n",
" \n", " \n",
" error_matrix = np.zeros((512,640))\n", " decode_matrix = np.zeros((512,640))\n",
" # loop through all the element in the matrix\n", " # loop through all the element in the matrix\n",
" for i in range(error_matrix.shape[0]):\n", " for i in range(decode_matrix.shape[0]):\n",
" for j in range(error_matrix.shape[1]):\n", " for j in range(decode_matrix.shape[1]):\n",
" # if it's the very first pixel on the image\n", " # if it's the very first pixel on the image\n",
" if i == 0 and j == 0:\n", " if i == 0 and j == 0:\n",
" error_matrix[i][j] = int(the_keys0[the_values0.index(encoded_matrix[i,j])])\n", " decode_matrix[i][j] = int(the_keys0[the_values0.index(encoded_matrix[i,j])])\n",
" # if it's on the boundary\n", " # if it's on the boundary\n",
" elif i == 0 or i == error_matrix.shape[0]-1 or j == 0 or j == error_matrix.shape[1]-1:\n", " elif i == 0 or i == decode_matrix.shape[0]-1 or j == 0 or j == decode_matrix.shape[1]-1:\n",
" error_matrix[i][j] = int(the_keys0[the_values0.index(encoded_matrix[i,j])]) + error_matrix[0][0]\n", " decode_matrix[i][j] = int(the_keys0[the_values0.index(encoded_matrix[i,j])]) + decode_matrix[0][0]\n",
" # if not the boundary\n", " # if not the boundary\n",
" else:\n", " else:\n",
" # predict the image with the known pixel value\n", " # predict the image with the known pixel value\n",
" z0 = error_matrix[i-1][j-1]\n", " z0 = decode_matrix[i-1][j-1]\n",
" z1 = error_matrix[i-1][j]\n", " z1 = decode_matrix[i-1][j]\n",
" z2 = error_matrix[i-1][j+1]\n", " z2 = decode_matrix[i-1][j+1]\n",
" z3 = error_matrix[i][j-1]\n", " z3 = decode_matrix[i][j-1]\n",
" y0 = int(-z0+z2-z3)\n", " y0 = int(-z0+z2-z3)\n",
" y1 = int(z0+z1+z2)\n", " y1 = int(z0+z1+z2)\n",
" y2 = int(-z0-z1-z2-z3)\n", " y2 = int(-z0-z1-z2-z3)\n",
...@@ -385,34 +400,38 @@ ...@@ -385,34 +400,38 @@
" # add on the difference by searching the dictionary\n", " # add on the difference by searching the dictionary\n",
" # !!!!!WARNING!!!! has to change this part, eveytime you change the number of bins\n", " # !!!!!WARNING!!!! has to change this part, eveytime you change the number of bins\n",
" if difference <= bins[0]:\n", " if difference <= bins[0]:\n",
" error_matrix[i][j] = int(the_keys1[the_values1.index(encoded_matrix[i,j])]) + int(predict)\n", " decode_matrix[i][j] = int(the_keys1[the_values1.index(encoded_matrix[i,j])]) + int(predict)\n",
" elif difference <= bins[1] and difference > bins[0]:\n", " elif difference <= bins[1] and difference > bins[0]:\n",
" error_matrix[i][j] = int(the_keys2[the_values2.index(encoded_matrix[i,j])]) + int(predict)\n", " decode_matrix[i][j] = int(the_keys2[the_values2.index(encoded_matrix[i,j])]) + int(predict)\n",
" elif difference <= bins[2] and difference > bins[1]:\n", " elif difference <= bins[2] and difference > bins[1]:\n",
" error_matrix[i][j] = int(the_keys3[the_values3.index(encoded_matrix[i,j])]) + int(predict)\n", " decode_matrix[i][j] = int(the_keys3[the_values3.index(encoded_matrix[i,j])]) + int(predict)\n",
" else:\n", " else:\n",
" error_matrix[i][j] = int(the_keys4[the_values4.index(encoded_matrix[i,j])]) + int(predict)\n", " decode_matrix[i][j] = int(the_keys4[the_values4.index(encoded_matrix[i,j])]) + int(predict)\n",
" \n", " \n",
" \n", " \n",
" return error_matrix.astype(int)" " return decode_matrix.astype(int)"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 19, "execution_count": 41,
"id": "f959fe93", "id": "f959fe93",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"def compress_rate(image, error, diff, bound, list_dic, bins):\n", "def compress_rate(image, error, diff, bound, list_dic, bins):\n",
" '''\n",
" This function is used to calculate the compression rate.\n",
" \n",
" '''\n",
" # the bits for the original image\n", " # the bits for the original image\n",
" o_len = 0\n", " o_len = 0\n",
" # the bits for the compressed image\n", " # the bits for the compressed image\n",
" c_len = 0\n", " c_len = 0\n",
" # initializing the varible \n", " # initializing the varible \n",
" im = np.reshape(image,(512, 640))\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", " real_b = np.hstack((image[0,:],image[-1,:],image[1:-1,0],image[1:-1,-1]))\n",
" original = im[1:-1,1:-1].reshape(-1)\n", " original = image[1:-1,1:-1].reshape(-1)\n",
" diff = diff.reshape(-1)\n", " diff = diff.reshape(-1)\n",
" \n", " \n",
" # calculate the bit for boundary\n", " # calculate the bit for boundary\n",
...@@ -444,7 +463,7 @@ ...@@ -444,7 +463,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 25, "execution_count": 42,
"id": "3e0e9742", "id": "3e0e9742",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
...@@ -470,7 +489,7 @@ ...@@ -470,7 +489,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 32, "execution_count": 33,
"id": "004e8ba8", "id": "004e8ba8",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
...@@ -478,13 +497,13 @@ ...@@ -478,13 +497,13 @@
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"[26, 40, 62]\n" "(512, 640)\n"
] ]
} }
], ],
"source": [ "source": [
"\n", "\n",
"print(bins)" "print(encoded_matrix.shape)"
] ]
}, },
{ {
......
File added
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