Commit 271201a3 authored by Kelly Chang's avatar Kelly Chang
Browse files

kelly changes

parent c2c824c5
Loading
Loading
Loading
Loading
(8 KiB)

File changed.

No diff preview for this file type.

+80 −244
Original line number Original line Diff line number Diff line
%% Cell type:code id:8868bc30 tags:
%% Cell type:code id:8868bc30 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
import seaborn as sns
import seaborn as sns
from sklearn.neighbors import KernelDensity
from sklearn.neighbors import KernelDensity
import pandas as pd
import pandas as pd
from collections import Counter
from collections import Counter
import time
import time
```
```


%% Cell type:code id:76317b02 tags:
%% Cell type:code id:0f944705 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))
        if file == '.DS_Store':
            continue
        else:
            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))
            #if file[-4:] == ".jp4" or file[-7:] == "_6.tiff":
            if file[-5:] != ".tiff" or file[-7:] == "_6.tiff":
                continue
            else:
                image_folder.append(os.path.join(scene, file))
    '''print(image_folder)
    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 image_folder #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:be1ff8a1 tags:
%% Cell type:code id:b18d5e38 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)
    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.floor(np.linalg.solve(A,y)[-1])
    #predict = np.floor(np.linalg.solve(A,y)[-1])
    predict = np.round(np.round((np.linalg.solve(A,y)[-1]),1))
    predict = np.round(np.round((np.linalg.solve(A,y)[-1]),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 = np.ravel(image[1:-1,1:-1])
    image = np.ravel(image[1:-1,1:-1])
    error = image-predict
    error = image-predict


    return image, predict, diff, error, A
    return image, predict, diff, error, A
```
```


%% Cell type:code id:8483903e tags:
%% Cell type:code id:35d4f6a0 tags:


``` python
``` python
class NodeTree(object):
class NodeTree(object):
    def __init__(self, left=None, right=None):
    def __init__(self, left=None, right=None):
        self.left = left
        self.left = left
        self.right = right
        self.right = right


    def children(self):
    def children(self):
        return self.left, self.right
        return self.left, self.right


    def __str__(self):
    def __str__(self):
        return self.left, self.right
        return self.left, self.right




def huffman_code_tree(node, binString=''):
def huffman_code_tree(node, binString=''):
    '''
    '''
    Function to find Huffman Code
    Function to find Huffman Code
    '''
    '''
    if type(node) is str:
    if type(node) is str:
        return {node: binString}
        return {node: binString}
    (l, r) = node.children()
    (l, r) = node.children()
    d = dict()
    d = dict()
    d.update(huffman_code_tree(l, binString + '0'))
    d.update(huffman_code_tree(l, binString + '0'))
    d.update(huffman_code_tree(r, binString + '1'))
    d.update(huffman_code_tree(r, binString + '1'))
    return d
    return d




def make_tree(nodes):
def make_tree(nodes):
    '''
    '''
    Function to make tree
    Function to make tree
    :param nodes: Nodes
    :param nodes: Nodes
    :return: Root of the tree
    :return: Root of the tree
    '''
    '''
    while len(nodes) > 1:
    while len(nodes) > 1:
        (key1, c1) = nodes[-1]
        (key1, c1) = nodes[-1]
        (key2, c2) = nodes[-2]
        (key2, c2) = nodes[-2]
        nodes = nodes[:-2]
        nodes = nodes[:-2]
        node = NodeTree(key1, key2)
        node = NodeTree(key1, key2)
        nodes.append((node, c1 + c2))
        nodes.append((node, c1 + c2))
        nodes = sorted(nodes, key=lambda x: x[1], reverse=True)
        nodes = sorted(nodes, key=lambda x: x[1], reverse=True)
    return nodes[0][0]
    return nodes[0][0]
```
```


%% Cell type:markdown id:c7104fbf tags:
%% Cell type:markdown id:c7104fbf tags:


### Huffman without dividing into bins
### Huffman without dividing into bins


%% Cell type:code id:a43f3f1c tags:
%% Cell type:code id:c50169ed tags:


``` python
``` python
scenes = file_extractor()
images = image_extractor(scenes)
def huffman_nb(image):
def huffman_nb(image):
    origin, predict, diff, error, A = plot_hist(image)
    origin, predict, diff, error, A = plot_hist(image)
    image = Image.open(image)
    image = Image.open(image)
    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)


    new_error = np.copy(image)
    new_error = np.copy(image)
    new_error[1:-1,1:-1] = np.reshape(error,(510, 638))
    new_error[1:-1,1:-1] = np.reshape(error,(510, 638))
    keep = new_error[0,0]
    keep = new_error[0,0]
    new_error[0,:] = new_error[0,:] - keep
    new_error[0,:] = new_error[0,:] - keep
    new_error[-1,:] = new_error[-1,:] - keep
    new_error[-1,:] = new_error[-1,:] - keep
    new_error[1:-1,0] = new_error[1:-1,0] - keep
    new_error[1:-1,0] = new_error[1:-1,0] - keep
    new_error[1:-1,-1] = new_error[1:-1,-1] - keep
    new_error[1:-1,-1] = new_error[1:-1,-1] - keep
    new_error[0,0] = keep
    new_error[0,0] = keep
    new_error = np.ravel(new_error)
    new_error = np.ravel(new_error)






    #ab_error = np.abs(new_error)
    #ab_error = np.abs(new_error)
    #string = [str(i) for i in ab_error]
    #string = [str(i) for i in ab_error]
    string = [str(i) for i in new_error.astype(int)]
    string = [str(i) for i in new_error.astype(int)]
    freq = dict(Counter(string))
    freq = dict(Counter(string))


    freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)
    freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)
    node = make_tree(freq)
    node = make_tree(freq)
    encoding = huffman_code_tree(node)
    encoding = huffman_code_tree(node)
    #encoded = ["1"+encoding[str(-i)] if i < 0 else "0"+encoding[str(i)] for i in error]
    #encoded = ["1"+encoding[str(-i)] if i < 0 else "0"+encoding[str(i)] for i in error]


    # return the huffman dictionary
    # return the huffman dictionary
    return encoding, new_error, image.reshape(-1)
    return encoding, new_error, image.reshape(-1)




def compress_rate_nb(image, error, encoding):
def compress_rate_nb(image, error, encoding):
    #original = original.reshape(-1)
    original = image.reshape(-1)
    #error = error.reshape(-1)
    error = error.reshape(-1)
    o_len = 0
    o_len = 0
    c_len = 0
    c_len = 0
    for i in range(0, len(original)):
    for i in range(0, len(original)):
        o_len += len(bin(original[i])[2:])
        o_len += len(bin(original[i])[2:])
        c_len += len(encoding[str(int(error[i]))])
        c_len += len(encoding[str(int(error[i]))])



    return c_len/o_len
    return c_len/o_len


```
```


%% Cell type:markdown id:eac2f456 tags:
%% Cell type:markdown id:e34201fd tags:

### Huffman with dividing into non-uniform bins

%% Cell type:markdown id:3a3f06a5 tags:

### Huffman with dividing into uniform bins

%% Cell type:code id:14075c94 tags:

``` python
def huffman_u(image):
    origin, predict, diff, error, A = plot_hist(image)
    image = Image.open(image)
    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)
    print(len(diff))

    boundary = np.hstack((image[0,:],image[-1,:],image[1:-1,0],image[1:-1,-1]))
    boundary = boundary - image[0,0]
    boundary[0] = image[0,0]

    string = [str(i) for i in boundary]
    freq = dict(Counter(string))
    freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)
    node = make_tree(freq)
    encode1 = huffman_code_tree(node)


    mask = diff <= 100
    string = [str(i) for i in error[mask].astype(int)]
    freq = dict(Counter(string))
    freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)
    node = make_tree(freq)
    encode2 = huffman_code_tree(node)


    mask = diff > 100
    #new_error = error[mask]
    #mask2 = diff[mask] <= 200
    #string = [str(i) for i in new_error[mask2].astype(int)]
    string = [str(i) for i in error[mask].astype(int)]
    freq = dict(Counter(string))
    freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)
    node = make_tree(freq)
    encode3 = huffman_code_tree(node)


    '''mask = diff > 200
    new_error = error[mask]
    mask2 = diff[mask] <= 300
    string = [str(i) for i in new_error[mask2].astype(int)]
    freq = dict(Counter(string))
    freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)
    node = make_tree(freq)
    encode4 = huffman_code_tree(node)


    mask = diff > 300
    string = [str(i) for i in error[mask].astype(int)]
    freq = dict(Counter(string))
    freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)
    node = make_tree(freq)
    encode5 = huffman_code_tree(node)'''




    new_error = np.copy(image)
    new_error[1:-1,1:-1] = np.reshape(error,(510, 638))
    keep = new_error[0,0]
    new_error[0,:] = new_error[0,:] - keep
    new_error[-1,:] = new_error[-1,:] - keep
    new_error[1:-1,0] = new_error[1:-1,0] - keep
    new_error[1:-1,-1] = new_error[1:-1,-1] - keep
    new_error[0,0] = keep
    new_error = np.ravel(new_error)

    # return the huffman dictionary
    #return encode1, encode2, encode3, encode4, encode5, np.ravel(image), error, diff, boundary
    print(len(diff))
    return encode1, encode2, encode3, np.ravel(image), error, diff, boundary

#def compress_rate_u(image, error, diff, bound, encode1, encode2, encode3, encode4, encode5):
def compress_rate_u(image, error, diff, bound, encode1, encode2, encode3):
    #original = original.reshape(-1)
    #error = error.reshape(-1)
    o_len = 0
    c_len = 0
    im = np.reshape(image,(512, 640))
    real_b = np.hstack((im[0,:],im[-1,:],im[1:-1,0],im[1:-1,-1]))
    original = im[1:-1,1:-1].reshape(-1)

    for i in range(0,len(bound)):
        o_len += len(bin(real_b[i])[2:])
        c_len += len(encode1[str(bound[i])])

    for i in range(0, len(original)):
        o_len += len(bin(original[i])[2:])
        if diff[i] <= 100:
            c_len += len(encode2[str(int(error[i]))])

        if diff[i] > 100:
            c_len += len(encode3[str(int(error[i]))])

        '''if diff[i] <= 200 and diff[i] > 100:
            c_len += len(encode3[str(int(error[i]))])'''

        '''if diff[i] <= 300 and diff[i] > 200:
            c_len += len(encode4[str(int(error[i]))])

        if diff[i] > 300:
            c_len += len(encode5[str(int(error[i]))])'''

    return c_len/o_len
scenes = file_extractor()
images = image_extractor(scenes)
encode1, encode2, encode3, image, error, diff, boundary = huffman_u(images[0])
compress_rate_u(image, error, diff, boundary, encode1, encode2, encode3)
```

%% Output

    325380
    325380


    0.4432273356119792
### Huffman dividing into bins


%% Cell type:code id:207b0bd2 tags:
%% Cell type:code id:205c4731 tags:


``` python
``` python
def huffman(image):
def huffman(image):
    origin, predict, diff, error, A = plot_hist(image)
    origin, predict, diff, error, A = plot_hist(image)


    image = Image.open(image)
    image = Image.open(image)
    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)


    boundary = np.hstack((image[0,:],image[-1,:],image[1:-1,0],image[1:-1,-1]))
    boundary = np.hstack((image[0,:],image[-1,:],image[1:-1,0],image[1:-1,-1]))
    boundary = boundary - image[0,0]
    boundary = boundary - image[0,0]
    boundary[0] = image[0,0]
    boundary[0] = image[0,0]


    string = [str(i) for i in boundary]
    string = [str(i) for i in boundary]
    freq = dict(Counter(string))
    freq = dict(Counter(string))
    freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)
    freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)
    node = make_tree(freq)
    node = make_tree(freq)
    encode1 = huffman_code_tree(node)
    encode1 = huffman_code_tree(node)




    mask = diff <= 25
    mask = diff <= 25
    string = [str(i) for i in error[mask].astype(int)]
    string = [str(i) for i in error[mask].astype(int)]
    freq = dict(Counter(string))
    freq = dict(Counter(string))
    freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)
    freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)
    node = make_tree(freq)
    node = make_tree(freq)
    encode2 = huffman_code_tree(node)
    encode2 = huffman_code_tree(node)




    mask = diff > 25
    mask = diff > 25
    new_error = error[mask]
    new_error = error[mask]
    mask2 = diff[mask] <= 40
    mask2 = diff[mask] <= 40
    string = [str(i) for i in new_error[mask2].astype(int)]
    string = [str(i) for i in new_error[mask2].astype(int)]
    freq = dict(Counter(string))
    freq = dict(Counter(string))
    freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)
    freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)
    node = make_tree(freq)
    node = make_tree(freq)
    encode3 = huffman_code_tree(node)
    encode3 = huffman_code_tree(node)




    mask = diff > 40
    mask = diff > 40
    new_error = error[mask]
    new_error = error[mask]
    mask2 = diff[mask] <= 70
    mask2 = diff[mask] <= 70
    string = [str(i) for i in new_error[mask2].astype(int)]
    string = [str(i) for i in new_error[mask2].astype(int)]
    freq = dict(Counter(string))
    freq = dict(Counter(string))
    freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)
    freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)
    node = make_tree(freq)
    node = make_tree(freq)
    encode4 = huffman_code_tree(node)
    encode4 = huffman_code_tree(node)




    mask = diff > 70
    mask = diff > 70
    string = [str(i) for i in error[mask].astype(int)]
    string = [str(i) for i in error[mask].astype(int)]
    freq = dict(Counter(string))
    freq = dict(Counter(string))
    freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)
    freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)
    node = make_tree(freq)
    node = make_tree(freq)
    encode5 = huffman_code_tree(node)
    encode5 = huffman_code_tree(node)




    new_error = np.copy(image)
    new_error = np.copy(image)
    new_error[1:-1,1:-1] = np.reshape(error,(510, 638))
    new_error[1:-1,1:-1] = np.reshape(error,(510, 638))
    keep = new_error[0,0]
    keep = new_error[0,0]
    new_error[0,:] = new_error[0,:] - keep
    new_error[0,:] = new_error[0,:] - keep
    new_error[-1,:] = new_error[-1,:] - keep
    new_error[-1,:] = new_error[-1,:] - keep
    new_error[1:-1,0] = new_error[1:-1,0] - keep
    new_error[1:-1,0] = new_error[1:-1,0] - keep
    new_error[1:-1,-1] = new_error[1:-1,-1] - keep
    new_error[1:-1,-1] = new_error[1:-1,-1] - keep
    new_error[0,0] = keep
    new_error[0,0] = keep




    #new_error = np.ravel(new_error)
    #new_error = np.ravel(new_error)


    bins = [25,40,70]
    bins = [25,40,70]

    list_dic = [encode1, encode2, encode3, encode4, encode5]
    # return the huffman dictionary
    # return the huffman dictionary
    return encode1, encode2, encode3, encode4, encode5, np.ravel(image), error, new_error, diff, boundary, bins, predict
    return list_dic, np.ravel(image), error, new_error, diff, boundary, bins, predict




def compress_rate(image, error, diff, bound, encode1, encode2, encode3, encode4, encode5):
def compress_rate(image, error, diff, bound, list_dic, bins):
    #original = original.reshape(-1)
    #original = original.reshape(-1)
    #error = error.reshape(-1)
    #error = error.reshape(-1)


    o_len = 0
    o_len = 0
    c_len = 0
    c_len = 0
    im = np.reshape(image,(512, 640))
    im = np.reshape(image,(512, 640))
    real_b = np.hstack((im[0,:],im[-1,:],im[1:-1,0],im[1:-1,-1]))
    real_b = np.hstack((im[0,:],im[-1,:],im[1:-1,0],im[1:-1,-1]))
    original = im[1:-1,1:-1].reshape(-1)
    original = im[1:-1,1:-1].reshape(-1)
    diff = diff.reshape(-1)


    # calculate the bit for boundary
    for i in range(0,len(bound)):
    for i in range(0,len(bound)):
        o_len += len(bin(real_b[i])[2:])
        o_len += len(bin(real_b[i])[2:])
        c_len += len(encode1[str(bound[i])])
        c_len += len(list_dic[0][str(bound[i])])


    for i in range(0, len(original)):

    for i in range(0,len(original)):
        o_len += len(bin(original[i])[2:])
        o_len += len(bin(original[i])[2:])
        if diff[i] <= 25:
        if diff[i] <= bins[0]:
            c_len += len(encode2[str(int(error[i]))])
            c_len += len(list_dic[1][str(int(error[i]))])


        if diff[i] <= 40 and diff[i] > 25:
        elif diff[i] <= bins[1] and diff[i] > bins[0]:
            c_len += len(encode3[str(int(error[i]))])
            c_len += len(list_dic[2][str(int(error[i]))])


        if diff[i] <= 70 and diff[i] > 40:
        elif diff[i] <= bins[2] and diff[i] > bins[1]:
            c_len += len(encode4[str(int(error[i]))])
            c_len += len(list_dic[3][str(int(error[i]))])
        else:
            c_len += len(list_dic[4][str(int(error[i]))])


        if diff[i] > 70:
            c_len += len(encode5[str(int(error[i]))])


    return c_len/o_len
    return c_len/o_len
scenes = file_extractor()
images = image_extractor(scenes)
encode1, encode2, encode3, encode4, encode5, image, error, diff, boundary, bins = huffman(images[0])
compress_rate(image, error, diff, boundary, encode1, encode2, encode3, encode4, encode5)
```
```


%% Output
%% Cell type:markdown id:2e84c206 tags:

    325380

    0.44205322265625

%% Cell type:markdown id:816764c9 tags:


## Huffman Divide into 6 bins
### Huffman dividing into bins


%% Cell type:code id:15eecad3 tags:
%% Cell type:code id:18e44483 tags:


``` python
``` python
def huffman6(image):
def huffman_u(image):
    origin, predict, diff, error, A = plot_hist(image)
    origin, predict, diff, error, A = plot_hist(image)
    image = Image.open(image)
    image = Image.open(image)
    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)




    boundary = np.hstack((image[0,:],image[-1,:],image[1:-1,0],image[1:-1,-1]))
    boundary = np.hstack((image[0,:],image[-1,:],image[1:-1,0],image[1:-1,-1]))
    boundary = boundary - image[0,0]
    boundary = boundary - image[0,0]
    boundary[0] = image[0,0]
    boundary[0] = image[0,0]


    string = [str(i) for i in boundary]
    string = [str(i) for i in boundary]
    freq = dict(Counter(string))
    freq = dict(Counter(string))
    freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)
    freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)
    node = make_tree(freq)
    node = make_tree(freq)
    encode1 = huffman_code_tree(node)
    encode1 = huffman_code_tree(node)




    mask = diff <= 5
    mask = diff <= 100
    string = [str(i) for i in error[mask].astype(int)]
    string = [str(i) for i in error[mask].astype(int)]
    freq = dict(Counter(string))
    freq = dict(Counter(string))
    freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)
    freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)
    node = make_tree(freq)
    node = make_tree(freq)
    encode2 = huffman_code_tree(node)
    encode2 = huffman_code_tree(node)




    mask = diff > 5
    mask = diff > 100
    new_error = error[mask]
    #new_error = error[mask]
    mask2 = diff[mask] <= 15
    #mask2 = diff[mask] <= 200
    string = [str(i) for i in new_error[mask2].astype(int)]
    #string = [str(i) for i in new_error[mask2].astype(int)]
    string = [str(i) for i in error[mask].astype(int)]
    freq = dict(Counter(string))
    freq = dict(Counter(string))
    freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)
    freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)
    node = make_tree(freq)
    node = make_tree(freq)
    encode3 = huffman_code_tree(node)
    encode3 = huffman_code_tree(node)




    mask = diff > 15
    '''mask = diff > 200
    new_error = error[mask]
    new_error = error[mask]
    mask2 = diff[mask] <= 30
    mask2 = diff[mask] <= 300
    string = [str(i) for i in new_error[mask2].astype(int)]
    string = [str(i) for i in new_error[mask2].astype(int)]
    freq = dict(Counter(string))
    freq = dict(Counter(string))
    freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)
    freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)
    node = make_tree(freq)
    node = make_tree(freq)
    encode4 = huffman_code_tree(node)
    encode4 = huffman_code_tree(node)




    mask = diff > 30
    mask = diff > 300
    new_error = error[mask]
    mask2 = diff[mask] <= 50
    string = [str(i) for i in new_error[mask2].astype(int)]
    freq = dict(Counter(string))
    freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)
    node = make_tree(freq)
    encode5 = huffman_code_tree(node)


    mask = diff > 50
    string = [str(i) for i in error[mask].astype(int)]
    string = [str(i) for i in error[mask].astype(int)]
    freq = dict(Counter(string))
    freq = dict(Counter(string))
    freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)
    freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)
    node = make_tree(freq)
    node = make_tree(freq)
    encode6 = huffman_code_tree(node)
    encode5 = huffman_code_tree(node)'''








    new_error = np.copy(image)
    new_error = np.copy(image)
    new_error[1:-1,1:-1] = np.reshape(error,(510, 638))
    new_error[1:-1,1:-1] = np.reshape(error,(510, 638))
    keep = new_error[0,0]
    keep = new_error[0,0]
    new_error[0,:] = new_error[0,:] - keep
    new_error[0,:] = new_error[0,:] - keep
    new_error[-1,:] = new_error[-1,:] - keep
    new_error[-1,:] = new_error[-1,:] - keep
    new_error[1:-1,0] = new_error[1:-1,0] - keep
    new_error[1:-1,0] = new_error[1:-1,0] - keep
    new_error[1:-1,-1] = new_error[1:-1,-1] - keep
    new_error[1:-1,-1] = new_error[1:-1,-1] - keep
    new_error[0,0] = keep
    new_error[0,0] = keep
    new_error = np.ravel(new_error)
    new_error = np.ravel(new_error)


    # return the huffman dictionary
    # return the huffman dictionary
    return encode1, encode2, encode3, encode4, encode5, encode6, np.ravel(image), error, diff, boundary
    #return encode1, encode2, encode3, encode4, encode5, np.ravel(image), error, diff, boundary
    return encode1, encode2, encode3, np.ravel(image), error, diff, boundary


def compress_rate6(image, error, diff, bound, encode1, encode2, encode3, encode4, encode5, encode6):
#def compress_rate_u(image, error, diff, bound, encode1, encode2, encode3, encode4, encode5):
def compress_rate_u(image, error, diff, bound, encode1, encode2, encode3):
    #original = original.reshape(-1)
    #original = original.reshape(-1)
    #error = error.reshape(-1)
    #error = error.reshape(-1)
    o_len = 0
    o_len = 0
    c_len = 0
    c_len = 0
    im = np.reshape(image,(512, 640))
    im = np.reshape(image,(512, 640))
    real_b = np.hstack((im[0,:],im[-1,:],im[1:-1,0],im[1:-1,-1]))
    real_b = np.hstack((im[0,:],im[-1,:],im[1:-1,0],im[1:-1,-1]))
    original = im[1:-1,1:-1].reshape(-1)
    original = im[1:-1,1:-1].reshape(-1)


    for i in range(0,len(bound)):
    for i in range(0,len(bound)):
        o_len += len(bin(real_b[i])[2:])
        o_len += len(bin(real_b[i])[2:])
        c_len += len(encode1[str(bound[i])])
        c_len += len(encode1[str(bound[i])])


    for i in range(0, len(original)):
    for i in range(0, len(original)):
        o_len += len(bin(original[i])[2:])
        o_len += len(bin(original[i])[2:])
        if diff[i] <= 5:
        if diff[i] <= 100:
            c_len += len(encode2[str(int(error[i]))])
            c_len += len(encode2[str(int(error[i]))])


        if diff[i] <= 15 and diff[i] > 5:
        if diff[i] > 100:
            c_len += len(encode3[str(int(error[i]))])
            c_len += len(encode3[str(int(error[i]))])


        if diff[i] <= 30 and diff[i] > 15:
        '''if diff[i] <= 200 and diff[i] > 100:
            c_len += len(encode4[str(int(error[i]))])
            c_len += len(encode3[str(int(error[i]))])'''


        if diff[i] <= 50 and diff[i] > 30:
        '''if diff[i] <= 300 and diff[i] > 200:
            c_len += len(encode5[str(int(error[i]))])
            c_len += len(encode4[str(int(error[i]))])


        if diff[i] > 50:
        if diff[i] > 300:
            c_len += len(encode6[str(int(error[i]))])
            c_len += len(encode5[str(int(error[i]))])'''


    return c_len/o_len
    return c_len/o_len
scenes = file_extractor()
scenes = file_extractor()
images = image_extractor(scenes)
images = image_extractor(scenes)
encode1, encode2, encode3, encode4, encode5, encode6, image, error, diff, boundary = huffman(images[0])
encode1, encode2, encode3, image, error, diff, boundary = huffman_u(images[0])
compress_rate(image, error, diff, boundary, encode1, encode2, encode3, encode4, encode5, encode6)
compress_rate_u(image, error, diff, boundary, encode1, encode2, encode3)
```
```


%% Output
%% Cell type:code id:e1ce9912 tags:

    0.4421759033203125

%% Cell type:code id:f8a8c717 tags:


``` python
``` python
scenes = file_extractor()
scenes = file_extractor()
images = image_extractor(scenes)
images = image_extractor(scenes)
num_images = im_distribution(images, "_9")
rate = []

for i in range(len(num_images)):
    encode1, encode2, encode3, encode4, encode5, encode6, image, error, diff, bound = huffman6(num_images[i])
    r = compress_rate6(image, error, diff, bound, encode1, encode2, encode3, encode4, encode5, encode6)
    rate.append(r)


print(f"Compression rate of huffman with different bins: {np.mean(rate)}")
```

%% Output

    Compression rate of huffman with different bins: 0.448723882039388

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

``` python
num_images = file_extractor('im')


rate = []
rate = []
rate_nb = []
rate_nb = []
rate_u = []
rate_u = []
for i in range(len(num_images)):
for i in range(len(images)):
    encode1, encode2, encode3, encode4, encode5, image, error, diff, bound = huffman(num_images[i])
    list_dic, image, error, new_error, diff, bound, bins, predict = huffman(images[i])
    r = compress_rate(image, error, diff, bound, encode1, encode2, encode3, encode4, encode5)
    r = compress_rate(image, error, diff, bound, list_dic, bins)
    rate.append(r)
    rate.append(r)
    encoding, error, image = huffman_nb(num_images[i])

    encoding, error, image = huffman_nb(images[i])

    r = compress_rate_nb(image, error, encoding)
    r = compress_rate_nb(image, error, encoding)
    rate_nb.append(r)
    rate_nb.append(r)
    encode1, encode2, encode3, image, error, diff, bound = huffman_u(num_images[i])
    r = compress_rate_u(image, error, diff, bound, encode1, encode2, encode3)
    rate_u.append(r)


print(f"Compression rate of huffman with different bins: {np.mean(rate)}")
print(f"Compression rate of huffman with different bins: {np.mean(rate)}")
print(f"Compression rate of huffman without bins: {np.mean(rate_nb)}")
print(f"Compression rate of huffman without bins: {np.mean(rate_nb)}")
print(f"Compression rate of huffman with uniform bins: {np.mean(rate_u)}")
```
```


%% Output
%% Output


    ---------------------------------------------------------------------------
    Compression rate of huffman with different bins: 0.40459069242931545
    NameError                                 Traceback (most recent call last)
    Compression rate of huffman without bins: 0.410545890687004
    /var/folders/z2/plvrsqjs023g1cmx7k19mhzr0000gn/T/ipykernel_3263/2742763429.py in <module>

    ----> 1 num_images = file_extractor('im')
%% Cell type:code id:7a9a31f6 tags:
          2

          3 rate = []
``` python
          4 rate_nb = []
```
          5 rate_u = []
    NameError: name 'file_extractor' is not defined


%% Cell type:code id:992dd8bb tags:
%% Cell type:code id:c71591f2 tags:


``` python
``` python
origin, predict, diff, error, A = plot_hist(images[0])
```
```


%% Cell type:code id:9200fa53 tags:
%% Cell type:code id:9200fa53 tags:


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