fen1ksss
fen1ksss

Reputation: 1120

How to convert vector<...> to cv::Mat?

Having vector<Descriptor> m_keyDescs

Descriptor specified like:

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

Pushed like:

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

How to convert this vector to cv::Mat?

I have tried

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

The project debugs without errors, but when it runs an error appears in Qt Creator on my mac:

test quit unexpectedly Click reopen to open the application again.

Upvotes: 2

Views: 7365

Answers (2)

Vikas
Vikas

Reputation: 2260

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

Upvotes: 0

Radford Parker
Radford Parker

Reputation: 731

You cannot convert a vector of a manually defined class directly to a Mat. For example, OpenCV has no idea where to put each element and the elements aren't even all the same variable type (the third isn't even a single element so it can't be an element in a Mat). However, you can convert a vector of ints or floats directly to a Mat, for example. See more information in the answer here.

Upvotes: 2

Related Questions