Python与C++如何遍历文件夹下的所有图片

发布时间:2021-07-24 14:44:15 作者:小新
来源:亿速云 阅读:582

这篇文章主要介绍了Python与C++如何遍历文件夹下的所有图片,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

Python遍历

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

# 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自带函数glob()遍历

OpenCV自带一个函数glob()可以遍历文件,如果用这个函数的话,遍历文件也是非常简单的。这个函数非常强大,人脸识别的时候用这个函数应该会比用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. 自己写一个遍历文件夹的函数

在windows下,没有dirent.h可用,但是可以根据windows.h自己写一个遍历函数。这就有点像是上面的glob的原理和实现了。

#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库来实现这一功能也是比较简洁的。为了用这个我还专门完全编译了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);
    }
  }
}

感谢你能够认真阅读完这篇文章,希望小编分享的“Python与C++如何遍历文件夹下的所有图片”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!

推荐阅读:
  1. python或C++读取指定文件夹下的所有图片
  2. 使用python怎么遍历文件夹下所有excel文件

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python c++

上一篇:怎么快速登陆ssh服务器

下一篇:使用SpringCloudStream怎么实现服务间的通讯

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》