Ejemplo n.º 1
0
def compress_images(DATA, k):
    # Do PCA
    Z = pca.compute_Z(DATA, centering=True, scaling=True)

    COV = pca.compute_covariance_matrix(np.transpose(Z))
    L, PCS = pca.find_pcs(COV)
    Z_star = pca.project_data(np.transpose(Z), PCS, L, k, 0)

    # Find X compressed
    U = np.transpose(PCS[:, 0:k])
    X_compressed = np.matmul(Z_star, U)

    # Images have values from 0 to 255, so rescale
    for row, image in enumerate(X_compressed):
        min = np.amin(image)
        max = np.amax(image)
        for col, pixel in enumerate(image):
            X_compressed[row][col] = (
                X_compressed[row][col] - min) * (255/(max - min))

    X_compressed = X_compressed.astype(int)

    # Output everything into output dir
    if not os.path.exists('Output'):
        os.mkdir('Output')
    imageNumber = 0

    for image in X_compressed:
        filename = 'Output/k' + str(k) + 'image' + str(imageNumber) + '.png'
        # Love hard coded numbers <3
        plt.imsave(filename, image.reshape(60, 48), cmap='gray')
        imageNumber += 1

    return
Ejemplo n.º 2
0
def compress_images(DATA, k):

    output = '.\output'
    if not os.path.exists(output):
        os.makedirs(output)

    #data is an array of tuples where item 1: is the file name item 2: is the image array
    images = []
    name = []
    for image_tuple in DATA:
        name.append(image_tuple[0])
        images.append(image_tuple[1])

    images = np.transpose(np.asarray(images))

    Z = pca.compute_Z(images)
    COV = pca.compute_covariance_matrix(Z)
    L, PCS = pca.find_pcs(COV)
    Z_star = pca.project_data(Z, PCS, L, k, 0)

    PCS = np.delete(PCS, range(k, PCS.shape[1]), axis=1)
    u_t = np.transpose(PCS)
    x_compressed = np.dot(Z_star, u_t)

    minimum = np.min(x_compressed)

    for value in x_compressed:
        value += abs(minimum)

    for column in range(0, images.shape[1]):
        image = np.reshape(x_compressed[:,column], (60, 48))
        plt.imsave( output + '\\' + name[column] + '_compressed.jpg', image, cmap='gray')
Ejemplo n.º 3
0
def compress_images(DATA, k):
    Z = pca.compute_Z(DATA, True, True)
    COV = pca.compute_covariance_matrix(Z)
    L, U = pca.find_pcs(COV)
    Z_star = pca.project_data(Z, U, L, k, 0)
    X_compressed = np.dot(Z_star, U[:, :k].T)
    save_data(X_compressed)
    return X_compressed
Ejemplo n.º 4
0
def compress_images(DATA, k):
    #print("compress")
    X = DATA
    # Centering and Scaling Test
    Z = pca.compute_Z(X, True, True)
    #print("[RESULT] After scaling and centering Value found for Z = ", Z)
    COV = pca.compute_covariance_matrix(Z)
    #print("[RESULT] COV = ", COV)
    L, PCS = pca.find_pcs(COV)
    #print("[RESULT] L = ", L)
    #print("[RESULT] PCS = ", PCS)
    Z_star = pca.project_data(Z, PCS, L, 1, 0)
    return Z_star
Ejemplo n.º 5
0
def compress_images(DATA, k):
    # transpose command
    Data_T = DATA.transpose()

    Z = pca.compute_Z(Data_T, True, False)
    COV = pca.compute_covariance_matrix(Z)
    L, PCS = pca.find_pcs(COV)
    pr_Data = pca.project_data(Z, PCS, L, k, 0)

    # get the principle component - complex matrix
    U = np.zeros((len(PCS), k), dtype=complex)
    for i in range(len(PCS)):
        for j in range(k):
            U[i][j] = PCS[i][j]

    # transpose of the principles components -( eigenvector)
    U_T = U.transpose()

    # get the data back the original size
    X = np.matmul(pr_Data, U_T)

    X -= X.min()
    # scaling to range 0-255
    X_inter = np.divide(X, 255 / X.max())
    # convert to int
    X_inter2 = X_inter.astype(int)

    # check folder exists?
    foldername = './Data/output'
    # # check the folder output exist?
    if (not os.path.isdir(foldername)):
        os.mkdir(foldername)

    backImage = np.zeros([60, 48], dtype=int)
    row = 0
    column = 0
    for j in range(len(X_inter2)):
        # plt.imsave(foldername,X_compressed, format="'png'")
        for k in range(len(X_inter2[0])):
            if (column == 48):
                row += 1
                column = 0
            backImage[row][column] = X_inter2[j][k]
            column += 1
        row = 0
        column = 0
        name = str(j)
        plt.imsave('./Data/output/' + name, backImage, format="png")

    return 0
Ejemplo n.º 6
0
def compress_images(DATA, k):
    Z = pca.compute_Z(DATA)
    COV = pca.compute_covariance_matrix(Z)
    L, PCS = pca.find_pcs(COV)
    Z_star = pca.project_data(Z, PCS, L, k, 0)
    PCS = PCS[:, :k]
    X_compressed = Z_star @ PCS.transpose()
    X_compressed = X_compressed.transpose()
    #print(X_compressed)
    if not os.path.exists('Output'):
        os.makedirs('Output')
    for i in range(0, len(X_compressed)):
        matplotlib.pyplot.imsave("Output/output" + str(i) + ".png",
                                 X_compressed[i].reshape(60, 48),
                                 cmap='gray')
Ejemplo n.º 7
0
def compress_images(DATA,k):
    var = 0
    count = 0
    Z = pca.compute_Z(DATA, centering=True, scaling=False)
    COV = pca.compute_covariance_matrix(Z)
    L, PCS = pca.find_pcs(COV)
    Z_star = pca.project_data(Z, PCS, L, k, var)
    PCS = PCS[:,: k]
    X_compressed = Z_star.dot(PCS.T)
    if not os.path.exists("./Output"):
         os.mkdir("./Output")
    for i in X_compressed.T:
        count += 1
        file_name = "./Output/"+str(count)+"compressed_k"+str(k)
        n = np.reshape(i,(60,48))
        plt.imsave(file_name,n,cmap='Greys_r')
Ejemplo n.º 8
0
def compress_images(DATA, k):
    exists = os.path.exists("Output")
    if not exists:
        os.mkdir('Output')
    Z = pca.compute_Z(DATA)
    COV = pca.compute_covariance_matrix(Z)
    L, PCS = pca.find_pcs(COV)
    Zstar = pca.project_data(Z, PCS, L, k, 0)
    PCS = PCS[:, :k]
    PCS = PCS.T
    compress = np.dot(Zstar, PCS)
    compress = compress.T
    for c, j in enumerate(compress):
        j = (j * 255) / (np.max(j) - np.min(j))
        py.imsave('Output/out%d.png' % c,
                  j.reshape(60, 48),
                  vmin=0,
                  vmax=255,
                  cmap='gray',
                  format='png')
Ejemplo n.º 9
0
def compress_images(DATA, k):
    global fileNames
    Z = pca.compute_Z(DATA)
    Z = Z.astype(float)
    COV = pca.compute_covariance_matrix(Z)
    L, PCS = pca.find_pcs(COV)
    Z_star = pca.project_data(Z, PCS, L, k, 0)
    # Xcompressed = Z * U^T
    Z_star = Z_star.dot(PCS[:, 0:k].transpose())
    Z_star = Z_star.astype(float)
    Z_star = scale(Z_star)
    if not os.path.isdir(os.path.join(os.getcwd(), r'Output')):
        os.mkdir(os.path.join(os.getcwd(), r'Output'))
    for j in range(0, Z_star.shape[1]):
        imgData = []
        for i in range(0, Z_star.shape[0]):
            imgData.append(Z_star[i][j])
        imgData = np.asarray(imgData)
        imgData = imgData.reshape(60, 48)
        filename = os.path.join(os.getcwd(), r'Output/') + str(
            fileNames[j].strip('.pgm')) + ".png"
        fileDir = os.path.expanduser(filename)
        plt.imsave(fileDir, imgData, cmap='gray')
Ejemplo n.º 10
0
def compress_images(DATA, k):

    Z = pca.compute_Z(DATA)
    cov = pca.compute_covariance_matrix(Z)
    PCS = pca.find_pcs(cov)
    L, PCS_1 = PCS
    projected_data = pca.project_data(Z, PCS_1, L, k, 0)
    PCS_1 = PCS_1.T
    PCS_1 = PCS_1[:k]
    print(PCS_1.shape)
    compressed = (projected_data).dot(PCS_1)

    os.makedirs(os.getcwd() + "/Output", exist_ok=True)
    output_dir = os.getcwd() + "/Output"
    number_counter = 0
    for image in compressed.T:
        image = 255 * (image - np.min(image)) / np.ptp(image)
        image = np.reshape(image, (60, 48))

        plt.imsave(output_dir + '/' + str(number_counter) + ".png",
                   image,
                   cmap='gray')
        number_counter += 1
Ejemplo n.º 11
0
def compress_images(DATA, k):

    # if it is not exist create output directory
    if not os.path.exists("Output"):
        os.makedirs("Output")

    faces = []
    filenames = []
    for face in DATA:
        filenames.append(face[1])
        faces.append(face[2])

    # we convert each image to a feature (column)
    faces = np.asarray(faces)
    faces = np.transpose(faces)

    # pca processes
    Z = pca.compute_Z(faces)
    COV = pca.compute_covariance_matrix(Z)
    L, PCS = pca.find_pcs(COV)
    Z_star = pca.project_data(Z, PCS, L, k, 0)

    # we use components for compressing files, we need to reduce component count
    component_matrix = np.delete(PCS, range(k, PCS.shape[1]), axis=1)
    Ut = component_matrix.T
    X_compressed = np.dot(Z_star, Ut)

    # write all images
    for ftr_ind in range(faces.shape[1]):
        # we need to reshape the feature as image.
        img = np.reshape(X_compressed[:, ftr_ind], (60, 48))
        plt.imsave("Output" + "/" + filenames[ftr_ind] + "_img.jpg",
                   img,
                   cmap="gray")

    print()
Ejemplo n.º 12
0
def compress_images(DATA, k):

    Z = pca.compute_Z(DATA, centering=True, scaling=False)
    COV = pca.compute_covariance_matrix(Z)
    L, PCS = pca.find_pcs(COV)
    Z_Star = pca.project_data(Z, PCS, L, k, 0)
    PCS = PCS[:, :k]

    XCompressed = Z_Star.dot(np.transpose(PCS))

    path = 'Output'
    if os.path.exists(path) == False:

        os.mkdir(path)

    for col in range(len(XCompressed[0])):
        XCompressed[:, col] = (
            (XCompressed[:, col] - XCompressed[:, col].min()) * 1 /
            (XCompressed[:, col].max() - XCompressed[:, col].min()) *
            255).astype(
                float)  #should this be rescaling cols separately? If so , HOW

    tempArray = np.zeros((len(XCompressed), 1))
    colData = 0
    rowData = 0

    for items in range(len(XCompressed[1])):
        for row in range(len(tempArray)):

            tempArray[row][0] = XCompressed[row][items]  #possibly row??

        here = str(path) + "/" + str(items) + ".jpg"

        imArray = tempArray.reshape((r, c))

        plt.imsave(here, imArray, format="jpg", cmap='gray')
Ejemplo n.º 13
0
import numpy as np
import matplotlib.pyplot as plt
import pca

X = np.array([[-1, -1], [-1, 1], [1, -1], [1, 1]])
Z = pca.compute_Z(X)
COV = pca.compute_covariance_matrix(Z)
L, PCS = pca.find_pcs(COV)
#Z_star = pca.project_data(Z, PCS, L, 1, 0)

Ejemplo n.º 14
0
    print(PCS_1.shape)
    compressed = (projected_data).dot(PCS_1)

    os.makedirs(os.getcwd() + "/Output", exist_ok=True)
    output_dir = os.getcwd() + "/Output"
    number_counter = 0
    for image in compressed.T:
        image = 255 * (image - np.min(image)) / np.ptp(image)
        image = np.reshape(image, (60, 48))

        plt.imsave(output_dir + '/' + str(number_counter) + ".png",
                   image,
                   cmap='gray')
        number_counter += 1


cwd = os.getcwd() + "/Data/Train/"
data = load_data(cwd)
compress_images(data, 2)

Z_2 = pca.compute_Z()

cov_2 = pca.compute_covariance_matrix(Z_2)
print("cov", cov_2)

PCS_2 = pca.find_pcs(cov_2)

L2, PCS_2 = PCS_2

print(pca.project_data(Z_2, PCS_2, L2, 0, 0.3))