从文件目录执行重复的功能,使用OpenCV 3.0

Files from directory to perform a repeated function, using OpenCV 3.0

本文关键字:使用 OpenCV 功能 文件目录 执行      更新时间:2023-10-16

我是c++编程的新手,我需要这样做:我有一个代码,接收两个图像的路径并输出一个数字(使用opencv的直方图相似度)。

我需要使用这个函数,而不是给它第二个图像文件,它将从一个文件夹加载图像,重复操作的次数,图像在这个文件夹中,并存储结果。

如果你能指出我可以使用的功能或在某个地方我可以学习这个,我会非常感激,我的知识非常有限。

我没有限制,但我也非常非常失落。我想我需要把函数我已经有和改变argv[1]为agv[I]。但是如何调用目录中的文件呢?我是否应该将所有文件的名称保存为txt格式?

OpenCV具有glob功能

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

在这里你可以找到关于这个函数的详细答案

首先,您不需要将所有文件的名称保存在txt文件中。我以前遇到过很多次同样的问题,并找到了以下解决方案。正如micka建议的那样,你必须使用direct .h,它与opencv

没有连接。
    把你的图片放在一个文件夹里,并命名它。
  1. 谷歌direct .h下载并添加到你的c++/opencv项目。(在我的例子中,我使用的是visual studio,所以我必须使用add new item选项。只需谷歌,找到如何添加头文件为您的项目)
  2. 添加后根据需要修改后运行以下代码

     #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("%sn", 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;
      }
    

    你可以看到,direction。h和sprintf在这里起了作用。我添加了一些描述作为上面的评论,并参考它们。希望这对你有所帮助。询问是否有什么需要澄清的。谢谢! !