Commit 482eeae2 authored by Nathaniel Callens's avatar Nathaniel Callens
Browse files

research in wavelets

parent 48804c56
Loading
Loading
Loading
Loading
+27 −6
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
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: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)
    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[0:-2,0:-2]   # get all the first pixel for the entire image
    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
    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
    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
    z3 = image[1:-1,0:-2]   # get all the forth 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]
    # flatten the neighbor pixlels and stack them together
    # 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
    # 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
    image_ravel = np.ravel(image[1:-1,1:-1])
    image_ravel = np.ravel(image[1:-1,1:-1])
    return image_ravel, predict, diff, image
    return image_ravel, predict, diff, image
```
```


%% 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, non_ravel = plot_hist(num_images, i)
    image, predict, difference, non_ravel = 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)))


#image, predict, difference = plot_hist(images, 0)
#image, predict, difference = plot_hist(images, 0)
```
```


%% 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.10891032218933105
    Average Time per Image for First: 0.10891032218933105


%% Cell type:code id:4c05b947 tags:
%% Cell type:code id:4c05b947 tags:


``` python
``` python
new_image, new_pred, new_diff, no_ravel = plot_hist(images, 10)
new_image, new_pred, new_diff, no_ravel = plot_hist(images, 10)
```
```


%% Cell type:code id:dda442ae tags:
%% Cell type:code id:dda442ae tags:


``` python
``` python
new_error = np.abs(new_image-new_pred)
new_error = np.abs(new_image-new_pred)
plt.hist(new_error, bins=20, density=True)
plt.hist(new_error, bins=20, density=True)
sns.kdeplot(new_error)
sns.kdeplot(new_error)
plt.xlabel("error")
plt.xlabel("error")
plt.show()
plt.show()
```
```


%% Output
%% Output




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


``` python
``` python
plt.hist(new_image, bins=25, density=True)
plt.hist(new_image, bins=25, density=True)
sns.kdeplot(new_image)
sns.kdeplot(new_image)
plt.xlabel("Actual Pixel Value")
plt.xlabel("Actual Pixel Value")
plt.show()
plt.show()
```
```


%% Output
%% Output




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


``` python
``` python
f_r = no_ravel[0]
f_r = no_ravel[0]
print(no_ravel.shape)
print(no_ravel.shape)
print(sys.getsizeof(no_ravel))
print(sys.getsizeof(no_ravel))
print((256).bit_length())
print((256).bit_length())
```
```


%% Output
%% Output


    (512, 640)
    (512, 640)
    1310832
    1310832
    9
    9


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


``` python
``` python
coeffs = pywt.dwt2(no_ravel, 'bior1.3')
coeffs = pywt.dwt2(no_ravel, 'bior1.3')
LL, (LH, HL, HH) = coeffs
LL, (LH, HL, HH) = coeffs
print(HH.shape)
decompress = pywt.idwt2(coeffs, 'bior1.3')
decompress = pywt.idwt2(coeffs, 'bior1.3')
print(np.mean(np.abs(decompress-no_ravel)))
"""print(decompress)
print(np.mean(np.abs(decompress-no_ravel)))"""
```
```


%% Output
%% Output


    5.667000202436157e-12
    (258, 322)

    'print(decompress)\nprint(np.mean(np.abs(decompress-no_ravel)))'


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


``` python
``` python
def compress(uncompressed):
def compress(uncompressed):
    """Compress a string to a list of output symbols."""
    """Compress a string to a list of output symbols."""


    # Build the dictionary.
    # Build the dictionary.
    dict_size = 256
    dict_size = 256
    dictionary = dict((chr(i), i) for i in range(dict_size))
    dictionary = dict((chr(i), i) for i in range(dict_size))
    # in Python 3: dictionary = {chr(i): i for i in range(dict_size)}
    # in Python 3: dictionary = {chr(i): i for i in range(dict_size)}


    w = ""
    w = ""
    result = []
    result = []
    for c in uncompressed:
    for c in uncompressed:
        wc = w + c
        wc = w + c
        if wc in dictionary:
        if wc in dictionary:
            w = wc
            w = wc
        else:
        else:
            result.append(dictionary[w])
            result.append(dictionary[w])
            # Add wc to the dictionary.
            # Add wc to the dictionary.
            dictionary[wc] = dict_size
            dictionary[wc] = dict_size
            dict_size += 1
            dict_size += 1
            w = c
            w = c


    # Output the code for w.
    # Output the code for w.
    if w:
    if w:
        result.append(dictionary[w])
        result.append(dictionary[w])
    return result
    return result


store = compress("Hello my name is Scout")
store = compress("Hello my name is Scout")
print(store)
print(store)
print(sys.getsizeof(store))
print(sys.getsizeof(store))
print(sys.getsizeof("Hello my name is Scout"))
print(sys.getsizeof("Hello my name is Scout"))
```
```


%% Output
%% Output


    [72, 101, 108, 108, 111, 32, 109, 121, 32, 110, 97, 109, 101, 32, 105, 115, 32, 83, 99, 111, 117, 116]
    [72, 101, 108, 108, 111, 32, 109, 121, 32, 110, 97, 109, 101, 32, 105, 115, 32, 83, 99, 111, 117, 116]
    256
    256
    71
    71


%% Cell type:code id:f9687830 tags:
%% Cell type:code id:f9687830 tags:


``` python
``` python
def wavelet(num_images, i):
def wavelet(num_images, i):


    image = Image.open(num_images[i])    #Open the image and read it as an Image object
    image = Image.open(num_images[i])    #Open the image and read it as an Image object
    image = np.array(im)[1:,:]
    image = np.array(im)[1:,:]
    coeffs = pywt.dwt2(image, 'bior1.3')
    coeffs = pywt.dwt2(image, 'bior1.3')
    return coeffs
    return coeffs


def huffman(coeffs):
    for i in range(len(coeffs)):




coef, t = wavelet(num_images)
coef, t = wavelet(num_images,0)


def wave_decompress(coeffs):
def wave_decompress(coeffs):
    times = []
    times = []
    for i in range(len(coeffs)):
    for i in range(len(coeffs)):
        start = time()
        start = time()
        decompress = pywt.idwt2(coeffs[i], 'bior1.3')
        decompress = pywt.idwt2(coeffs[i], 'bior1.3')
        stop = time()
        stop = time()
        times.append(stop-start)
        times.append(stop-start)
    return times
    return times
ti = wave_decompress(coef)
ti = wave_decompress(coef)
print(np.mean(ti))
print(np.mean(ti))
```
```


%% Output
%% Output


    0.01910218596458435
    ---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    ~\AppData\Local\Temp/ipykernel_22104/2202774112.py in <module>
         18         times.append(stop-start)
         19     return times
    ---> 20 ti = wave_decompress(coef)
         21 print(np.mean(ti))
    ~\AppData\Local\Temp/ipykernel_22104/2202774112.py in wave_decompress(coeffs)
         14     for i in range(len(coeffs)):
         15         start = time()
    ---> 16         decompress = pywt.idwt2(coeffs[i], 'bior1.3')
         17         stop = time()
         18         times.append(stop-start)
    ~\anaconda3\lib\site-packages\pywt\_multidim.py in idwt2(coeffs, wavelet, mode, axes)
        110     """
        111     # L -low-pass data, H - high-pass data
    --> 112     LL, (HL, LH, HH) = coeffs
        113     axes = tuple(axes)
        114     if len(axes) != 2:
    ValueError: too many values to unpack (expected 2)


%% Cell type:code id:e98eed4b tags:
%% Cell type:code id:e98eed4b tags:


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


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


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