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

changed variable names, comments, docstring

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