c++ - 问题乘以 Mat 矩阵

标签 c++ opencv matrix-multiplication

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

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 时,它​​返回给我:

  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) 它返回相同的问题。由于 reshape 功能,我遇到麻烦了吗?我正在尝试将我的样本垫转换为 vector ,这似乎可行,但我不明白为什么我什至不能将 nu 与 nu_transposed 相乘。

最佳答案

矩阵乘法只能用于 float 据,这就是断言错误试图告诉您的内容。

您的图像加载为 CV_8U 类型,您必须首先使用 convertTo 将其重新调整为 float 。成员(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);

关于c++ - 问题乘以 Mat 矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20562851/

相关文章:

c++ - 在 Visual Studio 2012 中,图书馆员和链接器有什么区别?

python - 如何测量标记分割图像的平均厚度

image - 如何正确打开/解码超过 65500 * 65500 像素的 jpeg 图像?

输入多于某些输入行时 C++ 程序不工作

c++ - g++ 使用 -Wpedantic 选项 : Is there an option to disable only the warning about unnamed structs? 编译 C++11

c++ - GLSL 属性位置返回 -1

python - 多带混合使接缝更明亮、更明显

c++ - 旋转并移动到 OpenGL 中的相对中心

python - 未知批量大小的 Tensorflow tensordot

c - 为什么多线程比单线程慢?