转自:http://blog.csdn.net/hei_ya/article/details/51387624
在生成训练集的时通常需要对文件夹中图片进行批处理,本文简要介绍图像批量读取、处理、保存的方法。
方法一:
1.生成图片描述文件
对于有多幅图像的文件夹,首先生成txt文件,保存图片路径。
在DOS模式下导入文件夹路径并生成TXT文件:
2.通过TXT批量处理图片
-
<span style=
"font-size:18px;">#include<opencv2/opencv.hpp>
-
#include<iostream>
-
#include<time.h>
-
#include<fstream>
-
-
using
namespace
std;
-
using
namespace cv;
-
-
void main()
-
{
-
ifstream file("C:/Users/Administrator/Desktop/date/MIT/MIT人脸库/faces/face.txt");
-
-
int img_index =
0;
-
-
while (!file.eof())
-
{
-
char txt_cont[
200];
-
file.getline(txt_cont,
200);
-
-
char img_file[
200],save_file[
200];
-
-
sprintf(img_file,
"C:/Users/Administrator/Desktop/date/MIT/MIT人脸库/faces/%s", txt_cont);
-
sprintf(save_file,
"C:/Users/Administrator/Desktop/date/MIT/MIT人脸库/save/%d.jpg", img_index);
-
-
Mat src = imread(img_file);
-
-
img_index++;
-
-
imwrite(save_file,src);
-
}
-
-
}</span>
方法二:
利用Directory类实现文件夹中特定格式图像的遍历,Directory的头文件是windows.h。
-
#include<opencv2/opencv.hpp>
-
#include<iostream>
-
#include<vector>
-
#include<string>
-
#include <windows.h>
-
-
using
namespace
std;
-
using
namespace cv;
-
-
-
void main()
-
{
-
Directory dir;
-
-
string path1 =
"C:\\Users\\Administrator\\Desktop\\date\\MIT\\MIT人脸库\\faces";
-
string exten1 =
"*.bmp";
-
-
vector<
string> filenames = dir.GetListFiles(path1, exten1,
false);
-
-
int size = filenames.size();
-
-
for (
int i =
0; i < size;i++)
-
{
-
cout << filenames[i] <<
endl;
-
}
-
}
![](https://i-blog.csdnimg.cn/blog_migrate/5a33e1501b361f1999ed225ae1ca5ed2.png)
2. Directory类定义为
-
class CV_EXPORTS Directory
-
{
-
public:
-
static
std::
vector<
std::
string> GetListFiles (
const
std::
string& path,
const
std::
string & exten =
"*",
bool addPath =
true );
-
static
std::
vector<
std::
string> GetListFilesR (
const
std::
string& path,
const
std::
string & exten =
"*",
bool addPath =
true );
-
static
std::
vector<
std::
string> GetListFolders(
const
std::
string& path,
const
std::
string & exten =
"*",
bool addPath =
true );
-
};
具体用法如下,使用这个类时,应该加上contrib.hpp头文件
-
#include <iostream>
-
using
namespace
std;
-
-
#include <opencv2\opencv.hpp>
-
#include <opencv2\highgui\highgui.hpp>
-
#include <opencv2\contrib\contrib.hpp>
-
using
namespace cv;
-
-
int main(int argc, char* argv[])
-
{
-
string dir_path =
"D:\\opencv_pic\\test\\";
-
Directory dir;
-
vector<
string> fileNames = dir.GetListFiles(dir_path,
"*.jpg",
false);
-
-
for(
int i =
0; i < fileNames.size(); i++)
-
{
-
//get image name
-
string fileName = fileNames[i];
-
string fileFullName = dir_path + fileName;
-
cout<<
"File name:"<<fileName<<
endl;
-
cout<<
"Full path:"<<fileFullName<<
endl;
-
-
//load image
-
IplImage* srcImg = cvLoadImage(fileFullName.c_str(), CV_LOAD_IMAGE_UNCHANGED);
-
cvShowImage(
"src", srcImg);
-
cvWaitKey(
0);
-
}
-
return
0;
-
}
参考:
http://blog.csdn.net/qq_34784753/article/details/68935628
转自:http://blog.csdn.net/hei_ya/article/details/51387624
在生成训练集的时通常需要对文件夹中图片进行批处理,本文简要介绍图像批量读取、处理、保存的方法。
方法一:
1.生成图片描述文件
对于有多幅图像的文件夹,首先生成txt文件,保存图片路径。
在DOS模式下导入文件夹路径并生成TXT文件:
2.通过TXT批量处理图片
-
<span style=
"font-size:18px;">#include<opencv2/opencv.hpp>
-
#include<iostream>
-
#include<time.h>
-
#include<fstream>
-
-
using
namespace
std;
-
using
namespace cv;
-
-
void main()
-
{
-
ifstream file("C:/Users/Administrator/Desktop/date/MIT/MIT人脸库/faces/face.txt");
-
-
int img_index =
0;
-
-
while (!file.eof())
-
{
-
char txt_cont[
200];
-
file.getline(txt_cont,
200);
-
-
char img_file[
200],save_file[
200];
-
-
sprintf(img_file,
"C:/Users/Administrator/Desktop/date/MIT/MIT人脸库/faces/%s", txt_cont);
-
sprintf(save_file,
"C:/Users/Administrator/Desktop/date/MIT/MIT人脸库/save/%d.jpg", img_index);
-
-
Mat src = imread(img_file);
-
-
img_index++;
-
-
imwrite(save_file,src);
-
}
-
-
}</span>
方法二:
利用Directory类实现文件夹中特定格式图像的遍历,Directory的头文件是windows.h。
-
#include<opencv2/opencv.hpp>
-
#include<iostream>
-
#include<vector>
-
#include<string>
-
#include <windows.h>
-
-
using
namespace
std;
-
using
namespace cv;
-
-
-
void main()
-
{
-
Directory dir;
-
-
string path1 =
"C:\\Users\\Administrator\\Desktop\\date\\MIT\\MIT人脸库\\faces";
-
string exten1 =
"*.bmp";
-
-
vector<
string> filenames = dir.GetListFiles(path1, exten1,
false);
-
-
int size = filenames.size();
-
-
for (
int i =
0; i < size;i++)
-
{
-
cout << filenames[i] <<
endl;
-
}
-
}
![](https://i-blog.csdnimg.cn/blog_migrate/5a33e1501b361f1999ed225ae1ca5ed2.png)
2. Directory类定义为
-
class CV_EXPORTS Directory
-
{
-
public:
-
static
std::
vector<
std::
string> GetListFiles (
const
std::
string& path,
const
std::
string & exten =
"*",
bool addPath =
true );
-
static
std::
vector<
std::
string> GetListFilesR (
const
std::
string& path,
const
std::
string & exten =
"*",
bool addPath =
true );
-
static
std::
vector<
std::
string> GetListFolders(
const
std::
string& path,
const
std::
string & exten =
"*",
bool addPath =
true );
-
};
具体用法如下,使用这个类时,应该加上contrib.hpp头文件
-
#include <iostream>
-
using
namespace
std;
-
-
#include <opencv2\opencv.hpp>
-
#include <opencv2\highgui\highgui.hpp>
-
#include <opencv2\contrib\contrib.hpp>
-
using
namespace cv;
-
-
int main(int argc, char* argv[])
-
{
-
string dir_path =
"D:\\opencv_pic\\test\\";
-
Directory dir;
-
vector<
string> fileNames = dir.GetListFiles(dir_path,
"*.jpg",
false);
-
-
for(
int i =
0; i < fileNames.size(); i++)
-
{
-
//get image name
-
string fileName = fileNames[i];
-
string fileFullName = dir_path + fileName;
-
cout<<
"File name:"<<fileName<<
endl;
-
cout<<
"Full path:"<<fileFullName<<
endl;
-
-
//load image
-
IplImage* srcImg = cvLoadImage(fileFullName.c_str(), CV_LOAD_IMAGE_UNCHANGED);
-
cvShowImage(
"src", srcImg);
-
cvWaitKey(
0);
-
}
-
return
0;
-
}
参考:
http://blog.csdn.net/qq_34784753/article/details/68935628
转自:http://blog.csdn.net/hei_ya/article/details/51387624
在生成训练集的时通常需要对文件夹中图片进行批处理,本文简要介绍图像批量读取、处理、保存的方法。
方法一:
1.生成图片描述文件
对于有多幅图像的文件夹,首先生成txt文件,保存图片路径。
在DOS模式下导入文件夹路径并生成TXT文件:
2.通过TXT批量处理图片
-
<span style=
"font-size:18px;">#include<opencv2/opencv.hpp>
-
#include<iostream>
-
#include<time.h>
-
#include<fstream>
-
-
using
namespace
std;
-
using
namespace cv;
-
-
void main()
-
{
-
ifstream file("C:/Users/Administrator/Desktop/date/MIT/MIT人脸库/faces/face.txt");
-
-
int img_index =
0;
-
-
while (!file.eof())
-
{
-
char txt_cont[
200];
-
file.getline(txt_cont,
200);
-
-
char img_file[
200],save_file[
200];
-
-
sprintf(img_file,
"C:/Users/Administrator/Desktop/date/MIT/MIT人脸库/faces/%s", txt_cont);
-
sprintf(save_file,
"C:/Users/Administrator/Desktop/date/MIT/MIT人脸库/save/%d.jpg", img_index);
-
-
Mat src = imread(img_file);
-
-
img_index++;
-
-
imwrite(save_file,src);
-
}
-
-
}</span>
方法二:
利用Directory类实现文件夹中特定格式图像的遍历,Directory的头文件是windows.h。
-
#include<opencv2/opencv.hpp>
-
#include<iostream>
-
#include<vector>
-
#include<string>
-
#include <windows.h>
-
-
using
namespace
std;
-
using
namespace cv;
-
-
-
void main()
-
{
-
Directory dir;
-
-
string path1 =
"C:\\Users\\Administrator\\Desktop\\date\\MIT\\MIT人脸库\\faces";
-
string exten1 =
"*.bmp";
-
-
vector<
string> filenames = dir.GetListFiles(path1, exten1,
false);
-
-
int size = filenames.size();
-
-
for (
int i =
0; i < size;i++)
-
{
-
cout << filenames[i] <<
endl;
-
}
-
}
![](https://i-blog.csdnimg.cn/blog_migrate/5a33e1501b361f1999ed225ae1ca5ed2.png)
2. Directory类定义为
-
class CV_EXPORTS Directory
-
{
-
public:
-
static
std::
vector<
std::
string> GetListFiles (
const
std::
string& path,
const
std::
string & exten =
"*",
bool addPath =
true );
-
static
std::
vector<
std::
string> GetListFilesR (
const
std::
string& path,
const
std::
string & exten =
"*",
bool addPath =
true );
-
static
std::
vector<
std::
string> GetListFolders(
const
std::
string& path,
const
std::
string & exten =
"*",
bool addPath =
true );
-
};
具体用法如下,使用这个类时,应该加上contrib.hpp头文件
-
#include <iostream>
-
using
namespace
std;
-
-
#include <opencv2\opencv.hpp>
-
#include <opencv2\highgui\highgui.hpp>
-
#include <opencv2\contrib\contrib.hpp>
-
using
namespace cv;
-
-
int main(int argc, char* argv[])
-
{
-
string dir_path =
"D:\\opencv_pic\\test\\";
-
Directory dir;
-
vector<
string> fileNames = dir.GetListFiles(dir_path,
"*.jpg",
false);
-
-
for(
int i =
0; i < fileNames.size(); i++)
-
{
-
//get image name
-
string fileName = fileNames[i];
-
string fileFullName = dir_path + fileName;
-
cout<<
"File name:"<<fileName<<
endl;
-
cout<<
"Full path:"<<fileFullName<<
endl;
-
-
//load image
-
IplImage* srcImg = cvLoadImage(fileFullName.c_str(), CV_LOAD_IMAGE_UNCHANGED);
-
cvShowImage(
"src", srcImg);
-
cvWaitKey(
0);
-
}
-
return
0;
-
}
参考:
http://blog.csdn.net/qq_34784753/article/details/68935628
转自:http://blog.csdn.net/hei_ya/article/details/51387624
在生成训练集的时通常需要对文件夹中图片进行批处理,本文简要介绍图像批量读取、处理、保存的方法。
方法一:
1.生成图片描述文件
对于有多幅图像的文件夹,首先生成txt文件,保存图片路径。
在DOS模式下导入文件夹路径并生成TXT文件:
2.通过TXT批量处理图片
-
<span style=
"font-size:18px;">#include<opencv2/opencv.hpp>
-
#include<iostream>
-
#include<time.h>
-
#include<fstream>
-
-
using
namespace
std;
-
using
namespace cv;
-
-
void main()
-
{
-
ifstream file("C:/Users/Administrator/Desktop/date/MIT/MIT人脸库/faces/face.txt");
-
-
int img_index =
0;
-
-
while (!file.eof())
-
{
-
char txt_cont[
200];
-
file.getline(txt_cont,
200);
-
-
char img_file[
200],save_file[
200];
-
-
sprintf(img_file,
"C:/Users/Administrator/Desktop/date/MIT/MIT人脸库/faces/%s", txt_cont);
-
sprintf(save_file,
"C:/Users/Administrator/Desktop/date/MIT/MIT人脸库/save/%d.jpg", img_index);
-
-
Mat src = imread(img_file);
-
-
img_index++;
-
-
imwrite(save_file,src);
-
}
-
-
}</span>
方法二:
利用Directory类实现文件夹中特定格式图像的遍历,Directory的头文件是windows.h。
-
#include<opencv2/opencv.hpp>
-
#include<iostream>
-
#include<vector>
-
#include<string>
-
#include <windows.h>
-
-
using
namespace
std;
-
using
namespace cv;
-
-
-
void main()
-
{
-
Directory dir;
-
-
string path1 =
"C:\\Users\\Administrator\\Desktop\\date\\MIT\\MIT人脸库\\faces";
-
string exten1 =
"*.bmp";
-
-
vector<
string> filenames = dir.GetListFiles(path1, exten1,
false);
-
-
int size = filenames.size();
-
-
for (
int i =
0; i < size;i++)
-
{
-
cout << filenames[i] <<
endl;
-
}
-
}
![](https://i-blog.csdnimg.cn/blog_migrate/5a33e1501b361f1999ed225ae1ca5ed2.png)
2. Directory类定义为
-
class CV_EXPORTS Directory
-
{
-
public:
-
static
std::
vector<
std::
string> GetListFiles (
const
std::
string& path,
const
std::
string & exten =
"*",
bool addPath =
true );
-
static
std::
vector<
std::
string> GetListFilesR (
const
std::
string& path,
const
std::
string & exten =
"*",
bool addPath =
true );
-
static
std::
vector<
std::
string> GetListFolders(
const
std::
string& path,
const
std::
string & exten =
"*",
bool addPath =
true );
-
};
具体用法如下,使用这个类时,应该加上contrib.hpp头文件
-
#include <iostream>
-
using
namespace
std;
-
-
#include <opencv2\opencv.hpp>
-
#include <opencv2\highgui\highgui.hpp>
-
#include <opencv2\contrib\contrib.hpp>
-
using
namespace cv;
-
-
int main(int argc, char* argv[])
-
{
-
string dir_path =
"D:\\opencv_pic\\test\\";
-
Directory dir;
-
vector<
string> fileNames = dir.GetListFiles(dir_path,
"*.jpg",
false);
-
-
for(
int i =
0; i < fileNames.size(); i++)
-
{
-
//get image name
-
string fileName = fileNames[i];
-
string fileFullName = dir_path + fileName;
-
cout<<
"File name:"<<fileName<<
endl;
-
cout<<
"Full path:"<<fileFullName<<
endl;
-
-
//load image
-
IplImage* srcImg = cvLoadImage(fileFullName.c_str(), CV_LOAD_IMAGE_UNCHANGED);
-
cvShowImage(
"src", srcImg);
-
cvWaitKey(
0);
-
}
-
return
0;
-
}
参考: