绪论

本文记录 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.jpgstring::size_type iPos = fn[i].find_last_of('/') + 1;string filename = fn[i].substr(iPos, fn[i].length() - iPos);//cout << filename << endl;//2.获取不带后缀的文件名,000001string 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::stringint 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 输出。

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

  1. R语言sys方法:sys.getpid函数获取R会话的进程ID、sys.glob函数和file.path函数匹配文件夹下的所有特定类型文件、sys.info函数获取系统和用户信息

    R语言sys方法:sys.getpid函数获取R会话的进程ID.sys.glob函数和file.path函数匹配文件夹下的所有特定类型文件.sys.info函数获取系统和用户信息 目录

  2. Windows批处理命令快速获取文件夹下特定类型的文件名(2022.5.15)

    Windows批处理命令快速获取文件夹下特定类型的文件名 (2022.5.15) 1.需求分析 2.batch简介 3.代码实现 3.1 八种编程语言分别实现 3.1.1 C# 实现 3.1.2 C+ ...

  3. 利用OpenCV的函数cvtcolor()进行颜色空间转换时需要注意的地方

    OpenCV的函数cvtcolor()的原型如下: C++原型: void cv::cvtColor(InputArray src,OutputArray dst,int code,int dstCn ...

  4. linux打开文件夹所有文件名,获取文件夹下的所有文件名 (linux windows)

    windows下获取文件夹下的所有文件名 1 #include "folder.hpp" 2 #include 3 4 void getFilesFromFolder(const ...

  5. 利用OpenCV的函数matchTemplate()实现在图像中寻找、检索、搜索模板图像【图像模板匹配】

    利用OpenCV的函数matchTemplate()实现在图像中寻找.检索.搜索模板图像[图像模板匹配] 在博文 https://www.hhai.cc/thread-220-1-1.html 中我们 ...

  6. Linux环境下服务器利用组播来获取客户端IP

    Linux环境下服务器利用组播来获取客户端IP 单播是两个主机之间端对端通信(比如TCP.UDP通信),而广播用于一个主机对整个局域网中所有主机的通信.单播和广播是两个极端,要么对一个主机通信,要么对 ...

  7. 利用python批量查询企业信息_python实现批量获取指定文件夹下的所有文件的厂商信息...

    本文实例讲述了python实现批量获取指定文件夹下的所有文件的厂商信息的方法.分享给大家供大家参考.具体如下: 功能代码如下: import os, string, shutil,re import ...

  8. Windows C++ 获取当前文件夹下有几个文件

    Windows C++ 获取当前文件夹下有几个文件 百度下 居然没有. linux下通过: struct dirent **namelist; int total=scandir(dir,&n ...

  9. Windows下快速获取一个文件夹下所有文件的名称列表

    Windows下快速获取一个文件夹下所有文件的名称列表 进入windows终端:win+R 输入cmd,回车 使用cd命令进入到目标文件夹.例如: 先进入D盘,输入d: 回车, 然后 cd openc ...

最新文章

  1. 【干货】裸金属服务Ironic项目介绍
  2. nodejs 当前文件路径_NodeJs的几种文件路径
  3. 【步态识别】基于CNN深度学习的步态识别算法的MATLAB仿真
  4. 蓝桥杯C++ AB组辅导课 第一讲 递归与递推 Acwing
  5. mfc读取txt文件并显示_Python入门丨文件读写
  6. C#与halcon联合开发——内存溢出
  7. hadoop loadBalance源码分析
  8. sql如何遍历几百万的表_SQL Server遍历表中记录的2种方法(使用表变量和游标)
  9. 欧洲与北美5G开战,最后的赢家却是高通?
  10. python中format函数用法简书_Python format 格式化函数
  11. 手把手教你申请计算机软件专利著作权(3)——软件使用说明书
  12. oracle begin 后声明,Oracle BEGIN END 详细用法
  13. 如何彻底关闭Windows更新
  14. 关于“wuauclt.exe”病毒的清理
  15. css 设置层级关系,css层级关系怎么设置
  16. markdown写公众号
  17. idea 全局搜索快捷键冲突_intellij idea 的全局搜索快捷键方法
  18. 大数据能否解决城市所面临的环境问题
  19. 解读ORACLE数据库的统一命名与编码规范
  20. TIPTOP ERP 如何客制单据别自动编码的应用

热门文章

  1. hashcat破解wifi密码(kali)
  2. SSL数字证书(三)使用 openssl 生成证书
  3. UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 0: ordinal not in range(128)
  4. hana经常使用函数
  5. activiti 撤销
  6. 永远不要去依赖别人_经典语录:不要轻易去依赖一个人,它会成为你的习惯
  7. 【面试次体验】堆糖前端开发实习生
  8. c语言解析hex文件格式,HEX文件格式,ihex,hex解析
  9. 无泪的眼神---上海街头真实的一幕!(转自 燕南社区)
  10. Legacy autograd function with non-static forward method is deprecated