Loading .ipynb_checkpoints/Error_to_Image-checkpoint.ipynb +2 −2 Original line number Original line Diff line number Diff line %% Cell type:code id:dbef8759 tags: %% Cell type:code id:dbef8759 tags: ``` python ``` python import numpy as np import numpy as np from prediction_MSE_Scout import file_extractor, image_extractor, im_distribution from prediction_MSE_Scout import file_extractor, image_extractor, im_distribution from matplotlib import pyplot as plt from matplotlib import pyplot as plt from itertools import product from itertools import product import os import os import sys import sys from PIL import Image from PIL import Image from scipy.optimize import minimize from scipy.optimize import minimize from time import time from time import time from numpy import linalg as la from numpy import linalg as la from scipy.stats import gaussian_kde from scipy.stats import gaussian_kde import seaborn as sns import seaborn as sns import pywt import pywt ``` ``` %% Cell type:code id:9ed20f84 tags: %% Cell type:code id:9ed20f84 tags: ``` python ``` python def plot_hist(tiff_list, i=0): def plot_hist(tiff_list, i=0): """ """ This function is the leftovers from the first attempt to plot histograms. This function is the leftovers from the first attempt to plot histograms. As it stands it needs some work in order to function again. We will As it stands it needs some work in order to function again. We will fix this later. 1/25/22 fix this later. 1/25/22 """ """ image = tiff_list[i] image = tiff_list[i] image = Image.open(image) #Open the image and read it as an Image object image = Image.open(image) #Open the image and read it as an Image object image = np.array(image)[1:,:] #Convert to an array, leaving out the first row because the first row is just housekeeping data image = np.array(image)[1:,:] #Convert to an array, leaving out the first row because the first row is just housekeeping data image_int = image.astype(np.int_) image_int = image.astype(np.int_) A = np.array([[3,0,-1],[0,3,3],[1,-3,-4]]) # the matrix for system of equation A = np.array([[3,0,-1],[0,3,3],[1,-3,-4]]) # the matrix for system of equation z0 = image_int[0:-2,0:-2] # get all the first pixel for the entire image z0 = image_int[0:-2,0:-2] # get all the first pixel for the entire image z1 = image_int[0:-2,1:-1] # get all the second pixel for the entire image z1 = image_int[0:-2,1:-1] # get all the second pixel for the entire image z2 = image_int[0:-2,2::] # get all the third pixel for the entire image z2 = image_int[0:-2,2::] # get all the third pixel for the entire image z3 = image_int[1:-1,0:-2] # get all the fourth pixel for the entire image z3 = image_int[1:-1,0:-2] # get all the fourth pixel for the entire image # calculate the out put of the system of equation # calculate the out put of the system of equation y0 = np.ravel(-z0+z2-z3) y0 = np.ravel(-z0+z2-z3) y1 = np.ravel(z0+z1+z2) y1 = np.ravel(z0+z1+z2) y2 = np.ravel(-z0-z1-z2-z3) y2 = np.ravel(-z0-z1-z2-z3) y = np.vstack((y0,y1,y2)) y = np.vstack((y0,y1,y2)) # use numpy solver to solve the system of equations all at once # use numpy solver to solve the system of equations all at once predict = np.linalg.solve(A,y)[-1] predict = np.linalg.solve(A,y)[-1] #predict = [] #predict = [] # flatten the neighbor pixels and stack them together # flatten the neighbor pixels and stack them together z0 = np.ravel(z0) z0 = np.ravel(z0) z1 = np.ravel(z1) z1 = np.ravel(z1) z2 = np.ravel(z2) z2 = np.ravel(z2) z3 = np.ravel(z3) z3 = np.ravel(z3) neighbor = np.vstack((z0,z1,z2,z3)).T neighbor = np.vstack((z0,z1,z2,z3)).T # calculate the difference # calculate the difference diff = np.max(neighbor,axis = 1) - np.min(neighbor, axis=1) diff = np.max(neighbor,axis = 1) - np.min(neighbor, axis=1) # flatten the image to a vector # flatten the image to a vector small_image = image_int[1:-1,1:-1] small_image = image_int[1:-1,1:-1] #Reshape the predictions to be a 2D array #Reshape the predictions to be a 2D array predict = np.pad(predict.reshape(510,638), pad_width=1) predict = np.pad(predict.reshape(510,638), pad_width=1) """predict[0,:] = image[0,:] """predict[0,:] = image[0,:] predict[:,0] = image[:,0] predict[:,0] = image[:,0] predict[:,-1] = image[:,-1] predict[:,-1] = image[:,-1] predict[-1,:] = image[-1,:]""" predict[-1,:] = image[-1,:]""" #Calculate the error between the original image and our predictions #Calculate the error between the original image and our predictions #Note that we only predicted on the inside square of the original image, excluding #Note that we only predicted on the inside square of the original image, excluding #The first row, column and last row, column #The first row, column and last row, column #error = (image_int - predict).astype(int) #Experiment #error = (image_int - predict).astype(int) #Experiment #this one works #this one works error = image_int - predict error = image_int - predict return predict, diff, image_int, error, A return predict, diff, image_int, error, A ``` ``` %% Cell type:code id:ba2881d9 tags: %% Cell type:code id:ba2881d9 tags: ``` python ``` python scenes = file_extractor() scenes = file_extractor() images = image_extractor(scenes) images = image_extractor(scenes) num_images = im_distribution(images, "_9") num_images = im_distribution(images, "_9") ``` ``` %% Cell type:code id:11e95c34 tags: %% Cell type:code id:11e95c34 tags: ``` python ``` python predict, diff, im, err, A = plot_hist(num_images, 0) predict, diff, im, err, A = plot_hist(num_images, 0) ``` ``` %% Cell type:code id:434e4d2f tags: %% Cell type:code id:434e4d2f tags: ``` python ``` python def reconstruct(error, A): def reconstruct(error, A): """ """ Function that reconstructs the original image Function that reconstructs the original image from the error matrix and using the predictive from the error matrix and using the predictive algorithm developed in the encoding. algorithm developed in the encoding. Parameters: Parameters: error (array): matrix of errors computed in encoding. Same error (array): matrix of errors computed in encoding. Same shape as the original image (512, 640) in this case shape as the original image (512, 640) in this case A (array): Matrix used for the system of equations to create predictions A (array): Matrix used for the system of equations to create predictions Returns: Returns: image (array): The reconstructed image image (array): The reconstructed image """ """ new_e = error.copy() new_e = error.copy() rows, columns = new_e.shape rows, columns = new_e.shape for r in range(1, rows-1): for r in range(1, rows-1): for c in range(1, columns-1): for c in range(1, columns-1): z0, z1, z2, z3 = new_e[r-1][c-1], new_e[r-1][c], new_e[r-1][c+1], new_e[r][c-1] z0, z1, z2, z3 = new_e[r-1][c-1], new_e[r-1][c], new_e[r-1][c+1], new_e[r][c-1] y = np.vstack((-z0+z2-z3, z0+z1+z2, -z0-z1-z2-z3)) y = np.vstack((-z0+z2-z3, z0+z1+z2, -z0-z1-z2-z3)) if r == 345 and c == 421: if r == 345 and c == 421: print(new_e[r][c]) print(new_e[r][c]) print(np.linalg.solve(A,y)[-1]) print(np.linalg.solve(A,y)[-1]) print(new_e[r][c] + np.linalg.solve(A,y)[-1]) print(new_e[r][c] + np.linalg.solve(A,y)[-1]) print(np.ceil(new_e[r][c]) + np.floor(np.linalg.solve(A,y)[-1])) print(np.ceil(new_e[r][c]) + np.floor(np.linalg.solve(A,y)[-1])) #Real solution that works, DO NOT DELETE #Real solution that works, DO NOT DELETE new_e[r][c] = int(np.ceil(new_e[r][c] + np.linalg.solve(A,y)[-1])) new_e[r][c] = int(np.ceil(new_e[r][c] + np.linalg.solve(A,y)[-1])) #new_e[r][c] = np.ceil(new_e[r][c]) + np.floor(np.linalg.solve(A,y)[-1]) #new_e[r][c] = np.ceil(new_e[r][c]) + np.floor(np.linalg.solve(A,y)[-1]) return new_e.astype(int) return new_e.astype(int) ``` ``` %% Cell type:code id:8d835626 tags: %% Cell type:code id:7f395ab2 tags: ``` python ``` python new_error = reconstruct(err, A) new_error = reconstruct(err, A) ``` ``` %% Output %% Output 3.499999999992724 3.499999999992724 [13644.5] [13644.5] [13648.] [13648.] [13648.] [13648.] %% Cell type:code id:b54c4328 tags: %% Cell type:code id:06ccaf8e tags: ``` python ``` python e = np.round(err, 1) e = np.round(err, 1) len(np.unique(e[1:-1, 1:-1])) len(np.unique(e[1:-1, 1:-1])) ``` ``` %% Output %% Output 518 518 Error_to_Image.ipynb +2 −2 Original line number Original line Diff line number Diff line %% Cell type:code id:dbef8759 tags: %% Cell type:code id:dbef8759 tags: ``` python ``` python import numpy as np import numpy as np from prediction_MSE_Scout import file_extractor, image_extractor, im_distribution from prediction_MSE_Scout import file_extractor, image_extractor, im_distribution from matplotlib import pyplot as plt from matplotlib import pyplot as plt from itertools import product from itertools import product import os import os import sys import sys from PIL import Image from PIL import Image from scipy.optimize import minimize from scipy.optimize import minimize from time import time from time import time from numpy import linalg as la from numpy import linalg as la from scipy.stats import gaussian_kde from scipy.stats import gaussian_kde import seaborn as sns import seaborn as sns import pywt import pywt ``` ``` %% Cell type:code id:9ed20f84 tags: %% Cell type:code id:9ed20f84 tags: ``` python ``` python def plot_hist(tiff_list, i=0): def plot_hist(tiff_list, i=0): """ """ This function is the leftovers from the first attempt to plot histograms. This function is the leftovers from the first attempt to plot histograms. As it stands it needs some work in order to function again. We will As it stands it needs some work in order to function again. We will fix this later. 1/25/22 fix this later. 1/25/22 """ """ image = tiff_list[i] image = tiff_list[i] image = Image.open(image) #Open the image and read it as an Image object image = Image.open(image) #Open the image and read it as an Image object image = np.array(image)[1:,:] #Convert to an array, leaving out the first row because the first row is just housekeeping data image = np.array(image)[1:,:] #Convert to an array, leaving out the first row because the first row is just housekeeping data image_int = image.astype(np.int_) image_int = image.astype(np.int_) A = np.array([[3,0,-1],[0,3,3],[1,-3,-4]]) # the matrix for system of equation A = np.array([[3,0,-1],[0,3,3],[1,-3,-4]]) # the matrix for system of equation z0 = image_int[0:-2,0:-2] # get all the first pixel for the entire image z0 = image_int[0:-2,0:-2] # get all the first pixel for the entire image z1 = image_int[0:-2,1:-1] # get all the second pixel for the entire image z1 = image_int[0:-2,1:-1] # get all the second pixel for the entire image z2 = image_int[0:-2,2::] # get all the third pixel for the entire image z2 = image_int[0:-2,2::] # get all the third pixel for the entire image z3 = image_int[1:-1,0:-2] # get all the fourth pixel for the entire image z3 = image_int[1:-1,0:-2] # get all the fourth pixel for the entire image # calculate the out put of the system of equation # calculate the out put of the system of equation y0 = np.ravel(-z0+z2-z3) y0 = np.ravel(-z0+z2-z3) y1 = np.ravel(z0+z1+z2) y1 = np.ravel(z0+z1+z2) y2 = np.ravel(-z0-z1-z2-z3) y2 = np.ravel(-z0-z1-z2-z3) y = np.vstack((y0,y1,y2)) y = np.vstack((y0,y1,y2)) # use numpy solver to solve the system of equations all at once # use numpy solver to solve the system of equations all at once predict = np.linalg.solve(A,y)[-1] predict = np.linalg.solve(A,y)[-1] #predict = [] #predict = [] # flatten the neighbor pixels and stack them together # flatten the neighbor pixels and stack them together z0 = np.ravel(z0) z0 = np.ravel(z0) z1 = np.ravel(z1) z1 = np.ravel(z1) z2 = np.ravel(z2) z2 = np.ravel(z2) z3 = np.ravel(z3) z3 = np.ravel(z3) neighbor = np.vstack((z0,z1,z2,z3)).T neighbor = np.vstack((z0,z1,z2,z3)).T # calculate the difference # calculate the difference diff = np.max(neighbor,axis = 1) - np.min(neighbor, axis=1) diff = np.max(neighbor,axis = 1) - np.min(neighbor, axis=1) # flatten the image to a vector # flatten the image to a vector small_image = image_int[1:-1,1:-1] small_image = image_int[1:-1,1:-1] #Reshape the predictions to be a 2D array #Reshape the predictions to be a 2D array predict = np.pad(predict.reshape(510,638), pad_width=1) predict = np.pad(predict.reshape(510,638), pad_width=1) """predict[0,:] = image[0,:] """predict[0,:] = image[0,:] predict[:,0] = image[:,0] predict[:,0] = image[:,0] predict[:,-1] = image[:,-1] predict[:,-1] = image[:,-1] predict[-1,:] = image[-1,:]""" predict[-1,:] = image[-1,:]""" #Calculate the error between the original image and our predictions #Calculate the error between the original image and our predictions #Note that we only predicted on the inside square of the original image, excluding #Note that we only predicted on the inside square of the original image, excluding #The first row, column and last row, column #The first row, column and last row, column #error = (image_int - predict).astype(int) #Experiment #error = (image_int - predict).astype(int) #Experiment #this one works #this one works error = image_int - predict error = image_int - predict return predict, diff, image_int, error, A return predict, diff, image_int, error, A ``` ``` %% Cell type:code id:ba2881d9 tags: %% Cell type:code id:ba2881d9 tags: ``` python ``` python scenes = file_extractor() scenes = file_extractor() images = image_extractor(scenes) images = image_extractor(scenes) num_images = im_distribution(images, "_9") num_images = im_distribution(images, "_9") ``` ``` %% Cell type:code id:11e95c34 tags: %% Cell type:code id:11e95c34 tags: ``` python ``` python predict, diff, im, err, A = plot_hist(num_images, 0) predict, diff, im, err, A = plot_hist(num_images, 0) ``` ``` %% Cell type:code id:434e4d2f tags: %% Cell type:code id:434e4d2f tags: ``` python ``` python def reconstruct(error, A): def reconstruct(error, A): """ """ Function that reconstructs the original image Function that reconstructs the original image from the error matrix and using the predictive from the error matrix and using the predictive algorithm developed in the encoding. algorithm developed in the encoding. Parameters: Parameters: error (array): matrix of errors computed in encoding. Same error (array): matrix of errors computed in encoding. Same shape as the original image (512, 640) in this case shape as the original image (512, 640) in this case A (array): Matrix used for the system of equations to create predictions A (array): Matrix used for the system of equations to create predictions Returns: Returns: image (array): The reconstructed image image (array): The reconstructed image """ """ new_e = error.copy() new_e = error.copy() rows, columns = new_e.shape rows, columns = new_e.shape for r in range(1, rows-1): for r in range(1, rows-1): for c in range(1, columns-1): for c in range(1, columns-1): z0, z1, z2, z3 = new_e[r-1][c-1], new_e[r-1][c], new_e[r-1][c+1], new_e[r][c-1] z0, z1, z2, z3 = new_e[r-1][c-1], new_e[r-1][c], new_e[r-1][c+1], new_e[r][c-1] y = np.vstack((-z0+z2-z3, z0+z1+z2, -z0-z1-z2-z3)) y = np.vstack((-z0+z2-z3, z0+z1+z2, -z0-z1-z2-z3)) if r == 345 and c == 421: if r == 345 and c == 421: print(new_e[r][c]) print(new_e[r][c]) print(np.linalg.solve(A,y)[-1]) print(np.linalg.solve(A,y)[-1]) print(new_e[r][c] + np.linalg.solve(A,y)[-1]) print(new_e[r][c] + np.linalg.solve(A,y)[-1]) print(np.ceil(new_e[r][c]) + np.floor(np.linalg.solve(A,y)[-1])) print(np.ceil(new_e[r][c]) + np.floor(np.linalg.solve(A,y)[-1])) #Real solution that works, DO NOT DELETE #Real solution that works, DO NOT DELETE new_e[r][c] = int(np.ceil(new_e[r][c] + np.linalg.solve(A,y)[-1])) new_e[r][c] = int(np.ceil(new_e[r][c] + np.linalg.solve(A,y)[-1])) #new_e[r][c] = np.ceil(new_e[r][c]) + np.floor(np.linalg.solve(A,y)[-1]) #new_e[r][c] = np.ceil(new_e[r][c]) + np.floor(np.linalg.solve(A,y)[-1]) return new_e.astype(int) return new_e.astype(int) ``` ``` %% Cell type:code id:8d835626 tags: %% Cell type:code id:7f395ab2 tags: ``` python ``` python new_error = reconstruct(err, A) new_error = reconstruct(err, A) ``` ``` %% Output %% Output 3.499999999992724 3.499999999992724 [13644.5] [13644.5] [13648.] [13648.] [13648.] [13648.] %% Cell type:code id:b54c4328 tags: %% Cell type:code id:06ccaf8e tags: ``` python ``` python e = np.round(err, 1) e = np.round(err, 1) len(np.unique(e[1:-1, 1:-1])) len(np.unique(e[1:-1, 1:-1])) ``` ``` %% Output %% Output 518 518 Loading
.ipynb_checkpoints/Error_to_Image-checkpoint.ipynb +2 −2 Original line number Original line Diff line number Diff line %% Cell type:code id:dbef8759 tags: %% Cell type:code id:dbef8759 tags: ``` python ``` python import numpy as np import numpy as np from prediction_MSE_Scout import file_extractor, image_extractor, im_distribution from prediction_MSE_Scout import file_extractor, image_extractor, im_distribution from matplotlib import pyplot as plt from matplotlib import pyplot as plt from itertools import product from itertools import product import os import os import sys import sys from PIL import Image from PIL import Image from scipy.optimize import minimize from scipy.optimize import minimize from time import time from time import time from numpy import linalg as la from numpy import linalg as la from scipy.stats import gaussian_kde from scipy.stats import gaussian_kde import seaborn as sns import seaborn as sns import pywt import pywt ``` ``` %% Cell type:code id:9ed20f84 tags: %% Cell type:code id:9ed20f84 tags: ``` python ``` python def plot_hist(tiff_list, i=0): def plot_hist(tiff_list, i=0): """ """ This function is the leftovers from the first attempt to plot histograms. This function is the leftovers from the first attempt to plot histograms. As it stands it needs some work in order to function again. We will As it stands it needs some work in order to function again. We will fix this later. 1/25/22 fix this later. 1/25/22 """ """ image = tiff_list[i] image = tiff_list[i] image = Image.open(image) #Open the image and read it as an Image object image = Image.open(image) #Open the image and read it as an Image object image = np.array(image)[1:,:] #Convert to an array, leaving out the first row because the first row is just housekeeping data image = np.array(image)[1:,:] #Convert to an array, leaving out the first row because the first row is just housekeeping data image_int = image.astype(np.int_) image_int = image.astype(np.int_) A = np.array([[3,0,-1],[0,3,3],[1,-3,-4]]) # the matrix for system of equation A = np.array([[3,0,-1],[0,3,3],[1,-3,-4]]) # the matrix for system of equation z0 = image_int[0:-2,0:-2] # get all the first pixel for the entire image z0 = image_int[0:-2,0:-2] # get all the first pixel for the entire image z1 = image_int[0:-2,1:-1] # get all the second pixel for the entire image z1 = image_int[0:-2,1:-1] # get all the second pixel for the entire image z2 = image_int[0:-2,2::] # get all the third pixel for the entire image z2 = image_int[0:-2,2::] # get all the third pixel for the entire image z3 = image_int[1:-1,0:-2] # get all the fourth pixel for the entire image z3 = image_int[1:-1,0:-2] # get all the fourth pixel for the entire image # calculate the out put of the system of equation # calculate the out put of the system of equation y0 = np.ravel(-z0+z2-z3) y0 = np.ravel(-z0+z2-z3) y1 = np.ravel(z0+z1+z2) y1 = np.ravel(z0+z1+z2) y2 = np.ravel(-z0-z1-z2-z3) y2 = np.ravel(-z0-z1-z2-z3) y = np.vstack((y0,y1,y2)) y = np.vstack((y0,y1,y2)) # use numpy solver to solve the system of equations all at once # use numpy solver to solve the system of equations all at once predict = np.linalg.solve(A,y)[-1] predict = np.linalg.solve(A,y)[-1] #predict = [] #predict = [] # flatten the neighbor pixels and stack them together # flatten the neighbor pixels and stack them together z0 = np.ravel(z0) z0 = np.ravel(z0) z1 = np.ravel(z1) z1 = np.ravel(z1) z2 = np.ravel(z2) z2 = np.ravel(z2) z3 = np.ravel(z3) z3 = np.ravel(z3) neighbor = np.vstack((z0,z1,z2,z3)).T neighbor = np.vstack((z0,z1,z2,z3)).T # calculate the difference # calculate the difference diff = np.max(neighbor,axis = 1) - np.min(neighbor, axis=1) diff = np.max(neighbor,axis = 1) - np.min(neighbor, axis=1) # flatten the image to a vector # flatten the image to a vector small_image = image_int[1:-1,1:-1] small_image = image_int[1:-1,1:-1] #Reshape the predictions to be a 2D array #Reshape the predictions to be a 2D array predict = np.pad(predict.reshape(510,638), pad_width=1) predict = np.pad(predict.reshape(510,638), pad_width=1) """predict[0,:] = image[0,:] """predict[0,:] = image[0,:] predict[:,0] = image[:,0] predict[:,0] = image[:,0] predict[:,-1] = image[:,-1] predict[:,-1] = image[:,-1] predict[-1,:] = image[-1,:]""" predict[-1,:] = image[-1,:]""" #Calculate the error between the original image and our predictions #Calculate the error between the original image and our predictions #Note that we only predicted on the inside square of the original image, excluding #Note that we only predicted on the inside square of the original image, excluding #The first row, column and last row, column #The first row, column and last row, column #error = (image_int - predict).astype(int) #Experiment #error = (image_int - predict).astype(int) #Experiment #this one works #this one works error = image_int - predict error = image_int - predict return predict, diff, image_int, error, A return predict, diff, image_int, error, A ``` ``` %% Cell type:code id:ba2881d9 tags: %% Cell type:code id:ba2881d9 tags: ``` python ``` python scenes = file_extractor() scenes = file_extractor() images = image_extractor(scenes) images = image_extractor(scenes) num_images = im_distribution(images, "_9") num_images = im_distribution(images, "_9") ``` ``` %% Cell type:code id:11e95c34 tags: %% Cell type:code id:11e95c34 tags: ``` python ``` python predict, diff, im, err, A = plot_hist(num_images, 0) predict, diff, im, err, A = plot_hist(num_images, 0) ``` ``` %% Cell type:code id:434e4d2f tags: %% Cell type:code id:434e4d2f tags: ``` python ``` python def reconstruct(error, A): def reconstruct(error, A): """ """ Function that reconstructs the original image Function that reconstructs the original image from the error matrix and using the predictive from the error matrix and using the predictive algorithm developed in the encoding. algorithm developed in the encoding. Parameters: Parameters: error (array): matrix of errors computed in encoding. Same error (array): matrix of errors computed in encoding. Same shape as the original image (512, 640) in this case shape as the original image (512, 640) in this case A (array): Matrix used for the system of equations to create predictions A (array): Matrix used for the system of equations to create predictions Returns: Returns: image (array): The reconstructed image image (array): The reconstructed image """ """ new_e = error.copy() new_e = error.copy() rows, columns = new_e.shape rows, columns = new_e.shape for r in range(1, rows-1): for r in range(1, rows-1): for c in range(1, columns-1): for c in range(1, columns-1): z0, z1, z2, z3 = new_e[r-1][c-1], new_e[r-1][c], new_e[r-1][c+1], new_e[r][c-1] z0, z1, z2, z3 = new_e[r-1][c-1], new_e[r-1][c], new_e[r-1][c+1], new_e[r][c-1] y = np.vstack((-z0+z2-z3, z0+z1+z2, -z0-z1-z2-z3)) y = np.vstack((-z0+z2-z3, z0+z1+z2, -z0-z1-z2-z3)) if r == 345 and c == 421: if r == 345 and c == 421: print(new_e[r][c]) print(new_e[r][c]) print(np.linalg.solve(A,y)[-1]) print(np.linalg.solve(A,y)[-1]) print(new_e[r][c] + np.linalg.solve(A,y)[-1]) print(new_e[r][c] + np.linalg.solve(A,y)[-1]) print(np.ceil(new_e[r][c]) + np.floor(np.linalg.solve(A,y)[-1])) print(np.ceil(new_e[r][c]) + np.floor(np.linalg.solve(A,y)[-1])) #Real solution that works, DO NOT DELETE #Real solution that works, DO NOT DELETE new_e[r][c] = int(np.ceil(new_e[r][c] + np.linalg.solve(A,y)[-1])) new_e[r][c] = int(np.ceil(new_e[r][c] + np.linalg.solve(A,y)[-1])) #new_e[r][c] = np.ceil(new_e[r][c]) + np.floor(np.linalg.solve(A,y)[-1]) #new_e[r][c] = np.ceil(new_e[r][c]) + np.floor(np.linalg.solve(A,y)[-1]) return new_e.astype(int) return new_e.astype(int) ``` ``` %% Cell type:code id:8d835626 tags: %% Cell type:code id:7f395ab2 tags: ``` python ``` python new_error = reconstruct(err, A) new_error = reconstruct(err, A) ``` ``` %% Output %% Output 3.499999999992724 3.499999999992724 [13644.5] [13644.5] [13648.] [13648.] [13648.] [13648.] %% Cell type:code id:b54c4328 tags: %% Cell type:code id:06ccaf8e tags: ``` python ``` python e = np.round(err, 1) e = np.round(err, 1) len(np.unique(e[1:-1, 1:-1])) len(np.unique(e[1:-1, 1:-1])) ``` ``` %% Output %% Output 518 518
Error_to_Image.ipynb +2 −2 Original line number Original line Diff line number Diff line %% Cell type:code id:dbef8759 tags: %% Cell type:code id:dbef8759 tags: ``` python ``` python import numpy as np import numpy as np from prediction_MSE_Scout import file_extractor, image_extractor, im_distribution from prediction_MSE_Scout import file_extractor, image_extractor, im_distribution from matplotlib import pyplot as plt from matplotlib import pyplot as plt from itertools import product from itertools import product import os import os import sys import sys from PIL import Image from PIL import Image from scipy.optimize import minimize from scipy.optimize import minimize from time import time from time import time from numpy import linalg as la from numpy import linalg as la from scipy.stats import gaussian_kde from scipy.stats import gaussian_kde import seaborn as sns import seaborn as sns import pywt import pywt ``` ``` %% Cell type:code id:9ed20f84 tags: %% Cell type:code id:9ed20f84 tags: ``` python ``` python def plot_hist(tiff_list, i=0): def plot_hist(tiff_list, i=0): """ """ This function is the leftovers from the first attempt to plot histograms. This function is the leftovers from the first attempt to plot histograms. As it stands it needs some work in order to function again. We will As it stands it needs some work in order to function again. We will fix this later. 1/25/22 fix this later. 1/25/22 """ """ image = tiff_list[i] image = tiff_list[i] image = Image.open(image) #Open the image and read it as an Image object image = Image.open(image) #Open the image and read it as an Image object image = np.array(image)[1:,:] #Convert to an array, leaving out the first row because the first row is just housekeeping data image = np.array(image)[1:,:] #Convert to an array, leaving out the first row because the first row is just housekeeping data image_int = image.astype(np.int_) image_int = image.astype(np.int_) A = np.array([[3,0,-1],[0,3,3],[1,-3,-4]]) # the matrix for system of equation A = np.array([[3,0,-1],[0,3,3],[1,-3,-4]]) # the matrix for system of equation z0 = image_int[0:-2,0:-2] # get all the first pixel for the entire image z0 = image_int[0:-2,0:-2] # get all the first pixel for the entire image z1 = image_int[0:-2,1:-1] # get all the second pixel for the entire image z1 = image_int[0:-2,1:-1] # get all the second pixel for the entire image z2 = image_int[0:-2,2::] # get all the third pixel for the entire image z2 = image_int[0:-2,2::] # get all the third pixel for the entire image z3 = image_int[1:-1,0:-2] # get all the fourth pixel for the entire image z3 = image_int[1:-1,0:-2] # get all the fourth pixel for the entire image # calculate the out put of the system of equation # calculate the out put of the system of equation y0 = np.ravel(-z0+z2-z3) y0 = np.ravel(-z0+z2-z3) y1 = np.ravel(z0+z1+z2) y1 = np.ravel(z0+z1+z2) y2 = np.ravel(-z0-z1-z2-z3) y2 = np.ravel(-z0-z1-z2-z3) y = np.vstack((y0,y1,y2)) y = np.vstack((y0,y1,y2)) # use numpy solver to solve the system of equations all at once # use numpy solver to solve the system of equations all at once predict = np.linalg.solve(A,y)[-1] predict = np.linalg.solve(A,y)[-1] #predict = [] #predict = [] # flatten the neighbor pixels and stack them together # flatten the neighbor pixels and stack them together z0 = np.ravel(z0) z0 = np.ravel(z0) z1 = np.ravel(z1) z1 = np.ravel(z1) z2 = np.ravel(z2) z2 = np.ravel(z2) z3 = np.ravel(z3) z3 = np.ravel(z3) neighbor = np.vstack((z0,z1,z2,z3)).T neighbor = np.vstack((z0,z1,z2,z3)).T # calculate the difference # calculate the difference diff = np.max(neighbor,axis = 1) - np.min(neighbor, axis=1) diff = np.max(neighbor,axis = 1) - np.min(neighbor, axis=1) # flatten the image to a vector # flatten the image to a vector small_image = image_int[1:-1,1:-1] small_image = image_int[1:-1,1:-1] #Reshape the predictions to be a 2D array #Reshape the predictions to be a 2D array predict = np.pad(predict.reshape(510,638), pad_width=1) predict = np.pad(predict.reshape(510,638), pad_width=1) """predict[0,:] = image[0,:] """predict[0,:] = image[0,:] predict[:,0] = image[:,0] predict[:,0] = image[:,0] predict[:,-1] = image[:,-1] predict[:,-1] = image[:,-1] predict[-1,:] = image[-1,:]""" predict[-1,:] = image[-1,:]""" #Calculate the error between the original image and our predictions #Calculate the error between the original image and our predictions #Note that we only predicted on the inside square of the original image, excluding #Note that we only predicted on the inside square of the original image, excluding #The first row, column and last row, column #The first row, column and last row, column #error = (image_int - predict).astype(int) #Experiment #error = (image_int - predict).astype(int) #Experiment #this one works #this one works error = image_int - predict error = image_int - predict return predict, diff, image_int, error, A return predict, diff, image_int, error, A ``` ``` %% Cell type:code id:ba2881d9 tags: %% Cell type:code id:ba2881d9 tags: ``` python ``` python scenes = file_extractor() scenes = file_extractor() images = image_extractor(scenes) images = image_extractor(scenes) num_images = im_distribution(images, "_9") num_images = im_distribution(images, "_9") ``` ``` %% Cell type:code id:11e95c34 tags: %% Cell type:code id:11e95c34 tags: ``` python ``` python predict, diff, im, err, A = plot_hist(num_images, 0) predict, diff, im, err, A = plot_hist(num_images, 0) ``` ``` %% Cell type:code id:434e4d2f tags: %% Cell type:code id:434e4d2f tags: ``` python ``` python def reconstruct(error, A): def reconstruct(error, A): """ """ Function that reconstructs the original image Function that reconstructs the original image from the error matrix and using the predictive from the error matrix and using the predictive algorithm developed in the encoding. algorithm developed in the encoding. Parameters: Parameters: error (array): matrix of errors computed in encoding. Same error (array): matrix of errors computed in encoding. Same shape as the original image (512, 640) in this case shape as the original image (512, 640) in this case A (array): Matrix used for the system of equations to create predictions A (array): Matrix used for the system of equations to create predictions Returns: Returns: image (array): The reconstructed image image (array): The reconstructed image """ """ new_e = error.copy() new_e = error.copy() rows, columns = new_e.shape rows, columns = new_e.shape for r in range(1, rows-1): for r in range(1, rows-1): for c in range(1, columns-1): for c in range(1, columns-1): z0, z1, z2, z3 = new_e[r-1][c-1], new_e[r-1][c], new_e[r-1][c+1], new_e[r][c-1] z0, z1, z2, z3 = new_e[r-1][c-1], new_e[r-1][c], new_e[r-1][c+1], new_e[r][c-1] y = np.vstack((-z0+z2-z3, z0+z1+z2, -z0-z1-z2-z3)) y = np.vstack((-z0+z2-z3, z0+z1+z2, -z0-z1-z2-z3)) if r == 345 and c == 421: if r == 345 and c == 421: print(new_e[r][c]) print(new_e[r][c]) print(np.linalg.solve(A,y)[-1]) print(np.linalg.solve(A,y)[-1]) print(new_e[r][c] + np.linalg.solve(A,y)[-1]) print(new_e[r][c] + np.linalg.solve(A,y)[-1]) print(np.ceil(new_e[r][c]) + np.floor(np.linalg.solve(A,y)[-1])) print(np.ceil(new_e[r][c]) + np.floor(np.linalg.solve(A,y)[-1])) #Real solution that works, DO NOT DELETE #Real solution that works, DO NOT DELETE new_e[r][c] = int(np.ceil(new_e[r][c] + np.linalg.solve(A,y)[-1])) new_e[r][c] = int(np.ceil(new_e[r][c] + np.linalg.solve(A,y)[-1])) #new_e[r][c] = np.ceil(new_e[r][c]) + np.floor(np.linalg.solve(A,y)[-1]) #new_e[r][c] = np.ceil(new_e[r][c]) + np.floor(np.linalg.solve(A,y)[-1]) return new_e.astype(int) return new_e.astype(int) ``` ``` %% Cell type:code id:8d835626 tags: %% Cell type:code id:7f395ab2 tags: ``` python ``` python new_error = reconstruct(err, A) new_error = reconstruct(err, A) ``` ``` %% Output %% Output 3.499999999992724 3.499999999992724 [13644.5] [13644.5] [13648.] [13648.] [13648.] [13648.] %% Cell type:code id:b54c4328 tags: %% Cell type:code id:06ccaf8e tags: ``` python ``` python e = np.round(err, 1) e = np.round(err, 1) len(np.unique(e[1:-1, 1:-1])) len(np.unique(e[1:-1, 1:-1])) ``` ``` %% Output %% Output 518 518