def reconstruct_shape(points, shape_model, shape_Vt=None, n_components=None): input_points = points.get_points() mean_points = shape_model.mean_values shape_Vt = shape_Vt if shape_Vt is not None else shape_model.Vt reconstructed = pca.reconstruct( input_points, shape_Vt, mean_points, n_components=n_components ) points.normalized_flattened_points_list = reconstructed
def reconstruct_texture(src_image, dst_image, texture_model, src_points, dst_points, output_points): """ Recontruct texture given the src and dst image Args: src_image(ndarray): numpy / OpenCV image in BGR dst_image(ndarray): numpy image / OpenCVImage, may be None, if None then we create an image just as big a the input image but then with a black background. texture_model(PCAModel): The PCAModel that holds the information that we need to reconstruct the image, see pca module. Make one by doing this: (see PCAModel on how it is stored in numpy file). texture_model = pca.PCAModel(model_texture_file) src_points(aam.AAMPoints): The AAMPoints object contains the location of the landmarks on the face that we need to perform piece wise affine warping. dst_points(aam.AAMPoints): The AAMPoints object contains the location of the landmarks on the face that we need to perform piece wise affine warping. """ Vt = texture_model.Vt triangles = texture_model.triangles mean_texture = texture_model.mean_values h, w, c = src_image.shape # empty input_texture input_texture = np.full((h, w, 3), fill_value=0, dtype=np.uint8) points2d_src = src_points.get_scaled_points(src_image.shape) points2d_dst = dst_points.get_scaled_points(dst_image.shape) points2d_output = output_points.get_scaled_points(src_image.shape) # get the texture from the rectangles. aam.piecewise_transform( src_image, points2d_src, points2d_dst, triangles, input_texture # this will be filled with the texture afterwards. ) # define the rectangle around the dst_points. offset_x, offset_y, w_slice, h_slice = dst_points.get_bounding_box() # cut out this region from the input_texture. input_texture = input_texture[offset_y: offset_y + h_slice, offset_x: offset_x + w_slice].flatten() # perfrom the PCA reconstruction using the input_texture. r_texture = pca.reconstruct(input_texture, Vt, mean_texture) # Make an image from the data, the texture is still of type `float`. r_texture = np.asarray(r_texture, np.uint8).reshape((h_slice, w_slice, 3)) # subtract the offset, this is needed because the image is now a # small rectangle around the face which starts at [0,0], wheras it first # was located at offset_x, offset_y. We need both rectangles to start at # [0, 0]. Please note that this should be improved to avoid confusion. points2d_dst[:, 0] -= offset_x points2d_dst[:, 1] -= offset_y # don't know why this was, removing still works, keeping it for a while. #points2d_src = points2d_src * 1.1 # get the texture from the rectangles. aam.piecewise_transform( r_texture, points2d_dst, # turn src and dst around points2d_output, # turn src and dst around triangles, dst_image )
import matplotlib.pyplot as plt import numpy as np from mlxtend.data import loadlocal_mnist from pca import PCA, plot, reconstruct k = 235 images, labels = loadlocal_mnist(images_path='./Data/train-images-idx3-ubyte', labels_path='./Data/train-labels-idx1-ubyte') mat = images[labels == 0].astype('float64') pca_mat, mean, k_evectors = PCA(k, mat, use_perc=False) recons_mat = reconstruct(pca_mat, mean, k_evectors) plot(mat[0], 'Original') plot(recons_mat[0], 'Reconstructed') plt.show()
real_l = np.abs(l) # Find number of components which sum to give 99% of total energy total_energy = np.repeat( np.sum( real_l ), samples.shape[1] ) threshold_energy = 0.99 * total_energy # Add one to transform from [0, N) to (0, N] k = np.argmin( np.square( np.cumsum(real_l) - threshold_energy ) ) + 1 # Plot Cumulative Energy plot.figure(1) plot.semilogx( range(1, len(real_l)+1), real_l, 'r--', label='Eigenvalue $| \\lambda_k |$' ) plot.semilogx( range(1, len(real_l)+1), np.cumsum( real_l ), 'r-', label='Cumulative Energy $e = \\sum_{{i=0}}^{{k-1}} | \\lambda_i |$' ) plot.semilogx( range(1, len(real_l)+1), threshold_energy, 'b-' , label='$0.99E$' ) plot.grid(True) plot.title( 'Components required to capture 99% of energy = '+str(k) ) plot.xlabel('Number of Components ($k$)') plot.legend() plot.tight_layout() plot.savefig('a_iii.pdf') # Plot top 10 eigenfaces for idx in range( 10 ): representation = uncompress(np.abs(v[idx])) imageio.imwrite( 'vector'+str(idx+1)+'.gif', np.matrix(representation) * 255. ) # Reconstruct random sample using `num_components` num_components = 50 idx = np.random.randint( 0, samples.shape[0] ) sample = np.asarray(samples[idx]).flatten() # Deconstruct coefficients = deconstruct( sample, num_components, v ) # Reconstruct reconstruction = reconstruct( coefficients, v ) reconstruction = uncompress( np.abs(reconstruction) ) imageio.imwrite( str(idx) + '_' + str(num_components) + '_' + os.path.split(dataset[idx])[1], np.matrix(reconstruction) * 255. )