问题乘以Mat矩阵 [英] Issues multiplying Mat matrices

查看:131
本文介绍了问题乘以Mat矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将图像投影到opencv的EigenFacesRecognizer返回的本征面协方差矩阵.我使用以下代码加载本征面参数,以加载图像并尝试将样本图像投影到pca子空间.

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);

我保留了5个特征向量,所以特征值的大小为5x1,convMat3600x5的平均值为1x3600.当我尝试计算pca_ed_sample时,它返回我:

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`

问题出在Nu Mat上,因为当我尝试计算nu * .nu.t();(1x3600 * 3600x1)时,它会返回相同的问题.由于重塑功能,我会遇到麻烦吗?我正在尝试将样本垫转换为向量,它似乎可以工作,但我不明白为什么我甚至无法将nu与nu_transposed相乘.

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.

推荐答案

矩阵乘法仅适用于浮点数据,这是断言错误试图告诉您的.

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

您的图片以CV_8U类型加载,您必须首先使用转换为成员.

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);

这篇关于问题乘以Mat矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆