2015-09-04 90 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帮助。询问你是否有什么要澄清。谢谢!!