C++opencv中讀取資料夾中的多幅圖片並按順序重新命名後儲存到另一資料夾中
阿新 • • 發佈:2018-11-01
#include <iostream> #include <fstream> #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> #include <opencv2/objdetect/objdetect.hpp> #include <opencv2/ml/ml.hpp> using namespace std; using namespace cv; int CropImageCount = 0; //圖片個數 int main() { Mat src; string ImgName; char saveName[256];//圖片檔名 ifstream fin("D:\\list.txt");// 需要改名資料夾中圖片的名字 //一行一行讀取檔案列表 while (getline(fin, ImgName)) // 把輸入流fin資訊,以\n為結尾讀入到ImgName中 { cout << "處理:" << ImgName << endl; ImgName = "D:\\低解析度截圖\\" + ImgName; src = imread(ImgName);//讀取圖片 sprintf_s(saveName, "D:\\低解析度截圖重新命名\\%d.jpg", ++CropImageCount);//生成裁剪出的負樣本圖片的檔名 imwrite(saveName, src);//儲存檔案 } system("pause"); }
然後OpenCV讀取TXT中圖片的方法:
#include "opencv2/opencv.hpp" #include "iostream" #include <fstream> using namespace std; using namespace cv; int main() { Mat image; string ImgName; ifstream fin("woman.txt");//開啟原始樣本圖片檔案列表 while(getline(fin,ImgName)) //一行一行讀取檔案列表 { cout<<"處理:"<<ImgName<<endl; ImgName = "D:\\Mycode\\woman\\" + ImgName+".png"; image= imread(ImgName);//讀取圖片 if(image.data ==0) {printf("[error] 沒有圖片\n");return -5;} } waitKey(0); return 0; }