error (array): matrix of errors computed in encoding. Same
error (array): matrix of errors computed in encoding. Same
shape as the original image (512, 640) in this case
shape as the original image (512, 640) in this case
A (array): Matrix used for the system of equations to create predictions
A (array): Matrix used for the system of equations to create predictions
Returns:
Returns:
image (array): The reconstructed image
image (array): The reconstructed image
"""
"""
new_e=error.copy()
new_e=error.copy()
rows,columns=new_e.shape
rows,columns=new_e.shape
forrinrange(1,rows-1):#Iterate through the inside square of the error matrix
forrinrange(1,rows-1):#Iterate through the inside square of the error matrix
forcinrange(1,columns-1):
forcinrange(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
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
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
#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
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)
#rounding the result because np.linalg.solve(A,y)
#can be a float. Since we did np.floor on it in
#can be a float. Since we did np.floor on it in
#prediction, we round to the nearest integer here
#prediction, we round to the nearest integer here
returnnew_e.astype(int)
returnnew_e.astype(int)
```
```
%% Cell type:code id:3cc609dc tags:
%% Cell type:code id:3cc609dc tags:
``` python
``` python
new_error=reconstruct(err,A)
new_error=reconstruct(err,A)
```
```
%% Cell type:code id:5d290a0c tags:
%% Cell type:code id:5d290a0c tags:
``` python
``` python
im==new_error
im==new_error
```
```
%% Output
%% Output
C:\Users\calle\AppData\Local\Temp/ipykernel_23384/389333.py:1: DeprecationWarning: elementwise comparison failed; this will raise an error in the future.
C:\Users\calle\AppData\Local\Temp/ipykernel_23384/389333.py:1: DeprecationWarning: elementwise comparison failed; this will raise an error in the future.
im == new_error
im == new_error
False
False
%% Cell type:code id:bb11dcd0 tags:
%% Cell type:code id:bb11dcd0 tags:
``` python
``` python
classNodeTree(object):
classNodeTree(object):
def__init__(self,left=None,right=None):
def__init__(self,left=None,right=None):
self.left=left
self.left=left
self.right=right
self.right=right
defchildren(self):
defchildren(self):
returnself.left,self.right
returnself.left,self.right
def__str__(self):
def__str__(self):
returnself.left,self.right
returnself.left,self.right
defhuffman_code_tree(node,binString=''):
defhuffman_code_tree(node,binString=''):
'''
'''
Function to find Huffman Code
Function to find Huffman Code
'''
'''
iftype(node)isstr:
iftype(node)isstr:
return{node:binString}
return{node:binString}
(l,r)=node.children()
(l,r)=node.children()
d=dict()
d=dict()
d.update(huffman_code_tree(l,binString+'0'))
d.update(huffman_code_tree(l,binString+'0'))
d.update(huffman_code_tree(r,binString+'1'))
d.update(huffman_code_tree(r,binString+'1'))
returnd
returnd
defmake_tree(nodes):
defmake_tree(nodes):
'''
'''
Function to make tree
Function to make tree
:param nodes: Nodes
:param nodes: Nodes
:return: Root of the tree
:return: Root of the tree
'''
'''
whilelen(nodes)>1:
whilelen(nodes)>1:
(key1,c1)=nodes[-1]
(key1,c1)=nodes[-1]
(key2,c2)=nodes[-2]
(key2,c2)=nodes[-2]
nodes=nodes[:-2]
nodes=nodes[:-2]
node=NodeTree(key1,key2)
node=NodeTree(key1,key2)
nodes.append((node,c1+c2))
nodes.append((node,c1+c2))
nodes=sorted(nodes,key=lambdax:x[1],reverse=True)
nodes=sorted(nodes,key=lambdax:x[1],reverse=True)
returnnodes[0][0]
returnnodes[0][0]
```
```
%% Cell type:code id:c01fda28 tags:
%% Cell type:code id:c01fda28 tags:
``` python
``` python
defencoder(images,i,plot=True):
defencoder(images,i,plot=True):
"""
"""
Function that creates Huffman encodings out of the error values
Function that creates Huffman encodings out of the error values
for a given image. The encodings are more efficient ways to store
for a given image. The encodings are more efficient ways to store
large integer values that the original image contains.
large integer values that the original image contains.
Parameters:
Parameters:
images (list): list of file paths to the images that
images (list): list of file paths to the images that
will be encoded.
will be encoded.
i (int): which index of the images list to grab and
i (int): which index of the images list to grab and
then encode.
then encode.
plot (bool): if true, this plots the error matrix to
plot (bool): if true, this plots the error matrix to
show the distribution of values.
show the distribution of values.
"""
"""
prediction,diff,original,error,A=predict(images,i)#Predict the values and return the error for the specified image
prediction,diff,original,error,A=predict(images,i)#Predict the values and return the error for the specified image
image=original
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
new_error=np.copy(image)#Create a new matrix that is a copy of the original image, this is the matrix we will
error (array): matrix of errors computed in encoding. Same
error (array): matrix of errors computed in encoding. Same
shape as the original image (512, 640) in this case
shape as the original image (512, 640) in this case
A (array): Matrix used for the system of equations to create predictions
A (array): Matrix used for the system of equations to create predictions
Returns:
Returns:
image (array): The reconstructed image
image (array): The reconstructed image
"""
"""
new_e=error.copy()
new_e=error.copy()
rows,columns=new_e.shape
rows,columns=new_e.shape
forrinrange(1,rows-1):#Iterate through the inside square of the error matrix
forrinrange(1,rows-1):#Iterate through the inside square of the error matrix
forcinrange(1,columns-1):
forcinrange(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
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
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
#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
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)
#rounding the result because np.linalg.solve(A,y)
#can be a float. Since we did np.floor on it in
#can be a float. Since we did np.floor on it in
#prediction, we round to the nearest integer here
#prediction, we round to the nearest integer here
returnnew_e.astype(int)
returnnew_e.astype(int)
```
```
%% Cell type:code id:3cc609dc tags:
%% Cell type:code id:3cc609dc tags:
``` python
``` python
new_error=reconstruct(err,A)
new_error=reconstruct(err,A)
```
```
%% Cell type:code id:5d290a0c tags:
%% Cell type:code id:5d290a0c tags:
``` python
``` python
im==new_error
im==new_error
```
```
%% Output
%% Output
C:\Users\calle\AppData\Local\Temp/ipykernel_23384/389333.py:1: DeprecationWarning: elementwise comparison failed; this will raise an error in the future.
C:\Users\calle\AppData\Local\Temp/ipykernel_23384/389333.py:1: DeprecationWarning: elementwise comparison failed; this will raise an error in the future.
im == new_error
im == new_error
False
False
%% Cell type:code id:bb11dcd0 tags:
%% Cell type:code id:bb11dcd0 tags:
``` python
``` python
classNodeTree(object):
classNodeTree(object):
def__init__(self,left=None,right=None):
def__init__(self,left=None,right=None):
self.left=left
self.left=left
self.right=right
self.right=right
defchildren(self):
defchildren(self):
returnself.left,self.right
returnself.left,self.right
def__str__(self):
def__str__(self):
returnself.left,self.right
returnself.left,self.right
defhuffman_code_tree(node,binString=''):
defhuffman_code_tree(node,binString=''):
'''
'''
Function to find Huffman Code
Function to find Huffman Code
'''
'''
iftype(node)isstr:
iftype(node)isstr:
return{node:binString}
return{node:binString}
(l,r)=node.children()
(l,r)=node.children()
d=dict()
d=dict()
d.update(huffman_code_tree(l,binString+'0'))
d.update(huffman_code_tree(l,binString+'0'))
d.update(huffman_code_tree(r,binString+'1'))
d.update(huffman_code_tree(r,binString+'1'))
returnd
returnd
defmake_tree(nodes):
defmake_tree(nodes):
'''
'''
Function to make tree
Function to make tree
:param nodes: Nodes
:param nodes: Nodes
:return: Root of the tree
:return: Root of the tree
'''
'''
whilelen(nodes)>1:
whilelen(nodes)>1:
(key1,c1)=nodes[-1]
(key1,c1)=nodes[-1]
(key2,c2)=nodes[-2]
(key2,c2)=nodes[-2]
nodes=nodes[:-2]
nodes=nodes[:-2]
node=NodeTree(key1,key2)
node=NodeTree(key1,key2)
nodes.append((node,c1+c2))
nodes.append((node,c1+c2))
nodes=sorted(nodes,key=lambdax:x[1],reverse=True)
nodes=sorted(nodes,key=lambdax:x[1],reverse=True)
returnnodes[0][0]
returnnodes[0][0]
```
```
%% Cell type:code id:c01fda28 tags:
%% Cell type:code id:c01fda28 tags:
``` python
``` python
defencoder(images,i,plot=True):
defencoder(images,i,plot=True):
"""
"""
Function that creates Huffman encodings out of the error values
Function that creates Huffman encodings out of the error values
for a given image. The encodings are more efficient ways to store
for a given image. The encodings are more efficient ways to store
large integer values that the original image contains.
large integer values that the original image contains.
Parameters:
Parameters:
images (list): list of file paths to the images that
images (list): list of file paths to the images that
will be encoded.
will be encoded.
i (int): which index of the images list to grab and
i (int): which index of the images list to grab and
then encode.
then encode.
plot (bool): if true, this plots the error matrix to
plot (bool): if true, this plots the error matrix to
show the distribution of values.
show the distribution of values.
"""
"""
prediction,diff,original,error,A=predict(images,i)#Predict the values and return the error for the specified image
prediction,diff,original,error,A=predict(images,i)#Predict the values and return the error for the specified image
image=original
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
new_error=np.copy(image)#Create a new matrix that is a copy of the original image, this is the matrix we will