Commit 51ac3d5f authored by Bryce Hepner's avatar Bryce Hepner

changed variable names, comments, docstring

parent 8b7366b7
......@@ -83,7 +83,7 @@
"metadata": {},
"outputs": [],
"source": [
"def predict_pix(tiff_image, difference = True):\n",
"def predict_pix(tiff_image_path, difference = True):\n",
" \"\"\"\n",
" This function predict the pixel values excluding the boundary.\n",
" Using the 4 neighbor pixel values and MSE to predict the next pixel value\n",
......@@ -95,25 +95,25 @@
" and the predicted pixel value is c.\n",
" \n",
" Input:\n",
" tiff_image (string): path to the tiff file\n",
" tiff_image_path (string): path to the tiff file\n",
" \n",
" Return:\n",
" image (512 X 640): original image \n",
" predict (325380,): predicted image excluding the boundary\n",
" diff. (325380,): IF difference = TRUE, difference between the min and max of four neighbors exclude the boundary\n",
" ELSE: the residuals of the four nearest pixels to a fitted hyperplane\n",
" error (325380,): difference between the original image and predicted image\n",
" A (3 X 3): system of equation\n",
" image ndarray(512 X 640): original image \n",
" predict ndarray(325380,): predicted image excluding the boundary\n",
" diff. ndarray(325380,): IF difference = TRUE, difference between the min and max of four neighbors exclude the boundary\n",
" ELSE: the residuals of the four nearest pixels to a fitted hyperplane\n",
" error ndarray(325380,): difference between the original image and predicted image\n",
" A ndarray(3 X 3): system of equation\n",
" \"\"\"\n",
" image = Image.open(tiff_image) #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",
" image_obj = Image.open(tiff_image_path) #Open the image and read it as an Image object\n",
" image_array = np.array(image_obj)[1:,:].astype(int) #Convert to an array, leaving out the first row because the first row is just housekeeping data\n",
" # image_array = image_array.astype(int) \n",
" A = np.array([[3,0,-1],[0,3,3],[1,-3,-4]]) # the matrix for system of equation\n",
" # where z0 = (-1,1), z1 = (0,1), z2 = (1,1), z3 = (-1,0)\n",
" z0 = image[0:-2,0:-2] # get all the first pixel for the entire image\n",
" z1 = image[0:-2,1:-1] # get all the second pixel for the entire image\n",
" z2 = image[0:-2,2::] # get all the third pixel for the entire image\n",
" z3 = image[1:-1,0:-2] # get all the forth pixel for the entire image\n",
" z0 = image_array[0:-2,0:-2] # get all the first pixel for the entire image\n",
" z1 = image_array[0:-2,1:-1] # get all the second pixel for the entire image\n",
" z2 = image_array[0:-2,2::] # get all the third pixel for the entire image\n",
" z3 = image_array[1:-1,0:-2] # get all the forth pixel for the entire image\n",
" \n",
" # calculate the out put of the system of equation\n",
" y0 = np.ravel(-z0+z2-z3)\n",
......@@ -143,13 +143,13 @@
" #Compute the best fitting hyperplane using least squares\n",
" #The res is the residuals of the four points used to fit the hyperplane (summed distance of each of the \n",
" #points to the hyperplane), it is a measure of gradient\n",
" f, diff, rank, s = la.lstsq(points, neighbor.T, rcond=None) \n",
" f, diff, rank, s = la.lstsq(points, neighbor.T, rcond=None)\n",
" diff = diff.astype(int)\n",
" \n",
" # calculate the error\n",
" error = np.ravel(image[1:-1,1:-1])-predict\n",
" error = np.ravel(image_array[1:-1,1:-1])-predict\n",
" \n",
" return image, predict, diff, error, A"
" return image_array, predict, diff, error, A"
]
},
{
......@@ -207,19 +207,19 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 10,
"id": "b7561883",
"metadata": {},
"outputs": [],
"source": [
"def huffman(image, num_bins=4, difference = True):\n",
"def huffman(tiff_image_path, num_bins=4, difference = True):\n",
" \"\"\"\n",
" This function is used to encode the error based on the difference\n",
" and split the difference into different bins\n",
" \n",
" Input:\n",
" image (string): path to the tiff file\n",
" num_bins (int): number of bins\n",
" tiff_image_path (string): path to the tiff file\n",
" num_bins (int): number of bins\n",
" \n",
" Return:\n",
" list_dic (num_bins + 1): a list of dictionary\n",
......@@ -232,23 +232,22 @@
" A (3 X 3): system of equation\n",
" \n",
" \"\"\"\n",
" # get the prediction error and difference\n",
" image, predict, diff, error, A = predict_pix(image, difference)\n",
" # get the image_array, etc\n",
" image_array, predict, diff, error, A = predict_pix(tiff_image_path, difference)\n",
" \n",
" # get the number of points in each bins\n",
" # calculate the number of points that will go in each bin\n",
" data_points_per_bin = len(diff) // num_bins\n",
" \n",
" # sort the difference and create the bins\n",
" sorted_diff = diff.copy()\n",
" sorted_diff.sort()\n",
" sorted_diff = np.sort(diff.copy())\n",
" bins = [sorted_diff[i*data_points_per_bin] for i in range(1,num_bins)]\n",
" \n",
" # get the boundary \n",
" boundary = np.hstack((image[0,:],image[-1,:],image[1:-1,0],image[1:-1,-1]))\n",
" boundary = np.hstack((image_array[0,:],image_array[-1,:],image_array[1:-1,0],image_array[1:-1,-1]))\n",
" \n",
" # take the difference of the boundary with the very first pixel\n",
" boundary = boundary - image[0,0]\n",
" boundary[0] = image[0,0]\n",
" boundary = boundary - image_array[0,0]\n",
" boundary[0] = image_array[0,0]\n",
" \n",
" # huffman encode the boundary\n",
" string = [str(i) for i in boundary]\n",
......@@ -298,7 +297,7 @@
" list_dic.append(encode)\n",
"\n",
" # create a error matrix that includes the boundary (used in encoding matrix)\n",
" new_error = np.copy(image)\n",
" new_error = np.copy(image_array)\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",
......@@ -309,13 +308,13 @@
" \n",
" diff = np.reshape(diff,(510,638))\n",
" # return the huffman dictionary\n",
" return list_dic, image, new_error, diff, boundary, predict, bins, A\n",
" return list_dic, image_array, new_error, diff, boundary, predict, bins, A\n",
" \n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 11,
"id": "2eb774d2",
"metadata": {},
"outputs": [],
......@@ -355,7 +354,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 12,
"id": "8eeb40d0",
"metadata": {},
"outputs": [],
......@@ -439,7 +438,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 13,
"id": "f959fe93",
"metadata": {},
"outputs": [],
......@@ -499,7 +498,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 14,
"id": "3e0e9742",
"metadata": {},
"outputs": [
......@@ -524,7 +523,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 15,
"id": "004e8ba8",
"metadata": {},
"outputs": [
......@@ -534,7 +533,7 @@
"0.4232928466796875"
]
},
"execution_count": 10,
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
......@@ -545,7 +544,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 16,
"id": "a282f9e6",
"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