简体   繁体   English

将图像数据复制到cv :: mat的向量中

[英]copying image data to a vector of cv::mat

vector <Mat> redAd(adFileName.size());
fill(redAd.begin(), redAd.end(), NULL);
for (int i = 0; i < adFileName.size(); ++i) {
    cout << adFileName[i].c_str() << endl;
    Mat im = imread(adFileName[i].c_str(), 1);
    cout << im.data << endl;
    redAd.push_back(im);
    cout << redAd[i].data << endl;
}

Here, adFileName is the string vector containing the file paths of the images. 在这里, adFileName是包含图像文件路径的字符串向量。 The aths are printed correctly. ath打印正确。 im.data gives a non-null value. im.data给出一个非空值。 But after pushing it into a vector <Mat> and printing an element shows "Application has stopped working". 但是将其推入vector <Mat>并打印元素后,显示“应用程序已停止工作”。

How should it be solved ? 应该如何解决?

Because, you set the vector size to adFileName.size() and then you push_back the new elements. 因为,您将向量大小设置为adFileName.size(),然后push_back新元素。 You should either contruct the vector with 0 size and use push_back or else use insert. 您应该使用0大小构造矢量并使用push_back或使用insert。

redAd.insert(redAd.begin()+i,1,im);

Instead of fill with NULLs, which is adding to the problem, you could just call the default vector constructor vector <Mat> redAd; 可以使用默认的向量构造函数vector <Mat> redAd;而不是用NULL fill (这会增加问题) vector <Mat> redAd; and then call redAd.reserve(adFileName.size()) and then leave the loop as is. 然后调用redAd.reserve(adFileName.size()) ,然后使循环保持原样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM