Commit fcbbeb88 authored by Kelly Chang's avatar Kelly Chang

solve coflict

parent 5abc68cc
No preview for this file type
...@@ -213,8 +213,8 @@ ...@@ -213,8 +213,8 @@
" diff (510, 638): difference of min and max of the 4 neighbors\n", " diff (510, 638): difference of min and max of the 4 neighbors\n",
" boundary (2300,): the boundary values after subtracting the very first pixel value\n", " boundary (2300,): the boundary values after subtracting the very first pixel value\n",
" predict (325380,): the list of predicted values\n", " predict (325380,): the list of predicted values\n",
" bins (num_bins)\n", " bins (num_bins - 1,): a list of threshold to cut the bins\n",
" 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 prediction error and difference\n",
...@@ -306,6 +306,9 @@ ...@@ -306,6 +306,9 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"def encoder(error, list_dic, diff, bound, bins):\n", "def encoder(error, list_dic, diff, bound, bins):\n",
" \"\"\"\n",
" This function en\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",
" #diff = np.reshape(diff,(510,638))\n", " #diff = np.reshape(diff,(510,638))\n",
...@@ -490,57 +493,7 @@ ...@@ -490,57 +493,7 @@
"id": "a282f9e6", "id": "a282f9e6",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": []
"def predict_pix_lstsq(tiff_list):\n",
" \"\"\"\n",
" Predict the next pixel using a fit hyperplane of the four closest pixels.\n",
" The gradient measure in this function is the summed distance to the fitted hyperplane\n",
" of each of the four points, aka the residual from the least squares function. The previous\n",
" predict_pix function uses the difference between the minimal and maximal pixels of the surrounding\n",
" four.\n",
" \"\"\"\n",
"\n",
" image = tiff_list\n",
" image = Image.open(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",
" A = np.array([[3,0,-1],[0,3,3],[1,-3,-4]]) # the matrix for system of equation\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",
" # calculate the out put of the system of equation\n",
" y0 = np.ravel(-z0+z2-z3)\n",
" y1 = np.ravel(z0+z1+z2)\n",
" y2 = np.ravel(-z0-z1-z2-z3)\n",
" y = np.vstack((y0,y1,y2))\n",
" \n",
" # use numpy solver to solve the system of equations all at once\n",
" predict = np.round(np.round((np.linalg.solve(A,y)[-1]),1)) #round the solution to the nearest integer so that encoding/decoding is easier\n",
" \n",
" points = np.array([[-1,-1,1], [-1,0,1], [-1,1,1], [0,-1,1]]) #Matrix system of points that will be used to solve the least squares fitting hyperplane\n",
"\n",
" \n",
" \n",
" # flatten the neighbor pixels and stack them together\n",
" z0 = np.ravel(z0)\n",
" z1 = np.ravel(z1)\n",
" z2 = np.ravel(z2)\n",
" z3 = np.ravel(z3)\n",
" neighbor = np.vstack((z0,z1,z2,z3)).T\n",
" \n",
" f, res, rank, s = la.lstsq(points, neighbor.T, rcond=None) \n",
" \n",
" \n",
" # calculate the difference\n",
" diff = np.max(neighbor,axis = 1) - np.min(neighbor, axis=1)\n",
" \n",
" # flatten the image to a vector\n",
" image = np.ravel(image[1:-1,1:-1])\n",
" error = image-predict\n",
" \n",
" return image, predict, res, error, A, diff"
]
} }
], ],
"metadata": { "metadata": {
...@@ -559,7 +512,7 @@ ...@@ -559,7 +512,7 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.8.11" "version": "3.9.1"
} }
}, },
"nbformat": 4, "nbformat": 4,
......
...@@ -307,7 +307,7 @@ ...@@ -307,7 +307,7 @@
"source": [ "source": [
"def encoder(error, list_dic, diff, bound, bins):\n", "def encoder(error, list_dic, diff, bound, bins):\n",
" \"\"\"\n", " \"\"\"\n",
" This function \n", " This function en\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",
...@@ -493,57 +493,7 @@ ...@@ -493,57 +493,7 @@
"id": "a282f9e6", "id": "a282f9e6",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": []
"def predict_pix_lstsq(tiff_list):\n",
" \"\"\"\n",
" Predict the next pixel using a fit hyperplane of the four closest pixels.\n",
" The gradient measure in this function is the summed distance to the fitted hyperplane\n",
" of each of the four points, aka the residual from the least squares function. The previous\n",
" predict_pix function uses the difference between the minimal and maximal pixels of the surrounding\n",
" four.\n",
" \"\"\"\n",
"\n",
" image = tiff_list\n",
" image = Image.open(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",
" A = np.array([[3,0,-1],[0,3,3],[1,-3,-4]]) # the matrix for system of equation\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",
" # calculate the out put of the system of equation\n",
" y0 = np.ravel(-z0+z2-z3)\n",
" y1 = np.ravel(z0+z1+z2)\n",
" y2 = np.ravel(-z0-z1-z2-z3)\n",
" y = np.vstack((y0,y1,y2))\n",
" \n",
" # use numpy solver to solve the system of equations all at once\n",
" predict = np.round(np.round((np.linalg.solve(A,y)[-1]),1)) #round the solution to the nearest integer so that encoding/decoding is easier\n",
" \n",
" points = np.array([[-1,-1,1], [-1,0,1], [-1,1,1], [0,-1,1]]) #Matrix system of points that will be used to solve the least squares fitting hyperplane\n",
"\n",
" \n",
" \n",
" # flatten the neighbor pixels and stack them together\n",
" z0 = np.ravel(z0)\n",
" z1 = np.ravel(z1)\n",
" z2 = np.ravel(z2)\n",
" z3 = np.ravel(z3)\n",
" neighbor = np.vstack((z0,z1,z2,z3)).T\n",
" \n",
" f, res, rank, s = la.lstsq(points, neighbor.T, rcond=None) \n",
" \n",
" \n",
" # calculate the difference\n",
" diff = np.max(neighbor,axis = 1) - np.min(neighbor, axis=1)\n",
" \n",
" # flatten the image to a vector\n",
" image = np.ravel(image[1:-1,1:-1])\n",
" error = image-predict\n",
" \n",
" return image, predict, res, error, A, diff"
]
} }
], ],
"metadata": { "metadata": {
...@@ -562,7 +512,7 @@ ...@@ -562,7 +512,7 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.8.11" "version": "3.9.1"
} }
}, },
"nbformat": 4, "nbformat": 4,
......
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