Loading .ipynb_checkpoints/Error_to_Image-checkpoint.ipynb +239 −4 Original line number Diff line number Diff line %% Cell type:code id:dbef8759 tags: ``` python import numpy as np from prediction_MSE_Scout import file_extractor, image_extractor, im_distribution 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 from numpy import linalg as la from scipy.stats import gaussian_kde import seaborn as sns from collections import Counter import pandas as pd import scipy as sp ``` %% Cell type:code id:9ed20f84 tags: ``` python def predict(tiff_list, i=0): """ This function predicts the pixel values based on a linear combination of the MSE from the three pixels above it and the one to the left. It uses a system of equations to fit the plane ax + by + c and takes c as the prediction for the unknown pixel. It does this all at once by constructing vectors and matrices of the surrounding pixels and solving each system simultaneously so as not to iterate through each one. Parameters: tiff_list: list, list of names of image file paths to access. These should be strings in the form of a path to the image i: int, which index in the tiff_list of images we want to predict on Returns: prediction: matrix (ndarray), the matrix of predicted values for the image using the previous four piexels diff: matrix (ndarray), the difference between the highest and lowest valued surrounding four pixels image_int: matrix (ndarray), the original image, changed into integers error: matrix (ndarray), a matrix of errors, so each entry is the difference between the integer predicted value and the actual value. Should be all integers A: matrix (3,3 ndarray), the matrix used to solve the MSE system """ 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_int = image.astype(int) A = np.array([[3,0,-1],[0,3,3],[1,-3,-4]]) # the matrix for system of equation z0 = image_int[0:-2,0:-2] # get all the first pixel for the entire image z1 = image_int[0:-2,1:-1] # get all the second pixel for the entire image z2 = image_int[0:-2,2::] # get all the third pixel for the entire image z3 = image_int[1:-1,0:-2] # get all the fourth 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] prediction = np.floor(np.linalg.solve(A,y)[-1]).astype(int) #predict = [] # flatten the neighbor pixels 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) diff = np.pad(diff.reshape(510,638), pad_width=1) #diff = np.pad(diff.reshape(510,638), pad_width=1) # flatten the image to a vector small_image = image_int[1:-1,1:-1] #Reshape the predictions to be a 2D array prediction = np.pad(prediction.reshape(510,638), pad_width=1) #Calculate the error between the original image and our predictions #Note that we only predicted on the inside square of the original image, excluding #The first row, column and last row, column #error = (image_int - predict).astype(int) #Experiment #this one works error = image_int - prediction return prediction, diff, image_int, error, A return prediction, diff, image_int, error[1:-1,1:-1], A ``` %% Cell type:code id:ba2881d9 tags: ``` python scenes = file_extractor() images = image_extractor(scenes) num_images = im_distribution(images, "11") ``` %% Cell type:code id:11e95c34 tags: ``` python prediction, diff, im, err, A = predict(images, 2) ``` %% Cell type:code id:434e4d2f tags: ``` python def reconstruct(error, A): """ Function that reconstructs the original image from the error matrix and using the predictive algorithm developed in the encoding. Parameters: error (array): matrix of errors computed in encoding. Same shape as the original image (512, 640) in this case A (array): Matrix used for the system of equations to create predictions Returns: image (array): The reconstructed image """ new_e = error.copy() rows, columns = new_e.shape for r in range(1, rows-1): #Iterate through the inside square of the error matrix for c in range(1, columns-1): z0, z1, z2, z3 = new_e[r-1][c-1], new_e[r-1][c], new_e[r-1][c+1], new_e[r][c-1] #Grab the four nearest pixels y = np.vstack((-z0+z2-z3, z0+z1+z2, -z0-z1-z2-z3)) #Create a vector of the linear combinations for the #solution to be solved new_e[r][c] = np.round(new_e[r][c] + np.linalg.solve(A,y)[-1], 1) #Add the error to the solved system solution #rounding the result because np.linalg.solve(A,y) #can be a float. Since we did np.floor on it in #prediction, we round to the nearest integer here return new_e.astype(int) ``` %% Cell type:code id:3cc609dc tags: ``` python new_error = reconstruct(err, A) ``` %% Cell type:code id:5d290a0c tags: ``` python im == new_error ``` %% Output array([[ True, True, True, ..., True, True, True], [ True, True, True, ..., True, True, True], [ True, True, True, ..., True, True, True], ..., [ True, True, True, ..., True, True, True], [ True, True, True, ..., True, True, True], [ True, True, True, ..., True, True, True]]) %% Cell type:code id:bb11dcd0 tags: ``` python class NodeTree(object): def __init__(self, left=None, right=None): self.left = left self.right = right def children(self): return self.left, self.right def __str__(self): return self.left, self.right def huffman_code_tree(node, binString=''): ''' Function to find Huffman Code ''' if type(node) is str: return {node: binString} (l, r) = node.children() d = dict() d.update(huffman_code_tree(l, binString + '0')) d.update(huffman_code_tree(r, binString + '1')) return d def make_tree(nodes): ''' Function to make tree :param nodes: Nodes :return: Root of the tree ''' while len(nodes) > 1: (key1, c1) = nodes[-1] (key2, c2) = nodes[-2] nodes = nodes[:-2] node = NodeTree(key1, key2) nodes.append((node, c1 + c2)) nodes = sorted(nodes, key=lambda x: x[1], reverse=True) return nodes[0][0] ``` %% Cell type:code id:c01fda28 tags: ``` python def encoder(images, i, plot=True): """ Function that creates Huffman encodings out of the error values for a given image. The encodings are more efficient ways to store large integer values that the original image contains. Parameters: images (list): list of file paths to the images that will be encoded. i (int): which index of the images list to grab and then encode. plot (bool): if true, this plots the error matrix to show the distribution of values. """ prediction, diff, original, error, A = predict(images, i) #Predict the values and return the error for the specified image image = original new_error = np.copy(image) #Create a new matrix that is a copy of the original image, this is the matrix we will #update on throughout #new_error[1:-1,1:-1] = np.reshape(error[1:-1,1:-1],(510, 638)) new_error[1:-1, 1:-1] = error[1:-1, 1:-1] #Set the inside of the updating matrix to be the same as the #error matrix retreived from predicting keep = new_error[0,0] #The top left entry stays the same new_error[0,:] = new_error[0,:] - keep #All edge pixels are set to be the difference between themselves and new_error[-1,:] = new_error[-1,:] - keep #the top left entry named "keep". This reduces their size to a more new_error[1:-1,0] = new_error[1:-1,0] - keep #manageable integer and makes them encodeable with the other error values new_error[1:-1,-1] = new_error[1:-1,-1] - keep new_error[0,0] = keep new_error = np.ravel(new_error) #Unravel it to plot it if plot: plt.hist(new_error[1:],bins=100) plt.show() string = [str(i) for i in new_error] #Create strings out of the integers in the new_error matrix freq = dict(Counter(string)) #Initialize a dictionary that maps integers to the string values freq = sorted(freq.items(), key=lambda x: x[1], reverse=True) #Create a frequency mapping of how often the string #values occur in the dictionary node = make_tree(freq) #Use the Huffman code given above to make a Huffman tree encoding_dict = huffman_code_tree(node) #Create the Huffman dictionary #encoded = ["1"+encoding[str(-i)] if i < 0 else "0"+encoding[str(i)] for i in error] encoded = new_error.reshape((512,640)).copy().astype(str).astype(object) #Reshape the error matrix and make a copy that #that is all strings so we can call the #dictionary on its entries for i in range(encoded.shape[0]): #Iterate through the string valued error dictionary for j in range(encoded.shape[1]): if i == 0 and j == 0: encoded[i][j] = encoded[i][j] #Replace each value in the dictionary with its encoding from the dictionary else: encoded[i][j] = encoding_dict[encoded[i][j]] return encoding_dict, encoded, new_error.reshape((512,640)), image #print(encoding) ``` %% Cell type:code id:ffa858e8 tags: ``` python encode_dict, encoding, error, orig_image = encoder(images, 2, plot=False) ``` %% Cell type:code id:825cc48c tags: ``` python def decoder(A, encoded_matrix, encoding_dict): """ Function that accecpts the prediction matrix A for the linear system, the encoded matrix of error values, and the encoding dicitonary. """ the_keys = list(encoding_dict.keys()) the_values = list(encoding_dict.values()) error_matrix = encoded_matrix.copy() for i in range(error_matrix.shape[0]): for j in range(error_matrix.shape[1]): if i == 0 and j == 0: error_matrix[i][j] = int(encoded_matrix[i][j]) elif i == 0 or i == error_matrix.shape[0]-1 or j == 0 or j == error_matrix.shape[1]-1: error_matrix[i][j] = int(the_keys[the_values.index(error_matrix[i,j])]) + error_matrix[0][0] else: """z0, z1, z2, z3 = error_matrix[i-1][j-1], error_matrix[i-1][j], \ error_matrix[i-1][j+1], error_matrix[i][j-1] y = np.vstack((-z0+z2-z3, z0+z1+z2, -z0-z1-z2-z3))""" error_matrix[i][j] = int(the_keys[the_values.index(error_matrix[i,j])]) return error_matrix.astype(int) ``` %% Cell type:code id:ba1d2c2c tags: ``` python em = decoder(A, encoding, encode_dict) ``` %% Cell type:code id:b2cdce6d tags: ``` python hopefully = reconstruct(em, A) #22487 22483 22521 22464 ``` %% Cell type:code id:285efcf0 tags: ``` python def test_decoder(): n = len(images)//12 fails = 0 for i in range(n): encode_dict1, encoding1, error1, orig_image1 = encoder(images, i, plot=False) new_error = decoder(A, encoding1, encode_dict1) reconstructed_image = reconstruct(new_error, A) if False in np.ravel(reconstructed_image == orig_image): fails += 0 return fails/n f = test_decoder() ``` %% Cell type:code id:30b1c87e tags: ``` python def entropy_func(images): entr = [] for i in range(len(images)): prediction, diff, im, err, A = predict(images, i) panda_im = pd.Series(np.ravel(im)) counts = panda_im.value_counts() entr.append(sp.stats.entropy(counts)) return entr e = entropy_func(images) ``` %% Cell type:code id:4c268907 tags: ``` python print(np.mean(e)) def huffman(image, i): pred, diff, origin, error, A = predict(image, i) pred = np.ravel(pred[1:-1, 1:-1]) error = np.ravel(error) boundary = np.hstack((origin[0,:],origin[-1,:],origin[1:-1,0],origin[1:-1,-1])) boundary = boundary - origin[0,0] boundary[0] = origin[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 <= 10 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 > 10 new_error = error[mask] mask2 = diff[mask] <= 25 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) encode3 = huffman_code_tree(node) mask = diff > 25 new_error = error[mask] mask2 = diff[mask] <= 45 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 > 45 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(origin) 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(origin), error, diff, boundary encode1, encode2, encode3, encode4, encode5, origin, error, diff, boundary = huffman(images, 0) ``` %% Cell type:code id:e98fc3cf tags: ``` python print(boundary) ``` %% Output [22541 -10 14 ... 62 151 208] %% Cell type:code id:f5e71acc tags: ``` python ``` %% Cell type:code id:642b95a3 tags: ``` python def compress_rate(image, error, diff, bound, encode1, encode2, encode3, encode4, encode5): #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] <= 10: c_len += len(encode2[str(int(error[i]))]) if diff[i] <= 25 and diff[i] > 10: c_len += len(encode3[str(int(error[i]))]) if diff[i] <= 45 and diff[i] > 25: c_len += len(encode4[str(int(error[i]))]) if diff[i] > 45: c_len += len(encode5[str(int(error[i]))]) return c_len/o_len compress_rate(origin, error, diff, boundary, encode1, encode2, encode3, encode4, encode5) ``` %% Output 0.4427516682942708 %% Cell type:code id:7d507cfb tags: ``` python def encode_multiple(error, diff, bound, encode1, encode2, encode3, encode4, encode5): #original = original.reshape(-1) #error = error.reshape(-1) original = len(np.ravel(error)) error = np.ravel(error) encode_error = error.astype(str).astype(object).copy() bound_error = bound.astype(str).astype(object).copy() for i in range(0,len(bound_error)): bound_error[i] = encode1[bound_error[i]] for i in range(0, original): if diff[i] <= 10: encode_error[i] = encode2[encode_error[i]] if diff[i] <= 25 and diff[i] > 10: encode_error[i] = encode3[encode_error[i]] if diff[i] <= 45 and diff[i] > 25: encode_error[i] = encode4[encode_error[i]] if diff[i] > 45: encode_error[i] = encode5[encode_error[i]] encode_error = np.pad(encode_error.reshape(510,638), pad_width=1) encode_error[0] = bound_error[:640] encode_error[-1] = bound_error[640:640*2] encode_error[1:-1,0] = bound_error[640*2:(640*2)+510] encode_error[1:-1,-1] = bound_error[(640*2)+510:] return encode_error, bound_error enc_mat, bound_e = encode_multiple(error, diff, boundary, encode1, encode2, encode3, encode4, encode5) ``` %% Cell type:code id:2faf5cd9 tags: ``` python def decode_multi(A, encoded_matrix, encode1, encode2, encode3, encode4, encode5, diff): """ Function that accecpts the prediction matrix A for the linear system, the encoded matrix of error values, and the encoding dicitonary. """ the_keys1 = list(encode1.keys()) the_values1 = list(encode1.values()) the_keys2 = list(encode2.keys()) the_values2 = list(encode2.values()) the_keys3 = list(encode3.keys()) the_values3 = list(encode3.values()) the_keys4 = list(encode4.keys()) the_values4 = list(encode4.values()) the_keys5 = list(encode5.keys()) the_values5 = list(encode5.values()) error_matrix = encoded_matrix.copy() for i in range(error_matrix.shape[0]): for j in range(error_matrix.shape[1]): if i == 0 and j == 0: error_matrix[i][j] = int(the_keys1[the_values1.index(encoded_matrix[i,j])]) elif i == 0 or i == error_matrix.shape[0]-1 or j == 0 or j == error_matrix.shape[1]-1: error_matrix[i][j] = int(the_keys1[the_values1.index(error_matrix[i,j])]) + int(error_matrix[0][0]) else: if diff[i*640 + j] <= 10: error_matrix[i][j] = int(the_keys2[the_values2.index(error_matrix[i,j])]) elif diff[i*640 + j] > 10 and diff[i*640 + j] <= 25: error_matrix[i,j] = int(the_keys3[the_values3.index(error_matrix[i,j])]) elif diff[i*640 + j] > 25 and diff[i*640 + j] <= 45: if error_matrix[i,j] == '101011': print(i,j) error_matrix[i,j] = int(the_keys4[the_values4.index(error_matrix[i,j])]) elif diff[i*640 + j] > 45: error_matrix[i,j] = int(the_keys5[the_values5.index(error_matrix[i,j])]) return error_matrix.astype(int) dec = decode_multi(A, enc_mat, encode1, encode2, encode3, encode4, encode5, diff) ``` %% Output 1 1 --------------------------------------------------------------------------- ValueError Traceback (most recent call last) ~\AppData\Local\Temp/ipykernel_1700/1235154671.py in <module> 42 return error_matrix.astype(int) 43 ---> 44 dec = decode_multi(A, enc_mat, encode1, encode2, encode3, encode4, encode5, diff) ~\AppData\Local\Temp/ipykernel_1700/1235154671.py in decode_multi(A, encoded_matrix, encode1, encode2, encode3, encode4, encode5, diff) 35 if error_matrix[i,j] == '101011': 36 print(i,j) ---> 37 error_matrix[i,j] = int(the_keys4[the_values4.index(error_matrix[i,j])]) 38 elif diff[i*640 + j] > 45: 39 error_matrix[i,j] = int(the_keys5[the_values5.index(error_matrix[i,j])]) ValueError: '101011' is not in list %% Cell type:code id:64832ca7 tags: ``` python print('101011' in enc_mat) ``` %% Output 6.7830123821108295 True Encoding_Kelly.ipynb +1 −2 Original line number Diff line number Diff line %% Cell type:code id:8868bc30 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,linprog import time import seaborn as sns from sklearn.neighbors import KernelDensity import pandas as pd from collections import Counter import time ``` %% Cell type:code id:76317b02 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:be1ff8a1 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) 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.floor(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]) error = image-predict return image, predict, diff, error, A ``` %% Cell type:code id:8483903e tags: ``` python class NodeTree(object): def __init__(self, left=None, right=None): self.left = left self.right = right def children(self): return self.left, self.right def __str__(self): return self.left, self.right def huffman_code_tree(node, binString=''): ''' Function to find Huffman Code ''' if type(node) is str: return {node: binString} (l, r) = node.children() d = dict() d.update(huffman_code_tree(l, binString + '0')) d.update(huffman_code_tree(r, binString + '1')) return d def make_tree(nodes): ''' Function to make tree :param nodes: Nodes :return: Root of the tree ''' while len(nodes) > 1: (key1, c1) = nodes[-1] (key2, c2) = nodes[-2] nodes = nodes[:-2] node = NodeTree(key1, key2) nodes.append((node, c1 + c2)) nodes = sorted(nodes, key=lambda x: x[1], reverse=True) return nodes[0][0] ``` %% Cell type:code id:64a3a193 tags: ``` python def reconstruct(error, A): """ Function that reconstructs the original image from the error matrix and using the predictive algorithm developed in the encoding. Parameters: error (array): matrix of errors computed in encoding. Same shape as the original image (512, 640) in this case A (array): Matrix used for the system of equations to create predictions Returns: image (array): The reconstructed image """ new_e = error.copy() rows, columns = new_e.shape for r in range(1, rows-1): for c in range(1, columns-1): z0, z1, z2, z3 = new_e[r-1][c-1], new_e[r-1][c], new_e[r-1][c+1], new_e[r][c-1] y = np.vstack((-z0+z2-z3, z0+z1+z2, -z0-z1-z2-z3)) '''if r == 345 and c == 421: print(new_e[r][c]) print(np.linalg.solve(A,y)[-1]) print(new_e[r][c] + np.linalg.solve(A,y)[-1]) print(np.ceil(new_e[r][c]) + np.floor(np.linalg.solve(A,y)[-1])) 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.floor(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''' #Real solution that works, DO NOT DELETE print(new_e[r][c]+ np.floor(np.linalg.solve(A,y)[-1])) new_e[r][c] = new_e[r][c] + np.floor(np.linalg.solve(A,y)[-1]) print(new_e[r][c]) #new_e[r][c] = np.ceil(new_e[r][c]) + np.floor(np.linalg.solve(A,y)[-1]) return new_e ``` %% Cell type:markdown id:c7104fbf tags: ### Huffman without dividing into bins %% Cell type:code id:a43f3f1c tags: ``` python scenes = file_extractor() images = image_extractor(scenes) def huffman_nb(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) 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) #ab_error = np.abs(new_error) #string = [str(i) for i in ab_error] string = [str(i) for i in new_error.astype(int)] freq = dict(Counter(string)) freq = sorted(freq.items(), key=lambda x: x[1], reverse=True) node = make_tree(freq) encoding = huffman_code_tree(node) #encoded = ["1"+encoding[str(-i)] if i < 0 else "0"+encoding[str(i)] for i in error] # return the huffman dictionary return encoding, new_error, image.reshape(-1) def compress_rate_nb(image, error, encoding): #original = original.reshape(-1) #error = error.reshape(-1) o_len = 0 c_len = 0 for i in range(0, len(original)): o_len += len(bin(original[i])[2:]) c_len += len(encoding[str(int(error[i]))]) return c_len/o_len encoding, error, image = huffman_nb(images[0]) print(compress_rate_nb(image, error, encoding)) ``` %% Output 0.444949904726781 %% Cell type:markdown id:eac2f456 tags: ### Huffman with dividing into non-uniform bins %% Cell type:code id:207b0bd2 tags: ``` python def huffman(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) 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 <= 25 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 > 25 new_error = error[mask] mask2 = diff[mask] <= 40 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) encode3 = huffman_code_tree(node) mask = diff > 40 new_error = error[mask] mask2 = diff[mask] <= 70 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 > 70 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 def compress_rate(image, error, diff, bound, encode1, encode2, encode3, encode4, encode5): #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] <= 25: c_len += len(encode2[str(int(error[i]))]) if diff[i] <= 40 and diff[i] > 25: c_len += len(encode3[str(int(error[i]))]) if diff[i] <= 70 and diff[i] > 40: c_len += len(encode4[str(int(error[i]))]) if diff[i] > 70: c_len += len(encode5[str(int(error[i]))]) return c_len/o_len scenes = file_extractor() images = image_extractor(scenes) encode1, encode2, encode3, encode4, encode5, image, error, diff, boundary = huffman(images[0]) compress_rate(image, error, diff, boundary, encode1, encode2, encode3, encode4, encode5) ``` %% Output 0.44205322265625 %% 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) 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 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 0.4432273356119792 %% Cell type:code id:f8b93cc5 tags: ``` python ``` %% Cell type:code id:6abed5da tags: ``` python scenes = file_extractor() images = image_extractor(scenes) num_images = im_distribution(images, "_9") rate = [] rate_nb = [] rate_u = [] for i in range(len(num_images)): encode1, encode2, encode3, encode4, encode5, image, error, diff, bound = huffman(num_images[i]) r = compress_rate(image, error, diff, bound, encode1, encode2, encode3, encode4, encode5) rate.append(r) encoding, error, image = huffman_nb(num_images[i]) r = compress_rate_nb(image, error, encoding) 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 without bins: {np.mean(rate_nb)}") print(f"Compression rate of huffman with uniform bins: {np.mean(rate_u)}") ``` %% Output Compression rate of huffman with different bins: 0.44946919759114584 Compression rate of huffman without bins: 0.4513634314749933 Compression rate of huffman with uniform bins: 0.44956921895345053 %% Cell type:code id:15eecad3 tags: ``` python def huffman(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) 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 <= 10 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 > 10 new_error = error[mask] mask2 = diff[mask] <= 25 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) encode3 = huffman_code_tree(node) mask = diff > 25 new_error = error[mask] mask2 = diff[mask] <= 45 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 > 45 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 def compress_rate(image, error, diff, bound, encode1, encode2, encode3, encode4, encode5): #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] <= 10: c_len += len(encode2[str(int(error[i]))]) if diff[i] <= 25 and diff[i] > 10: c_len += len(encode3[str(int(error[i]))]) if diff[i] <= 45 and diff[i] > 25: c_len += len(encode4[str(int(error[i]))]) if diff[i] > 45: c_len += len(encode5[str(int(error[i]))]) return c_len/o_len scenes = file_extractor() images = image_extractor(scenes) encode1, encode2, encode3, encode4, encode5, image, error, diff, boundary = huffman(images[0]) compress_rate(image, error, diff, boundary, encode1, encode2, encode3, encode4, encode5) ``` %% Output 0.44225341796875 0.4427516682942708 %% Cell type:code id:f8a8c717 tags: ``` python scenes = file_extractor() images = image_extractor(scenes) num_images = im_distribution(images, "_9") rate = [] for i in range(len(num_images)): encode1, encode2, encode3, encode4, encode5, image, error, diff, bound = huffman(num_images[i]) r = compress_rate(image, error, diff, bound, encode1, encode2, encode3, encode4, encode5) rate.append(r) print(f"Compression rate of huffman with different bins: {np.mean(rate)}") ``` %% Output Compression rate of huffman with different bins: 0.4488415273030599 %% Cell type:code id:992dd8bb tags: ``` python origin, predict, diff, error, A = plot_hist(images[0]) ``` %% Cell type:code id:904ba7b1 tags: ``` python plt.hist(error,bins=50) plt.show() mask = diff <= 20 plt.hist(error[mask],bins=50) plt.show() mask = diff > 20 new_error = error[mask] mask2 = diff[mask] <= 35 plt.hist(new_error[mask2],bins=50) plt.show() mask = diff > 35 new_error = error[mask] mask2 = diff[mask] <= 50 plt.hist(new_error[mask2],bins=50) plt.show() mask = diff > 50 #new_error = error[mask] #mask2 = diff[mask] <= 400 plt.hist(error[mask],bins=50) plt.show() ``` %% Output %% Cell type:code id:2f5ef010 tags: ``` python image = Image.open(images[0]) 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) 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] print(image.shape) ``` %% Output (512, 640) %% Cell type:code id:4860903b tags: ``` python print(boundary) ``` %% Output [22554 -2 -35 ... -16 40 19] %% Cell type:code id:f145c221 tags: ``` python ``` Loading
.ipynb_checkpoints/Error_to_Image-checkpoint.ipynb +239 −4 Original line number Diff line number Diff line %% Cell type:code id:dbef8759 tags: ``` python import numpy as np from prediction_MSE_Scout import file_extractor, image_extractor, im_distribution 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 from numpy import linalg as la from scipy.stats import gaussian_kde import seaborn as sns from collections import Counter import pandas as pd import scipy as sp ``` %% Cell type:code id:9ed20f84 tags: ``` python def predict(tiff_list, i=0): """ This function predicts the pixel values based on a linear combination of the MSE from the three pixels above it and the one to the left. It uses a system of equations to fit the plane ax + by + c and takes c as the prediction for the unknown pixel. It does this all at once by constructing vectors and matrices of the surrounding pixels and solving each system simultaneously so as not to iterate through each one. Parameters: tiff_list: list, list of names of image file paths to access. These should be strings in the form of a path to the image i: int, which index in the tiff_list of images we want to predict on Returns: prediction: matrix (ndarray), the matrix of predicted values for the image using the previous four piexels diff: matrix (ndarray), the difference between the highest and lowest valued surrounding four pixels image_int: matrix (ndarray), the original image, changed into integers error: matrix (ndarray), a matrix of errors, so each entry is the difference between the integer predicted value and the actual value. Should be all integers A: matrix (3,3 ndarray), the matrix used to solve the MSE system """ 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_int = image.astype(int) A = np.array([[3,0,-1],[0,3,3],[1,-3,-4]]) # the matrix for system of equation z0 = image_int[0:-2,0:-2] # get all the first pixel for the entire image z1 = image_int[0:-2,1:-1] # get all the second pixel for the entire image z2 = image_int[0:-2,2::] # get all the third pixel for the entire image z3 = image_int[1:-1,0:-2] # get all the fourth 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] prediction = np.floor(np.linalg.solve(A,y)[-1]).astype(int) #predict = [] # flatten the neighbor pixels 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) diff = np.pad(diff.reshape(510,638), pad_width=1) #diff = np.pad(diff.reshape(510,638), pad_width=1) # flatten the image to a vector small_image = image_int[1:-1,1:-1] #Reshape the predictions to be a 2D array prediction = np.pad(prediction.reshape(510,638), pad_width=1) #Calculate the error between the original image and our predictions #Note that we only predicted on the inside square of the original image, excluding #The first row, column and last row, column #error = (image_int - predict).astype(int) #Experiment #this one works error = image_int - prediction return prediction, diff, image_int, error, A return prediction, diff, image_int, error[1:-1,1:-1], A ``` %% Cell type:code id:ba2881d9 tags: ``` python scenes = file_extractor() images = image_extractor(scenes) num_images = im_distribution(images, "11") ``` %% Cell type:code id:11e95c34 tags: ``` python prediction, diff, im, err, A = predict(images, 2) ``` %% Cell type:code id:434e4d2f tags: ``` python def reconstruct(error, A): """ Function that reconstructs the original image from the error matrix and using the predictive algorithm developed in the encoding. Parameters: error (array): matrix of errors computed in encoding. Same shape as the original image (512, 640) in this case A (array): Matrix used for the system of equations to create predictions Returns: image (array): The reconstructed image """ new_e = error.copy() rows, columns = new_e.shape for r in range(1, rows-1): #Iterate through the inside square of the error matrix for c in range(1, columns-1): z0, z1, z2, z3 = new_e[r-1][c-1], new_e[r-1][c], new_e[r-1][c+1], new_e[r][c-1] #Grab the four nearest pixels y = np.vstack((-z0+z2-z3, z0+z1+z2, -z0-z1-z2-z3)) #Create a vector of the linear combinations for the #solution to be solved new_e[r][c] = np.round(new_e[r][c] + np.linalg.solve(A,y)[-1], 1) #Add the error to the solved system solution #rounding the result because np.linalg.solve(A,y) #can be a float. Since we did np.floor on it in #prediction, we round to the nearest integer here return new_e.astype(int) ``` %% Cell type:code id:3cc609dc tags: ``` python new_error = reconstruct(err, A) ``` %% Cell type:code id:5d290a0c tags: ``` python im == new_error ``` %% Output array([[ True, True, True, ..., True, True, True], [ True, True, True, ..., True, True, True], [ True, True, True, ..., True, True, True], ..., [ True, True, True, ..., True, True, True], [ True, True, True, ..., True, True, True], [ True, True, True, ..., True, True, True]]) %% Cell type:code id:bb11dcd0 tags: ``` python class NodeTree(object): def __init__(self, left=None, right=None): self.left = left self.right = right def children(self): return self.left, self.right def __str__(self): return self.left, self.right def huffman_code_tree(node, binString=''): ''' Function to find Huffman Code ''' if type(node) is str: return {node: binString} (l, r) = node.children() d = dict() d.update(huffman_code_tree(l, binString + '0')) d.update(huffman_code_tree(r, binString + '1')) return d def make_tree(nodes): ''' Function to make tree :param nodes: Nodes :return: Root of the tree ''' while len(nodes) > 1: (key1, c1) = nodes[-1] (key2, c2) = nodes[-2] nodes = nodes[:-2] node = NodeTree(key1, key2) nodes.append((node, c1 + c2)) nodes = sorted(nodes, key=lambda x: x[1], reverse=True) return nodes[0][0] ``` %% Cell type:code id:c01fda28 tags: ``` python def encoder(images, i, plot=True): """ Function that creates Huffman encodings out of the error values for a given image. The encodings are more efficient ways to store large integer values that the original image contains. Parameters: images (list): list of file paths to the images that will be encoded. i (int): which index of the images list to grab and then encode. plot (bool): if true, this plots the error matrix to show the distribution of values. """ prediction, diff, original, error, A = predict(images, i) #Predict the values and return the error for the specified image image = original new_error = np.copy(image) #Create a new matrix that is a copy of the original image, this is the matrix we will #update on throughout #new_error[1:-1,1:-1] = np.reshape(error[1:-1,1:-1],(510, 638)) new_error[1:-1, 1:-1] = error[1:-1, 1:-1] #Set the inside of the updating matrix to be the same as the #error matrix retreived from predicting keep = new_error[0,0] #The top left entry stays the same new_error[0,:] = new_error[0,:] - keep #All edge pixels are set to be the difference between themselves and new_error[-1,:] = new_error[-1,:] - keep #the top left entry named "keep". This reduces their size to a more new_error[1:-1,0] = new_error[1:-1,0] - keep #manageable integer and makes them encodeable with the other error values new_error[1:-1,-1] = new_error[1:-1,-1] - keep new_error[0,0] = keep new_error = np.ravel(new_error) #Unravel it to plot it if plot: plt.hist(new_error[1:],bins=100) plt.show() string = [str(i) for i in new_error] #Create strings out of the integers in the new_error matrix freq = dict(Counter(string)) #Initialize a dictionary that maps integers to the string values freq = sorted(freq.items(), key=lambda x: x[1], reverse=True) #Create a frequency mapping of how often the string #values occur in the dictionary node = make_tree(freq) #Use the Huffman code given above to make a Huffman tree encoding_dict = huffman_code_tree(node) #Create the Huffman dictionary #encoded = ["1"+encoding[str(-i)] if i < 0 else "0"+encoding[str(i)] for i in error] encoded = new_error.reshape((512,640)).copy().astype(str).astype(object) #Reshape the error matrix and make a copy that #that is all strings so we can call the #dictionary on its entries for i in range(encoded.shape[0]): #Iterate through the string valued error dictionary for j in range(encoded.shape[1]): if i == 0 and j == 0: encoded[i][j] = encoded[i][j] #Replace each value in the dictionary with its encoding from the dictionary else: encoded[i][j] = encoding_dict[encoded[i][j]] return encoding_dict, encoded, new_error.reshape((512,640)), image #print(encoding) ``` %% Cell type:code id:ffa858e8 tags: ``` python encode_dict, encoding, error, orig_image = encoder(images, 2, plot=False) ``` %% Cell type:code id:825cc48c tags: ``` python def decoder(A, encoded_matrix, encoding_dict): """ Function that accecpts the prediction matrix A for the linear system, the encoded matrix of error values, and the encoding dicitonary. """ the_keys = list(encoding_dict.keys()) the_values = list(encoding_dict.values()) error_matrix = encoded_matrix.copy() for i in range(error_matrix.shape[0]): for j in range(error_matrix.shape[1]): if i == 0 and j == 0: error_matrix[i][j] = int(encoded_matrix[i][j]) elif i == 0 or i == error_matrix.shape[0]-1 or j == 0 or j == error_matrix.shape[1]-1: error_matrix[i][j] = int(the_keys[the_values.index(error_matrix[i,j])]) + error_matrix[0][0] else: """z0, z1, z2, z3 = error_matrix[i-1][j-1], error_matrix[i-1][j], \ error_matrix[i-1][j+1], error_matrix[i][j-1] y = np.vstack((-z0+z2-z3, z0+z1+z2, -z0-z1-z2-z3))""" error_matrix[i][j] = int(the_keys[the_values.index(error_matrix[i,j])]) return error_matrix.astype(int) ``` %% Cell type:code id:ba1d2c2c tags: ``` python em = decoder(A, encoding, encode_dict) ``` %% Cell type:code id:b2cdce6d tags: ``` python hopefully = reconstruct(em, A) #22487 22483 22521 22464 ``` %% Cell type:code id:285efcf0 tags: ``` python def test_decoder(): n = len(images)//12 fails = 0 for i in range(n): encode_dict1, encoding1, error1, orig_image1 = encoder(images, i, plot=False) new_error = decoder(A, encoding1, encode_dict1) reconstructed_image = reconstruct(new_error, A) if False in np.ravel(reconstructed_image == orig_image): fails += 0 return fails/n f = test_decoder() ``` %% Cell type:code id:30b1c87e tags: ``` python def entropy_func(images): entr = [] for i in range(len(images)): prediction, diff, im, err, A = predict(images, i) panda_im = pd.Series(np.ravel(im)) counts = panda_im.value_counts() entr.append(sp.stats.entropy(counts)) return entr e = entropy_func(images) ``` %% Cell type:code id:4c268907 tags: ``` python print(np.mean(e)) def huffman(image, i): pred, diff, origin, error, A = predict(image, i) pred = np.ravel(pred[1:-1, 1:-1]) error = np.ravel(error) boundary = np.hstack((origin[0,:],origin[-1,:],origin[1:-1,0],origin[1:-1,-1])) boundary = boundary - origin[0,0] boundary[0] = origin[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 <= 10 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 > 10 new_error = error[mask] mask2 = diff[mask] <= 25 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) encode3 = huffman_code_tree(node) mask = diff > 25 new_error = error[mask] mask2 = diff[mask] <= 45 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 > 45 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(origin) 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(origin), error, diff, boundary encode1, encode2, encode3, encode4, encode5, origin, error, diff, boundary = huffman(images, 0) ``` %% Cell type:code id:e98fc3cf tags: ``` python print(boundary) ``` %% Output [22541 -10 14 ... 62 151 208] %% Cell type:code id:f5e71acc tags: ``` python ``` %% Cell type:code id:642b95a3 tags: ``` python def compress_rate(image, error, diff, bound, encode1, encode2, encode3, encode4, encode5): #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] <= 10: c_len += len(encode2[str(int(error[i]))]) if diff[i] <= 25 and diff[i] > 10: c_len += len(encode3[str(int(error[i]))]) if diff[i] <= 45 and diff[i] > 25: c_len += len(encode4[str(int(error[i]))]) if diff[i] > 45: c_len += len(encode5[str(int(error[i]))]) return c_len/o_len compress_rate(origin, error, diff, boundary, encode1, encode2, encode3, encode4, encode5) ``` %% Output 0.4427516682942708 %% Cell type:code id:7d507cfb tags: ``` python def encode_multiple(error, diff, bound, encode1, encode2, encode3, encode4, encode5): #original = original.reshape(-1) #error = error.reshape(-1) original = len(np.ravel(error)) error = np.ravel(error) encode_error = error.astype(str).astype(object).copy() bound_error = bound.astype(str).astype(object).copy() for i in range(0,len(bound_error)): bound_error[i] = encode1[bound_error[i]] for i in range(0, original): if diff[i] <= 10: encode_error[i] = encode2[encode_error[i]] if diff[i] <= 25 and diff[i] > 10: encode_error[i] = encode3[encode_error[i]] if diff[i] <= 45 and diff[i] > 25: encode_error[i] = encode4[encode_error[i]] if diff[i] > 45: encode_error[i] = encode5[encode_error[i]] encode_error = np.pad(encode_error.reshape(510,638), pad_width=1) encode_error[0] = bound_error[:640] encode_error[-1] = bound_error[640:640*2] encode_error[1:-1,0] = bound_error[640*2:(640*2)+510] encode_error[1:-1,-1] = bound_error[(640*2)+510:] return encode_error, bound_error enc_mat, bound_e = encode_multiple(error, diff, boundary, encode1, encode2, encode3, encode4, encode5) ``` %% Cell type:code id:2faf5cd9 tags: ``` python def decode_multi(A, encoded_matrix, encode1, encode2, encode3, encode4, encode5, diff): """ Function that accecpts the prediction matrix A for the linear system, the encoded matrix of error values, and the encoding dicitonary. """ the_keys1 = list(encode1.keys()) the_values1 = list(encode1.values()) the_keys2 = list(encode2.keys()) the_values2 = list(encode2.values()) the_keys3 = list(encode3.keys()) the_values3 = list(encode3.values()) the_keys4 = list(encode4.keys()) the_values4 = list(encode4.values()) the_keys5 = list(encode5.keys()) the_values5 = list(encode5.values()) error_matrix = encoded_matrix.copy() for i in range(error_matrix.shape[0]): for j in range(error_matrix.shape[1]): if i == 0 and j == 0: error_matrix[i][j] = int(the_keys1[the_values1.index(encoded_matrix[i,j])]) elif i == 0 or i == error_matrix.shape[0]-1 or j == 0 or j == error_matrix.shape[1]-1: error_matrix[i][j] = int(the_keys1[the_values1.index(error_matrix[i,j])]) + int(error_matrix[0][0]) else: if diff[i*640 + j] <= 10: error_matrix[i][j] = int(the_keys2[the_values2.index(error_matrix[i,j])]) elif diff[i*640 + j] > 10 and diff[i*640 + j] <= 25: error_matrix[i,j] = int(the_keys3[the_values3.index(error_matrix[i,j])]) elif diff[i*640 + j] > 25 and diff[i*640 + j] <= 45: if error_matrix[i,j] == '101011': print(i,j) error_matrix[i,j] = int(the_keys4[the_values4.index(error_matrix[i,j])]) elif diff[i*640 + j] > 45: error_matrix[i,j] = int(the_keys5[the_values5.index(error_matrix[i,j])]) return error_matrix.astype(int) dec = decode_multi(A, enc_mat, encode1, encode2, encode3, encode4, encode5, diff) ``` %% Output 1 1 --------------------------------------------------------------------------- ValueError Traceback (most recent call last) ~\AppData\Local\Temp/ipykernel_1700/1235154671.py in <module> 42 return error_matrix.astype(int) 43 ---> 44 dec = decode_multi(A, enc_mat, encode1, encode2, encode3, encode4, encode5, diff) ~\AppData\Local\Temp/ipykernel_1700/1235154671.py in decode_multi(A, encoded_matrix, encode1, encode2, encode3, encode4, encode5, diff) 35 if error_matrix[i,j] == '101011': 36 print(i,j) ---> 37 error_matrix[i,j] = int(the_keys4[the_values4.index(error_matrix[i,j])]) 38 elif diff[i*640 + j] > 45: 39 error_matrix[i,j] = int(the_keys5[the_values5.index(error_matrix[i,j])]) ValueError: '101011' is not in list %% Cell type:code id:64832ca7 tags: ``` python print('101011' in enc_mat) ``` %% Output 6.7830123821108295 True
Encoding_Kelly.ipynb +1 −2 Original line number Diff line number Diff line %% Cell type:code id:8868bc30 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,linprog import time import seaborn as sns from sklearn.neighbors import KernelDensity import pandas as pd from collections import Counter import time ``` %% Cell type:code id:76317b02 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:be1ff8a1 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) 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.floor(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]) error = image-predict return image, predict, diff, error, A ``` %% Cell type:code id:8483903e tags: ``` python class NodeTree(object): def __init__(self, left=None, right=None): self.left = left self.right = right def children(self): return self.left, self.right def __str__(self): return self.left, self.right def huffman_code_tree(node, binString=''): ''' Function to find Huffman Code ''' if type(node) is str: return {node: binString} (l, r) = node.children() d = dict() d.update(huffman_code_tree(l, binString + '0')) d.update(huffman_code_tree(r, binString + '1')) return d def make_tree(nodes): ''' Function to make tree :param nodes: Nodes :return: Root of the tree ''' while len(nodes) > 1: (key1, c1) = nodes[-1] (key2, c2) = nodes[-2] nodes = nodes[:-2] node = NodeTree(key1, key2) nodes.append((node, c1 + c2)) nodes = sorted(nodes, key=lambda x: x[1], reverse=True) return nodes[0][0] ``` %% Cell type:code id:64a3a193 tags: ``` python def reconstruct(error, A): """ Function that reconstructs the original image from the error matrix and using the predictive algorithm developed in the encoding. Parameters: error (array): matrix of errors computed in encoding. Same shape as the original image (512, 640) in this case A (array): Matrix used for the system of equations to create predictions Returns: image (array): The reconstructed image """ new_e = error.copy() rows, columns = new_e.shape for r in range(1, rows-1): for c in range(1, columns-1): z0, z1, z2, z3 = new_e[r-1][c-1], new_e[r-1][c], new_e[r-1][c+1], new_e[r][c-1] y = np.vstack((-z0+z2-z3, z0+z1+z2, -z0-z1-z2-z3)) '''if r == 345 and c == 421: print(new_e[r][c]) print(np.linalg.solve(A,y)[-1]) print(new_e[r][c] + np.linalg.solve(A,y)[-1]) print(np.ceil(new_e[r][c]) + np.floor(np.linalg.solve(A,y)[-1])) 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.floor(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''' #Real solution that works, DO NOT DELETE print(new_e[r][c]+ np.floor(np.linalg.solve(A,y)[-1])) new_e[r][c] = new_e[r][c] + np.floor(np.linalg.solve(A,y)[-1]) print(new_e[r][c]) #new_e[r][c] = np.ceil(new_e[r][c]) + np.floor(np.linalg.solve(A,y)[-1]) return new_e ``` %% Cell type:markdown id:c7104fbf tags: ### Huffman without dividing into bins %% Cell type:code id:a43f3f1c tags: ``` python scenes = file_extractor() images = image_extractor(scenes) def huffman_nb(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) 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) #ab_error = np.abs(new_error) #string = [str(i) for i in ab_error] string = [str(i) for i in new_error.astype(int)] freq = dict(Counter(string)) freq = sorted(freq.items(), key=lambda x: x[1], reverse=True) node = make_tree(freq) encoding = huffman_code_tree(node) #encoded = ["1"+encoding[str(-i)] if i < 0 else "0"+encoding[str(i)] for i in error] # return the huffman dictionary return encoding, new_error, image.reshape(-1) def compress_rate_nb(image, error, encoding): #original = original.reshape(-1) #error = error.reshape(-1) o_len = 0 c_len = 0 for i in range(0, len(original)): o_len += len(bin(original[i])[2:]) c_len += len(encoding[str(int(error[i]))]) return c_len/o_len encoding, error, image = huffman_nb(images[0]) print(compress_rate_nb(image, error, encoding)) ``` %% Output 0.444949904726781 %% Cell type:markdown id:eac2f456 tags: ### Huffman with dividing into non-uniform bins %% Cell type:code id:207b0bd2 tags: ``` python def huffman(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) 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 <= 25 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 > 25 new_error = error[mask] mask2 = diff[mask] <= 40 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) encode3 = huffman_code_tree(node) mask = diff > 40 new_error = error[mask] mask2 = diff[mask] <= 70 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 > 70 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 def compress_rate(image, error, diff, bound, encode1, encode2, encode3, encode4, encode5): #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] <= 25: c_len += len(encode2[str(int(error[i]))]) if diff[i] <= 40 and diff[i] > 25: c_len += len(encode3[str(int(error[i]))]) if diff[i] <= 70 and diff[i] > 40: c_len += len(encode4[str(int(error[i]))]) if diff[i] > 70: c_len += len(encode5[str(int(error[i]))]) return c_len/o_len scenes = file_extractor() images = image_extractor(scenes) encode1, encode2, encode3, encode4, encode5, image, error, diff, boundary = huffman(images[0]) compress_rate(image, error, diff, boundary, encode1, encode2, encode3, encode4, encode5) ``` %% Output 0.44205322265625 %% 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) 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 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 0.4432273356119792 %% Cell type:code id:f8b93cc5 tags: ``` python ``` %% Cell type:code id:6abed5da tags: ``` python scenes = file_extractor() images = image_extractor(scenes) num_images = im_distribution(images, "_9") rate = [] rate_nb = [] rate_u = [] for i in range(len(num_images)): encode1, encode2, encode3, encode4, encode5, image, error, diff, bound = huffman(num_images[i]) r = compress_rate(image, error, diff, bound, encode1, encode2, encode3, encode4, encode5) rate.append(r) encoding, error, image = huffman_nb(num_images[i]) r = compress_rate_nb(image, error, encoding) 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 without bins: {np.mean(rate_nb)}") print(f"Compression rate of huffman with uniform bins: {np.mean(rate_u)}") ``` %% Output Compression rate of huffman with different bins: 0.44946919759114584 Compression rate of huffman without bins: 0.4513634314749933 Compression rate of huffman with uniform bins: 0.44956921895345053 %% Cell type:code id:15eecad3 tags: ``` python def huffman(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) 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 <= 10 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 > 10 new_error = error[mask] mask2 = diff[mask] <= 25 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) encode3 = huffman_code_tree(node) mask = diff > 25 new_error = error[mask] mask2 = diff[mask] <= 45 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 > 45 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 def compress_rate(image, error, diff, bound, encode1, encode2, encode3, encode4, encode5): #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] <= 10: c_len += len(encode2[str(int(error[i]))]) if diff[i] <= 25 and diff[i] > 10: c_len += len(encode3[str(int(error[i]))]) if diff[i] <= 45 and diff[i] > 25: c_len += len(encode4[str(int(error[i]))]) if diff[i] > 45: c_len += len(encode5[str(int(error[i]))]) return c_len/o_len scenes = file_extractor() images = image_extractor(scenes) encode1, encode2, encode3, encode4, encode5, image, error, diff, boundary = huffman(images[0]) compress_rate(image, error, diff, boundary, encode1, encode2, encode3, encode4, encode5) ``` %% Output 0.44225341796875 0.4427516682942708 %% Cell type:code id:f8a8c717 tags: ``` python scenes = file_extractor() images = image_extractor(scenes) num_images = im_distribution(images, "_9") rate = [] for i in range(len(num_images)): encode1, encode2, encode3, encode4, encode5, image, error, diff, bound = huffman(num_images[i]) r = compress_rate(image, error, diff, bound, encode1, encode2, encode3, encode4, encode5) rate.append(r) print(f"Compression rate of huffman with different bins: {np.mean(rate)}") ``` %% Output Compression rate of huffman with different bins: 0.4488415273030599 %% Cell type:code id:992dd8bb tags: ``` python origin, predict, diff, error, A = plot_hist(images[0]) ``` %% Cell type:code id:904ba7b1 tags: ``` python plt.hist(error,bins=50) plt.show() mask = diff <= 20 plt.hist(error[mask],bins=50) plt.show() mask = diff > 20 new_error = error[mask] mask2 = diff[mask] <= 35 plt.hist(new_error[mask2],bins=50) plt.show() mask = diff > 35 new_error = error[mask] mask2 = diff[mask] <= 50 plt.hist(new_error[mask2],bins=50) plt.show() mask = diff > 50 #new_error = error[mask] #mask2 = diff[mask] <= 400 plt.hist(error[mask],bins=50) plt.show() ``` %% Output %% Cell type:code id:2f5ef010 tags: ``` python image = Image.open(images[0]) 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) 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] print(image.shape) ``` %% Output (512, 640) %% Cell type:code id:4860903b tags: ``` python print(boundary) ``` %% Output [22554 -2 -35 ... -16 40 19] %% Cell type:code id:f145c221 tags: ``` python ```