Exemplo n.º 1
0
        pca = PCA()
        #set dimension
        pca.set_target_dim(i)
        #compute PCA
        pca.init(RealFeatures(images))

        pca.apply_to_feature_vector(
            RealFeatures(imageAsRow).get_feature_vector(0))

        #reconstruct
        projection = pca.apply_to_feature_vector(
            RealFeatures(imageAsRow).get_feature_vector(0))

        reconstruction = np.asmatrix( np.asarray(projection, np.float64))* \
                         np.asmatrix( pca.get_transformation_matrix()).T
        reconstruction = reconstruction + pca.get_mean()

        #normlize image
        reconstruction_normalize = np.zeros((IMAGE_HEIGHT, IMAGE_WIDHT, 1),
                                            np.uint8)
        reconstruction_normalize = cv2.normalize(reconstruction,
                                                 reconstruction_normalize, 0,
                                                 255, cv2.NORM_MINMAX,
                                                 cv2.CV_8UC1)
        reconstruction_normalize = reconstruction_normalize.reshape(
            IMAGE_HEIGHT, IMAGE_WIDHT)
        #show reconstruction
        cv2.imshow("reconstruction" + str(i), reconstruction_normalize)
    cv2.waitKey(0)
Exemplo n.º 2
0
    #Reconstruct 10 eigen vectors to 300, step 15
    for i in reconstructions:

        print "Reconstruct with " + str(i) + " eigenvectors"

        pca = PCA()
        #set dimension
        pca.set_target_dim(i);
        #compute PCA
        pca.init(RealFeatures(images))

        pca.apply_to_feature_vector(RealFeatures(imageAsRow)
                                    .get_feature_vector(0));

        #reconstruct
        projection = pca.apply_to_feature_vector(RealFeatures(imageAsRow)
                                                .get_feature_vector(0));

        reconstruction = np.asmatrix( np.asarray(projection, np.float64))* \
                         np.asmatrix( pca.get_transformation_matrix()).T
        reconstruction = reconstruction + pca.get_mean()

        #prepare the data to visualize in one window
        show_images_titles.append( str(i) + " eigenvectors" );
        show_images.append(reconstruction.reshape(IMAGE_HEIGHT, IMAGE_WIDHT))

    plot_gallery(show_images, show_images_titles, IMAGE_HEIGHT,
                 IMAGE_WIDHT, 4, 4);
    pl.show()