溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Python與C++如何遍歷文件夾下的所有圖片

發(fā)布時(shí)間:2021-07-24 14:44:15 來(lái)源:億速云 閱讀:597 作者:小新 欄目:編程語(yǔ)言

這篇文章主要介紹了Python與C++如何遍歷文件夾下的所有圖片,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

Python遍歷

在之前的數(shù)獨(dú)項(xiàng)目中,進(jìn)行圖像處理的時(shí)候用到了遍歷文件夾下所有的圖片。主要是利用glob模塊。glob是python自己帶的一個(gè)文件操作相關(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自帶一個(gè)函數(shù)glob()可以遍歷文件,如果用這個(gè)函數(shù)的話,遍歷文件也是非常簡(jiǎn)單的。這個(gè)函數(shù)非常強(qiáng)大,人臉識(shí)別的時(shí)候用這個(gè)函數(shù)應(yīng)該會(huì)比用at.txt更加方便。一個(gè)參考示例如下。

#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. 自己寫(xiě)一個(gè)遍歷文件夾的函數(shù)

在windows下,沒(méi)有dirent.h可用,但是可以根據(jù)windows.h自己寫(xiě)一個(gè)遍歷函數(shù)。這就有點(diǎn)像是上面的glob的原理和實(shí)現(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庫(kù),用boost庫(kù)來(lái)實(shí)現(xiàn)這一功能也是比較簡(jiǎn)潔的。為了用這個(gè)我還專(zhuān)門(mé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);
    }
  }
}

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“Python與C++如何遍歷文件夾下的所有圖片”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI