opencv从文件中批量读取图像
2016-09-22 17:10
337 查看
用opencv处理图像,特别涉及到机器学习,需要批量地读取图像。
方法1比较简单,唯一的要求就是文件夹下的图片名称是有规律的,如 ***(0)、***(1)、***(2)、***(3)·····
[cpp]
view plain
copy
print?
#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;
}
![](http://static.blog.csdn.net/images/save_snippets.png)
方法二需要一个 .txt文件存放文件中待读取图像的名称,每行为一条图像名。
[cpp]
view plain
copy
print?
#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 4;
}
方法1比较简单,唯一的要求就是文件夹下的图片名称是有规律的,如 ***(0)、***(1)、***(2)、***(3)·····
[cpp]
view plain
copy
print?
#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;
}
![](http://static.blog.csdn.net/images/save_snippets.png)
#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文件存放文件中待读取图像的名称,每行为一条图像名。
[cpp]
view plain
copy
print?
#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 4;
}
相关文章推荐
- OpenCV图像编程实例-图像文件批量读取
- opencv从文件中批量读取图像
- OpenCV图像文件批量读取编程实例
- opencv读取摄像头图像和读取视频文件图像
- OpenCV应用笔记】(彩色/灰度)图像像素值读取并保存到txt文件
- 利用OpenCV进行【视频的读取】与【保存视频文件或者图像】源码!~
- 基于OpenCV依次读取文件夹下的所有图像文件
- 图像文件批量读取
- OpenCV学习笔记---- 挨个读取图像文件夹下的所有文件,进行样本训练
- 图像批量读取(opencv C++ python)
- python+opencv 读取文件夹下的所有图像并批量保存ROI
- OpenCV入门教程(7)-图像文件的读取和存储
- 基于OpenCV依次读取文件夹下的所有图像文件
- 【OpenCV应用笔记】(彩色/灰度)图像像素值读取并保存到txt文件
- Asp.Net 文件操作基类(读取,删除,批量拷贝,删除,写)操作
- Matlab中从文件夹中批量读取文件的做法
- Asp.Net 文件操作基类(读取,删除,批量拷贝,批量删除,写入)
- java读取bmp图像文件
- Asp.net(C#)对文件操作的方法(读取,删除,批量拷贝,删除...)
- Asp.Net 文件操作基类(读取,删除,批量拷贝,删除,写入,获取文件夹大小,文件属性,遍历目录)(二)