Commit 1991ccfc authored by Kelly Chang's avatar Kelly Chang
parents 3e116aa0 27b7f6c2
{
"cells": [
{
"cell_type": "code",
"execution_count": 130,
"id": "dbef8759",
"metadata": {
"id": "dbef8759"
},
"outputs": [],
"source": [
"import numpy as np\n",
"from prediction_MSE_Scout import file_extractor, image_extractor, im_distribution\n",
"from matplotlib import pyplot as plt\n",
"from itertools import product\n",
"import os\n",
"import sys\n",
"from PIL import Image\n",
"from scipy.optimize import minimize\n",
"from time import time\n",
"from numpy import linalg as la\n",
"from scipy.stats import gaussian_kde\n",
"import seaborn as sns\n",
"import pywt"
]
},
{
"cell_type": "code",
"execution_count": 364,
"id": "9ed20f84",
"metadata": {
"id": "9ed20f84"
},
"outputs": [],
"source": [
"def plot_hist(tiff_list, i=0):\n",
" \"\"\"\n",
" This function is the leftovers from the first attempt to plot histograms.\n",
" As it stands it needs some work in order to function again. We will\n",
" fix this later. 1/25/22\n",
" \"\"\"\n",
" \n",
" image = tiff_list[i]\n",
" image = Image.open(image) #Open the image and read it as an Image object\n",
" image = np.array(image)[1:,:] #Convert to an array, leaving out the first row because the first row is just housekeeping data\n",
" image_int = image.astype(np.int_)\n",
" \n",
" A = np.array([[3,0,-1],[0,3,3],[1,-3,-4]]) # the matrix for system of equation\n",
" \n",
" z0 = image_int[0:-2,0:-2] # get all the first pixel for the entire image\n",
" z1 = image_int[0:-2,1:-1] # get all the second pixel for the entire image\n",
" z2 = image_int[0:-2,2::] # get all the third pixel for the entire image\n",
" z3 = image_int[1:-1,0:-2] # get all the fourth pixel for the entire image\n",
" \n",
" # calculate the out put of the system of equation\n",
" y0 = np.ravel(-z0+z2-z3)\n",
" y1 = np.ravel(z0+z1+z2)\n",
" y2 = np.ravel(-z0-z1-z2-z3)\n",
" y = np.vstack((y0,y1,y2))\n",
" \n",
" # use numpy solver to solve the system of equations all at once\n",
" predict = np.linalg.solve(A,y)[-1]\n",
" #predict = []\n",
" \n",
" # flatten the neighbor pixels and stack them together\n",
" z0 = np.ravel(z0)\n",
" z1 = np.ravel(z1)\n",
" z2 = np.ravel(z2)\n",
" z3 = np.ravel(z3)\n",
" neighbor = np.vstack((z0,z1,z2,z3)).T\n",
" \n",
" # calculate the difference\n",
" diff = np.max(neighbor,axis = 1) - np.min(neighbor, axis=1)\n",
" \n",
" \n",
" # flatten the image to a vector\n",
" small_image = image_int[1:-1,1:-1]\n",
" \n",
" #Reshape the predictions to be a 2D array\n",
" predict = np.pad(predict.reshape(510,638), pad_width=1)\n",
" \"\"\"predict[0,:] = image[0,:]\n",
" predict[:,0] = image[:,0]\n",
" predict[:,-1] = image[:,-1]\n",
" predict[-1,:] = image[-1,:]\"\"\"\n",
" \n",
" \n",
" #Calculate the error between the original image and our predictions\n",
" #Note that we only predicted on the inside square of the original image, excluding\n",
" #The first row, column and last row, column\n",
" #error = (image_int - predict).astype(int) #Experiment\n",
" \n",
" #this one works\n",
" error = image_int - predict\n",
"\n",
" \n",
" return predict, diff, image_int, error, A"
]
},
{
"cell_type": "code",
"execution_count": 202,
"id": "ba2881d9",
"metadata": {},
"outputs": [],
"source": [
"scenes = file_extractor()\n",
"images = image_extractor(scenes)\n",
"num_images = im_distribution(images, \"_9\")"
]
},
{
"cell_type": "code",
"execution_count": 365,
"id": "11e95c34",
"metadata": {},
"outputs": [],
"source": [
"predict, diff, im, err, A = plot_hist(num_images, 0)"
]
},
{
"cell_type": "code",
"execution_count": 402,
"id": "434e4d2f",
"metadata": {},
"outputs": [],
"source": [
"def reconstruct(error, A):\n",
" \"\"\"\n",
" Function that reconstructs the original image\n",
" from the error matrix and using the predictive\n",
" algorithm developed in the encoding.\n",
" \n",
" Parameters:\n",
" error (array): matrix of errors computed in encoding. Same \n",
" shape as the original image (512, 640) in this case\n",
" A (array): Matrix used for the system of equations to create predictions\n",
" Returns: \n",
" image (array): The reconstructed image\n",
" \"\"\"\n",
" new_e = error.copy()\n",
" rows, columns = new_e.shape\n",
"\n",
" for r in range(1, rows-1):\n",
" for c in range(1, columns-1):\n",
" 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]\n",
" y = np.vstack((-z0+z2-z3, z0+z1+z2, -z0-z1-z2-z3))\n",
"\n",
" if r == 345 and c == 421:\n",
" print(new_e[r][c])\n",
" print(np.linalg.solve(A,y)[-1])\n",
" print(new_e[r][c] + np.linalg.solve(A,y)[-1])\n",
" print(np.ceil(new_e[r][c]) + np.floor(np.linalg.solve(A,y)[-1]))\n",
" \n",
" #Real solution that works, DO NOT DELETE\n",
" new_e[r][c] = int(np.ceil(new_e[r][c] + np.linalg.solve(A,y)[-1])) \n",
" \n",
" #new_e[r][c] = np.ceil(new_e[r][c]) + np.floor(np.linalg.solve(A,y)[-1])\n",
" \n",
" return new_e.astype(int)\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 403,
"id": "7f395ab2",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3.499999999992724\n",
"[13644.5]\n",
"[13648.]\n",
"[13648.]\n"
]
}
],
"source": [
"new_error = reconstruct(err, A)"
]
},
{
"cell_type": "code",
"execution_count": 420,
"id": "06ccaf8e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"518"
]
},
"execution_count": 420,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"e = np.round(err, 1)\n",
"len(np.unique(e[1:-1, 1:-1]))"
]
}
],
"metadata": {
"colab": {
"collapsed_sections": [],
"name": "Wavelet_Huffman.ipynb",
"provenance": []
},
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.11"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
This diff is collapsed.
{
"cells": [
{
"cell_type": "code",
"execution_count": 130,
"id": "dbef8759",
"metadata": {
"id": "dbef8759"
},
"outputs": [],
"source": [
"import numpy as np\n",
"from prediction_MSE_Scout import file_extractor, image_extractor, im_distribution\n",
"from matplotlib import pyplot as plt\n",
"from itertools import product\n",
"import os\n",
"import sys\n",
"from PIL import Image\n",
"from scipy.optimize import minimize\n",
"from time import time\n",
"from numpy import linalg as la\n",
"from scipy.stats import gaussian_kde\n",
"import seaborn as sns\n",
"import pywt"
]
},
{
"cell_type": "code",
"execution_count": 364,
"id": "9ed20f84",
"metadata": {
"id": "9ed20f84"
},
"outputs": [],
"source": [
"def plot_hist(tiff_list, i=0):\n",
" \"\"\"\n",
" This function is the leftovers from the first attempt to plot histograms.\n",
" As it stands it needs some work in order to function again. We will\n",
" fix this later. 1/25/22\n",
" \"\"\"\n",
" \n",
" image = tiff_list[i]\n",
" image = Image.open(image) #Open the image and read it as an Image object\n",
" image = np.array(image)[1:,:] #Convert to an array, leaving out the first row because the first row is just housekeeping data\n",
" image_int = image.astype(np.int_)\n",
" \n",
" A = np.array([[3,0,-1],[0,3,3],[1,-3,-4]]) # the matrix for system of equation\n",
" \n",
" z0 = image_int[0:-2,0:-2] # get all the first pixel for the entire image\n",
" z1 = image_int[0:-2,1:-1] # get all the second pixel for the entire image\n",
" z2 = image_int[0:-2,2::] # get all the third pixel for the entire image\n",
" z3 = image_int[1:-1,0:-2] # get all the fourth pixel for the entire image\n",
" \n",
" # calculate the out put of the system of equation\n",
" y0 = np.ravel(-z0+z2-z3)\n",
" y1 = np.ravel(z0+z1+z2)\n",
" y2 = np.ravel(-z0-z1-z2-z3)\n",
" y = np.vstack((y0,y1,y2))\n",
" \n",
" # use numpy solver to solve the system of equations all at once\n",
" predict = np.linalg.solve(A,y)[-1]\n",
" #predict = []\n",
" \n",
" # flatten the neighbor pixels and stack them together\n",
" z0 = np.ravel(z0)\n",
" z1 = np.ravel(z1)\n",
" z2 = np.ravel(z2)\n",
" z3 = np.ravel(z3)\n",
" neighbor = np.vstack((z0,z1,z2,z3)).T\n",
" \n",
" # calculate the difference\n",
" diff = np.max(neighbor,axis = 1) - np.min(neighbor, axis=1)\n",
" \n",
" \n",
" # flatten the image to a vector\n",
" small_image = image_int[1:-1,1:-1]\n",
" \n",
" #Reshape the predictions to be a 2D array\n",
" predict = np.pad(predict.reshape(510,638), pad_width=1)\n",
" \"\"\"predict[0,:] = image[0,:]\n",
" predict[:,0] = image[:,0]\n",
" predict[:,-1] = image[:,-1]\n",
" predict[-1,:] = image[-1,:]\"\"\"\n",
" \n",
" \n",
" #Calculate the error between the original image and our predictions\n",
" #Note that we only predicted on the inside square of the original image, excluding\n",
" #The first row, column and last row, column\n",
" #error = (image_int - predict).astype(int) #Experiment\n",
" \n",
" #this one works\n",
" error = image_int - predict\n",
"\n",
" \n",
" return predict, diff, image_int, error, A"
]
},
{
"cell_type": "code",
"execution_count": 202,
"id": "ba2881d9",
"metadata": {},
"outputs": [],
"source": [
"scenes = file_extractor()\n",
"images = image_extractor(scenes)\n",
"num_images = im_distribution(images, \"_9\")"
]
},
{
"cell_type": "code",
"execution_count": 365,
"id": "11e95c34",
"metadata": {},
"outputs": [],
"source": [
"predict, diff, im, err, A = plot_hist(num_images, 0)"
]
},
{
"cell_type": "code",
"execution_count": 402,
"id": "434e4d2f",
"metadata": {},
"outputs": [],
"source": [
"def reconstruct(error, A):\n",
" \"\"\"\n",
" Function that reconstructs the original image\n",
" from the error matrix and using the predictive\n",
" algorithm developed in the encoding.\n",
" \n",
" Parameters:\n",
" error (array): matrix of errors computed in encoding. Same \n",
" shape as the original image (512, 640) in this case\n",
" A (array): Matrix used for the system of equations to create predictions\n",
" Returns: \n",
" image (array): The reconstructed image\n",
" \"\"\"\n",
" new_e = error.copy()\n",
" rows, columns = new_e.shape\n",
"\n",
" for r in range(1, rows-1):\n",
" for c in range(1, columns-1):\n",
" 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]\n",
" y = np.vstack((-z0+z2-z3, z0+z1+z2, -z0-z1-z2-z3))\n",
"\n",
" if r == 345 and c == 421:\n",
" print(new_e[r][c])\n",
" print(np.linalg.solve(A,y)[-1])\n",
" print(new_e[r][c] + np.linalg.solve(A,y)[-1])\n",
" print(np.ceil(new_e[r][c]) + np.floor(np.linalg.solve(A,y)[-1]))\n",
" \n",
" #Real solution that works, DO NOT DELETE\n",
" new_e[r][c] = int(np.ceil(new_e[r][c] + np.linalg.solve(A,y)[-1])) \n",
" \n",
" #new_e[r][c] = np.ceil(new_e[r][c]) + np.floor(np.linalg.solve(A,y)[-1])\n",
" \n",
" return new_e.astype(int)\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 403,
"id": "7f395ab2",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3.499999999992724\n",
"[13644.5]\n",
"[13648.]\n",
"[13648.]\n"
]
}
],
"source": [
"new_error = reconstruct(err, A)"
]
},
{
"cell_type": "code",
"execution_count": 420,
"id": "06ccaf8e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"518"
]
},
"execution_count": 420,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"e = np.round(err, 1)\n",
"len(np.unique(e[1:-1, 1:-1]))"
]
}
],
"metadata": {
"colab": {
"collapsed_sections": [],
"name": "Wavelet_Huffman.ipynb",
"provenance": []
},
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.11"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
#!/usr/bin/env python
# coding: utf-8
# In[72]:
import numpy as np
from matplotlib import pyplot as plt
from itertools import product
import os
import sys
from PIL import Image
from scipy.optimize import minimize
from time import time
from numpy import linalg as la
from scipy.stats import gaussian_kde, entropy
import seaborn as sns
import pywt
import math
#import cv2
# In[15]:
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
# In[16]:
def plot_hist(tiff_list, i):
"""
This function is the leftovers from the first attempt to plot histograms.
As it stands it needs some work in order to function again. We will
fix this later. 1/25/22
"""
image = tiff_list[i]
image = Image.open(image) #Open the image and read it as an Image object
image = np.array(image)[1:,:] #Convert to an array, leaving out the first row because the first row is just housekeeping data
image = image.astype(int)
A = np.array([[3,0,-1],[0,3,3],[1,-3,-4]]) # the matrix for system of equation
z0 = image[0:-2,0:-2] # get all the first pixel for the entire image
z1 = image[0:-2,1:-1] # get all the second pixel for the entire image
z2 = image[0:-2,2::] # get all the third pixel for the entire image
z3 = image[1:-1,0:-2] # get all the forth pixel for the entire image
# calculate the out put of the system of equation
y0 = np.ravel(-z0+z2-z3)
y1 = np.ravel(z0+z1+z2)
y2 = np.ravel(-z0-z1-z2-z3)
y = np.vstack((y0,y1,y2))
# use numpy solver to solve the system of equations all at once
predict = np.linalg.solve(A,y)[-1]
#predict = []
# 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)
"""for i in range(len(neighbor)):
if neighbor[i][0] >= max(neighbor[i][3], neighbor[i][1]):
predict.append(min(neighbor[i][3], neighbor[i][1]))
elif neighbor[i][0] < min(neighbor[i][3], neighbor[i][1]):
predict.append(max(neighbor[i][3], neighbor[i][1]))
else:
predict.append(neighbor[i][3] + neighbor[i][1] - neighbor[i][0])"""
# flatten the image to a vector
image_ravel = np.ravel(image[1:-1,1:-1])
return image_ravel, predict, diff, image
# In[17]:
scenes = file_extractor()
images = image_extractor(scenes)
num_images = im_distribution(images, "_1")
error_mean = []
error_mean1 = []
diff_mean = []
times = []
times1 = []
all_error = []
for i in range(len(num_images)):
"""start1 = time()
image_1, predict_1, difference_1, x_s_1 = plot_hist(num_images, i, "second")
stop1 = time()
times1.append(stop1-start1)
error1 = np.abs(image_1-predict_1)
error_mean1.append(np.mean(np.ravel(error1)))"""
start = time()
image, predict, difference, non_ravel = plot_hist(num_images, i)
stop = time()
times.append(stop-start)
error = np.abs(image-predict)
all_error.append(np.ravel(error))
error_mean.append(np.mean(np.ravel(error)))
diff_mean.append(np.mean(np.ravel(difference)))
#image, predict, difference = plot_hist(images, 0)
# In[18]:
print(f"Average Error: {np.mean(error_mean)}")
print(f"Standard Deviaiton of Mean Errors: {np.sqrt(np.var(error_mean))}")
print(f"Average Difference: {np.mean(diff_mean)}")
print(f"Average Time per Image for First: {np.mean(times)}")
# In[19]:
new_image, new_pred, new_diff, no_ravel = plot_hist(images, 10)
# In[21]:
new_error = new_image-new_pred
plt.hist(new_error, bins=20, density=True)
sns.kdeplot(new_error)
plt.xlabel("error")
plt.show()
# In[41]:
image = Image.open(images[0]) #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(np.int64)
print("Std Deviation of E: ", np.std(new_error))
print("Normal bits: ", int(image[0][0]).bit_length())
H = np.log2(np.std(new_error)) + 1.943
print("Encoded Bits: ", H)
# In[47]:
# In[9]:
pred = new_pred.reshape((510,638))
real_pred = no_ravel.copy()
real_pred[1:-1, 1:-1] = pred
# In[10]:
coeffs = pywt.dwt2(no_ravel, 'bior1.3')
LL, (LH, HL, HH) = coeffs
print(HH.shape)
decompress = pywt.idwt2(coeffs, 'bior1.3')
"""print(decompress)
print(np.mean(np.abs(decompress-no_ravel)))"""
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment