示例#1
0
    def run(self):
        data = self.imgHandler.constructData()
        # print(data.head())
        # pca = PCA(n_components=4, svd_solver='full')
        # pca.fit(data)
        # data_PCA = pca.transform(data)
        # print(pca.explained_variance_ratio_)
        p = PCA(3)
        p.loadData(data.copy())
        data_PCA, transformation_mat = p.transform()
        reconstruct = p.reconstruct(data_PCA)
        cv.imwrite('eigenfaces/rec.jpg',
                   np.array(reconstruct)[:, 0].reshape(self.res, self.res))
        cv.imwrite('eigenfaces/img1.jpg',
                   np.array(data_PCA)[:, 0].reshape(self.res, self.res))
        cv.imwrite('eigenfaces/img2.jpg',
                   np.array(data_PCA)[:, 1].reshape(self.res, self.res))
        cv.imwrite('eigenfaces/img3.jpg',
                   np.array(data_PCA)[:, 2].reshape(self.res, self.res))
        # cv.imwrite('eigenfaces/img4.jpg',np.array(data_PCA)[:,3].reshape(self.res,self.res))

        # mean_init = data_PCA.mean(axis=1)

        # self.__init__('verification_2','eigenfaces')
        # data_test = self.imgHandler.constructData()
        # data_test_PCA = p.project(data_test)
        # mean_test = np.array(data_test_PCA.mean(axis=1))
        # print(mean_init)
        # print(mean_test)
        # dist = np.linalg.norm(data_PCA[:,0]-data_test_PCA[:,0])
        # print(dist)
        # cv.imwrite('eigenfaces/rec.jpg',np.array(reconstruct)[:,0].reshape(self.res,self.res))
        # cv.imwrite('eigenfaces/img1.jpg',np.array(data_PCA)[:,0].reshape(self.res,self.res))
        # cv.imwrite('eigenfaces/img2.jpg',np.array(data_PCA)[:,1].reshape(self.res,self.res))
        # cv.imwrite('eigenfaces/img3.jpg',np.array(data_PCA)[:,2].reshape(self.res,self.res))
        # cv.imwrite('eigenfaces/img4.jpg',np.array(data_PCA)[:,3].reshape(self.res,self.res))
        cv.waitKey()
示例#2
0
import cv2 as cv
from pca import PCA
import pandas as pd

img = cv.imread('random_img/win_small.jpeg', cv.IMREAD_GRAYSCALE)
a_ratio = img.shape[0] / img.shape[1]
df = pd.DataFrame(img.copy(), index=None, columns=None)
p = PCA(4)
p.loadData(df)
img_PCA = p.transform()
pixels = img_PCA.shape[0] * img_PCA.shape[1]
compressed_img = img_PCA.reshape()
print(img_PCA[0].shape)
print(img_PCA[1])
# reconstruct = p.reconstruct(img_PCA[0]).astype(int)
# print(reconstruct.shape)
cv.imwrite('random_img/win_small_original.jpeg', img)
cv.imwrite('random_img/win_small_compressed.jpeg', reconstruct)