根据当前文件夹下所有文件随机自动生成训练和测试样本列表

第一次写博客。最近在做图像目标识别的研究,在做样本时自己写了一些实用的小工具,分享到这里。

本工具是用来根据该工具所在文件夹下指定类型的所有文件,随机自动生成训练和测试样本列表的程序,此工具在VS下可直接编译运行,不需要额外的库。

在程序的这里可以指定样本文件的格式

if (strcmp(fname.c_str(),"jpg")==0||strcmp(fname.c_str(),"JPG")==0)//这里指定的文件类型是jpg格式的图片,可以改为或添加其他格式

在main函数修改percent可以修改测试样本所占总样本数比例

double percent = 0.2;    //测试样本占总样本比例

下面是生成的文件样例:

text.txt
102
127
136
137
145
146
155
159
167
196
198
204
214
train.txt
03
04
05
06
07
08
09
10
100
101
11
12

代码块


#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <Windows.h>
#include <io.h>
#include <atlconv.h>

using namespace std;

void getFiles( string path, vector<string>& files )
{
    //文件句柄
    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 );
            }
            else
            {
                string fname;
                fname.append(fileinfo.name);

                int dex = fname.rfind(".");
                fname = fname.substr(dex+1);
                //string ImageFormat = "jpg";
                //if ( strcmp(fname.c_str(), ImageFormat.c_str())==0||fname.c_str() == ImageFormat)
                if (strcmp(fname.c_str(),"jpg")==0||strcmp(fname.c_str(),"JPG")==0)//这里指定的文件类型是jpg格式的图片,可以改为或添加其他格式
                {
                    files.push_back(p.assign(path).append("\\").append(fileinfo.name) );
                }
            }
        }while(_findnext(hFile, &fileinfo)  == 0);
        _findclose(hFile);
    }
}

string GetProgramDir()  
{   
//  char exeFullPath[MAX_PATH]; // Full path
//  string strPath = "";

    char buf[MAX_PATH];

    GetCurrentDirectory(1000,buf);  //得到当前工作路径
    //GetModuleFileName(NULL,exeFullPath,MAX_PATH);
    //strPath=(string)exeFullPath;    // Get full path of the file
    //int pos = strPath.find_last_of('\\', strPath.length());
    return buf;//strPath.substr(0, pos);  // Return the directory without the file name
} 

template<class T>
bool IsExist(vector<T> &vec, T n)
{
    //vector<T>::iterator it = vec.begin();
    for (int i=0; i<vec.size(); i++)
    {
        if (vec.at(i) == n)
        {
            return true;
        }
    }
    return false;
}

int main()
{
    vector<string> files;
    vector<int> TestList;
    getFiles(GetProgramDir(), files );

    ofstream ftrain("train.txt");  //训练样本列表
    ofstream ftest("test.txt");  //测试样本列表

    int i=0;
    double percent = 0.2;    //测试样本占总样本比例
    while (i<(double)files.size()*percent)
    {
        int n = rand()%files.size();
        if (!IsExist<int>(TestList , n))
        {
            TestList.push_back(n);
            ++i;
        }
    }

    for (int i=0; i<files.size(); i++)
    {
        string filename = files.at(i);
        int starts = filename.rfind("\\");
        int ends = filename.rfind(".");
        filename = filename.substr(starts+1,ends-starts-1);
        if (IsExist<int>(TestList , i))
        {
            ftest<<filename.c_str()<<endl;
        }
        else
        {
            ftrain<<filename.c_str()<<endl;
        }
    }

    ftrain.close();
    ftest.close();

    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值