2013-11-09 33 views
2

我現在目前正在BackgroundSubtractorMOG和BackgroundSubtractorMOG2在OpenCV中,我想嘗試JPG圖像序列作爲我的幀如何讀取OpenCV中「.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); 
} 

的人士還因爲當我用VideoCapture訪問我,我得到錯誤。 avi視頻剪輯。

VideoCapture capture("C:\movie1.avi"); 
if(!capture.isOpened()){ 
    //error in opening the video input 
    cerr << "Unable to open video file: " << videoFilename << endl; 
    exit(EXIT_FAILURE); 
} 
//read input data. ESC or 'q' for quitting 
while((char)keyboard != 'q' && (char)keyboard != 27){ 
//read the current frame 
if(!capture.read(frame)) { 
    cerr << "Unable to read next frame." << endl; 
    cerr << "Exiting..." << endl; 
    exit(EXIT_FAILURE); 
} 

回答

4
  • 試VideoCapture捕獲( 「C:/movie1.avi」); //使用單個「/」或雙「\」!
  • VideoCapture可以讀取圖像sequqnces過,如果他們是正確編號:

    VideoCapture捕捉( 「C:/images/%3d-capture.jpg」);順便說一句//,相同斜線probs如上

2

的VideoCapture方法通過@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); 
} 
1

我已經試過這個代碼cv::VideoCapture cap("G:/var/cache/zoneminder/events/1/13/10/21/07/50/00/%3d-capture.jpg");和它的作品。儘管如此,圖像文件的文件名中應該有越來越多的序列號。

-1

步驟:

  1. 商店檔案夾在一個文本文件 /home/user/Desktop/Test/train.txt
  2. 我的文本文件具有項的路徑:

    /home/user/Desktop/Test/001.ak47/001_ /home/user/Desktop/Test/002.american-flag/002_ /home/user/Desktop/Test/007.bat/007_ /home/user /Desktop/Test/014.blimp/014_ /home/user/Desktop/Test/022.b uddha-101/022_

  3. sprintf(path,"%s%04d.jpg",name,i);部分代碼會附加文件名。 我的文件的格式爲001_0001.jpg的用於文件夾001.ak47

  4. 的代碼讀取從每個文件夾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); 
        }