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
forrinrange(1,rows-1):#Iterate through the inside square of the error matrix
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
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
returnnew_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
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
False
%% Cell type:code id:bb11dcd0 tags:
``` python
classNodeTree(object):
def__init__(self,left=None,right=None):
self.left=left
self.right=right
defchildren(self):
returnself.left,self.right
def__str__(self):
returnself.left,self.right
defhuffman_code_tree(node,binString=''):
'''
Function to find Huffman Code
'''
iftype(node)isstr:
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'))
returnd
defmake_tree(nodes):
'''
Function to make tree
:param nodes: Nodes
:return: Root of the tree
'''
whilelen(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=lambdax:x[1],reverse=True)
returnnodes[0][0]
```
%% Cell type:code id:c01fda28 tags:
``` python
defencoder(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
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
forrinrange(1,rows-1):#Iterate through the inside square of the error matrix
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
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
returnnew_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
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
False
%% Cell type:code id:bb11dcd0 tags:
``` python
classNodeTree(object):
def__init__(self,left=None,right=None):
self.left=left
self.right=right
defchildren(self):
returnself.left,self.right
def__str__(self):
returnself.left,self.right
defhuffman_code_tree(node,binString=''):
'''
Function to find Huffman Code
'''
iftype(node)isstr:
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'))
returnd
defmake_tree(nodes):
'''
Function to make tree
:param nodes: Nodes
:return: Root of the tree
'''
whilelen(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=lambdax:x[1],reverse=True)
returnnodes[0][0]
```
%% Cell type:code id:c01fda28 tags:
``` python
defencoder(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