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::stringglob 函數的原型如下
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 引用,所以必須用匹配的類型。
當參數 recursive為 false 時,僅僅遍歷指定文件夾內符合模式的文件,
當 recursive 為 true 時,會同時遍歷指定文件夾的子文件夾。
最后,補充一個代碼,來自這篇博文:
由于 glob 遍歷圖像名稱不是按順序進行遍歷的;
在讀取圖像序列的時候經常要按順序讀取,如在多目標跟蹤中;
這時可以 sort 進行排序;
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: 44vector 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 函数获取文件夹下所有文件绝对路径的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 密码字典生成器
- 下一篇: Unity 2D 自定义碰撞系统(一)