Commit da4cfd74 authored by Nathaniel Callens's avatar Nathaniel Callens
Browse files

updates

parent b3297cd5
Loading
Loading
Loading
Loading
+64 −12
Original line number Diff line number Diff line
%% Cell type:code id:dbef8759 tags:

``` python
import numpy as np
from prediction_MSE_Scout import file_extractor, image_extractor, im_distribution
from matplotlib import pyplot as plt
from itertools import product
import os
import sys
from PIL import Image
from scipy.optimize import minimize
from time import time
from numpy import linalg as la
from scipy.stats import gaussian_kde
import seaborn as sns
import pywt
```

%% Cell type:code id:9ed20f84 tags:

``` python
def plot_hist(tiff_list, i=0):
    """
    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
    fix this later. 1/25/22
    """

    image = tiff_list[i]
    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 = image.astype(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

    z0 = image[0:-2,0:-2]   # get all the first pixel for the entire image
    z1 = image[0:-2,1:-1]   # get all the second pixel for the entire image
    z2 = image[0:-2,2::]    # get all the third pixel for the entire image
    z3 = image[1:-1,0:-2]   # get all the forth 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
    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

    # calculate the out put of the system of equation
    y0 = np.ravel(-z0+z2-z3)
    y1 = np.ravel(z0+z1+z2)
    y2 = np.ravel(-z0-z1-z2-z3)
    y = np.vstack((y0,y1,y2))

    # use numpy solver to solve the system of equations all at once
    predict = np.linalg.solve(A,y)[-1]
    #predict = []

    # flatten the neighbor pixlels and stack them together
    # flatten the neighbor pixels and stack them together
    z0 = np.ravel(z0)
    z1 = np.ravel(z1)
    z2 = np.ravel(z2)
    z3 = np.ravel(z3)
    neighbor = np.vstack((z0,z1,z2,z3)).T

    # calculate the difference
    diff = np.max(neighbor,axis = 1) - np.min(neighbor, axis=1)


    # flatten the image to a vector
    small_image = image[1:-1,1:-1]
    small_image = image_int[1:-1,1:-1]

    #Reshape the predictions to be a 2D array
    predict = np.pad(predict.reshape(510,638), pad_width=1)
    predict[0,:] = image[0,:]
    """predict[0,:] = image[0,:]
    predict[:,0] = image[:,0]
    predict[:,-1] = image[:,-1]
    predict[-1,:] = image[-1,:]
    predict[-1,:] = image[-1,:]"""


    #Calculate the error between the original image and our predictions
    #Note that we only predicted on the inside square of the original image, excluding
    #The first row, column and last row, column
    error = image - predict
    #error = (image_int - predict).astype(int) #Experiment

    #this one works
    error = image_int - predict

    return predict, diff, image, error, A

    return predict, diff, image_int, error, A
```

%% Cell type:code id:ba2881d9 tags:

``` python
scenes = file_extractor()
images = image_extractor(scenes)
num_images = im_distribution(images, "_1")
num_images = im_distribution(images, "_9")
```

%% Cell type:code id:11e95c34 tags:

``` python
predict, diff, im, err, A = plot_hist(num_images, 0)
```

%% Cell type:code id:434e4d2f tags:

``` python
def reconstruct(error, A):
    """
    Function that reconstructs the original image
    from the error matrix and using the predictive
    algorithm developed in the encoding.

    Parameters:
        error (array): matrix of errors computed in encoding. Same
                       shape as the original image (512, 640) in this case
        A (array): Matrix used for the system of equations to create predictions
    Returns:
        image (array): The reconstructed image
    """
    new_e = error.copy()
    rows, columns = new_e.shape

    for r in range(1, rows-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]
            y = np.vstack((-z0+z2-z3, z0+z1+z2, -z0-z1-z2-z3))

            if r == 345 and c == 421:
                print(new_e[r][c])
                print(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.linalg.solve(A,y)[-1]))

            #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] = new_e[r][c] + np.ceil(np.linalg.solve(A,y)[-1])

    return new_e.astype(int)

```

%% Cell type:code id:ef632a8f tags:

``` python
new_error = reconstruct(err, A)
```

%% Output

    3.499999999992724
    [22627.5]
    [22631.]
    [22631.]

%% Cell type:code id:6e6fb5cd tags:

``` python
new_error == im
```

%% Output

    array([[ True,  True,  True, ...,  True,  True,  True],
           [ True,  True,  True, ...,  True,  True,  True],
           [ True,  True,  True, ...,  True,  True,  True],
           ...,
           [ True,  True,  True, ...,  True,  True,  True],
           [ True,  True,  True, ...,  True,  True,  True],
           [ True,  True,  True, ...,  True,  True,  True]])
+64 −12
Original line number Diff line number Diff line
%% Cell type:code id:dbef8759 tags:

``` python
import numpy as np
from prediction_MSE_Scout import file_extractor, image_extractor, im_distribution
from matplotlib import pyplot as plt
from itertools import product
import os
import sys
from PIL import Image
from scipy.optimize import minimize
from time import time
from numpy import linalg as la
from scipy.stats import gaussian_kde
import seaborn as sns
import pywt
```

%% Cell type:code id:9ed20f84 tags:

``` python
def plot_hist(tiff_list, i=0):
    """
    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
    fix this later. 1/25/22
    """

    image = tiff_list[i]
    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 = image.astype(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

    z0 = image[0:-2,0:-2]   # get all the first pixel for the entire image
    z1 = image[0:-2,1:-1]   # get all the second pixel for the entire image
    z2 = image[0:-2,2::]    # get all the third pixel for the entire image
    z3 = image[1:-1,0:-2]   # get all the forth 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
    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

    # calculate the out put of the system of equation
    y0 = np.ravel(-z0+z2-z3)
    y1 = np.ravel(z0+z1+z2)
    y2 = np.ravel(-z0-z1-z2-z3)
    y = np.vstack((y0,y1,y2))

    # use numpy solver to solve the system of equations all at once
    predict = np.linalg.solve(A,y)[-1]
    #predict = []

    # flatten the neighbor pixlels and stack them together
    # flatten the neighbor pixels and stack them together
    z0 = np.ravel(z0)
    z1 = np.ravel(z1)
    z2 = np.ravel(z2)
    z3 = np.ravel(z3)
    neighbor = np.vstack((z0,z1,z2,z3)).T

    # calculate the difference
    diff = np.max(neighbor,axis = 1) - np.min(neighbor, axis=1)


    # flatten the image to a vector
    small_image = image[1:-1,1:-1]
    small_image = image_int[1:-1,1:-1]

    #Reshape the predictions to be a 2D array
    predict = np.pad(predict.reshape(510,638), pad_width=1)
    predict[0,:] = image[0,:]
    """predict[0,:] = image[0,:]
    predict[:,0] = image[:,0]
    predict[:,-1] = image[:,-1]
    predict[-1,:] = image[-1,:]
    predict[-1,:] = image[-1,:]"""


    #Calculate the error between the original image and our predictions
    #Note that we only predicted on the inside square of the original image, excluding
    #The first row, column and last row, column
    error = image - predict
    #error = (image_int - predict).astype(int) #Experiment

    #this one works
    error = image_int - predict

    return predict, diff, image, error, A

    return predict, diff, image_int, error, A
```

%% Cell type:code id:ba2881d9 tags:

``` python
scenes = file_extractor()
images = image_extractor(scenes)
num_images = im_distribution(images, "_1")
num_images = im_distribution(images, "_9")
```

%% Cell type:code id:11e95c34 tags:

``` python
predict, diff, im, err, A = plot_hist(num_images, 0)
```

%% Cell type:code id:434e4d2f tags:

``` python
def reconstruct(error, A):
    """
    Function that reconstructs the original image
    from the error matrix and using the predictive
    algorithm developed in the encoding.

    Parameters:
        error (array): matrix of errors computed in encoding. Same
                       shape as the original image (512, 640) in this case
        A (array): Matrix used for the system of equations to create predictions
    Returns:
        image (array): The reconstructed image
    """
    new_e = error.copy()
    rows, columns = new_e.shape

    for r in range(1, rows-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]
            y = np.vstack((-z0+z2-z3, z0+z1+z2, -z0-z1-z2-z3))

            if r == 345 and c == 421:
                print(new_e[r][c])
                print(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.linalg.solve(A,y)[-1]))

            #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] = new_e[r][c] + np.ceil(np.linalg.solve(A,y)[-1])

    return new_e.astype(int)

```

%% Cell type:code id:ef632a8f tags:

``` python
new_error = reconstruct(err, A)
```

%% Output

    3.499999999992724
    [22627.5]
    [22631.]
    [22631.]

%% Cell type:code id:6e6fb5cd tags:

``` python
new_error == im
```

%% Output

    array([[ True,  True,  True, ...,  True,  True,  True],
           [ True,  True,  True, ...,  True,  True,  True],
           [ True,  True,  True, ...,  True,  True,  True],
           ...,
           [ True,  True,  True, ...,  True,  True,  True],
           [ True,  True,  True, ...,  True,  True,  True],
           [ True,  True,  True, ...,  True,  True,  True]])