2

vector<Descriptor> m_keyDescs

指定的描述符如下:

Descriptor(float x, float y, vector<double> const& f)
{
    xi = x;
    yi = y;
    fv = f;
}

像这样推:

m_keyDescs.push_back(Descriptor(descxi, descyi, fv));

如何将此向量转换为 cv::Mat?

我努力了

descriptors_scene = cv::Mat(m_keyDescs).reshape(1);

该项目调试没有错误,但是当它运行时,我的 Mac 上的 Qt Creator 中出现错误:

测试意外退出 单击重新打开以再次打开应用程序。

4

2 回答 2

2

您不能将手动定义的类的向量直接转换为 Mat。例如,OpenCV 不知道将每个元素放在哪里,并且这些元素甚至都不是相同的变量类型(第三个甚至不是单个元素,因此它不能是 Mat 中的元素)。但是,例如,您可以将整数或浮点数的向量直接转换为 Mat。在此处的答案中查看更多信息。

于 2013-02-13T22:05:29.157 回答
0
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

class Descriptor {
public:
  float xi;
  float yi;
  vector< double > fv;
  Descriptor(float x, float y, vector<double> const& f) :
    xi(x), yi(y), fv(f){}
};

int main(int argc, char** argv) {
  vector<Descriptor> m_keyDescs;
  for (int i = 0; i < 10; i++) {
    vector<double> f(10, 23);
    m_keyDescs.push_back(Descriptor(i+3, i+5, f));
  }
  Mat_<Descriptor> mymat(1, m_keyDescs.size(), &m_keyDescs[0], sizeof(Descriptor));
  for (int i = 0; i < 10; i++) {
    Descriptor d = mymat(0, i);
    cout << "xi:" << d.xi << ", yi:" << d.yi << ", fv:[";
    for (int j = 0; j < d.fv.size(); j++)
      cout << d.fv[j] << ", ";
    cout << "]" << endl;
  }
}
于 2013-06-13T17:56:18.527 回答