PHP前端开发

Python与C++如何遍历文件夹下的所有图片的实现代码分享

百变鹏仔 3小时前 #Python
文章标签 遍历

这篇文章主要介绍了 python与c++ 遍历文件夹下的所有图片实现代码的相关资料,需要的朋友可以参考下

 Pyhton与C++ 遍历文件夹下的所有图片实现代码

前言

虽然本文说的是遍历图片,但是遍历其他文件也是可以的。

在进行图像处理的时候,大部分时候只需要处理单张图片。但是一旦把图像处理和机器学习相结合,或者做一些稍大一些的任务的时候,常常需要处理好多图片。而这里面,一个最基本的问题就是如何遍历这些图片。

立即学习“Python免费学习笔记(深入)”;

用OpenCV做过人脸识别的人应该知道,那个项目中并没有进行图片的遍历,而是用了一种辅助方案,生成了一个包含所有图片路径的文件at.txt,然后通过这个路径来读取所有图片。而且这个辅助文件不仅包含了图片的路径,还包含了图片对应的标签。所以在进行训练的时候直接通过这个辅助文件来读取训练用的图片和标签。

其实如果去看看教程,会发现这个at.txt的生成是通过Python代码来实现。所以今天就来看一下如何用C++来实现文件夹下所有图片的遍历。

当然在此之前还是先给出Python遍历的代码,以备后用。

Python遍历

在之前的数独项目中,进行图像处理的时候用到了遍历文件夹下所有的图片。主要是利用glob模块。glob是python自己带的一个文件操作相关模块,内容不多,可以用它查找符合自己目的的文件。

# encoding: UTF-8import glob as gbimport cv2#Returns a list of all folders with participant numbersimg_path = gb.glob("numbers\*.jpg") for path in img_path:  img = cv2.imread(path)   cv2.imshow('img',img)  cv2.waitKey(1000)

C++遍历

1. opencv自带函数glob()遍历

OpenCV自带一个函数glob()可以遍历文件,如果用这个函数的话,遍历文件也是非常简单的。这个函数非常强大,人脸识别的时候用这个函数应该会比用at.txt更加方便。一个参考示例如下。

#include<opencv2>#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<: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 <p>需要注意的是,这里的路径和模式都用的是cv::String。</p><p><strong>2. 自己写一个遍历文件夹的函数</strong></p><p>在windows下,没有<a href="http://www.php.cn/wiki/1275.html" target="_blank">dir</a>ent.h可用,但是可以根据windows.h自己写一个遍历函数。这就有点像是上面的glob的原理和实现了。</p><pre class="brush:cpp;">#include<opencv2>#include<iostream>#include <windows.h> // for windows systemsusing namespace std;using namespace cv;void read_files(std::vector<string> &amp;filepaths,std::vector<string> &amp;filenames, const string &amp;directory);int main(){  string folder = "G:/temp_picture/";  vector<string> filepaths,filenames;  read_files(filepaths,filenames, folder);  for (size_t i = 0; i  &amp;filepaths, std::vector<string> &amp;filenames, const string &amp;directory){  HANDLE dir;  WIN32_FIND_DATA file_data;  if ((dir = FindFirstFile((directory + "/*").c_str(), &amp;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 &amp; 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, &amp;file_data));  FindClose(dir);}</string></string></string></string></windows.h></iostream></opencv2>

3. 基于Boost

如果电脑上配置了boost库,用boost库来实现这一功能也是比较简洁的。为了用这个我还专门完全编译了Boost。

然而只用到了filesystem。

#include <boost>#include<iostream>#include<opencv2>using namespace cv;using namespace std;using namespace boost::filesystem;void readFilenamesBoost(vector<string> &amp;filenames, const string &amp;folder);int main(){  string folder = "G:/temp_picture/";  vector<string> filenames;  readFilenamesBoost(filenames, folder);  for (size_t i = 0; i  &amp;filenames, const string &amp;folder){  path directory(folder);  directory_iterator itr(directory), end_itr;  string current_file = itr-&gt;path().string();  for (; itr != end_itr; ++itr)  {    if (is_regular_file(itr-&gt;path()))    {      string filename = itr-&gt;path().filename().string(); // returns just filename      filenames.push_back(filename);    }  }}</string></string></opencv2></iostream></boost>

各种方法都记录在这里,以便以后用的时候查找。