在矢量Opencv中加载许多图像<Mat>

Load many images in vector<Mat> Opencv

本文关键字:lt Mat gt 图像 许多 Opencv 加载      更新时间:2023-10-16

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

*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,等等…

如果有人能帮我解决这个问题,我会很感激的。

所以,在你的代码中:

// 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()
}

我的眼睛。我找到了这个表格。

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);
       }