2013-02-11 96 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; 
    } 
} 
相關問題