这篇文章主要介绍了 Python与C++ 遍历文件夹下的所有图片实现代码的相关资料,需要的朋友可以参考下
Pyhton与C++ 遍历文件夹下的所有图片实现代码
前言
虽然本文说的是遍历图片,但是遍历其他文件也是可以的。
在进行图像处理的时候,大部分时候只需要处理单张图片。但是一旦把图像处理和机器学习相结合,或者做一些稍大一些的任务的时候,常常需要处理好多图片。而这里面,一个最基本的问题就是如何遍历这些图片。
用OpenCV做过人脸识别的人应该知道,那个项目中并没有进行图片的遍历,而是用了一种辅助方案,生成了一个包含所有图片路径的文件at.txt,然后通过这个路径来读取所有图片。而且这个辅助文件不仅包含了图片的路径,还包含了图片对应的标签。所以在进行训练的时候直接通过这个辅助文件来读取训练用的图片和标签。
其实如果去看看教程,会发现这个at.txt的生成是通过Python代码来实现。所以今天就来看一下如何用C++来实现文件夹下所有图片的遍历。
当然在此之前还是先给出Python遍历的代码,以备后用。
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++如何遍历文件夹下的所有图片的实现代码分享的详细内容。更多信息请关注PHP中文网其他相关文章!

SlicingaPythonlistisdoneusingthesyntaxlist[start:stop:step].Here'showitworks:1)Startistheindexofthefirstelementtoinclude.2)Stopistheindexofthefirstelementtoexclude.3)Stepistheincrementbetweenelements.It'susefulforextractingportionsoflistsandcanuseneg

numpyallowsforvariousoperationsonArrays:1)basicarithmeticlikeaddition,减法,乘法和division; 2)evationAperationssuchasmatrixmultiplication; 3)element-wiseOperations wiseOperationswithOutexpliitloops; 4)

Arresinpython,尤其是Throughnumpyandpandas,weessentialFordataAnalysis,offeringSpeedAndeffied.1)NumpyArseNable efflaysenable efficefliceHandlingAtaSetSetSetSetSetSetSetSetSetSetSetsetSetSetSetSetsopplexoperationslikemovingaverages.2)

列表sandnumpyArraysInpyThonHavedIfferentMemoryfootprints:listSaremoreFlexibleButlessMemory-效率,而alenumpyArraySareSareOptimizedFornumericalData.1)listsStorReereReereReereReereFerenceStoObjects,withoverHeadeBheadaroundAroundaroundaround64bytaround64bitson64-bitsysysysyssyssyssyssyssyssysssys2)

toensurepythonscriptsbehavecorrectlyacrycrossdevelvermations,登台和生产,USETHESTERTATE:1)Environment varriablesforsimplesettings,2)configurationFilesForefilesForcomPlexSetups,3)dynamiCofforAdaptapity.eachmethodofferSuniquebeneiquebeneiquebeneniqueBenefitsaniqueBenefitsandrefitsandRequiresandRequireSandRequireSca

Python列表切片的基本语法是list[start:stop:step]。1.start是包含的第一个元素索引,2.stop是排除的第一个元素索引,3.step决定元素之间的步长。切片不仅用于提取数据,还可以修改和反转列表。

ListSoutPerformarRaysin:1)DynamicsizicsizingandFrequentInsertions/删除,2)储存的二聚体和3)MemoryFeliceFiceForceforseforsparsedata,butmayhaveslightperformancecostsinclentoperations。

toConvertapythonarraytoalist,usEthelist()constructororageneratorexpression.1)intimpthearraymoduleandcreateanArray.2)USELIST(ARR)或[XFORXINARR] to ConconverTittoalist,请考虑performorefformanceandmemoryfformanceandmemoryfformienceforlargedAtasetset。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

记事本++7.3.1
好用且免费的代码编辑器

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

Dreamweaver CS6
视觉化网页开发工具

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。