如何遍历某一文件夹下所有图片并切割制作负样本
2017-01-06 00:44
405 查看
主要功能
遍历某一文件夹下的所有文件得到某一文件夹下的所有文件名及全路径名
切割单张图片得到多张图片并存储到指定文件夹
自动根据文件夹下的文件名(不带扩展名)创建文件夹
代码展示
#include "cv.h" #include "highgui.h" #include <iostream> #include <string> #include<direct.h>//////创建文件夹的头文件 #include <io.h> using namespace std; using namespace cv; //从背景图片中随机抽取图像块,多用于生成负样本 #define kImageBlockWidth 40 //图像块大小 #define kImageBlockHeight 40 #define kLoopTimes 60 //期望样本数 //////制作负样本函数 void make_neg_pic_process( string pic_path_and_name,//////picture full path Name string pic_save_path,///////save path string str_only_pic_name////only picture name ); ///////遍历文件夹,得到文件下的所有文件名 void getFiles( string path,//////要操作的文件夹路径 vector<string>& files,//////存储文件夹下的所有文件名(不带后缀名) Vector<string>& pic_full_path_and_name////////////存储文件夹下的所有文件的全路径文件名 ); string g_str_pics_path = "C:\\Users\\Administrator\\Desktop\\车辆正样本_大庆长龙";/////存放要处理图片的文件夹路径 int main(int argc, char* argv[]) { /////读取图片文件夹下的所有文件,去掉后缀,名字放到str_vec_for_only_files_name中 vector<string>str_vec_for_only_files_name; Vector<string> pic_full_path_and_name;////////////存储文件夹下的所有文件的全路径文件名 getFiles(g_str_pics_path,str_vec_for_only_files_name,pic_full_path_and_name); //////创建文件夹存储负样本 for(int i = 0; i < str_vec_for_only_files_name.size();i++) { mkdir((str_vec_for_only_files_name[i]).c_str());//////以每个图片名作为文件夹的名字作为创建文件夹 /////得到每一个图片的全路径文件名 //string pic_full_path = g_str_pics_path + "\\" + str_vec_for_files_name[i] + ".png"; make_neg_pic_process(pic_full_path_and_name[i],str_vec_for_only_files_name[i],str_vec_for_only_files_name[i]); cout<<"处理完第"<<i+1<<"幅图片!"<<endl; } system("pause"); return 0; } ///////遍历文件夹,得到文件下的所有文件名 void getFiles( string path,//////要操作的文件夹路径 vector<string>& files,//////存储文件夹下的所有文件名(不带后缀名) Vector<string>& pic_full_path_and_name ) { //文件句柄 long hFile = 0; //文件信息 struct _finddata_t fileinfo; string p; if((hFile = _findfirst(p.assign(path).append("\\*").c_str(),&fileinfo)) != -1) { do { //如果是目录,迭代之 //如果不是,加入列表 if((fileinfo.attrib & _A_SUBDIR)) { if(strcmp(fileinfo.name,".") != 0 && strcmp(fileinfo.name,"..") != 0) getFiles( p.assign(path).append("\\").append(fileinfo.name), files,pic_full_path_and_name ); } else { pic_full_path_and_name.push_back(p.assign(path).append("\\").append(fileinfo.name) ); string str_tmp(fileinfo.name); string str_tmp1(str_tmp.begin(),str_tmp.end()-4); files.push_back(str_tmp1); } }while(_findnext(hFile, &fileinfo) == 0); //for(int i = 0; i < files.size();i++)//////输出文件名 //{ // cout<< i <<"个的名字为:"<<files[i]<<endl; //} _findclose(hFile); } } //////制作负样本函数 void make_neg_pic_process( string pic_path_and_name,//////picture full path Name string pic_save_path,///////save path string str_only_pic_name////only picture name ) { int originX = 0, originY = 0; int width_limited = 0, height_limited = 0; int width = 0, height = 0; //IplImage *bgImage = cvLoadImage("neg\\lijia.jpg"); IplImage *bgImage = cvLoadImage(pic_path_and_name.c_str()); IplImage *blockImage = cvCreateImage(cvSize(kImageBlockWidth, kImageBlockHeight), bgImage->depth, bgImage->nChannels); width = bgImage->width; height = bgImage->height; width_limited = width - kImageBlockWidth; height_limited = height - kImageBlockHeight; //cout << width_limited << " " << height_limited; for (int i = 0; i < kLoopTimes; i++) { originX = rand() % width_limited; originY = rand() % height_limited; cvZero(blockImage); CvPoint2D32f center_block = cvPoint2D32f(originX + kImageBlockWidth / 2, originY + kImageBlockHeight / 2); cvGetRectSubPix(bgImage, blockImage, center_block); char saveFileName[100] = { '\0' }; sprintf(saveFileName, "%s_%d.jpg",str_only_pic_name.c_str(), i + 1); //cvSaveImage(saveFileName, blockImage); string str_temp = pic_save_path; str_temp.append("\\").append(saveFileName); cvSaveImage(str_temp.c_str(), blockImage); } cvReleaseImage(&bgImage); cvReleaseImage(&blockImage); }
相关文章推荐
- 如何遍历所有文件夹,获取某一类型文件
- 如何遍历读取文件夹内所有图片并处理
- 如何遍历文件夹下的所有图片(Pyhton&C++)
- matlab如何循环读入某一文件夹下的所有图片 对某文件夹下的图片进行重新排序
- 如何读取一个文件夹下的所有图像文件,并制作负样本到指定目录
- ASP.NET2.0 遍历文件夹下所有图片
- ASP.NET2.0 遍历文件夹下所有图片【月儿原创】
- JS如何遍历一个文件夹下的所有文件与目录
- ASP.NET2.0 遍历文件夹下所有图片http://blog.csdn.net/21aspnet/archive/2007/04/04/1551222.aspx
- iphone 利用数组遍历某一文件夹中的图片
- 如何读取某个指定文件夹下所有文件,并筛选出其中的图片文件(jpeg,bmp)展示出来。
- 通过datalist遍历文件夹下所有图片
- [导入]ASP.NET2.0 遍历文件夹下所有图片【月儿原创】
- ASP.NET2.0 遍历文件夹下所有图片
- VC遍历指定文件夹下的所有文件 及如何删除文件夹
- 通过datalist遍历文件夹下所有图片
- 如何用C遍历一个文件夹中的所有文件
- ASP.NET2.0 遍历文件夹下所有图片
- ASP.NET2.0 遍历文件夹下所有图片
- ASP.NET2.0 遍历文件夹下所有图片