Python與C++ 遍歷文件夾下的所有圖片實現(xiàn)代碼
Pyhton與C++ 遍歷文件夾下的所有圖片實現(xiàn)代碼
前言
雖然本文說的是遍歷圖片,但是遍歷其他文件也是可以的。
在進行圖像處理的時候,大部分時候只需要處理單張圖片。但是一旦把圖像處理和機器學(xué)習(xí)相結(jié)合,或者做一些稍大一些的任務(wù)的時候,常常需要處理好多圖片。而這里面,一個最基本的問題就是如何遍歷這些圖片。
用OpenCV做過人臉識別的人應(yīng)該知道,那個項目中并沒有進行圖片的遍歷,而是用了一種輔助方案,生成了一個包含所有圖片路徑的文件at.txt,然后通過這個路徑來讀取所有圖片。而且這個輔助文件不僅包含了圖片的路徑,還包含了圖片對應(yīng)的標(biāo)簽。所以在進行訓(xùn)練的時候直接通過這個輔助文件來讀取訓(xùn)練用的圖片和標(biāo)簽。
其實如果去看看教程,會發(fā)現(xiàn)這個at.txt的生成是通過Python代碼來實現(xiàn)。所以今天就來看一下如何用C++來實現(xiàn)文件夾下所有圖片的遍歷。
當(dāng)然在此之前還是先給出Python遍歷的代碼,以備后用。
Python遍歷
在之前的數(shù)獨項目中,進行圖像處理的時候用到了遍歷文件夾下所有的圖片。主要是利用glob模塊。glob是python自己帶的一個文件操作相關(guān)模塊,內(nèi)容不多,可以用它查找符合自己目的的文件。
# encoding: UTF-8
import glob as gb
import cv2
#Returns a list of all folders with participant numbers
img_path = gb.glob("numbers\\*.jpg")
for path in img_path:
img = cv2.imread(path)
cv2.imshow('img',img)
cv2.waitKey(1000)
C++遍歷
1. opencv自帶函數(shù)glob()遍歷
OpenCV自帶一個函數(shù)glob()可以遍歷文件,如果用這個函數(shù)的話,遍歷文件也是非常簡單的。這個函數(shù)非常強大,人臉識別的時候用這個函數(shù)應(yīng)該會比用at.txt更加方便。一個參考示例如下。
#include<opencv2\opencv.hpp>
#include<iostream>
using namespace std;
using namespace cv;
vector<Mat> read_images_in_folder(cv::String pattern);
int main()
{
cv::String pattern = "G:/temp_picture/*.jpg";
vector<Mat> images = read_images_in_folder(pattern);
return 0;
}
vector<Mat> read_images_in_folder(cv::String pattern)
{
vector<cv::String> fn;
glob(pattern, fn, false);
vector<Mat> images;
size_t count = fn.size(); //number of png files in images folder
for (size_t i = 0; i < count; i++)
{
images.push_back(imread(fn[i]));
imshow("img", imread(fn[i]));
waitKey(1000);
}
return images;
}
需要注意的是,這里的路徑和模式都用的是cv::String。
2. 自己寫一個遍歷文件夾的函數(shù)
在windows下,沒有dirent.h可用,但是可以根據(jù)windows.h自己寫一個遍歷函數(shù)。這就有點像是上面的glob的原理和實現(xiàn)了。
#include<opencv2\opencv.hpp>
#include<iostream>
#include <windows.h> // for windows systems
using namespace std;
using namespace cv;
void read_files(std::vector<string> &filepaths,std::vector<string> &filenames, const string &directory);
int main()
{
string folder = "G:/temp_picture/";
vector<string> filepaths,filenames;
read_files(filepaths,filenames, folder);
for (size_t i = 0; i < filepaths.size(); ++i)
{
//Mat src = imread(filepaths[i]);
Mat src = imread(folder + filenames[i]);
if (!src.data)
cerr << "Problem loading image!!!" << endl;
imshow(filenames[i], src);
waitKey(1000);
}
return 0;
}
void read_files(std::vector<string> &filepaths, std::vector<string> &filenames, const string &directory)
{
HANDLE dir;
WIN32_FIND_DATA file_data;
if ((dir = FindFirstFile((directory + "/*").c_str(), &file_data)) == INVALID_HANDLE_VALUE)
return; /* No files found */
do {
const string file_name = file_data.cFileName;
const string file_path = directory + "/" + file_name;
const bool is_directory = (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
if (file_name[0] == '.')
continue;
if (is_directory)
continue;
filepaths.push_back(file_path);
filenames.push_back(file_name);
} while (FindNextFile(dir, &file_data));
FindClose(dir);
}
3. 基于Boost
如果電腦上配置了boost庫,用boost庫來實現(xiàn)這一功能也是比較簡潔的。為了用這個我還專門完全編譯了Boost。
然而只用到了filesystem。
#include <boost/filesystem.hpp>
#include<iostream>
#include<opencv2\opencv.hpp>
using namespace cv;
using namespace std;
using namespace boost::filesystem;
void readFilenamesBoost(vector<string> &filenames, const string &folder);
int main()
{
string folder = "G:/temp_picture/";
vector<string> filenames;
readFilenamesBoost(filenames, folder);
for (size_t i = 0; i < filenames.size(); ++i)
{
Mat src = imread(folder + filenames[i]);
if (!src.data)
cerr << "Problem loading image!!!" << endl;
imshow("img", src);
waitKey(1000);
}
return 0;
}
void readFilenamesBoost(vector<string> &filenames, const string &folder)
{
path directory(folder);
directory_iterator itr(directory), end_itr;
string current_file = itr->path().string();
for (; itr != end_itr; ++itr)
{
if (is_regular_file(itr->path()))
{
string filename = itr->path().filename().string(); // returns just filename
filenames.push_back(filename);
}
}
}
各種方法都記錄在這里,以便以后用的時候查找。
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
函數(shù)外初始化與函數(shù)內(nèi)初始化詳細解析
函數(shù)內(nèi)初始化:bool FillStr(char *&szDst, int nSize);第一個參數(shù)中的&一定不能少,這是因為在函數(shù)外部我們只聲明了這個指針,具體這個指針指向內(nèi)存中的哪個地址我們并不知道,所以&是為了說明傳遞的是這個指針的引用,那么在函數(shù)內(nèi)初始化后這個指針的地址也就是外面指針的地址了2013-09-09
C語言 fseek(f,0,SEEK_SET)函數(shù)案例詳解
這篇文章主要介紹了C語言 fseek(f,0,SEEK_SET)函數(shù)案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-08-08

