1

I am very new to C++ programming and I need to do this: I have a code that receives the path to two images and outputs a number (histogram similarity using opencv).

I need to use this function so that instead of giving it the second image file, it will load images from a folder, repeating the operation as many times as images are in this folder, and storing the result.

If you could point towards functions I can use or somewhere I can learn this I would very much appreciate it, my knowledge is very limited.

I have no restrictions but I'm also very very lost. I suppose I need to put the function I already have and change argv[1] for agv[i]. But how do I call the files from the directory? Should I save all the names for the files in a txt?

halfer
  • 19,824
  • 17
  • 99
  • 186
Fer Nava
  • 43
  • 3
  • google for "dirent.h" – Micka Sep 04 '15 at 21:40
  • possible duplicate of [How can I get the list of files in a directory using C or C++?](http://stackoverflow.com/questions/612097/how-can-i-get-the-list-of-files-in-a-directory-using-c-or-c) – herohuyongtao Sep 05 '15 at 03:42
  • possible duplicate of [How to get all images in folder using c++](http://stackoverflow.com/questions/31346132/how-to-get-all-images-in-folder-using-c) – Miki Sep 05 '15 at 12:35

2 Answers2

5

OpenCV has glob function

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

here you can find a detailed answer about this function

Community
  • 1
  • 1
sturkmen
  • 3,495
  • 4
  • 27
  • 63
0

First of all you don't need to save all the names for the files in a txt file. I have faced the same problem many times ago and found the following solution. As micka suggested you have to use dirent.h which has no connection with opencv

  1. Put your images in one folder and name it.
  2. Google for dirent.h and download it and add it to your C++/opencv project. (In my case I have used visual studio so I had to use add new item option. Just google and find how to add header file for your project)
  3. After adding it run the following code after changing it according to your needs

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

    So you see that the dirent.h and sprintf do the job here.I have added some description as comments above and refer them too.Hope this helps. Ask if you have anything to clarify. Thanks!!

d91
  • 83
  • 1
  • 8