Python遍历
在之前的数独项目中,进行图像处理的时候用到了遍历文件夹下所有的图片。主要是利用glob模块。glob是python自己带的一个文件操作相关模块,内容不多,可以用它查找符合自己目的的文件。
?12345678910 # 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++遍历
- opencv自带函数glob()遍历
OpenCV自带一个函数glob()可以遍历文件,如果用这个函数的话,遍历文件也是非常简单的。这个函数非常强大,人脸识别的时候用这个函数应该会比用at.txt更加方便。一个参考示例如下。
12345678910111213141516171819202122232425262728293031 #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; }
- 自己写一个遍历文件夹的函数
在windows下,没有dirent.h可用,但是可以根据windows.h自己写一个遍历函数。这就有点像是上面的glob的原理和实现了。
?12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 #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); }
- 基于Boost
如果电脑上配置了boost库,用boost库来实现这一功能也是比较简洁的。为了用这个我还专门完全编译了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); } } }