OpenCV创建包含每行中多个图像的Matrix

时间:2013-01-30 01:37:31

标签: c++ opencv vector

我需要在OpenCV中创建图像的数据矩阵。基本上矩阵的每一行将包含同一个人的多个图像。我发现这个asRowMatrix tutorial是由@写的 但是我目前还不知道如何将多个图像复制到Matrix的一行中。我有一个图像路径的文本文件,用“;”分隔当路径指的是新主题时,例如:

Subject1/Image1.png
Subject1/Image2.png
;
Subject2/Image1.png

我最初的想法是拥有一个2D矢量:

Vector<Vector<Mat>> intra;
    while(file.good()) {
    getline(file, path);
    if((path.compare(";"))!=0){
        try{
 //Add images to person-index
            intra[curRow].push_back(imread(path,0));
        } catch (Exception const & e){
            cerr<<"OpenCV exception: "<<e.what()<<std::endl;
        }
    } else{
//";" found --> increment person-index
        curRow++;
    }
}
imshow("Intra[0,0]",intra[0][0]);

然而,我收到一个错误,我认为这是因为矢量不是大小(curRow + 1)

OpenCV Error: Assertion failed (i < size()) in unknown function, file c:\opencv\
include\opencv2\core\operations.hpp, line 2357
OpenCV exception: c:\opencv\include\opencv2\core\operations.hpp:2357: error: (-2
15) i < size()

在else中调整向量的大小并没有解决问题!任何有关解决此问题或使用不同OpenCV数据结构的指示都将非常感激!

1 个答案:

答案 0 :(得分:1)

我决定在每次添加图像时动态调整矢量大小,而不是使用push_back。这可能效率低下,但它解决了错误引用的问题。我通过@jrok回答2D向量元素访问question得到了这个想法。编辑解决方案:

int curRow=0;
int numImages=0;

string line, path, temp;
while(file.good()) {
    getline(file, path);
    if((path.compare(";"))!=0){
        try{
            faces[curRow].resize(numImages+1);
            faces[curRow][numImages] = imread(path,0);
            numImages++;
        } catch (Exception const & e){
            cerr<<"OpenCV exception: "<<e.what()<<std::endl;
        }
    } else{
        numImages=0;
        curRow++;
    }
}

希望这有助于其他人面临同样的问题!