Project 'Elphel/master' was moved to 'Elphel/image-compression'. Please update any links and bookmarks that may still have the old path.
Commit cfb96985 authored by Bryce Hepner's avatar Bryce Hepner

Mathematical changes and testing

parent 9b1db8a3
from turtle import color
from WorkingPyDemo import *
def produce_error_array(encoded_string, list_dic, bins, use_diff):
"""
......@@ -88,17 +87,18 @@ def color_adjust(visual_array):
adjusted_array = visual_array - min_of_errors
adjusted_array = np.round(adjusted_array*255/np.max(adjusted_array))
return adjusted_array
scenes = file_extractor(folder_name)
images = image_extractor(scenes)
list_dic = np.load("first_dic.npy", allow_pickle="TRUE")
bins = [21,32,48]
encoded_string2 = bytes_to_bitstring(read_from_file(images[250][:-5] + "_Compressed.txt"))
original_array, error_array = produce_error_array(encoded_string2, list_dic, bins, False)
adjusted_errors = color_adjust(abs(error_array))
original_array_adjusted = color_adjust(original_array)
# print(adjusted_errors)
plt.subplot(121)
plt.imshow(original_array_adjusted,cmap='gray',vmin = 0, vmax=255)
plt.subplot(122)
plt.imshow(adjusted_errors,cmap = 'gray', vmin = 0, vmax=255)
plt.show()
\ No newline at end of file
if __name__ == "__main__":
scenes = file_extractor(folder_name)
images = image_extractor(scenes)
list_dic = np.load("first_dic.npy", allow_pickle="TRUE")
bins = [21,32,48]
encoded_string2 = bytes_to_bitstring(read_from_file(images[1][:-5] + "_Compressed.txt"))
original_array, error_array = produce_error_array(encoded_string2, list_dic, bins, False)
adjusted_errors = color_adjust(abs(error_array))
original_array_adjusted = color_adjust(original_array)
# print(adjusted_errors)
plt.subplot(121)
plt.imshow(original_array_adjusted,cmap='gray',vmin = 0, vmax=255)
plt.subplot(122)
plt.imshow(adjusted_errors,cmap = 'gray', vmin = 0, vmax=255)
plt.show()
\ No newline at end of file
from matplotlib.image import composite_images
from WorkingPyDemo import *
def remove_noise(images, which_sensor):
same_sensor_images = []
which_sensor = str(which_sensor)
average_image = np.zeros_like(np.array(Image.open(images[0]))[1:])
for i, image_name in enumerate(images):
if int(which_sensor) > 9:
if image_name[-7:-5] == which_sensor:
same_sensor_images.append(image_name)
else:
if image_name[-7:-5] == "_" + which_sensor:
same_sensor_images.append(image_name)
for i, image_name in enumerate(same_sensor_images):
# print(image_name)
image_object = Image.open(image_name)
# print(np.array(image_object).shape)
# print(np.array(image_object)[1:] + average_image)
average_image = np.array(image_object)[1:] + average_image
return average_image/len(same_sensor_images)
scenes = file_extractor(folder_name)
images = image_extractor(scenes)
average_image = remove_noise(images,"7")
def color_adjust(visual_array):
min_of_errors = np.min(visual_array)
adjusted_array = visual_array - min_of_errors
adjusted_array = np.round(adjusted_array*255/np.max(adjusted_array))
return adjusted_array
print(np.max(average_image))
print(np.min(average_image))
plt.imshow(color_adjust(average_image),cmap='gray',vmin = 0, vmax=255)
plt.show()
# print(np.linalg.inv(np.array([[3,0,-1],[0,3,3],[1,-3,-4]])))
print(np.linalg.pinv(np.array([[-1,-1,1], [-1,0,1], [-1,1,1], [0,-1,1]])))
\ No newline at end of file
......@@ -76,7 +76,8 @@ def predict_pix(tiff_image_path, difference = True):
image_obj = Image.open(tiff_image_path) #Open the image and read it as an Image object
image_array = np.array(image_obj)[1:,:].astype(int) #Convert to an array, leaving out the first row because the first row is just housekeeping data
# image_array = image_array.astype(int)
A = np.array([[3,0,-1],[0,3,3],[1,-3,-4]]) # the matrix for system of equation
# A = np.array([[3,0,-1],[0,3,3],[1,-3,-4]]) # the matrix for system of equation
Ainv = np.array([[0.5,-0.5,-0.5],[-0.5,1.83333333,1.5],[0.5,-1.5,-1.5]])
# where z0 = (-1,1), z1 = (0,1), z2 = (1,1), z3 = (-1,0)
z0 = image_array[0:-2,0:-2] # get all the first pixel for the entire image
z1 = image_array[0:-2,1:-1] # get all the second pixel for the entire image
......@@ -91,8 +92,8 @@ def predict_pix(tiff_image_path, difference = True):
# use numpy solver to solve the system of equations all at once
#predict = np.floor(np.linalg.solve(A,y)[-1])
predict = np.round(np.round((np.linalg.solve(A,y)[-1]),1))
# predict = np.round(np.round((np.linalg.solve(A,y)[-1]),1))
predict = np.round(np.round((Ainv[-1]@y)),1)
#Matrix system of points that will be used to solve the least squares fitting hyperplane
points = np.array([[-1,-1,1], [-1,0,1], [-1,1,1], [0,-1,1]])
......@@ -113,7 +114,12 @@ def predict_pix(tiff_image_path, difference = True):
#points to the hyperplane), it is a measure of gradient
f, diff, rank, s = la.lstsq(points, neighbor.T, rcond=None)
diff = diff.astype(int)
# Pinv = np.linalg.pinv(points)
# b = [z0,z1,z2,z3]
# x = Pinv@np.array(b)
# diff = np.linalg.norm(b - points@x,ord=2)
# diff = diff.astype(int)
# calculate the error
error = np.ravel(image_array[1:-1,1:-1])-predict
......@@ -402,6 +408,7 @@ def decoder(encoded_string, list_dic, bins, use_diff):
decode_matrix (512, 640): decoded matrix
"""
A = np.array([[3,0,-1],[0,3,3],[1,-3,-4]]) # the matrix for system of equation
Ainv = np.array([[0.5,-0.5,-0.5],[-0.5,1.83333333,1.5],[0.5,-1.5,-1.5]])
# change the dictionary back to list
# !!!!!WARNING!!!! has to change this part, everytime you change the number of bins
the_keys0 = list(list_dic[0].keys())
......@@ -421,7 +428,7 @@ def decoder(encoded_string, list_dic, bins, use_diff):
#Matrix system of points that will be used to solve the least squares fitting hyperplane
points = np.array([[-1,-1,1], [-1,0,1], [-1,1,1], [0,-1,1]])
# Pinv = np.linalg.pinv(points)
decode_matrix = np.zeros((512,640))
# loop through all the element in the matrix
for i in range(decode_matrix.shape[0]):
......@@ -449,11 +456,14 @@ def decoder(encoded_string, list_dic, bins, use_diff):
if use_diff:
difference = max(z0,z1,z2,z3) - min(z0,z1,z2,z3)
else:
# b = [z0,z1,z2,z3]
# x = Pinv@np.array(b)
# difference = np.linalg.norm(b - points@x,ord=2)
f, difference, rank, s = la.lstsq(points, [z0,z1,z2,z3], rcond=None)
difference = difference.astype(int)
predict = np.round(np.round(np.linalg.solve(A,y)[-1][0],1))
# predict = np.round(np.round(np.linalg.solve(A,y)[-1][0],1))
predict = np.round(np.round((Ainv[-1]@y)[0],1))
# add on the difference by searching the dictionary
# !!!!!WARNING!!!! has to change this part, eveytime you change the number of bins
if difference <= bins[0]:
......@@ -501,19 +511,19 @@ def text_to_tiff(filename, list_dic, bins):
reconstruct_image = reconstruct_image.astype(np.uint16)
reconstruct_image = Image.fromarray(reconstruct_image)
reconstruct_image.save(filename[:-16]+"_reconstructed.tiff", "TIFF")
# starttime = time()
if __name__ == "__main__":
scenes = file_extractor(folder_name)
images = image_extractor(scenes)
newnamesforlater = []
# list_dic, bins = make_dictionary(images, 4, False)
# list_dic, bins = make_dictionary(images[0:1], 4, False)
file_sizes_new = []
file_sizes_old = []
list_dic = np.load("first_dic.npy", allow_pickle="TRUE")
bins = [21,32,48]
# np.save("first_dic.npy", list_dic)
for i in range(len(images)):
for i in range(len(images[0:5])):
# image, new_error, diff = huffman(images[i], 4, False)
# encoded_string = encoder(new_error, list_dic, diff, bins)
# inletters = bitstring_to_bytes(encoded_string)
......@@ -535,18 +545,21 @@ if __name__ == "__main__":
file_sizes_new.append(os.path.getsize("first_dic.npy"))
print(np.sum(file_sizes_new)/np.sum(file_sizes_old))
list_dic = np.load("first_dic.npy", allow_pickle="TRUE")
# list_dic = np.load("first_dic.npy", allow_pickle="TRUE")
bins = [21,32,48]
# starttime = time()
for i,item in enumerate(newnamesforlater[0:5]):
for i,item in enumerate(newnamesforlater):
print(item)
image, new_error, diff = huffman(images[i], 4, False)
encoded_string2 = bytes_to_bitstring(read_from_file(item))
starttime = time()
reconstruct_image = decoder(encoded_string2, list_dic, bins, False)
print(np.allclose(image, reconstruct_image))
print(time() - starttime)
# text_to_tiff("images/1626033496_437803/1626033496_437803_3._Compressed.txt", list_dic, bins)
# original_image = Image.open("images/1626033496_437803/1626033496_437803_3.tiff")
# original_image = np.array(original_image)[1:]
# secondimage = Image.open("images/1626033496_437803/1626033496_437803_3_reconstructed.tiff")
# secondimage = np.array(secondimage)
# print(np.allclose(original_image, secondimage))
\ No newline at end of file
# print(np.allclose(original_image, secondimage))
\ No newline at end of file
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