Windows 下 C++ 利用 OpenCV glob 函数获取文件夹下所有文件绝对路径

绪论

本文记录 Windows 下 C++ 利用 OpenCv glob 函数得到 文件夹下所有文件的绝对路径含文件名)。本文还含有 std::string::find()等函数的记录。如果是 Python 就很简单了。但是 C++还是不简单的。

#include <opencv2/opencv.hpp>
//#include <opencv2/core/utility.hpp>
#include<vector>
#include<string>
using namespace std;

std::vector<cv::String> filePathNames;    
//path of folder, you can replace "*.*" by "*.jpg" or "*.png"
std::string folderPath = "C:/codes/*.*"; 
cv::glob(folderPath, fileNames); 

// cv::string 似乎没有 find ,replace的方法
std::string stdFilePath = cvFilePath; // cv::string --->> std::string

glob 函数的原型如下


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

注意 pattern 和 result 都是 cv :: String 类型。 但是 pattern 呢,即使我们传进去的参数时 std::string ,它也会自动生成一个 cv::string 的 copy。result 参数呢?因为它被当作非 const 引用,所以必须用匹配的类型。

当参数 recursivefalse 时,仅仅遍历指定文件夹内符合模式的文件
recursivetrue 时,会同时遍历指定文件夹的子文件夹

最后,补充一个代码,来自这篇博文

由于 glob 遍历图像名称不是按顺序进行遍历的;
在读取图像序列的时候经常要按顺序读取,如在多目标跟踪中;
这时可以 sort 进行排序;

//获取文件夹下所有图像名称,
// 图像名称按升序排列
int imageNameLists(string filePath, vector<string>& nameArr)
{
    vector<cv::String> fn;
    cv::glob(filePath, fn, false);
    size_t count = fn.size();
    if (count==0)
    {
        cout << "file " << filePath << " not  exits"<<endl;
        return -1;
    }
    for (int i = 0; i < count; ++i)
    {
        //1.获取不带路径的文件名,000001.jpg
        string::size_type iPos = fn[i].find_last_of('/') + 1;
        string filename = fn[i].substr(iPos, fn[i].length() - iPos);
        //cout << filename << endl;
        //2.获取不带后缀的文件名,000001
        string name = filename.substr(0, filename.rfind("."));
        //cout << name << endl;
        nameArr.emplace_back(name);
    }
    sort(nameArr.begin(), nameArr.end(),
         [](string a, string b) {return stoi(a) < stoi(b); });
    return 0;
}

std::string::find() std::string::rfind()

由于对 std::string::find() std::string::rfind() 方法不熟悉,做个记录。
下面的 pos 参数是从 主串查找起点的 索引(从 0 开始),返回的是模式串在主串中的位置。
注意 (3),模式串 s 可以不被完全使用,可以用参数 n 指定只用模式串的前 n 个字符组成的子串作为模式串。

1)size_t find (const string& str, size_t pos = 0) const; //查找对象–string类对象
2)size_t find (const char s, size_t pos = 0) const; //查找对象–字符串

3)size_t find (const char s, size_t pos, size_t n) const; //查找对象–字符串的前n个字符
4)size_t find (char c, size_t pos = 0) const; //查找对象–字符
结果:找到 – 返回 第一个字符的索引

5)string::rfind(string, pos) 是从pos开始由右往左找,返回找到的位置。

#include <iostream>       // std::cout
#include <string>         // std::string
 
int main ()
{
  std::string str ("There are two needles in this haystack with needles.");
  std::string str2 ("needle");
 
  // different member versions of find in the same order as above:
  std::size_t found = str.find(str2);
  if (found!=std::string::npos)
    std::cout << "first 'needle' found at: " << found << '\n';
 
 // 主串是 str = "There are two needles in this haystack with needles."
 // 模式串是"needles are small"的前7个字符
  found=str.find("needles are small",found+1,7);
  if (found!=std::string::npos)
    std::cout << "second 'needle' found at: " << found << '\n';
 
  found=str.find("haystack");
  if (found!=std::string::npos)
    std::cout << "'haystack' also found at: " << found << '\n';
 
  found=str.find('.');
  if (found!=std::string::npos)
    std::cout << "Period found at: " << found << '\n';
 
  // let's replace the first needle:
  str.replace(str.find(str2),str2.length(),"preposition");  //replace 用法
  std::cout << str << '\n';
 
  found = str.rfind(str2);
  if (found != std::string::npos)
        std::cout << "From right to left, needle' found at: " << found << '\n';
  return 0;
}

结果如下所示

结果:
first 'needle' found at: 14
second 'needle' found at: 44
'haystack' also found at: 30
Period found at: 51
There are two prepositions in this haystack with needles
From right to left, needle' found at: 44

vector emplace_back() 和 push_back() 的区别

C++ STL vector 数据结构,emplace_back() 和 push_back() 的区别,就在于底层实现的机制不同。push_back() 向容器尾部添加元素时首先会创建这个元素然后再将这个元素拷贝或者移动到容器中如果是拷贝的话,事后会自行销毁先前创建的这个元素);而 emplace_back() 在实现时,则是直接在容器尾部创建这个元素,省去了拷贝或移动元素的过程。

std::string::npos

npos是一个常数,表示size_t的最大值(Maximum value for size_t)。许多容器都提供这个东西,用来表示不存在的位置

stoi

把数字字符串转换成 int 输出。

  • 3
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
引用提供了关于OpenCV中的glob函数的信息。glob函数的作用是将指定路径下的所有文件名存储到一个vector中。函数的定义如下:void cv::glob(String pattern, std::vector<String> &result, bool recursive = false)。其中,pattern是路径模式,result是存储文件名的vector,recursive是一个布尔值,表示是否递归地搜索子文件夹函数可以直接通过cv::glob()进行调用。 引用提供了一个使用glob函数的示例代码。代码中,首先创建了一个存储文件名的vector filenames_in_folder。然后,使用glob函数将指定路径下的文件名存储到filenames_in_folder_cv中。最后,通过循环将filenames_in_folder_cv中的文件名添加到filenames_in_folder中。 另外,引用也给出了一个使用glob函数的示例代码。代码中,首先指定了一个文件夹路径imagepath,并创建了一个vector everyimgpath来存储每张图片的路径。然后,使用glob函数将imagepath路径下的所有图片路径保存到everyimgpath中。接着,创建了一个大小与everyimgpath相同的vector imgname,用于存储每张图片的名称。通过循环遍历everyimgpath中的路径,使用正则表达式提取出每张图片的名称,并将其存储到imgname中。最后,通过循环输出每张图片的路径和名称。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [OPENCV函数整理](https://blog.csdn.net/juluwangriyue/article/details/110127676)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [C++把图片的文件名保存到数组里,opencvglob()函数](https://blog.csdn.net/dwf1354046363/article/details/118195805)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

培之

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值