Commit 1991ccfc authored by Kelly Chang's avatar Kelly Chang
Browse files
parents 3e116aa0 27b7f6c2
Loading
Loading
Loading
Loading
+160 −0
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
import pywt
```

%% Cell type:code id:9ed20f84 tags:

``` python
def plot_hist(tiff_list, i=0):
    """
    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_int = image.astype(np.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]
    #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)


    # flatten the image to a vector
    small_image = image_int[1:-1,1:-1]

    #Reshape the predictions to be a 2D array
    predict = np.pad(predict.reshape(510,638), pad_width=1)
    """predict[0,:] = image[0,:]
    predict[:,0] = image[:,0]
    predict[:,-1] = image[:,-1]
    predict[-1,:] = image[-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 - predict


    return predict, diff, image_int, error, A
```

%% Cell type:code id:ba2881d9 tags:

``` python
scenes = file_extractor()
images = image_extractor(scenes)
num_images = im_distribution(images, "_9")
```

%% Cell type:code id:11e95c34 tags:

``` python
predict, diff, im, err, A = plot_hist(num_images, 0)
```

%% 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):
        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]))

            #Real solution that works, DO NOT DELETE
            new_e[r][c] = int(np.ceil(new_e[r][c] + np.linalg.solve(A,y)[-1]))

            #new_e[r][c] = np.ceil(new_e[r][c]) + np.floor(np.linalg.solve(A,y)[-1])

    return new_e.astype(int)

```

%% Cell type:code id:7f395ab2 tags:

``` python
new_error = reconstruct(err, A)
```

%% Output

    3.499999999992724
    [13644.5]
    [13648.]
    [13648.]

%% Cell type:code id:06ccaf8e tags:

``` python
e = np.round(err, 1)
len(np.unique(e[1:-1, 1:-1]))
```

%% Output

    518
+315 −0
Original line number Diff line number Diff line
%% Cell type:code id:9d3f0b36 tags:

``` python
from prediction_MSE_Scout import file_extractor, image_extractor, im_distribution
import numpy as np
from matplotlib import pyplot as plt
import os
import sys
from PIL import Image
import math
```

%% Cell type:code id:e20525b8 tags:

``` python
scenes = file_extractor()
images = image_extractor(scenes)
num_images = im_distribution(images, "_1")
```

%% Cell type:code id:837df9c4 tags:

``` python
def compress(inputFile):
    twoBytes = 256*256
    # Read the input file into a numpy array of 8-bit values
    #
    # The img.shape is a 3-type with rows,columns,channels, where
    # channels is the number of components in each pixel.  The img.dtype
    # is 'uint8', meaning that each component is an 8-bit unsigned
    # integer.

    #img = netpbm.imread(inputFile).astype('uint8')
    img = Image.open(inputFile)    #Open the image and read it as an Image object
    img = np.array(img)[1:,:]    #Convert to an array, leaving out the first row because the first row is just housekeeping data
    img = img.astype('uint8')

    # Compress the image
    #
    #
    # Note that single-channel images will have a 'shape' with only two
    # components: the y dimensions and the x dimension.  So you will
    # have to detect this and set the number of channels accordingly.
    # Furthermore, single-channel images must be indexed as img[y,x]
    # instead of img[y,x,1].  You'll need two pieces of similar code:
    # one piece for the single-channel case and one piece for the
    # multi-channel case.

    #startTime = time.time()

    outputBytes = bytearray()

    # initialize dictionary
    d = {}
    counter = 256
    for i in range(-counter, counter):
        d[str(i)] = i
    # Set Dictionary limit

    # Make a list to hold bytes
    tempBytes = []
    # A counter for the number of bytes
    numBytes = 0
    multichannel = False

    # for a single channel image
    if (len(img.shape) == 2) :
        multichannel = False

    # Go through whole image
    for y in range(img.shape[0]):
        for x in range(img.shape[1]):
            # Initialize prediction to image value
            prediction = img[y][x]
            #"""
            # Modify prediction to show the difference between prior pixels and current pixel
            if(x != 0):
                prediction = prediction - img[y][x-1]
            elif(y != 0):
                prediction = prediction - img[y-1][x]
            else:
                prediction = prediction - (img[y][x-1]/3 + img[y-1][x]/3 + img[y-1][x-1]/3)
            #"""
            # Add the predicted value to the bytestream
            tempBytes.append(prediction)
            numBytes += 1
    # Using a string variable as it allows for concatenation
    s = ""
    # Set s to the first value of the bytestream
    s = str(int(tempBytes[0]))
    # Go through all bytes
    for i in range(1, numBytes):
        # Do LZW encoding
        # If trying to add entry larger than max size of the dictionary reinitialize the dictionary
        if(counter >= twoBytes):
            counter = 256
            d = {}
            for i in range(-counter, counter):
                d[str(i)] = i

        # Add the next byte to the current string. Uses a delimeter to distinguish numbers
        w = s +"|"+str(tempBytes[i])

        # Checking if it has been seen before
        if w in d:
            s = w

        else:
            # Output bytes by splitting integer into two bytes, this allows for a larger dictionary
            outputBytes.append((int(d[s]) >> 8) & 0xFF)
            outputBytes.append(int(d[s]) & 0xFF)
            # Add to dictionarry
            d[w] = counter
            counter += 1
            s = str(int(tempBytes[i]))
    # Check if the last byte was added or not
    if s in d:
        outputBytes.append((int(d[s]) >> 8) & 0xFF)
        outputBytes.append(int(d[s]) & 0xFF)



    return outputBytes, img.shape[0], img.shape[1]
```

%% Cell type:code id:dec67245 tags:

``` python
test = images[0]
out, rows, cols = compress(test)
print(out[19])
```

%% Output

    103

    C:\Users\calle\AppData\Local\Temp/ipykernel_12604/4289951463.py:56: RuntimeWarning: overflow encountered in ubyte_scalars
      prediction = prediction - img[y][x-1]
    C:\Users\calle\AppData\Local\Temp/ipykernel_12604/4289951463.py:58: RuntimeWarning: overflow encountered in ubyte_scalars
      prediction = prediction - img[y-1][x]

    245

%% Cell type:code id:51938ebb tags:

``` python
# Uncompress an image

def uncompress(byteArray, rows, columns):
    twoBytes = 256*256
    # Check that it's a known file

    """if inputFile.readline() != headerText + '\n':
        sys.stderr.write( "Input is not in the '%s' format.\n" % headerText )
        sys.exit(1)"""

    # Read the rows, columns, and channels.  counter

    #rows, columns, channels = [ int(x) for x in inputFile.readline().split() ]

    # Read the raw bytes.

    inputBytes = byteArray

    # Build the image
    #
    # REPLACE THIS WITH YOUR OWN CODE TO CONVERT THE 'inputBytes' ARRAY INTO AN IMAGE IN 'img'.


    result = []

    # initialize the dictionary in the opposite was as compress and use an array as the value
    d = {} # create a dictionary
    counter = 256

    # Initialize dictionary with values equalling keys from [-256,256]
    for i in range(-counter, counter):
        d[i] = [i]

    img = np.empty([rows,columns], dtype=np.uint8 )

    byteIter = iter(inputBytes)

    # Get encoding in the form of next two bytes
    new = (byteIter.__next__() >> 8) + byteIter.__next__()
    s = d[new]

    result.append(s[0])

    for i in range(1, len(inputBytes)//2):

        # again reset the dictionary if it reaches the limit
        # Initialize dictionary with values equalling keys from [-256,256]

        if counter >= twoBytes:
            d = {} # initialize blank dictionary
            counter = 256
            for i in range(-counter, counter):
                d[i] = [i]


        new = (byteIter.__next__() >> 8) + byteIter.__next__()

        #retrieve value of dictionary entry from dictionary or create entry assuming it has not yet been entered into dictionary

        if new in d:
            d_value = d[new]
        else:
            d_value = []
            for j in s:
                d_value.append(j)
            d_value.append(s[0])

        #add dictionary entry value to the result
        for k in range(len(d_value)):
            result.append(d_value[k])

        #Create entry in dictionary
        temp = []
        for j in s:
            temp.append(j)
        temp.append(s[0])
        d[counter] = temp
        counter += 1


        # reset decoded string to dictionary entry value
        s = d_value

    print(result[:20])
    channels = 1

    #implement predictive encoding
    prediction = 0
    counter = 0

    # for a single channel image
    if (channels == 1):
        # Go through whole image
        for y in range(rows):
            for x in range(columns):
                #'''
                if(x != 0):
                    prediction = img[y][x-1]
                elif(y != 0):
                    prediction = img[y-1][x]
                else:
                    prediction = (img[y][x-1]/3 + img[y-1][x]/3 + img[y-1][x-1]/3)
                #'''
                img[y,x] = result[counter] + prediction
                counter += 1

    return img

    # Output the image

    #netpbm.imsave( outputFile, img )

```

%% Cell type:code id:74528264 tags:

``` python
imgg = uncompress(out, rows, cols)
```

%% Output

    [164, 246, 24, 0, 4, 239, 251, 13, 12, 245, 246, 6, 6, 250, 0, 0, 3, 4, 242, 0]

    ---------------------------------------------------------------------------
    IndexError                                Traceback (most recent call last)
    ~\AppData\Local\Temp/ipykernel_12604/601870618.py in <module>
    ----> 1 imgg = uncompress(out, rows, cols)

    ~\AppData\Local\Temp/ipykernel_12604/2882586717.py in uncompress(byteArray, rows, columns)
        102                     prediction = (img[y][x-1]/3 + img[y-1][x]/3 + img[y-1][x-1]/3)
        103                 #'''
    --> 104                 img[y,x] = result[counter] + prediction
        105                 counter += 1
        106
    IndexError: list index out of range

%% Cell type:code id:ea5c3c61 tags:

``` python
img = Image.open(test)    #Open the image and read it as an Image object
img = np.array(img)[1:,:]    #Convert to an array, leaving out the first row because the first row is just housekeeping data
img = img.astype('uint8')
np.unique(img.ravel())
```

%% Output

    array([  0,   1,   2,   3,   4,   5,   6,   7,   8,   9,  10,  11,  12,
            13,  14,  15,  16,  17,  18,  19,  20,  21,  22,  23,  24,  25,
            26,  27,  28,  29,  30,  31,  32,  33,  34,  35,  36,  37,  38,
            39,  40,  41,  42,  43,  44,  45,  46,  47,  48,  49,  50,  51,
            52,  53,  54,  55,  56,  57,  58,  59,  60,  61,  62,  63,  64,
            65,  66,  67,  68,  69,  70,  71,  72,  73,  74,  75,  76,  77,
            78,  79,  80,  81,  82,  83,  84,  85,  86,  87,  88,  89,  90,
            91,  92,  93,  94,  95,  96,  97,  98,  99, 100, 101, 102, 103,
           104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116,
           117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129,
           130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142,
           143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155,
           156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168,
           169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181,
           182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194,
           195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207,
           208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220,
           221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233,
           234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246,
           247, 248, 249, 250, 251, 252, 253, 254, 255], dtype=uint8)
+21 −280

File changed.

Preview size limit exceeded, changes collapsed.

Error_to_Image.ipynb

0 → 100644
+234 −0

File added.

Preview size limit exceeded, changes collapsed.

+393 −0

File added.

Preview size limit exceeded, changes collapsed.

Loading