patrikhuber / eos

A lightweight 3D Morphable Face Model library in modern C++
Apache License 2.0
1.92k stars 598 forks source link

PCA basis calculation #50

Closed samsgates closed 8 years ago

samsgates commented 8 years ago

i tried to load PCA model by pca calculated data. i have the below PCA data to load PCA model

-Mean -eigenvectors -eigenvalues

PCA component - 7 Num of row (x,y,z,x,y,z..) - 15420

i need pca_basis to load PCA model, how to calculate PCA basis data, eigenvector is equal to PCA basis? but it's row wise pca component (like 7 x 15420) we need column wise pca component ( 15420 x 7)

PcaModel Constructor: PcaModel(cv::Mat mean, cv::Mat pca_basis, cv::Mat eigenvalues, std::vector<std::array<int, 3>> triangle_list) : mean(mean), normalised_pca_basis(pca_basis), eigenvalues(eigenvalues), triangle_list(triangle_list)

Code: eos::morphablemodel::PcaModel shapeModel(pt_mean, pt_eig_vecs, pt_eig_vals, triangleList);

patrikhuber commented 8 years ago

I'm not sure what exactly your question is. Just use OpenCV PCA or Matlab pca to do standard PCA on your data and feed that into the eos PcaModel constructor. The dimensions that the different cv::Mat's should have are all nicely documented in the PcaModel class.

samsgates commented 8 years ago

Thanks. i have converted eigenvectors values as vertical matrix from horizontal and problem is solved. PCA model loaded successfully. i can get mean, and nun of component values from PCAshapeModel with out any issues, but i can't generate sample mesh

Testing code:

vector fitted_coeffs; fitted_coeffs.assign (15420,0.3); eos::render::Mesh mesh = morphModel.draw_sample(fitted_coeffs, vector()); eos::render::write_obj(mesh, "sam.obj");

Error message: OpenCV Error: Sizes of input arguments do not match (The operation is neither 'array op array' (where arrays have the same size and the same number of channels), nor 'array op scalar', nor 'scalar op array') in arithm_op, file /var/opencvpython/opencv/modules/core/src/arithm.cpp, line 639

Problematic line (Debug line): cv::Mat model_sample = mean + normalised_pca_basis * alphas;

problem is adding mean and pca_basis values, i don't know what's wrong with mean and pca_basis values, both cv:Mat's are same row counts

matrix values mean = 15420 x 1 pca_basis = 15420 x 7

how to solve this issue?

patrikhuber commented 8 years ago

What is the dimension of your alphas? You can easily figure this out by yourself. Do you know how matrix multiplication works? If you multiply two matrices, one of dimensions m x n and the second one r x s, then n needs to be equal to r for it to work and the resulting matrix will be a m x s matrix. So check that normalised_pca_basis * alphas gives you a vector of the same dimensions as mean, so you can add them. Best get a sheet of paper.

samsgates commented 8 years ago

alpha dimension is 1x15420, now i understand the complete PCA system and calculation. problem is solved. i am a bad student when i studying in school, three times failed in math subject. but now become a PCA system expert after explore your project.
Thanks for your support,

patrikhuber commented 8 years ago

I'll close this then ;-)