【问题标题】:Opencv C++ Read opencv Mat assigned to Eigen VectorOpencv C++读取分配给特征向量的opencv Mat
【发布时间】:2016-04-05 14:29:52
【问题描述】:

我在 opencv 中有一个矩阵 data = 8 rows x 1 cols 声明。我想读取所有值并分配给特征向量。

#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;
}

但在 Vector actualLable 处输出全部为 0。 如何从 Mat 数据中检索实际值?

【问题讨论】:

  • 为什么在写之前把值存入int变量?
  • @user3896254 你能具体告诉我我应该在哪里改变吗?
  • int val = train_label.at&lt;double&gt;(i, k);double val = train_label.at&lt;double&gt;(i, k);

标签: c++ opencv vector eigen


【解决方案1】:

我在这里找到了解决方案想分享完整的程序

#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;
}

如果有什么我仍然可以用最短的代码改进,请发表评论。 谢谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-20
    • 1970-01-01
    • 1970-01-01
    • 2018-04-27
    • 1970-01-01
    相关资源
    最近更新 更多