Commit 5c7cf95e authored by Kelly Chang's avatar Kelly Chang
Browse files

kelly changes

parent e4df997c
Loading
Loading
Loading
Loading
+15 −24
Original line number Original line Diff line number Diff line
%% Cell type:code id:5a56346b tags:
%% Cell type:code id:5a56346b tags:


``` python
``` python
import numpy as np
import numpy as np
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,linprog
from scipy.optimize import minimize,linprog
import time
import time
```
```


%% Cell type:code id:5997a0a2 tags:
%% Cell type:code id:5997a0a2 tags:


``` python
``` python
def file_extractor(dirname="images"):
def file_extractor(dirname="images"):
    files = os.listdir(dirname)
    files = os.listdir(dirname)
    scenes = []
    scenes = []
    for file in files:
    for file in files:
        scenes.append(os.path.join(dirname, file))
        scenes.append(os.path.join(dirname, file))
    return scenes
    return scenes


def image_extractor(scenes):
def image_extractor(scenes):
    image_folder = []
    image_folder = []
    for scene in scenes:
    for scene in scenes:
        files = os.listdir(scene)
        files = os.listdir(scene)
        for file in files:
        for file in files:
            image_folder.append(os.path.join(scene, file))
            image_folder.append(os.path.join(scene, file))
    images = []
    images = []
    for folder in image_folder:
    for folder in image_folder:
        ims = os.listdir(folder)
        ims = os.listdir(folder)
        for im in ims:
        for im in ims:
            if im[-4:] == ".jp4" or im[-7:] == "_6.tiff":
            if im[-4:] == ".jp4" or im[-7:] == "_6.tiff":
                continue
                continue
            else:
            else:
                images.append(os.path.join(folder, im))
                images.append(os.path.join(folder, im))
    return images #returns a list of file paths to .tiff files in the specified directory given in file_extractor
    return images #returns a list of file paths to .tiff files in the specified directory given in file_extractor


def im_distribution(images, num):
def im_distribution(images, num):
    """
    """
    Function that extracts tiff files from specific cameras and returns a list of all
    Function that extracts tiff files from specific cameras and returns a list of all
    the tiff files corresponding to that camera. i.e. all pictures labeled "_7.tiff" or otherwise
    the tiff files corresponding to that camera. i.e. all pictures labeled "_7.tiff" or otherwise
    specified camera numbers.
    specified camera numbers.


    Parameters:
    Parameters:
        images (list): list of all tiff files, regardless of classification. This is NOT a list of directories but
        images (list): list of all tiff files, regardless of classification. This is NOT a list of directories but
        of specific tiff files that can be opened right away. This is the list that we iterate through and
        of specific tiff files that can be opened right away. This is the list that we iterate through and
        divide.
        divide.


        num (str): a string designation for the camera number that we want to extract i.e. "14" for double digits
        num (str): a string designation for the camera number that we want to extract i.e. "14" for double digits
        of "_1" for single digits.
        of "_1" for single digits.


    Returns:
    Returns:
        tiff (list): A list of tiff files that have the specified designation from num. They are the files extracted
        tiff (list): A list of tiff files that have the specified designation from num. They are the files extracted
        from the 'images' list that correspond to the given num.
        from the 'images' list that correspond to the given num.
    """
    """
    tiff = []
    tiff = []
    for im in images:
    for im in images:
        if im[-7:-6] == num:
        if im[-7:-6] == num:
            tiff.append(im)
            tiff.append(im)
    return tiff
    return tiff
```
```


%% Cell type:markdown id:d016e765 tags:
%% Cell type:markdown id:d016e765 tags:


## use scipy.optimize.minimize
## use scipy.optimize.minimize


%% Cell type:code id:20cca46f tags:
%% Cell type:code id:20cca46f tags:


``` python
``` python
def plot_hist(tiff_list):
def plot_hist(tiff_list):
    """
    """
    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
    """
    """
    '''jj = 0
    '''jj = 0
    fig, axs = plt.subplots(nrows=2, ncols=2, figsize=(15,12))
    fig, axs = plt.subplots(nrows=2, ncols=2, figsize=(15,12))
    for cam, ax in zip(cameras, axs.ravel()):
    for cam, ax in zip(cameras, axs.ravel()):
        diff = []
        diff = []
        for ii in range(len(cam)):
        for ii in range(len(cam)):
            image = Image.open(cam[ii])    #Open the image and read it as an Image object
            image = Image.open(cam[ii])    #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
            ar1, ar2 = image.shape
            ar1, ar2 = image.shape
            ind1, ind2 = np.random.randint(1,ar1-1), np.random.randint(1,ar2-1) #ind1 randomly selects a row, ind2 randomly selects a column,
            ind1, ind2 = np.random.randint(1,ar1-1), np.random.randint(1,ar2-1) #ind1 randomly selects a row, ind2 randomly selects a column,
                                                                            #this is now a random pixel selection within the image
                                                                            #this is now a random pixel selection within the image


            surrounding = []                                                #initialize a list to be filled the 8 surrounding pixels
            surrounding = []                                                #initialize a list to be filled the 8 surrounding pixels
            for i,j in product(np.arange(-1,2), repeat=2):                  #Iterate through the combinations of surrounding pixel indices
            for i,j in product(np.arange(-1,2), repeat=2):                  #Iterate through the combinations of surrounding pixel indices
                if i == 0 and j == 0:                                       #Avoid the target pixel
                if i == 0 and j == 0:                                       #Avoid the target pixel
                    continue
                    continue
                else:
                else:
                    surrounding.append(image[ind1+i, ind1+j])               #Add the other 8 pixels to the list
                    surrounding.append(image[ind1+i, ind1+j])               #Add the other 8 pixels to the list
            diff.append(np.max(surrounding)-np.min(surrounding))
            diff.append(np.max(surrounding)-np.min(surrounding))
        ax.hist(diff)
        ax.hist(diff)
        ax.set_title(f"tiff {jj}")
        ax.set_title(f"tiff {jj}")
        jj += 1
        jj += 1
    plt.tight_layout()
    plt.tight_layout()
    plt.show()
    plt.show()
    return '''
    return '''


    image = tiff_list
    image = tiff_list
    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
    row, col = image.shape
    row, col = image.shape
    predict = np.empty([row,col])     # create a empty matrix to update prediction
    predict = np.empty([row,col])     # create a empty matrix to update prediction
    predict[0,:] = image[0,:]       # keep the first row from the image
    predict[0,:] = image[0,:]       # keep the first row from the image
    predict[:,0] = image[:,0]       # keep the first columen from the image
    predict[:,0] = image[:,0]       # keep the first columen from the image
    predict[-1,:] = image[-1,:]       # keep the first row from the image
    predict[-1,:] = image[-1,:]       # keep the first row from the image
    predict[:,-1] = image[:,-1]       # keep the first columen from the image
    predict[:,-1] = image[:,-1]       # keep the first columen from the image
    diff = np.empty([row,col])
    diff = np.empty([row,col])
    diff[0,:] = np.zeros(col)       # keep the first row from the image
    diff[0,:] = np.zeros(col)       # keep the first row from the image
    diff[:,0] = np.zeros(row)
    diff[:,0] = np.zeros(row)
    diff[-1,:] = np.zeros(col)       # keep the first row from the image
    diff[-1,:] = np.zeros(col)       # keep the first row from the image
    diff[:,-1] = np.zeros(row)
    diff[:,-1] = np.zeros(row)
    guess = [100,100,100]           # initial guess for the variables
    guess = [100,100,100]           # initial guess for the variables
    for r in range(1,row-1):                  # loop through the rth row
    for r in range(1,row-1):                  # loop through the rth row
        for c in range(1,col-1):              # loop through the cth column
        for c in range(1,col-1):              # loop through the cth column
            #surrounding = np.array([predict[r-1,c-1], predict[r-1,c], predict[r-1,c+1], predict[r,c-1]])
            #surrounding = np.array([predict[r-1,c-1], predict[r-1,c], predict[r-1,c+1], predict[r,c-1]])
            actual_surrounding = np.array([image[r-1,c-1], image[r-1,c], image[r-1,c+1], image[r,c-1]])
            actual_surrounding = np.array([image[r-1,c-1], image[r-1,c], image[r-1,c+1], image[r,c-1]])
            #z = np.array([image[r-1,c-1], image[r-1,c], image[r-1,c+1], image[r,c-1]])
            #z = np.array([image[r-1,c-1], image[r-1,c], image[r-1,c+1], image[r,c-1]])
            x = np.array([-1,0,1,-1])
            x = np.array([-1,0,1,-1])
            y = np.array([-1,-1,-1,0])
            y = np.array([-1,-1,-1,0])
            def E2(var):
            def E2(var):
                return sum((actual_surrounding - var[0]*x + var[1]*y + var[2])**2)
                return sum((actual_surrounding - var[0]*x + var[1]*y + var[2])**2)
            res = minimize(E2,guess)
            res = minimize(E2,guess)
            var = res.x
            var = res.x
            guess = var
            guess = var
            #predict[r,c] = np.mean(actual_surrounding)       # take the mean of the previous 4 pixels
            #predict[r,c] = np.mean(actual_surrounding)       # take the mean of the previous 4 pixels
            predict[r,c] = np.abs(var[2])
            predict[r,c] = np.abs(var[2])
            diff[r,c] = (np.max(actual_surrounding)-np.min(actual_surrounding))
            diff[r,c] = (np.max(actual_surrounding)-np.min(actual_surrounding))


    predict = np.ravel(predict[1:-1,1:-1])
    predict = np.ravel(predict[1:-1,1:-1])
    diff = np.ravel(diff[1:-1,1:-1])
    diff = np.ravel(diff[1:-1,1:-1])
    image = np.ravel(image[1:-1,1:-1])
    image = np.ravel(image[1:-1,1:-1])
    return image, predict, diff
    return image, predict, diff
```
```


%% Cell type:code id:d78b8bdb tags:
%% Cell type:code id:d78b8bdb tags:


``` python
``` python
scenes = file_extractor()
scenes = file_extractor()
images = image_extractor(scenes)
images = image_extractor(scenes)
start = time.time()
start = time.time()
image, predict, diff = plot_hist(images[0])
image, predict, diff = plot_hist(images[0])
end = time.time()
end = time.time()
```
```


%% Cell type:code id:fffb55a4 tags:
%% Cell type:code id:fffb55a4 tags:


``` python
``` python
fig = plt.figure(figsize = (10,10))
fig = plt.figure(figsize = (10,10))
ax = fig.add_subplot()
ax = fig.add_subplot()
x = np.abs(predict-image)
x = np.abs(predict-image)
y = diff
y = diff
print("mean of error = " + str(np.mean(np.abs(image-predict))))
print("mean of error = " + str(np.mean(np.abs(image-predict))))
print("time to run = " + str(end - start))
print("time to run = " + str(end - start))


plt.plot(x,y,'o',alpha = 0.2)
plt.plot(x,y,'o',alpha = 0.2)
plt.rcParams.update({'font.size': 20})
plt.rcParams.update({'font.size': 20})
plt.xlabel("differnece to the true value" )
plt.xlabel("differnece to the true value" )
plt.ylabel("differnece of min and max of true value of the surroundings")
plt.ylabel("differnece of min and max of true value of the surroundings")
plt.show()
plt.show()
```
```


%% Output
%% Output


    mean of error = 18.740279171184074
    mean of error = 18.740279171184074
    time to run = 867.391261100769
    time to run = 867.391261100769




%% Cell type:markdown id:d866df6f tags:
%% Cell type:markdown id:5437bd8d tags:


## use MSE
## use MSE


%% Cell type:code id:73412bc9 tags:
%% Cell type:code id:73412bc9 tags:


``` python
``` python
def plot_hist(tiff_list):
def plot_hist(tiff_list):
    """
    """
    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
    image = tiff_list
    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 = image.astype(int)
    image = image.astype(int)
    row, col = image.shape
    A = np.array([[3,0,-1],[0,3,3],[1,-3,-4]]) # the matrix for system of equation
    predict = np.empty([row,col])     # create a empty matrix to update prediction
    z0 = image[0:-2,0:-2]   # get all the first pixel for the entire image
    predict[0,:] = np.copy(image[0,:])       # keep the first row from the image
    z1 = image[0:-2,1:-1]   # get all the second pixel for the entire image
    predict[:,0] = np.copy(image[:,0])      # keep the first columen from the image
    z2 = image[0:-2,2::]    # get all the third pixel for the entire image
    predict[-1,:] = np.copy(image[-1,:])       # keep the first row from the image
    z3 = image[1:-1,0:-2]   # get all the forth pixel for the entire image
    predict[:,-1] = np.copy(image[:,-1])      # keep the first columen from the image
    # calculate the out put of the system of equation
    diff = np.empty([row,col])
    diff[0,:] = np.zeros(col)       # keep the first row from the image
    diff[:,0] = np.zeros(row)
    diff[-1,:] = np.zeros(col)       # keep the first row from the image
    diff[:,-1] = np.zeros(row)
    A = np.array([[3,0,-1],[0,3,3],[1,-3,-4]])
    z0 = image[0:-2,0:-2]
    z1 = image[0:-2,1:-1]
    z2 = image[0:-2,2::]
    z3 = image[1:-1,0:-2]
    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
    predict = np.linalg.solve(A,y)[-1]
    predict = np.linalg.solve(A,y)[-1]

    # flatten the neighbor pixlels 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
    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
    image = np.ravel(image[1:-1,1:-1])
    image = np.ravel(image[1:-1,1:-1])
    return image, predict, diff
    return image, predict, diff
```
```


%% Cell type:code id:1bc0bed5 tags:
%% Cell type:code id:1bc0bed5 tags:


``` python
``` python
scenes = file_extractor()
scenes = file_extractor()
images = image_extractor(scenes)
images = image_extractor(scenes)
start = time.time()
start = time.time()
image, predict, diff = plot_hist(images[0])
image, predict, diff = plot_hist(images[0])
end = time.time()
end = time.time()
```
```


%% Cell type:code id:a8c46fb2 tags:
%% Cell type:code id:0f908e6c tags:


``` python
``` python
fig = plt.figure(figsize = (10,10))
fig = plt.figure(figsize = (10,10))
ax = fig.add_subplot()
ax = fig.add_subplot()
x = np.abs(predict-image)
x = np.abs(predict-image)
y = diff
y = diff
print("mean of error = " + str(np.mean(np.abs(image-predict))))
print("mean of error = " + str(np.mean(np.abs(image-predict))))
print("time to run = " + str(end - start))
print("time to run = " + str(end - start))


plt.plot(x,y,'o',alpha = 0.2)
plt.plot(x,y,'o',alpha = 0.2)
plt.rcParams.update({'font.size': 20})
plt.rcParams.update({'font.size': 20})
plt.xlabel("differnece to the true value" )
plt.xlabel("differnece to the true value" )
plt.ylabel("differnece of min and max of true value of the surroundings")
plt.ylabel("differnece of min and max of true value of the surroundings")
plt.show()
plt.show()
```
```


%% Output
%% Output


    mean of error = 18.740279058331875
    mean of error = 18.740279058331875
    time to run = 0.06311893463134766
    time to run = 0.07091403007507324




%% Cell type:code id:0c73bbe2 tags:
%% Cell type:code id:fb798c32 tags:


``` python
``` python
```
```


%% Cell type:code id:1f19bb5d tags:
%% Cell type:code id:23f8c8a7 tags:


``` python
``` python
```
```
+15 −13
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 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
```
```


%% Cell type:code id:b7a550e0 tags:
%% Cell type:code id:b7a550e0 tags:


``` python
``` python
def file_extractor(dirname="images"):
def file_extractor(dirname="images"):
    files = os.listdir(dirname)
    files = os.listdir(dirname)
    scenes = []
    scenes = []
    for file in files:
    for file in files:
        scenes.append(os.path.join(dirname, file))
        scenes.append(os.path.join(dirname, file))
    return scenes
    return scenes


def image_extractor(scenes):
def image_extractor(scenes):
    image_folder = []
    image_folder = []
    for scene in scenes:
    for scene in scenes:
        files = os.listdir(scene)
        files = os.listdir(scene)
        for file in files:
        for file in files:
            image_folder.append(os.path.join(scene, file))
            image_folder.append(os.path.join(scene, file))
    images = []
    images = []
    for folder in image_folder:
    for folder in image_folder:
        ims = os.listdir(folder)
        ims = os.listdir(folder)
        for im in ims:
        for im in ims:
            if im[-4:] == ".jp4" or im[-7:] == "_6.tiff":
            if im[-4:] == ".jp4" or im[-7:] == "_6.tiff":
                continue
                continue
            else:
            else:
                images.append(os.path.join(folder, im))
                images.append(os.path.join(folder, im))
    return images #returns a list of file paths to .tiff files in the specified directory given in file_extractor
    return images #returns a list of file paths to .tiff files in the specified directory given in file_extractor


def im_distribution(images, num):
def im_distribution(images, num):
    """
    """
    Function that extracts tiff files from specific cameras and returns a list of all
    Function that extracts tiff files from specific cameras and returns a list of all
    the tiff files corresponding to that camera. i.e. all pictures labeled "_7.tiff" or otherwise
    the tiff files corresponding to that camera. i.e. all pictures labeled "_7.tiff" or otherwise
    specified camera numbers.
    specified camera numbers.


    Parameters:
    Parameters:
        images (list): list of all tiff files, regardless of classification. This is NOT a list of directories but
        images (list): list of all tiff files, regardless of classification. This is NOT a list of directories but
        of specific tiff files that can be opened right away. This is the list that we iterate through and
        of specific tiff files that can be opened right away. This is the list that we iterate through and
        divide.
        divide.


        num (str): a string designation for the camera number that we want to extract i.e. "14" for double digits
        num (str): a string designation for the camera number that we want to extract i.e. "14" for double digits
        of "_1" for single digits.
        of "_1" for single digits.


    Returns:
    Returns:
        tiff (list): A list of tiff files that have the specified designation from num. They are the files extracted
        tiff (list): A list of tiff files that have the specified designation from num. They are the files extracted
        from the 'images' list that correspond to the given num.
        from the 'images' list that correspond to the given num.
    """
    """
    tiff = []
    tiff = []
    for im in images:
    for im in images:
        if im[-7:-5] == num:
        if im[-7:-5] == num:
            tiff.append(im)
            tiff.append(im)
    return tiff
    return tiff
```
```


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


``` python
``` python
def plot_hist(tiff_list,i):
def plot_hist(tiff_list,i):
    """
    """
    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 = image.astype(int)
    image = image.astype(int)
    row, col = image.shape
    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]])
    z0 = image[0:-2,0:-2]   # get all the first pixel for the entire image
    z0 = image[0:-2,0:-2]
    z1 = image[0:-2,1:-1]   # get all the second pixel for the entire image
    z1 = image[0:-2,1:-1]
    z2 = image[0:-2,2::]    # get all the third pixel for the entire image
    z2 = image[0:-2,2::]
    z3 = image[1:-1,0:-2]   # get all the forth pixel for the entire image
    z3 = image[1:-1,0:-2]
    # 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
    predict = np.linalg.solve(A,y[:10])[-1]
    predict = np.linalg.solve(A,y)[-1]

    # flatten the neighbor pixlels 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
    diff = np.max(neighbor,axis = 1) - np.max(neighbor, axis=1)
    # calculate the difference

    diff = np.max(neighbor,axis = 1) - np.min(neighbor, axis=1)
    # flatten the image to a vector
    image = np.ravel(image[1:-1,1:-1])
    image = np.ravel(image[1:-1,1:-1])
    return image, predict, diff
    return image, predict, diff

```
```


%% Cell type:code id:8e3ef654 tags:
%% Cell type:code id:8e3ef654 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")
error_mean = []
error_mean = []
error_mean1 = []
error_mean1 = []
diff_mean = []
diff_mean = []
times = []
times = []
times1 = []
times1 = []
all_error = []
all_error = []
for i in range(len(num_images)):
for i in range(len(num_images)):
    """start1 = time()
    """start1 = time()
    image_1, predict_1, difference_1, x_s_1 = plot_hist(num_images, i, "second")
    image_1, predict_1, difference_1, x_s_1 = plot_hist(num_images, i, "second")
    stop1 = time()
    stop1 = time()
    times1.append(stop1-start1)
    times1.append(stop1-start1)
    error1 = np.abs(image_1-predict_1)
    error1 = np.abs(image_1-predict_1)
    error_mean1.append(np.mean(np.ravel(error1)))"""
    error_mean1.append(np.mean(np.ravel(error1)))"""
    start = time()
    start = time()
    image, predict, difference = plot_hist(num_images, i)
    image, predict, difference = plot_hist(num_images, i)
    stop = time()
    stop = time()
    times.append(stop-start)
    times.append(stop-start)
    error = np.abs(image-predict)
    error = np.abs(image-predict)
    all_error.append(np.ravel(error))
    all_error.append(np.ravel(error))
    error_mean.append(np.mean(np.ravel(error)))
    error_mean.append(np.mean(np.ravel(error)))
    diff_mean.append(np.mean(np.ravel(difference)))
    diff_mean.append(np.mean(np.ravel(difference)))
```
```


%% Cell type:code id:fa65dcd6 tags:
%% Cell type:code id:fa65dcd6 tags:


``` python
``` python
print(f"Average Error First and Second Added: {np.mean(error_mean)}")
print(f"Average Error First and Second Added: {np.mean(error_mean)}")
print(f"Standard Deviaiton of Mean Errors: {np.sqrt(np.var(error_mean))}")
print(f"Standard Deviaiton of Mean Errors: {np.sqrt(np.var(error_mean))}")
print(f"Average Difference: {np.mean(diff_mean)}")
print(f"Average Difference: {np.mean(diff_mean)}")
print(f"Average Time per Image for First: {np.mean(times)}")
print(f"Average Time per Image for First: {np.mean(times)}")
```
```


%% Output
%% Output


    Average Error First and Second Added: 20.017164930235474
    Average Error First and Second Added: 20.017164930235474
    Standard Deviaiton of Mean Errors: 0.16101183692475135
    Standard Deviaiton of Mean Errors: 0.16101183692475135
    Average Difference: -53.678648426455226
    Average Difference: 53.678648426455226
    Average Time per Image for First: 0.05081495642662048
    Average Time per Image for First: 0.04535740613937378


%% Cell type:code id:b7e88aab tags:
%% Cell type:code id:b7e88aab tags:


``` python
``` python
```
```
+15 −24
Original line number Original line Diff line number Diff line
%% Cell type:code id:5a56346b tags:
%% Cell type:code id:5a56346b tags:


``` python
``` python
import numpy as np
import numpy as np
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,linprog
from scipy.optimize import minimize,linprog
import time
import time
```
```


%% Cell type:code id:5997a0a2 tags:
%% Cell type:code id:5997a0a2 tags:


``` python
``` python
def file_extractor(dirname="images"):
def file_extractor(dirname="images"):
    files = os.listdir(dirname)
    files = os.listdir(dirname)
    scenes = []
    scenes = []
    for file in files:
    for file in files:
        scenes.append(os.path.join(dirname, file))
        scenes.append(os.path.join(dirname, file))
    return scenes
    return scenes


def image_extractor(scenes):
def image_extractor(scenes):
    image_folder = []
    image_folder = []
    for scene in scenes:
    for scene in scenes:
        files = os.listdir(scene)
        files = os.listdir(scene)
        for file in files:
        for file in files:
            image_folder.append(os.path.join(scene, file))
            image_folder.append(os.path.join(scene, file))
    images = []
    images = []
    for folder in image_folder:
    for folder in image_folder:
        ims = os.listdir(folder)
        ims = os.listdir(folder)
        for im in ims:
        for im in ims:
            if im[-4:] == ".jp4" or im[-7:] == "_6.tiff":
            if im[-4:] == ".jp4" or im[-7:] == "_6.tiff":
                continue
                continue
            else:
            else:
                images.append(os.path.join(folder, im))
                images.append(os.path.join(folder, im))
    return images #returns a list of file paths to .tiff files in the specified directory given in file_extractor
    return images #returns a list of file paths to .tiff files in the specified directory given in file_extractor


def im_distribution(images, num):
def im_distribution(images, num):
    """
    """
    Function that extracts tiff files from specific cameras and returns a list of all
    Function that extracts tiff files from specific cameras and returns a list of all
    the tiff files corresponding to that camera. i.e. all pictures labeled "_7.tiff" or otherwise
    the tiff files corresponding to that camera. i.e. all pictures labeled "_7.tiff" or otherwise
    specified camera numbers.
    specified camera numbers.


    Parameters:
    Parameters:
        images (list): list of all tiff files, regardless of classification. This is NOT a list of directories but
        images (list): list of all tiff files, regardless of classification. This is NOT a list of directories but
        of specific tiff files that can be opened right away. This is the list that we iterate through and
        of specific tiff files that can be opened right away. This is the list that we iterate through and
        divide.
        divide.


        num (str): a string designation for the camera number that we want to extract i.e. "14" for double digits
        num (str): a string designation for the camera number that we want to extract i.e. "14" for double digits
        of "_1" for single digits.
        of "_1" for single digits.


    Returns:
    Returns:
        tiff (list): A list of tiff files that have the specified designation from num. They are the files extracted
        tiff (list): A list of tiff files that have the specified designation from num. They are the files extracted
        from the 'images' list that correspond to the given num.
        from the 'images' list that correspond to the given num.
    """
    """
    tiff = []
    tiff = []
    for im in images:
    for im in images:
        if im[-7:-6] == num:
        if im[-7:-6] == num:
            tiff.append(im)
            tiff.append(im)
    return tiff
    return tiff
```
```


%% Cell type:markdown id:d016e765 tags:
%% Cell type:markdown id:d016e765 tags:


## use scipy.optimize.minimize
## use scipy.optimize.minimize


%% Cell type:code id:20cca46f tags:
%% Cell type:code id:20cca46f tags:


``` python
``` python
def plot_hist(tiff_list):
def plot_hist(tiff_list):
    """
    """
    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
    """
    """
    '''jj = 0
    '''jj = 0
    fig, axs = plt.subplots(nrows=2, ncols=2, figsize=(15,12))
    fig, axs = plt.subplots(nrows=2, ncols=2, figsize=(15,12))
    for cam, ax in zip(cameras, axs.ravel()):
    for cam, ax in zip(cameras, axs.ravel()):
        diff = []
        diff = []
        for ii in range(len(cam)):
        for ii in range(len(cam)):
            image = Image.open(cam[ii])    #Open the image and read it as an Image object
            image = Image.open(cam[ii])    #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
            ar1, ar2 = image.shape
            ar1, ar2 = image.shape
            ind1, ind2 = np.random.randint(1,ar1-1), np.random.randint(1,ar2-1) #ind1 randomly selects a row, ind2 randomly selects a column,
            ind1, ind2 = np.random.randint(1,ar1-1), np.random.randint(1,ar2-1) #ind1 randomly selects a row, ind2 randomly selects a column,
                                                                            #this is now a random pixel selection within the image
                                                                            #this is now a random pixel selection within the image


            surrounding = []                                                #initialize a list to be filled the 8 surrounding pixels
            surrounding = []                                                #initialize a list to be filled the 8 surrounding pixels
            for i,j in product(np.arange(-1,2), repeat=2):                  #Iterate through the combinations of surrounding pixel indices
            for i,j in product(np.arange(-1,2), repeat=2):                  #Iterate through the combinations of surrounding pixel indices
                if i == 0 and j == 0:                                       #Avoid the target pixel
                if i == 0 and j == 0:                                       #Avoid the target pixel
                    continue
                    continue
                else:
                else:
                    surrounding.append(image[ind1+i, ind1+j])               #Add the other 8 pixels to the list
                    surrounding.append(image[ind1+i, ind1+j])               #Add the other 8 pixels to the list
            diff.append(np.max(surrounding)-np.min(surrounding))
            diff.append(np.max(surrounding)-np.min(surrounding))
        ax.hist(diff)
        ax.hist(diff)
        ax.set_title(f"tiff {jj}")
        ax.set_title(f"tiff {jj}")
        jj += 1
        jj += 1
    plt.tight_layout()
    plt.tight_layout()
    plt.show()
    plt.show()
    return '''
    return '''


    image = tiff_list
    image = tiff_list
    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
    row, col = image.shape
    row, col = image.shape
    predict = np.empty([row,col])     # create a empty matrix to update prediction
    predict = np.empty([row,col])     # create a empty matrix to update prediction
    predict[0,:] = image[0,:]       # keep the first row from the image
    predict[0,:] = image[0,:]       # keep the first row from the image
    predict[:,0] = image[:,0]       # keep the first columen from the image
    predict[:,0] = image[:,0]       # keep the first columen from the image
    predict[-1,:] = image[-1,:]       # keep the first row from the image
    predict[-1,:] = image[-1,:]       # keep the first row from the image
    predict[:,-1] = image[:,-1]       # keep the first columen from the image
    predict[:,-1] = image[:,-1]       # keep the first columen from the image
    diff = np.empty([row,col])
    diff = np.empty([row,col])
    diff[0,:] = np.zeros(col)       # keep the first row from the image
    diff[0,:] = np.zeros(col)       # keep the first row from the image
    diff[:,0] = np.zeros(row)
    diff[:,0] = np.zeros(row)
    diff[-1,:] = np.zeros(col)       # keep the first row from the image
    diff[-1,:] = np.zeros(col)       # keep the first row from the image
    diff[:,-1] = np.zeros(row)
    diff[:,-1] = np.zeros(row)
    guess = [100,100,100]           # initial guess for the variables
    guess = [100,100,100]           # initial guess for the variables
    for r in range(1,row-1):                  # loop through the rth row
    for r in range(1,row-1):                  # loop through the rth row
        for c in range(1,col-1):              # loop through the cth column
        for c in range(1,col-1):              # loop through the cth column
            #surrounding = np.array([predict[r-1,c-1], predict[r-1,c], predict[r-1,c+1], predict[r,c-1]])
            #surrounding = np.array([predict[r-1,c-1], predict[r-1,c], predict[r-1,c+1], predict[r,c-1]])
            actual_surrounding = np.array([image[r-1,c-1], image[r-1,c], image[r-1,c+1], image[r,c-1]])
            actual_surrounding = np.array([image[r-1,c-1], image[r-1,c], image[r-1,c+1], image[r,c-1]])
            #z = np.array([image[r-1,c-1], image[r-1,c], image[r-1,c+1], image[r,c-1]])
            #z = np.array([image[r-1,c-1], image[r-1,c], image[r-1,c+1], image[r,c-1]])
            x = np.array([-1,0,1,-1])
            x = np.array([-1,0,1,-1])
            y = np.array([-1,-1,-1,0])
            y = np.array([-1,-1,-1,0])
            def E2(var):
            def E2(var):
                return sum((actual_surrounding - var[0]*x + var[1]*y + var[2])**2)
                return sum((actual_surrounding - var[0]*x + var[1]*y + var[2])**2)
            res = minimize(E2,guess)
            res = minimize(E2,guess)
            var = res.x
            var = res.x
            guess = var
            guess = var
            #predict[r,c] = np.mean(actual_surrounding)       # take the mean of the previous 4 pixels
            #predict[r,c] = np.mean(actual_surrounding)       # take the mean of the previous 4 pixels
            predict[r,c] = np.abs(var[2])
            predict[r,c] = np.abs(var[2])
            diff[r,c] = (np.max(actual_surrounding)-np.min(actual_surrounding))
            diff[r,c] = (np.max(actual_surrounding)-np.min(actual_surrounding))


    predict = np.ravel(predict[1:-1,1:-1])
    predict = np.ravel(predict[1:-1,1:-1])
    diff = np.ravel(diff[1:-1,1:-1])
    diff = np.ravel(diff[1:-1,1:-1])
    image = np.ravel(image[1:-1,1:-1])
    image = np.ravel(image[1:-1,1:-1])
    return image, predict, diff
    return image, predict, diff
```
```


%% Cell type:code id:d78b8bdb tags:
%% Cell type:code id:d78b8bdb tags:


``` python
``` python
scenes = file_extractor()
scenes = file_extractor()
images = image_extractor(scenes)
images = image_extractor(scenes)
start = time.time()
start = time.time()
image, predict, diff = plot_hist(images[0])
image, predict, diff = plot_hist(images[0])
end = time.time()
end = time.time()
```
```


%% Cell type:code id:fffb55a4 tags:
%% Cell type:code id:fffb55a4 tags:


``` python
``` python
fig = plt.figure(figsize = (10,10))
fig = plt.figure(figsize = (10,10))
ax = fig.add_subplot()
ax = fig.add_subplot()
x = np.abs(predict-image)
x = np.abs(predict-image)
y = diff
y = diff
print("mean of error = " + str(np.mean(np.abs(image-predict))))
print("mean of error = " + str(np.mean(np.abs(image-predict))))
print("time to run = " + str(end - start))
print("time to run = " + str(end - start))


plt.plot(x,y,'o',alpha = 0.2)
plt.plot(x,y,'o',alpha = 0.2)
plt.rcParams.update({'font.size': 20})
plt.rcParams.update({'font.size': 20})
plt.xlabel("differnece to the true value" )
plt.xlabel("differnece to the true value" )
plt.ylabel("differnece of min and max of true value of the surroundings")
plt.ylabel("differnece of min and max of true value of the surroundings")
plt.show()
plt.show()
```
```


%% Output
%% Output


    mean of error = 18.740279171184074
    mean of error = 18.740279171184074
    time to run = 867.391261100769
    time to run = 867.391261100769




%% Cell type:markdown id:d866df6f tags:
%% Cell type:markdown id:5437bd8d tags:


## use MSE
## use MSE


%% Cell type:code id:73412bc9 tags:
%% Cell type:code id:73412bc9 tags:


``` python
``` python
def plot_hist(tiff_list):
def plot_hist(tiff_list):
    """
    """
    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
    image = tiff_list
    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 = image.astype(int)
    image = image.astype(int)
    row, col = image.shape
    A = np.array([[3,0,-1],[0,3,3],[1,-3,-4]]) # the matrix for system of equation
    predict = np.empty([row,col])     # create a empty matrix to update prediction
    z0 = image[0:-2,0:-2]   # get all the first pixel for the entire image
    predict[0,:] = np.copy(image[0,:])       # keep the first row from the image
    z1 = image[0:-2,1:-1]   # get all the second pixel for the entire image
    predict[:,0] = np.copy(image[:,0])      # keep the first columen from the image
    z2 = image[0:-2,2::]    # get all the third pixel for the entire image
    predict[-1,:] = np.copy(image[-1,:])       # keep the first row from the image
    z3 = image[1:-1,0:-2]   # get all the forth pixel for the entire image
    predict[:,-1] = np.copy(image[:,-1])      # keep the first columen from the image
    # calculate the out put of the system of equation
    diff = np.empty([row,col])
    diff[0,:] = np.zeros(col)       # keep the first row from the image
    diff[:,0] = np.zeros(row)
    diff[-1,:] = np.zeros(col)       # keep the first row from the image
    diff[:,-1] = np.zeros(row)
    A = np.array([[3,0,-1],[0,3,3],[1,-3,-4]])
    z0 = image[0:-2,0:-2]
    z1 = image[0:-2,1:-1]
    z2 = image[0:-2,2::]
    z3 = image[1:-1,0:-2]
    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
    predict = np.linalg.solve(A,y)[-1]
    predict = np.linalg.solve(A,y)[-1]

    # flatten the neighbor pixlels 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
    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
    image = np.ravel(image[1:-1,1:-1])
    image = np.ravel(image[1:-1,1:-1])
    return image, predict, diff
    return image, predict, diff
```
```


%% Cell type:code id:1bc0bed5 tags:
%% Cell type:code id:1bc0bed5 tags:


``` python
``` python
scenes = file_extractor()
scenes = file_extractor()
images = image_extractor(scenes)
images = image_extractor(scenes)
start = time.time()
start = time.time()
image, predict, diff = plot_hist(images[0])
image, predict, diff = plot_hist(images[0])
end = time.time()
end = time.time()
```
```


%% Cell type:code id:a8c46fb2 tags:
%% Cell type:code id:0f908e6c tags:


``` python
``` python
fig = plt.figure(figsize = (10,10))
fig = plt.figure(figsize = (10,10))
ax = fig.add_subplot()
ax = fig.add_subplot()
x = np.abs(predict-image)
x = np.abs(predict-image)
y = diff
y = diff
print("mean of error = " + str(np.mean(np.abs(image-predict))))
print("mean of error = " + str(np.mean(np.abs(image-predict))))
print("time to run = " + str(end - start))
print("time to run = " + str(end - start))


plt.plot(x,y,'o',alpha = 0.2)
plt.plot(x,y,'o',alpha = 0.2)
plt.rcParams.update({'font.size': 20})
plt.rcParams.update({'font.size': 20})
plt.xlabel("differnece to the true value" )
plt.xlabel("differnece to the true value" )
plt.ylabel("differnece of min and max of true value of the surroundings")
plt.ylabel("differnece of min and max of true value of the surroundings")
plt.show()
plt.show()
```
```


%% Output
%% Output


    mean of error = 18.740279058331875
    mean of error = 18.740279058331875
    time to run = 0.06311893463134766
    time to run = 0.07091403007507324




%% Cell type:code id:0c73bbe2 tags:
%% Cell type:code id:fb798c32 tags:


``` python
``` python
```
```


%% Cell type:code id:1f19bb5d tags:
%% Cell type:code id:23f8c8a7 tags:


``` python
``` python
```
```
+15 −13
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 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
```
```


%% Cell type:code id:b7a550e0 tags:
%% Cell type:code id:b7a550e0 tags:


``` python
``` python
def file_extractor(dirname="images"):
def file_extractor(dirname="images"):
    files = os.listdir(dirname)
    files = os.listdir(dirname)
    scenes = []
    scenes = []
    for file in files:
    for file in files:
        scenes.append(os.path.join(dirname, file))
        scenes.append(os.path.join(dirname, file))
    return scenes
    return scenes


def image_extractor(scenes):
def image_extractor(scenes):
    image_folder = []
    image_folder = []
    for scene in scenes:
    for scene in scenes:
        files = os.listdir(scene)
        files = os.listdir(scene)
        for file in files:
        for file in files:
            image_folder.append(os.path.join(scene, file))
            image_folder.append(os.path.join(scene, file))
    images = []
    images = []
    for folder in image_folder:
    for folder in image_folder:
        ims = os.listdir(folder)
        ims = os.listdir(folder)
        for im in ims:
        for im in ims:
            if im[-4:] == ".jp4" or im[-7:] == "_6.tiff":
            if im[-4:] == ".jp4" or im[-7:] == "_6.tiff":
                continue
                continue
            else:
            else:
                images.append(os.path.join(folder, im))
                images.append(os.path.join(folder, im))
    return images #returns a list of file paths to .tiff files in the specified directory given in file_extractor
    return images #returns a list of file paths to .tiff files in the specified directory given in file_extractor


def im_distribution(images, num):
def im_distribution(images, num):
    """
    """
    Function that extracts tiff files from specific cameras and returns a list of all
    Function that extracts tiff files from specific cameras and returns a list of all
    the tiff files corresponding to that camera. i.e. all pictures labeled "_7.tiff" or otherwise
    the tiff files corresponding to that camera. i.e. all pictures labeled "_7.tiff" or otherwise
    specified camera numbers.
    specified camera numbers.


    Parameters:
    Parameters:
        images (list): list of all tiff files, regardless of classification. This is NOT a list of directories but
        images (list): list of all tiff files, regardless of classification. This is NOT a list of directories but
        of specific tiff files that can be opened right away. This is the list that we iterate through and
        of specific tiff files that can be opened right away. This is the list that we iterate through and
        divide.
        divide.


        num (str): a string designation for the camera number that we want to extract i.e. "14" for double digits
        num (str): a string designation for the camera number that we want to extract i.e. "14" for double digits
        of "_1" for single digits.
        of "_1" for single digits.


    Returns:
    Returns:
        tiff (list): A list of tiff files that have the specified designation from num. They are the files extracted
        tiff (list): A list of tiff files that have the specified designation from num. They are the files extracted
        from the 'images' list that correspond to the given num.
        from the 'images' list that correspond to the given num.
    """
    """
    tiff = []
    tiff = []
    for im in images:
    for im in images:
        if im[-7:-5] == num:
        if im[-7:-5] == num:
            tiff.append(im)
            tiff.append(im)
    return tiff
    return tiff
```
```


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


``` python
``` python
def plot_hist(tiff_list,i):
def plot_hist(tiff_list,i):
    """
    """
    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 = image.astype(int)
    image = image.astype(int)
    row, col = image.shape
    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]])
    z0 = image[0:-2,0:-2]   # get all the first pixel for the entire image
    z0 = image[0:-2,0:-2]
    z1 = image[0:-2,1:-1]   # get all the second pixel for the entire image
    z1 = image[0:-2,1:-1]
    z2 = image[0:-2,2::]    # get all the third pixel for the entire image
    z2 = image[0:-2,2::]
    z3 = image[1:-1,0:-2]   # get all the forth pixel for the entire image
    z3 = image[1:-1,0:-2]
    # 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
    predict = np.linalg.solve(A,y[:10])[-1]
    predict = np.linalg.solve(A,y)[-1]

    # flatten the neighbor pixlels 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
    diff = np.max(neighbor,axis = 1) - np.max(neighbor, axis=1)
    # calculate the difference

    diff = np.max(neighbor,axis = 1) - np.min(neighbor, axis=1)
    # flatten the image to a vector
    image = np.ravel(image[1:-1,1:-1])
    image = np.ravel(image[1:-1,1:-1])
    return image, predict, diff
    return image, predict, diff

```
```


%% Cell type:code id:8e3ef654 tags:
%% Cell type:code id:8e3ef654 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")
error_mean = []
error_mean = []
error_mean1 = []
error_mean1 = []
diff_mean = []
diff_mean = []
times = []
times = []
times1 = []
times1 = []
all_error = []
all_error = []
for i in range(len(num_images)):
for i in range(len(num_images)):
    """start1 = time()
    """start1 = time()
    image_1, predict_1, difference_1, x_s_1 = plot_hist(num_images, i, "second")
    image_1, predict_1, difference_1, x_s_1 = plot_hist(num_images, i, "second")
    stop1 = time()
    stop1 = time()
    times1.append(stop1-start1)
    times1.append(stop1-start1)
    error1 = np.abs(image_1-predict_1)
    error1 = np.abs(image_1-predict_1)
    error_mean1.append(np.mean(np.ravel(error1)))"""
    error_mean1.append(np.mean(np.ravel(error1)))"""
    start = time()
    start = time()
    image, predict, difference = plot_hist(num_images, i)
    image, predict, difference = plot_hist(num_images, i)
    stop = time()
    stop = time()
    times.append(stop-start)
    times.append(stop-start)
    error = np.abs(image-predict)
    error = np.abs(image-predict)
    all_error.append(np.ravel(error))
    all_error.append(np.ravel(error))
    error_mean.append(np.mean(np.ravel(error)))
    error_mean.append(np.mean(np.ravel(error)))
    diff_mean.append(np.mean(np.ravel(difference)))
    diff_mean.append(np.mean(np.ravel(difference)))
```
```


%% Cell type:code id:fa65dcd6 tags:
%% Cell type:code id:fa65dcd6 tags:


``` python
``` python
print(f"Average Error First and Second Added: {np.mean(error_mean)}")
print(f"Average Error First and Second Added: {np.mean(error_mean)}")
print(f"Standard Deviaiton of Mean Errors: {np.sqrt(np.var(error_mean))}")
print(f"Standard Deviaiton of Mean Errors: {np.sqrt(np.var(error_mean))}")
print(f"Average Difference: {np.mean(diff_mean)}")
print(f"Average Difference: {np.mean(diff_mean)}")
print(f"Average Time per Image for First: {np.mean(times)}")
print(f"Average Time per Image for First: {np.mean(times)}")
```
```


%% Output
%% Output


    Average Error First and Second Added: 20.017164930235474
    Average Error First and Second Added: 20.017164930235474
    Standard Deviaiton of Mean Errors: 0.16101183692475135
    Standard Deviaiton of Mean Errors: 0.16101183692475135
    Average Difference: -53.678648426455226
    Average Difference: 53.678648426455226
    Average Time per Image for First: 0.05081495642662048
    Average Time per Image for First: 0.04535740613937378


%% Cell type:code id:b7e88aab tags:
%% Cell type:code id:b7e88aab tags:


``` python
``` python
```
```