" 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",
" # 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",
" #mse_start = (points@fit).T\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",
...
...
@@ -712,8 +753,6 @@
" \n",
" f, res, rank, s = la.lstsq(points, neighbor.T, rcond=None) \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",
" # 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",
" #mse_start = (points@fit).T\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",
...
...
@@ -712,8 +753,6 @@
" \n",
" f, res, rank, s = la.lstsq(points, neighbor.T, rcond=None) \n",