Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/158.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何读取“顺序”;。jpg";OpenCV中的图像?_C++_Opencv_Image Processing - Fatal编程技术网

C++ 如何读取“顺序”;。jpg";OpenCV中的图像?

C++ 如何读取“顺序”;。jpg";OpenCV中的图像?,c++,opencv,image-processing,C++,Opencv,Image Processing,我现在正在OpenCV中尝试BackgroundSubtractorMOG和BackgroundSubtractorMOG2,我想尝试将jpg图像序列作为我的帧源 frame = imread("C:\images\001-capture.jpg"); if(!frame.data){ //error in opening the first image cerr << "Unable to open first image frame: " <<

我现在正在OpenCV中尝试BackgroundSubtractorMOG和BackgroundSubtractorMOG2,我想尝试将jpg图像序列作为我的帧源

frame = imread("C:\images\001-capture.jpg");
    if(!frame.data){
    //error in opening the first image
    cerr << "Unable to open first image frame: " << fistFrameFilename << endl;
    //exit(EXIT_FAILURE);
}
frame=imread(“C:\images\001 capture.jpg”);
如果(!frame.data){
//打开第一个图像时出错
cerr
  • 尝试视频捕获(“C:/movie1.avi”);//使用单个“/”或双“\”
  • 如果图像序列编号正确,VideoCapture也可以读取图像序列:

    VideoCapture捕获(“C:/images/%3d capture.jpg”);//顺便说一句,与上面相同的斜杠问题


@berak所述的视频捕获方法是正确的;尽管我在使用它时不可避免地会遇到问题。在阅读顺序图像时,我始终喜欢下面所述的更直接的方法。它让您在不限制速度的情况下,对数据的浏览方式有更多的控制

char* Dataset_Dir( "C:/Data/" ); // Or take it from argv[1]
cv::Mat normal_matrix;
std::vector<cv::Mat>* image_stack;
for( int i=1; i<=endNumber; ++i )
{
    // Gives the entire stack of images for you to go through
    image_stack->push_back(cv::imread(std::format("%s/%03d-capture.png", Dataset, i), CV_LOAD_IMAGE_COLOR)); 

    normal_matrix = cv::imread(std::format("%s/%03d-capture.png", Dataset, i), CV_LOAD_IMAGE_COLOR);
}
char*Dataset\u Dir(“C:/Data/”);或从argv[1]获取
cv::Mat正规矩阵;
std::vector*图像_堆栈;
对于(inti=1;ipush_back(cv::imread(std::format(“%s/%03d capture.png”,Dataset,i),cv_LOAD_IMAGE_COLOR));
正常矩阵=cv::imread(标准::格式(“%s/%03d capture.png”,数据集,i),cv\u LOAD\u IMAGE\u COLOR);
}

我试过这段代码
cv::VideoCapture(“G:/var/cache/zonemider/events/1/13/10/21/07/50/00/%3d capture.jpg”);
可以正常工作。但图像文件的文件名中应该有越来越多的数字,以便按顺序读取。

步骤:

  • 将文件夹路径存储在文本文件中 /home/user/Desktop/Test/train.txt
  • 我的文本文件包含以下条目:

    /主页/用户/桌面/测试/001.ak47/001_ /主页/用户/桌面/测试/002.american-flag/002_ /主页/用户/桌面/测试/007.bat/007_ /主页/用户/桌面/测试/014.blimp/014_ /主页/用户/桌面/测试/022.1-101/022_

  • sprintf(路径,“%s%04d.jpg”,名称,i);
    部分代码会追加文件名。 对于文件夹
    001.ak47
    等,我的文件的格式为
    001_0001.jpg

  • 代码从每个文件夹读取20个图像并显示

     int main ()
        {
               ifstream file("/home/user/Desktop/Test/train.txt");
               string temp;
               string string2;
               int count = 0; // number of folders
                   int number_of_folders = 5 ;
                   char name[70];
               char path[70];
               while(count != number_of_folders)
               {
                    getline(file, temp); // read first line of folder             //basically path to first folder
                    int i=1;
                    strcpy(name, temp.c_str());
    
                    while(1)
                  {
    
                    sprintf(path,"%s%04d.jpg",name,i);
                    Mat src= imread(path,1);
    
                     if(!src.data || src.rows == 0 ||  i == 21 ) break;        //use only 20 images for training
                    imshow("src",src);
                    i++;
                    waitKey();
    
                   }
    
    
                    count = count+1 ;
                    if(count == number_of_folders)
                        break;
               }
             file.close();
             return(0);
        }
    
  •  int main ()
        {
               ifstream file("/home/user/Desktop/Test/train.txt");
               string temp;
               string string2;
               int count = 0; // number of folders
                   int number_of_folders = 5 ;
                   char name[70];
               char path[70];
               while(count != number_of_folders)
               {
                    getline(file, temp); // read first line of folder             //basically path to first folder
                    int i=1;
                    strcpy(name, temp.c_str());
    
                    while(1)
                  {
    
                    sprintf(path,"%s%04d.jpg",name,i);
                    Mat src= imread(path,1);
    
                     if(!src.data || src.rows == 0 ||  i == 21 ) break;        //use only 20 images for training
                    imshow("src",src);
                    i++;
                    waitKey();
    
                   }
    
    
                    count = count+1 ;
                    if(count == number_of_folders)
                        break;
               }
             file.close();
             return(0);
        }