opencv从文件中批量读取图像

用opencv处理图像,特别涉及到机器学习,需要批量地读取图像。

方法1比较简单,唯一的要求就是文件夹下的图片名称是有规律的,如 ***(0)、***(1)、***(2)、***(3)·····

  1. #include "opencv2/opencv.hpp"  
  2. #include "iostream"  
  3.   
  4. using namespace std;  
  5. using namespace cv;  
  6.   
  7. #define  NUM  100     //读取image的个数  
  8. int main()  
  9. {  
  10.     Mat image;  
  11.     string ImgName;  
  12.     int n=1;  
  13.     while(n<=NUM)   //100  
  14.     {   
  15.         ImgName="woman";  
  16.         //int 转换string  
  17.         stringstream ss;  
  18.         string str;  
  19.         ss<<n;  
  20.         ss>>str;  
  21.   
  22.         ImgName=ImgName+" ("+str+")";    //图像文件明格式:ImgName(n)  
  23.         ImgName = "D:\\Mycode\\imagebank\\woman\\" + ImgName+".png";  
  24.         cout<<"处理:"<<ImgName<<endl;  
  25.         image= imread(ImgName);//读取图片  
  26.         if(image.data ==0)  
  27.         { printf("[error] 没有图片\n");}  
  28.         n++;  
  29.    }  
  30.   
  31.     waitKey(0);  
  32.     system("pause");  
  33.     return 4;  
  34. }  
#include "opencv2/opencv.hpp"
#include "iostream"

using namespace std;
using namespace cv;

#define  NUM  100     //读取image的个数
int main()
{
    Mat image;
    string ImgName;
    int n=1;
    while(n<=NUM)   //100
    { 
		ImgName="woman";
		//int 转换string
		stringstream ss;
		string str;
		ss<<n;
		ss>>str;

		ImgName=ImgName+" ("+str+")";    //图像文件明格式:ImgName(n)
		ImgName = "D:\\Mycode\\imagebank\\woman\\" + ImgName+".png";
		cout<<"处理:"<<ImgName<<endl;
		image= imread(ImgName);//读取图片
		if(image.data ==0)
		{ printf("[error] 没有图片\n");}
		n++;
   }

    waitKey(0);
    system("pause");
    return 4;
}

方法二需要一个 .txt文件存放文件中待读取图像的名称,每行为一条图像名。

  1. #include "opencv2/opencv.hpp"  
  2. #include "iostream"  
  3. #include <fstream>  
  4. using namespace std;  
  5. using namespace cv;  
  6.   
  7. int main()  
  8. {  
  9.     Mat image;  
  10.     string ImgName;  
  11.     ifstream fin("woman.txt");//打开原始样本图片文件列表  
  12.     while(getline(fin,ImgName)) //一行一行读取文件列表  
  13.     {    
  14.       cout<<"处理:"<<ImgName<<endl;  
  15.       ImgName = "D:\\Mycode\\woman\\" + ImgName+".png";  
  16.       image= imread(ImgName);//读取图片  
  17.   
  18.      if(image.data ==0)  
  19.       {printf("[error] 没有图片\n");return -5;}  
  20.     }  
  21.         waitKey(0);   
  22. return 4;  
  23. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值