【问题标题】:OpenCV create Matrix containing multiple images in each rowOpenCV创建矩阵,每行包含多个图像
【发布时间】:2013-01-30 01:37:31
【问题描述】:

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

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

我最初的想法是有一个二维向量:

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 数据结构的指针将不胜感激!

【问题讨论】:

    标签: c++ opencv vector


    【解决方案1】:

    我决定在每次添加图像时动态调整矢量大小,而不是使用 push_back。这可能效率低下,但它解决了不正确引用的问题。我通过@jrok 对二维向量元素访问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++;
        }
    }
    

    希望这可以帮助其他面临同样问题的人!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多