#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;
}