在vector <mat> Opencv </mat>中加载许多图像

时间:2014-08-16 11:16:19

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

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

*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,依此类推..

我很感激,如果有人可以帮我解决它!

2 个答案:

答案 0 :(得分:2)

所以,在你的代码中:

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

答案 1 :(得分:0)

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

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