处理图像数据集时通常要读写整个文件夹里的图像,这时就会用的图像的批量读写。
比较常用的方法就是生成一个包含所有图像的txt列表
生成txt文件的方法如下:
利用cmd进入dos
利用路径进入指定文件夹后生成txt文件
然后可以利用txt列表读入图像并做处理。
#include "opencv2/opencv.hpp" #include "iostream" #include <fstream> #include <windows.h> #include <string> using namespace std; using namespace cv; int main() { Mat image; string ImgName; string savefile; int count = 1; ifstream fin("D:/opencv pro/readfiles/readfiles/English1.txt");//打开原始样本图片文件列表 while (getline(fin, ImgName)) //逐行读取文件列表 { cout << "processing:" << ImgName << endl; ImgName = "D:/opencv pro/readfiles/readfiles/" + ImgName; savefile = "D:/opencv pro/readfiles/readfiles/saved/" + to_string(count) + ".jpg"; 指定存储路径 image = imread(ImgName);//读取图片 //imshow("1", image); if (image.data == 0) { printf("[error] 没有图片 "); return -1; } count++; imwrite(savefile, image); } waitKey(600000); //存储图像 return 0; }