您的位置:首页 > 其它

复制图片至另外一个文件夹

2015-06-05 19:52 387 查看
随机选取7500张图片到指定文件夹

#include <stdio.h>
#include <iostream>
#include <string>
#include <io.h>
#include <sstream>
#include <opencv2/opencv.hpp>
#include <atlbase.h>
using namespace cv;
using namespace std;

#define Random(x) (rand() % x) //通过取余取得指定范围的随机数

string int2str(int &i) {
string s;
stringstream ss(s);
ss << i;

return ss.str();
}
int main()
{

string fileFolderPath = ".\\images";
string fileExtension = "jpg";
string fileFolder = fileFolderPath + "\\*." + fileExtension;
int randNum[7500];
int num=0;
int dis=7500;               //产生[0, dis)之间的随机数,注意不包括dis
for(int i=0; i<7500; i++)
{
randNum[i]= Random(dis);
//printf("%d\n", Random(dis));
}

// 遍历文件夹
char fileName[1000];
char save_path[80];
struct _finddata_t fileInfo;    // 文件信息结构体

// 1. 第一次查找
long findResult = _findfirst(fileFolder.c_str(), &fileInfo);
if (findResult == -1)
{
_findclose(findResult);
return 0;
}

// 2. 循环查找
do
{
num++;

sprintf(fileName, "%s\\%s", fileFolderPath.c_str(), fileInfo.name);

sprintf(save_path, "%s%d%s", ".\\savedimages\\image", randNum[num], ".jpg");//保存的图片名
if ( fileInfo.attrib == _A_ARCH)  // 是存档类型文件
{

string s;
Mat frame;
frame = imread(fileName);    // 读入图片
IplImage* image=(&(IplImage)frame);

if(fileInfo.name==int2str(randNum[num]))
cvSaveImage( save_path, image);

}

} while (!_findnext(findResult, &fileInfo)||num<7500);

_findclose(findResult);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: