1

I am trying to project an image to eigenface convariance matrix that EigenFacesRecognizer of opencv returns. I use the following code to load eigenfaces parameters loading an image and trying to project the sample image to pca subspace.

Ptr<FaceRecognizer> model = createEigenFaceRecognizer();
model->load("eigenfaces.yml"); // Load eigenfaces parameters
Mat eigenvalues = model->getMat("eigenvalues"); // Eigen values of PCA
Mat convMat = model->getMat("eigenvectors"); //Convariance matrix 
Mat mean = model->getMat("mean"); // Mean value

string path = fileName;

Mat sample ,pca_ed_sample;
sample = imread(path, CV_LOAD_IMAGE_GRAYSCALE); //size 60x60
Mat nu =  sample.reshape(1,3600 ).t(); //1x3600
pca_ed_sample = (nu - mean)*(convMat);

I am keeping 5 eigenvectors, so the size of eigenvalues 5x1, convMat3600x5 mean 1x3600. When I am trying to calculate pca_ed_sample it returns me:

  cv::Exception at memory location 0x0011d300.Dimensionality reduction using default opencv  eigenfaces...
  OpenCV Error: Assertion failed (type == B.type() && (type == CV_32FC1 || type ==
  CV_64FC1 || type == CV_32FC2 || type == CV_64FC2)) in unknown function, file .\
  src\matmul.cpp, line 711`

The problem stands in nu Mat since when I trying to calculate nu*.nu.t();(1x3600* 3600x1) it returns the same issue. Am I having troubles due to reshape function?? I am trying to transform my sample mat to a vector, it seems to work but I cant understand why I cant even multiply nu with nu_transposed.

Jose Ramon
  • 5,572
  • 25
  • 76
  • 152

1 Answers1

2

Matrix multiplication is only possible with floating point data, which is what the assertion error is trying to tell you.

Your image is loaded as type CV_8U, and you must first rescale that to float using the convertTo member.

sample = imread(path, CV_LOAD_IMAGE_GRAYSCALE); //size 60x60
cv::Mat sample_float;
sample.convertTo(sample_float, CV_32F); // <-- Convert to CV_32F for matrix mult
Mat nu =  sample_float.reshape(1,3600 ).t(); //1x3600
pca_ed_sample = (nu - mean)*(convMat);
Hannes Ovrén
  • 21,229
  • 9
  • 65
  • 75