2013-02-11 93 views
1

已经指定像vector<Descriptor> m_keyDescs如何将矢量<...>转换为cv :: Mat?

描述:

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

推,如:

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

如何这个向量转换为CV ::垫?

我已经试过

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

项目调试没有错误,但运行时出现错误Qt Creator中在我的Mac:

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

回答

2

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

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; 
    } 
} 
相关问题