處理圖像數據集時通常要讀寫整個文件夾里的圖像,這時就會用的圖像的批量讀寫。
比較常用的方法就是生成一個包含所有圖像的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] 沒有圖片\n"); return -1; } count++; imwrite(savefile, image); } waitKey(600000); //存儲圖像 return 0; }
