日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

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

發(fā)布時間:2023/12/20 c/c++ 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Windows 下 C++ 利用 OpenCV glob 函数获取文件夹下所有文件绝对路径 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

緒論

本文記錄 Windows 下 C++ 利用 OpenCv glob 函數(shù)得到 文件夾下所有文件的絕對路徑含文件名)。本文還含有 std::string::find()等函數(shù)的記錄。如果是 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 函數(shù)的原型如下

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

注意 pattern 和 result 都是 cv :: String 類型。 但是 pattern 呢,即使我們傳進(jìn)去的參數(shù)時 std::string ,它也會自動生成一個 cv::string 的 copy。result 參數(shù)呢?因為它被當(dāng)作非 const 引用,所以必須用匹配的類型。

當(dāng)參數(shù) recursivefalse 時,僅僅遍歷指定文件夾內(nèi)符合模式的文件
當(dāng) recursivetrue 時,會同時遍歷指定文件夾的子文件夾

最后,補(bǔ)充一個代碼,來自這篇博文:

由于 glob 遍歷圖像名稱不是按順序進(jìn)行遍歷的;
在讀取圖像序列的時候經(jīng)常要按順序讀取,如在多目標(biāo)跟蹤中;
這時可以 sort 進(jìn)行排序;

//獲取文件夾下所有圖像名稱, // 圖像名稱按升序排列 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 參數(shù)是從 主串查找起點的 索引(從 0 開始),返回的是模式串在主串中的位置。
注意 (3),模式串 s 可以不被完全使用,可以用參數(shù) 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; //查找對象–字符
結(jié)果:找到 – 返回 第一個字符的索引

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

結(jié)果如下所示

結(jié)果: 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() 的區(qū)別

C++ STL vector 數(shù)據(jù)結(jié)構(gòu),emplace_back() 和 push_back() 的區(qū)別,就在于底層實現(xiàn)的機(jī)制不同。push_back() 向容器尾部添加元素時首先會創(chuàng)建這個元素然后再將這個元素拷貝或者移動到容器中如果是拷貝的話,事后會自行銷毀先前創(chuàng)建的這個元素);而 emplace_back() 在實現(xiàn)時,則是直接在容器尾部創(chuàng)建這個元素,省去了拷貝或移動元素的過程。

std::string::npos

npos是一個常數(shù),表示size_t的最大值(Maximum value for size_t)。許多容器都提供這個東西,用來表示不存在的位置

stoi

把數(shù)字字符串轉(zhuǎn)換成 int 輸出。

總結(jié)

以上是生活随笔為你收集整理的Windows 下 C++ 利用 OpenCV glob 函数获取文件夹下所有文件绝对路径的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。