简体   繁体   English

Opencv C ++读取分配给特征向量的opencv Mat

[英]Opencv C++ Read opencv Mat assigned to Eigen Vector

I have a matrix data = 8 rows x 1 cols declare in opencv. 我在opencv中声明了一个矩阵data = 8 rows x 1 cols I want to read all the value and assign to Eigen Vector. 我想读取所有值并将其分配给特征向量。

#include <opencv2/core.hpp>
#include <opencv2/opencv.hpp>
#include <vector>
#include <set>
#include <Eigen/Core>
#include <Eigen/Dense>
using namespace Eigen;

int main(int argc, char** argv)
{
Mat data;
int unique = data.rows;
VectorXd actualLabel(unique);
for(int i=0;i<unique;i++)
{
   for(int k = 0; k < train_label.cols; k++)
   {
       int val = train_label.at<double>(i, k);
       actualLabel(i) = val;
       cout<< actualLabel(i) << endl;
    }
}

return 0;
}

but the output is all 0 at Vector actualLable. 但在Vector ActualLable处输出为全0 How can i retrieve the actual value from Mat data? 如何从Mat数据中检索实际值?

I found the solution here would like to share the full program 我发现这里的解决方案想共享整个程序

#include <opencv2/core.hpp>
#include <opencv2/opencv.hpp>
#include <vector>
#include <set>
#include <Eigen/Core>
#include <Eigen/Dense>
using namespace Eigen;

int main(int argc, char** argv)
{
//example if data with assigned value 
Mat kern = (Mat_<double>(8, 1) << 0, -1, 0,-1, 5, -1,0, -1);
cout << kern << endl;
int unique = data.rows;
VectorXd actualLabel(unique);
for(int i=0;i<unique;i++)
{
   for(int k = 0; k < train_label.cols; k++)
   {
       double val = train_label.at<Vec2d>(i, k)[0];
       actualLabel(i) = val;
       cout<< actualLabel(i) << endl;
    }
}

return 0;
}

please comment if anything i can still improve here with shortest code. 请评论是否有什么我可以用最短的代码改进的地方。 Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM