opencv批量读取图片

转自:http://blog.csdn.net/hei_ya/article/details/51387624

在生成训练集的时通常需要对文件夹中图片进行批处理,本文简要介绍图像批量读取、处理、保存的方法。


方法一:

1.生成图片描述文件

    对于有多幅图像的文件夹,首先生成txt文件,保存图片路径。


   在DOS模式下导入文件夹路径并生成TXT文件:



2.通过TXT批量处理图片


  
  
  1. <span style= "font-size:18px;">#include<opencv2/opencv.hpp>
  2. #include<iostream>
  3. #include<time.h>
  4. #include<fstream>
  5. using namespace std;
  6. using namespace cv;
  7. void main()
  8. {
  9. ifstream file("C:/Users/Administrator/Desktop/date/MIT/MIT人脸库/faces/face.txt");
  10. int img_index = 0;
  11. while (!file.eof())
  12. {
  13. char txt_cont[ 200];
  14. file.getline(txt_cont, 200);
  15. char img_file[ 200],save_file[ 200];
  16. sprintf(img_file, "C:/Users/Administrator/Desktop/date/MIT/MIT人脸库/faces/%s", txt_cont);
  17. sprintf(save_file, "C:/Users/Administrator/Desktop/date/MIT/MIT人脸库/save/%d.jpg", img_index);
  18. Mat src = imread(img_file);
  19. img_index++;
  20. imwrite(save_file,src);
  21. }
  22. }</span>

方法二:

利用Directory类实现文件夹中特定格式图像的遍历,Directory的头文件是windows.h。


  
  
  1. #include<opencv2/opencv.hpp>
  2. #include<iostream>
  3. #include<vector>
  4. #include<string>
  5. #include <windows.h>
  6. using namespace std;
  7. using namespace cv;
  8. void main()
  9. {
  10. Directory dir;
  11. string path1 = "C:\\Users\\Administrator\\Desktop\\date\\MIT\\MIT人脸库\\faces";
  12. string exten1 = "*.bmp";
  13. vector< string> filenames = dir.GetListFiles(path1, exten1, false);
  14. int size = filenames.size();
  15. for ( int i = 0; i < size;i++)
  16. {
  17. cout << filenames[i] << endl;
  18. }
  19. }

2. Directory类定义为


  
  
  1. class CV_EXPORTS Directory
  2. {
  3. public:
  4. static std:: vector< std:: string> GetListFiles ( const std:: string& path, const std:: string & exten = "*", bool addPath = true );
  5. static std:: vector< std:: string> GetListFilesR ( const std:: string& path, const std:: string & exten = "*", bool addPath = true );
  6. static std:: vector< std:: string> GetListFolders( const std:: string& path, const std:: string & exten = "*", bool addPath = true );
  7. };

具体用法如下,使用这个类时,应该加上contrib.hpp头文件


  
  
  1. #include <iostream>
  2. using namespace std;
  3. #include <opencv2\opencv.hpp>
  4. #include <opencv2\highgui\highgui.hpp>
  5. #include <opencv2\contrib\contrib.hpp>
  6. using namespace cv;
  7. int main(int argc, char* argv[])
  8. {
  9. string dir_path = "D:\\opencv_pic\\test\\";
  10. Directory dir;
  11. vector< string> fileNames = dir.GetListFiles(dir_path, "*.jpg", false);
  12. for( int i = 0; i < fileNames.size(); i++)
  13. {
  14. //get image name
  15. string fileName = fileNames[i];
  16. string fileFullName = dir_path + fileName;
  17. cout<< "File name:"<<fileName<< endl;
  18. cout<< "Full path:"<<fileFullName<< endl;
  19. //load image
  20. IplImage* srcImg = cvLoadImage(fileFullName.c_str(), CV_LOAD_IMAGE_UNCHANGED);
  21. cvShowImage( "src", srcImg);
  22. cvWaitKey( 0);
  23. }
  24. return 0;
  25. }
参考: http://blog.csdn.net/qq_34784753/article/details/68935628


转自:http://blog.csdn.net/hei_ya/article/details/51387624

在生成训练集的时通常需要对文件夹中图片进行批处理,本文简要介绍图像批量读取、处理、保存的方法。


方法一:

1.生成图片描述文件

    对于有多幅图像的文件夹,首先生成txt文件,保存图片路径。


   在DOS模式下导入文件夹路径并生成TXT文件:



2.通过TXT批量处理图片


  
  
  1. <span style= "font-size:18px;">#include<opencv2/opencv.hpp>
  2. #include<iostream>
  3. #include<time.h>
  4. #include<fstream>
  5. using namespace std;
  6. using namespace cv;
  7. void main()
  8. {
  9. ifstream file("C:/Users/Administrator/Desktop/date/MIT/MIT人脸库/faces/face.txt");
  10. int img_index = 0;
  11. while (!file.eof())
  12. {
  13. char txt_cont[ 200];
  14. file.getline(txt_cont, 200);
  15. char img_file[ 200],save_file[ 200];
  16. sprintf(img_file, "C:/Users/Administrator/Desktop/date/MIT/MIT人脸库/faces/%s", txt_cont);
  17. sprintf(save_file, "C:/Users/Administrator/Desktop/date/MIT/MIT人脸库/save/%d.jpg", img_index);
  18. Mat src = imread(img_file);
  19. img_index++;
  20. imwrite(save_file,src);
  21. }
  22. }</span>

方法二:

利用Directory类实现文件夹中特定格式图像的遍历,Directory的头文件是windows.h。


  
  
  1. #include<opencv2/opencv.hpp>
  2. #include<iostream>
  3. #include<vector>
  4. #include<string>
  5. #include <windows.h>
  6. using namespace std;
  7. using namespace cv;
  8. void main()
  9. {
  10. Directory dir;
  11. string path1 = "C:\\Users\\Administrator\\Desktop\\date\\MIT\\MIT人脸库\\faces";
  12. string exten1 = "*.bmp";
  13. vector< string> filenames = dir.GetListFiles(path1, exten1, false);
  14. int size = filenames.size();
  15. for ( int i = 0; i < size;i++)
  16. {
  17. cout << filenames[i] << endl;
  18. }
  19. }

2. Directory类定义为


  
  
  1. class CV_EXPORTS Directory
  2. {
  3. public:
  4. static std:: vector< std:: string> GetListFiles ( const std:: string& path, const std:: string & exten = "*", bool addPath = true );
  5. static std:: vector< std:: string> GetListFilesR ( const std:: string& path, const std:: string & exten = "*", bool addPath = true );
  6. static std:: vector< std:: string> GetListFolders( const std:: string& path, const std:: string & exten = "*", bool addPath = true );
  7. };

具体用法如下,使用这个类时,应该加上contrib.hpp头文件


  
  
  1. #include <iostream>
  2. using namespace std;
  3. #include <opencv2\opencv.hpp>
  4. #include <opencv2\highgui\highgui.hpp>
  5. #include <opencv2\contrib\contrib.hpp>
  6. using namespace cv;
  7. int main(int argc, char* argv[])
  8. {
  9. string dir_path = "D:\\opencv_pic\\test\\";
  10. Directory dir;
  11. vector< string> fileNames = dir.GetListFiles(dir_path, "*.jpg", false);
  12. for( int i = 0; i < fileNames.size(); i++)
  13. {
  14. //get image name
  15. string fileName = fileNames[i];
  16. string fileFullName = dir_path + fileName;
  17. cout<< "File name:"<<fileName<< endl;
  18. cout<< "Full path:"<<fileFullName<< endl;
  19. //load image
  20. IplImage* srcImg = cvLoadImage(fileFullName.c_str(), CV_LOAD_IMAGE_UNCHANGED);
  21. cvShowImage( "src", srcImg);
  22. cvWaitKey( 0);
  23. }
  24. return 0;
  25. }
参考: http://blog.csdn.net/qq_34784753/article/details/68935628


转自:http://blog.csdn.net/hei_ya/article/details/51387624

在生成训练集的时通常需要对文件夹中图片进行批处理,本文简要介绍图像批量读取、处理、保存的方法。


方法一:

1.生成图片描述文件

    对于有多幅图像的文件夹,首先生成txt文件,保存图片路径。


   在DOS模式下导入文件夹路径并生成TXT文件:



2.通过TXT批量处理图片


  
  
  1. <span style= "font-size:18px;">#include<opencv2/opencv.hpp>
  2. #include<iostream>
  3. #include<time.h>
  4. #include<fstream>
  5. using namespace std;
  6. using namespace cv;
  7. void main()
  8. {
  9. ifstream file("C:/Users/Administrator/Desktop/date/MIT/MIT人脸库/faces/face.txt");
  10. int img_index = 0;
  11. while (!file.eof())
  12. {
  13. char txt_cont[ 200];
  14. file.getline(txt_cont, 200);
  15. char img_file[ 200],save_file[ 200];
  16. sprintf(img_file, "C:/Users/Administrator/Desktop/date/MIT/MIT人脸库/faces/%s", txt_cont);
  17. sprintf(save_file, "C:/Users/Administrator/Desktop/date/MIT/MIT人脸库/save/%d.jpg", img_index);
  18. Mat src = imread(img_file);
  19. img_index++;
  20. imwrite(save_file,src);
  21. }
  22. }</span>

方法二:

利用Directory类实现文件夹中特定格式图像的遍历,Directory的头文件是windows.h。


  
  
  1. #include<opencv2/opencv.hpp>
  2. #include<iostream>
  3. #include<vector>
  4. #include<string>
  5. #include <windows.h>
  6. using namespace std;
  7. using namespace cv;
  8. void main()
  9. {
  10. Directory dir;
  11. string path1 = "C:\\Users\\Administrator\\Desktop\\date\\MIT\\MIT人脸库\\faces";
  12. string exten1 = "*.bmp";
  13. vector< string> filenames = dir.GetListFiles(path1, exten1, false);
  14. int size = filenames.size();
  15. for ( int i = 0; i < size;i++)
  16. {
  17. cout << filenames[i] << endl;
  18. }
  19. }

2. Directory类定义为


  
  
  1. class CV_EXPORTS Directory
  2. {
  3. public:
  4. static std:: vector< std:: string> GetListFiles ( const std:: string& path, const std:: string & exten = "*", bool addPath = true );
  5. static std:: vector< std:: string> GetListFilesR ( const std:: string& path, const std:: string & exten = "*", bool addPath = true );
  6. static std:: vector< std:: string> GetListFolders( const std:: string& path, const std:: string & exten = "*", bool addPath = true );
  7. };

具体用法如下,使用这个类时,应该加上contrib.hpp头文件


  
  
  1. #include <iostream>
  2. using namespace std;
  3. #include <opencv2\opencv.hpp>
  4. #include <opencv2\highgui\highgui.hpp>
  5. #include <opencv2\contrib\contrib.hpp>
  6. using namespace cv;
  7. int main(int argc, char* argv[])
  8. {
  9. string dir_path = "D:\\opencv_pic\\test\\";
  10. Directory dir;
  11. vector< string> fileNames = dir.GetListFiles(dir_path, "*.jpg", false);
  12. for( int i = 0; i < fileNames.size(); i++)
  13. {
  14. //get image name
  15. string fileName = fileNames[i];
  16. string fileFullName = dir_path + fileName;
  17. cout<< "File name:"<<fileName<< endl;
  18. cout<< "Full path:"<<fileFullName<< endl;
  19. //load image
  20. IplImage* srcImg = cvLoadImage(fileFullName.c_str(), CV_LOAD_IMAGE_UNCHANGED);
  21. cvShowImage( "src", srcImg);
  22. cvWaitKey( 0);
  23. }
  24. return 0;
  25. }
参考: http://blog.csdn.net/qq_34784753/article/details/68935628


转自:http://blog.csdn.net/hei_ya/article/details/51387624

在生成训练集的时通常需要对文件夹中图片进行批处理,本文简要介绍图像批量读取、处理、保存的方法。


方法一:

1.生成图片描述文件

    对于有多幅图像的文件夹,首先生成txt文件,保存图片路径。


   在DOS模式下导入文件夹路径并生成TXT文件:



2.通过TXT批量处理图片


  
  
  1. <span style= "font-size:18px;">#include<opencv2/opencv.hpp>
  2. #include<iostream>
  3. #include<time.h>
  4. #include<fstream>
  5. using namespace std;
  6. using namespace cv;
  7. void main()
  8. {
  9. ifstream file("C:/Users/Administrator/Desktop/date/MIT/MIT人脸库/faces/face.txt");
  10. int img_index = 0;
  11. while (!file.eof())
  12. {
  13. char txt_cont[ 200];
  14. file.getline(txt_cont, 200);
  15. char img_file[ 200],save_file[ 200];
  16. sprintf(img_file, "C:/Users/Administrator/Desktop/date/MIT/MIT人脸库/faces/%s", txt_cont);
  17. sprintf(save_file, "C:/Users/Administrator/Desktop/date/MIT/MIT人脸库/save/%d.jpg", img_index);
  18. Mat src = imread(img_file);
  19. img_index++;
  20. imwrite(save_file,src);
  21. }
  22. }</span>

方法二:

利用Directory类实现文件夹中特定格式图像的遍历,Directory的头文件是windows.h。


  
  
  1. #include<opencv2/opencv.hpp>
  2. #include<iostream>
  3. #include<vector>
  4. #include<string>
  5. #include <windows.h>
  6. using namespace std;
  7. using namespace cv;
  8. void main()
  9. {
  10. Directory dir;
  11. string path1 = "C:\\Users\\Administrator\\Desktop\\date\\MIT\\MIT人脸库\\faces";
  12. string exten1 = "*.bmp";
  13. vector< string> filenames = dir.GetListFiles(path1, exten1, false);
  14. int size = filenames.size();
  15. for ( int i = 0; i < size;i++)
  16. {
  17. cout << filenames[i] << endl;
  18. }
  19. }

2. Directory类定义为


  
  
  1. class CV_EXPORTS Directory
  2. {
  3. public:
  4. static std:: vector< std:: string> GetListFiles ( const std:: string& path, const std:: string & exten = "*", bool addPath = true );
  5. static std:: vector< std:: string> GetListFilesR ( const std:: string& path, const std:: string & exten = "*", bool addPath = true );
  6. static std:: vector< std:: string> GetListFolders( const std:: string& path, const std:: string & exten = "*", bool addPath = true );
  7. };

具体用法如下,使用这个类时,应该加上contrib.hpp头文件


  
  
  1. #include <iostream>
  2. using namespace std;
  3. #include <opencv2\opencv.hpp>
  4. #include <opencv2\highgui\highgui.hpp>
  5. #include <opencv2\contrib\contrib.hpp>
  6. using namespace cv;
  7. int main(int argc, char* argv[])
  8. {
  9. string dir_path = "D:\\opencv_pic\\test\\";
  10. Directory dir;
  11. vector< string> fileNames = dir.GetListFiles(dir_path, "*.jpg", false);
  12. for( int i = 0; i < fileNames.size(); i++)
  13. {
  14. //get image name
  15. string fileName = fileNames[i];
  16. string fileFullName = dir_path + fileName;
  17. cout<< "File name:"<<fileName<< endl;
  18. cout<< "Full path:"<<fileFullName<< endl;
  19. //load image
  20. IplImage* srcImg = cvLoadImage(fileFullName.c_str(), CV_LOAD_IMAGE_UNCHANGED);
  21. cvShowImage( "src", srcImg);
  22. cvWaitKey( 0);
  23. }
  24. return 0;
  25. }
参考:
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值