Commit e4b83f96 authored by Kelly Chang's avatar Kelly Chang
Browse files

kelly push

parent 62b8cccf
Loading
Loading
Loading
Loading
+2 −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
```

%% 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:-6] == num:
            tiff.append(im)
    return tiff
```

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

``` python
def plot_hist(tiff_list):
    """
    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
    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)
    row, col = image.shape
    predict = np.empty([row,col])     # create a empty matrix to update prediction
    predict[0,:] = np.copy(image[0,:])       # keep the first row from the image
    predict[:,0] = np.copy(image[:,0])      # keep the first columen from the image
    predict[-1,:] = np.copy(image[-1,:])       # keep the first row from the image
    predict[:,-1] = np.copy(image[:,-1])      # keep the first columen from the image
    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]])
    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 = -z0+z2-z3
    y1 = z0+z1+z2
    y2 = -z0-z1-z2-z3
    predict = [np.linalg.solve(A,np.array([y0[r,c],y1[r,c],y2[r,c]]))[-1] for r in range(0,row-2) for c in range(0,col-2)]
    diff = [(np.max([z0[r,c],z1[r,c],z2[r,c],z3[r,c]])-np.min([z0[r,c],z1[r,c],z2[r,c],z3[r,c]])) for r in range(0,row-2) for c in range(0,col-2)]
    '''
    for r in range(1,row-1):                  # loop through the rth row
        for c in range(1,col-1):              # loop through the cth column
            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([int(image[r-1,c-1]), int(image[r-1,c]), int(image[r-1,c+1]), int(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]])
            y = np.array([-z[0]+z[2]-z[3], z[0]+z[1]+z[2], -z[0]-z[1]-z[2]-z[3]])
            predict[r,c] = np.linalg.solve(A,y)[-1]
            diff[r,c] = (np.max(actual_surrounding)-np.min(actual_surrounding))
    predict = np.ravel(predict[1:-1,1:-1])
    diff = np.ravel(diff[1:-1,1:-1])
    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)
image, predict, diff = plot_hist(images[0])
```

%% Cell type:code id:dda442ae tags:

``` python
fig = plt.figure(figsize = (10,10))
ax = fig.add_subplot()
x = np.abs(predict-image)
y = diff
plt.plot(x,y,'o',alpha = 0.2)
plt.rcParams.update({'font.size': 20})
plt.xlabel("differnece to the true value" )
plt.ylabel("differnece of min and max of true value of the surroundings")
plt.show()
```

%% Output


%% Cell type:code id:58da6063 tags:

``` python
image = Image.open(images[0])    #Open the image and read it as an Image object
image = np.array(image)[1:,:]
#z = np.array([image[1-1,1-1], image[1-1,1], image[1-1,1+1], image[1,1-1]])
z = np.array([22554,22552,22519,22561])
print(z)
'''A = np.array([[-3,0,1],[0,-3,3],[-1,-3,4]])
y = np.array([z[0]-z[2]+z[3], z[0]+z[1]+z[2], -z[0]-z[1]-z[2]-z[3]])
a,b,c = np.linalg.solve(A,y)'''
A = np.array([[3,0,-1],[0,3,3],[1,-3,-4]])
y = np.array([-z[0]+z[2]-z[3], z[0]+z[1]+z[2], -z[0]-z[1]-z[2]-z[3]])
print(y)
a,b,c = np.linalg.solve(A,y)
print(a,b,c)
```

%% Output

    [22554 22552 22519 22561]
    [-22596  67625 -90186]
    -17.49999999999879 -1.833333333338184 22543.500000000004

%% Cell type:code id:2562feeb tags:

``` python
i0 = (a*(-1) + b*(1) + c)
i1 = (a*(0) + b*(1) + c)
i2 = (a*(1) + b*(1) + c)
i3 = (a*(-1) + b*(0) + c)
print(sum([(i0-z[0])**2,(i1-z[1])**2,(i2-z[2])**2,(i3-z[3])**2]))
```

%% Cell type:code id:470cc137 tags:

``` python
a = 0
b = 2
c = 2
i0 = (a*(-1) + b*(1) + c)
i1 = (a*(0) + b*(1) + c)
i2 = (a*(1) + b*(1) + c)
i3 = (a*(-1) + b*(0) + c)
print(sum([(i0-z[0])**2,(i1-z[1])**2,(i2-z[2])**2,(i3-z[3])**2]))
```

%% Cell type:code id:3292b395 tags:

``` python
z = np.hstack((image[0,:3], image[1,0]))
x = np.array([-1,0,1,-1])
y = np.array([-1,-1,-1,0])
A = np.array([[-3,0,1],[0,-3,3],[1,3,-4]])
y = np.array([z[0]-z[2]+z[3], z[0]+z[1]+z[2], -z[0]-z[1]-z[2]-z[3]])
print(np.linalg.solve(A,y)[-1])
```

%% Cell type:code id:f9687830 tags:

``` python
0.5**2 + 1.5**2
```

%% Cell type:code id:e98eed4b tags:

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

%% 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:-6] == num:
            tiff.append(im)
    return tiff
```

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

``` python
def plot_hist(tiff_list):
    """
    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
    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)
    row, col = image.shape
    predict = np.empty([row,col])     # create a empty matrix to update prediction
    predict[0,:] = np.copy(image[0,:])       # keep the first row from the image
    predict[:,0] = np.copy(image[:,0])      # keep the first columen from the image
    predict[-1,:] = np.copy(image[-1,:])       # keep the first row from the image
    predict[:,-1] = np.copy(image[:,-1])      # keep the first columen from the image
    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]])
    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 = -z0+z2-z3
    y1 = z0+z1+z2
    y2 = -z0-z1-z2-z3
    predict = [np.linalg.solve(A,np.array([y0[r,c],y1[r,c],y2[r,c]]))[-1] for r in range(0,row-2) for c in range(0,col-2)]
    diff = [(np.max([z0[r,c],z1[r,c],z2[r,c],z3[r,c]])-np.min([z0[r,c],z1[r,c],z2[r,c],z3[r,c]])) for r in range(0,row-2) for c in range(0,col-2)]
    '''
    for r in range(1,row-1):                  # loop through the rth row
        for c in range(1,col-1):              # loop through the cth column
            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([int(image[r-1,c-1]), int(image[r-1,c]), int(image[r-1,c+1]), int(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]])
            y = np.array([-z[0]+z[2]-z[3], z[0]+z[1]+z[2], -z[0]-z[1]-z[2]-z[3]])
            predict[r,c] = np.linalg.solve(A,y)[-1]
            diff[r,c] = (np.max(actual_surrounding)-np.min(actual_surrounding))
    predict = np.ravel(predict[1:-1,1:-1])
    diff = np.ravel(diff[1:-1,1:-1])
    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)
image, predict, diff = plot_hist(images[0])
```

%% Cell type:code id:dda442ae tags:

``` python
fig = plt.figure(figsize = (10,10))
ax = fig.add_subplot()
x = np.abs(predict-image)
y = diff
plt.plot(x,y,'o',alpha = 0.2)
plt.rcParams.update({'font.size': 20})
plt.xlabel("differnece to the true value" )
plt.ylabel("differnece of min and max of true value of the surroundings")
plt.show()
```

%% Output


%% Cell type:code id:58da6063 tags:

``` python
image = Image.open(images[0])    #Open the image and read it as an Image object
image = np.array(image)[1:,:]
#z = np.array([image[1-1,1-1], image[1-1,1], image[1-1,1+1], image[1,1-1]])
z = np.array([22554,22552,22519,22561])
print(z)
'''A = np.array([[-3,0,1],[0,-3,3],[-1,-3,4]])
y = np.array([z[0]-z[2]+z[3], z[0]+z[1]+z[2], -z[0]-z[1]-z[2]-z[3]])
a,b,c = np.linalg.solve(A,y)'''
A = np.array([[3,0,-1],[0,3,3],[1,-3,-4]])
y = np.array([-z[0]+z[2]-z[3], z[0]+z[1]+z[2], -z[0]-z[1]-z[2]-z[3]])
print(y)
a,b,c = np.linalg.solve(A,y)
print(a,b,c)
```

%% Output

    [22554 22552 22519 22561]
    [-22596  67625 -90186]
    -17.49999999999879 -1.833333333338184 22543.500000000004

%% Cell type:code id:2562feeb tags:

``` python
i0 = (a*(-1) + b*(1) + c)
i1 = (a*(0) + b*(1) + c)
i2 = (a*(1) + b*(1) + c)
i3 = (a*(-1) + b*(0) + c)
print(sum([(i0-z[0])**2,(i1-z[1])**2,(i2-z[2])**2,(i3-z[3])**2]))
```

%% Cell type:code id:470cc137 tags:

``` python
a = 0
b = 2
c = 2
i0 = (a*(-1) + b*(1) + c)
i1 = (a*(0) + b*(1) + c)
i2 = (a*(1) + b*(1) + c)
i3 = (a*(-1) + b*(0) + c)
print(sum([(i0-z[0])**2,(i1-z[1])**2,(i2-z[2])**2,(i3-z[3])**2]))
```

%% Cell type:code id:3292b395 tags:

``` python
z = np.hstack((image[0,:3], image[1,0]))
x = np.array([-1,0,1,-1])
y = np.array([-1,-1,-1,0])
A = np.array([[-3,0,1],[0,-3,3],[1,3,-4]])
y = np.array([z[0]-z[2]+z[3], z[0]+z[1]+z[2], -z[0]-z[1]-z[2]-z[3]])
print(np.linalg.solve(A,y)[-1])
```

%% Cell type:code id:f9687830 tags:

``` python
0.5**2 + 1.5**2
```

%% Cell type:code id:e98eed4b tags:

``` python
```