【问题标题】:Load many images in vector<Mat> Opencv在vector<Mat> Opencv中加载许多图像
【发布时间】:2014-08-16 11:16:18
【问题描述】:

我正在尝试将多个图像存储在矢量上的文件夹中以供以后处理。问题是我得到了错误:

*Debug Assertion Failed!

Expression: vector subscript out of range.*

我正在测试的代码如下。

stringstream Nombre2;
vector<Mat> Imagen2;
for (int a=0; a<=Count;a++) 
{
    Nombre2.clear();
    Nombre2 << "C:\\Users\\Azu\\Documents\\Visual Studio 2010\\Projects\\SIFT2\\SIFT2\\BBDDFaces\\"<< a+1 << ".pgm";
    imread(Nombre2.str()).copyTo(Imagen2[a]);
}

图像名称是数字的:1.pgm,2.pgm,等等..

如果有人能帮我解决这个问题,我将不胜感激!

【问题讨论】:

  • a&lt;Count 不是a&lt;=Count

标签: c++ opencv image-processing vector mat


【解决方案1】:

所以,在你的代码中:

// since Imagen2 is empty, Imagen2[a] is an access violation.
imread(Nombre2.str()).copyTo(Imagen2[a]); 

不要太复杂,用 cv::format 代替 stringstream 就可以了,不要无用的 copyTo():

vector<Mat> images;         // another thing. use english for variable names, nothing else...
for (int a=0; a<Count;a++)  // a <=Count would do one too many...
{
    string name = format("C:\\bla\\BBDDFaces\\%d.pgm", a);
    Mat img = imread(name); // pgm implies grayscale, maybe even: imread(name,0); to return CV_8U
    if ( img.empty() )      // please, *always check* resource-loading.
    {
        cerr << "whaa " << name << " can't be loaded!" << endl;
        continue;
    }
    images.push_back(img);

    // show result:
    imshow("test",img);
    waitKey();              // yes, you need the waitKey()
}

【讨论】:

  • 感谢您的回答!但是现在,如何显示每个矢量图像?或者我如何访问它们?如果我这样做:imshow ("1" images [0]); 我可视化加载的第一张图像,但没有任何索引。提前致谢!
  • 谢谢!!我尝试了这个修改,它的工作原理!
  • 所以,玩得开心,飞行 opencv ;)
  • 什么是“使用英语,别无其他”:(
【解决方案2】:

我是劳罗·雷耶斯。 我找到了这个表格。

String filename = "lg.mp4";   //SOLO FUNCIONA CON VIDEOS TIPO MP4
    VideoCapture capture(filename);
    Mat frame;
    Mat imagenesT;
    vector<Mat> images;  // almacenar imagenes en la cola
    Mat converted_image;
 double dHeight = capture.get(CV_CAP_PROP_FRAME_COUNT);
 cout<< "tiene frames ___" << dHeight << endl;  
    if( !capture.isOpened() )
        throw "Error when reading steam_avi";
    capture >> frame;
    cvtColor(frame, converted_image, CV_BGR2GRAY);//2XYZ);

    for(int i=0 ;i< 584;i++ )
       {
        capture >> frame;
        if(frame.empty())
            break;
        cvtColor(frame, converted_image, CV_BGR2GRAY);//2XYZ);

        images.push_back(converted_image.clone()); //almacenar imagenes en la cola
       imshow("out", converted_image);
       imshow("in",frame);
       waitKey(20); // waits to display frame
        }  

    for(int i=0 ;i< 584;i++ )
       {    
     imshow("salida1",images[i]);
       waitKey(30);
       }

【讨论】:

  • 抱歉,我用 mp4 和 avi 视频试探
猜你喜欢
  • 1970-01-01
  • 2013-04-13
  • 1970-01-01
  • 1970-01-01
  • 2015-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多