Commit 3e116aa0 authored by Kelly Chang's avatar Kelly Chang
Browse files

Kelly

parent 1037f11d
Loading
Loading
Loading
Loading
+148 −464

File changed.

Preview size limit exceeded, changes collapsed.

+0 −0
Original line number Diff line number Diff line
+11 −1
Original line number Diff line number Diff line
%% Cell type:code id:dbef8759 tags:

``` python
import numpy as np
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
```

%% Cell type:code id:b7a550e0 tags:

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

def image_extractor(scenes):
    image_folder = []
    for scene in scenes:
        files = os.listdir(scene)
        for file in files:
            image_folder.append(os.path.join(scene, file))
    images = []
    for folder in image_folder:
        ims = os.listdir(folder)
        for im in ims:
            if im[-4:] == ".jp4" or im[-7:] == "_6.tiff":
                continue
            else:
                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

def im_distribution(images, num):
    """
    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
    specified camera numbers.

    Parameters:
        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
        divide.

        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.

    Returns:
        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.
    """
    tiff = []
    for im in images:
        if im[-7:-5] == num:
            tiff.append(im)
    return tiff
```

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

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

```

%% Cell type:code id:8e3ef654 tags:

``` python
scenes = file_extractor()
images = image_extractor(scenes)
num_images = im_distribution(images, "_9")
error_mean = []
error_mean1 = []
diff_mean = []
times = []
times1 = []
all_error = []
for i in range(len(num_images)):
    """start1 = time()
    image_1, predict_1, difference_1, x_s_1 = plot_hist(num_images, i, "second")
    stop1 = time()
    times1.append(stop1-start1)
    error1 = np.abs(image_1-predict_1)
    error_mean1.append(np.mean(np.ravel(error1)))"""
    start = time()
    image, predict, difference = plot_hist(num_images, i)
    stop = time()
    times.append(stop-start)
    error = np.abs(image-predict)
    all_error.append(np.ravel(error))
    error_mean.append(np.mean(np.ravel(error)))
    diff_mean.append(np.mean(np.ravel(difference)))
```

%% Cell type:code id:fa65dcd6 tags:

``` python
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"Average Difference: {np.mean(diff_mean)}")
print(f"Average Time per Image for First: {np.mean(times)}")
```

%% Output

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

%% Cell type:code id:b7e88aab tags:

``` python
print(predict)
```

%% Output

    [22643.5 22595.  22580.  ... 22923.  22984.  22937.5]

%% Cell type:code id:7c55a3e1 tags:

``` python
```