2015-09-04 77 views
1

我對C++編程非常陌生,我需要這樣做:我有一個代碼接收兩個圖像的路徑並輸出數字(使用opencv輸出直方圖相似度) 。目錄中的文件使用OpenCV 3.0執行重複功能

我需要使用這個功能,以便不用給它第二個圖像文件,它會從文件夾中加載圖像,重複該操作的次數,使圖像在這個文件夾中,並存儲結果。

如果您可以指向我可以使用的功能或某處我可以學習這些,我將非常感謝它,我的知識非常有限。

我沒有任何限制,但我也非常失落。我想我需要把我已經有的函數,並改變爲agv [i]的argv [1]。但是,我如何從目錄中調用文件?我應該保存txt文件的所有名稱嗎?

+0

谷歌爲「dirent.h」 – Micka

+0

可能重複[我怎樣才能使用C或C++目錄中的文件列表?](http://stackoverflow.com/questions/612097/how- can-i-get-the-list-of-a-directory-using-c-or-c) – herohuyongtao

+0

[如何使用C++獲取文件夾中的所有圖像](http:// stackoverflow。 COM /問題/ 31346132 /如何到獲取,所有圖像式文件夾,使用-C) – Miki

回答

5

OpenCV的glob功能

void cv::glob ( String  pattern, 
    std::vector<String> &  result, 
    bool recursive = false 
) 

here你可以找到關於此功能

0

首先你並不需要爲所有的文件名保存一個txt文件的詳細解答。我多次遇到同樣的問題,並找到以下解決方案。作爲micka建議你必須使用dirent.h這與OpenCV的

  1. 沒有連接將您的圖像在一個文件夾並命名進去。
  2. Google for dirent.h並將其下載並添加到您的C++/opencv項目中。 (在我的例子中,我已經使用了視覺工作室,所以我不得不使用添加新的項目選項,只需google,並找到如何爲您的項目添加頭文件)
  3. 添加後,根據需要更改後運行以下代碼

    #include "dirent.h" 
    //you have to add here opencv header files also 
    int main() 
    {   
        char buffer[1000]; 
        DIR *dir; 
        struct dirent *ent; 
        if ((dir = opendir("file path to your folder that images are stored"))!= NULL) { 
    
        while (((ent = readdir (dir)) != NULL)) 
        { 
        char *b=ent->d_name; 
    
        if(b[0]!='.') 
        { 
           sprintf(buffer,"file path to your folder that images are stored\\%s",ent->d_name); 
         /* This character array named "buffer" contains the file path to 
         each of your image in a folder. For a example if 
         your image file paths are 
    
         c://yourfolder//dew3.jpg 
         c://yourfolder//d3dse.jpg 
         c://yourfolder//se.jpg  
    
         and during the first iteration of while loop string in "buffer" 
         will be 
         c://yourfolder//dew3.jpg 
    
         and during the 2nd iteration of while loop string in "buffer" 
         will be 
         c://yourfolder//d3dse.jpg 
    
         and during the 3rd iteration of while loop string in "buffer" 
         will be 
         c://yourfolder//se.jpg 
         */ 
    
          // do your opencv stuff here 
          printf("%s\n", ent->d_name); /* print all the files and   
          directories within directory */ 
    
         }   
         else 
         {} 
    
    
        } 
        closedir (dir); 
    } 
    else 
        { 
        /* could not open directory */ 
        perror (""); 
        return EXIT_FAILURE; 
        } 
        return 0; 
        } 
    

    所以你看到dirent.h和sprintf做的工作已經這兒過得加入了一些描述如上述評論,並指他們這too.Hope幫助。詢問你是否有什麼要澄清。謝謝!!