C++获取文件路径下的所有jpg文件--分割string

拜拜、爱过 提交于 2020-01-07 20:50:20
//https://docs.opencv.org/3.4.8/d3/dc0/group__imgproc__shape.html#ga3d476a3417130ae5154aea421ca7ead9
//https://docs.opencv.org/3.4.8/db/dd6/classcv_1_1RotatedRect.html#a055a5d35e50bce65e3b1dee318dd3044
//https://docs.opencv.org/3.4.8/df/dee/samples_2cpp_2minarea_8cpp-example.html#a13
//https://blog.csdn.net/lanyuelvyun/article/details/76614872

long findfileinfolder(const char * dir_name, string extend_name, std::vector<std::string> &fileList, std::vector<std::string> &nameList)//文件路径
{
    long number = 0;
    if( NULL == dir_name )
    {
        cout<<" dir_name is null ! "<<endl;
        return 0;
    }
    struct stat s;
    lstat( dir_name , &s );
    struct dirent * filename;    // return value for readdir()
    DIR * dir;                   // return value for opendir()
    dir = opendir( dir_name );
    if( NULL == dir )
    {
        cout<<"Can not open dir "<<dir_name<<endl;
        return 0;
    }
    /* read all the files in the dir ~ */
    while( ( filename = readdir(dir) ) != NULL )
    {
        // get rid of "." and ".."
        if( strcmp( filename->d_name , "." ) == 0 ||
            strcmp( filename->d_name , "..") == 0    )
            continue;

        string sFilename(filename->d_name);
        string suffixStr = sFilename.substr(sFilename.find_last_of('.') + 1);//获取文件后缀

        if (suffixStr.compare(extend_name) == 0) {//根据后缀筛选文件

            //cout<<filename->d_name <<endl;
            ++number;
            string st1 = dir_name;
            string st2 = filename->d_name;
            fileList.push_back(st1+st2);
            nameList.push_back(st2);
        }

    }
    return number;
}
    std::vector<std::string> fileList;
    std::vector<std::string> nameList;
    long numFile = findfileinfolder("/home/spple/labelme_json/", "json", fileList, nameList);
    if (numFile==0)
        return 0;

    for (int f = 0; f < fileList.size(); ++f) {
        ifstream is(fileList[f], ios::binary);
        if (!is.is_open())
        {
            cout << "open json file failed." << endl;
            return -1;
        }
    }

C++常见问题: 字符串分割函数 split

https://www.cnblogs.com/dfcao/p/cpp-FAQ-split.html

void SplitString(const std::string& s, std::vector<std::string>& v, const std::string& c)
{
  std::string::size_type pos1, pos2;
  pos2 = s.find(c);
  pos1 = 0;
  while(std::string::npos != pos2)
  {
    v.push_back(s.substr(pos1, pos2-pos1));
 
    pos1 = pos2 + c.size();
    pos2 = s.find(c, pos1);
  }
  if(pos1 != s.length())
    v.push_back(s.substr(pos1));
}

放上自己的代码:

int findfileinfolder(const char * dir_name, string extend_name, std::vector<std::string> &fileList, std::vector<std::string> &nameList)//文件路径
{
    int number = 0;
    if( NULL == dir_name )
    {
        cout<<" dir_name is null ! "<<endl;
        return 0;
    }
    struct stat s;
    lstat( dir_name , &s );
    struct dirent * filename;    // return value for readdir()
    DIR * dir;                   // return value for opendir()
    dir = opendir( dir_name );
    if( NULL == dir )
    {
        cout<<"Can not open dir "<<dir_name<<endl;
        return 0;
    }
    /* read all the files in the dir ~ */
    while( ( filename = readdir(dir) ) != NULL )
    {
        // get rid of "." and ".."
        if( strcmp( filename->d_name , "." ) == 0 ||
            strcmp( filename->d_name , "..") == 0    )
            continue;

        string sFilename(filename->d_name);
        string suffixStr = sFilename.substr(sFilename.find_last_of('.') + 1);//获取文件后缀

        if (suffixStr.compare(extend_name) == 0) {//根据后缀筛选文件

            //cout<<filename->d_name <<endl;
            ++number;
            string st1 = dir_name;
            string st2 = filename->d_name;
            fileList.push_back(st1+st2);
            nameList.push_back(st2);
        }

    }
    return number;
}

void SplitString(const std::string& s, std::vector<std::string>& v, const std::string& c)
{
    std::string::size_type pos1, pos2;
    pos2 = s.find(c);
    pos1 = 0;
    while(std::string::npos != pos2)
    {
        v.push_back(s.substr(pos1, pos2-pos1));

        pos1 = pos2 + c.size();
        pos2 = s.find(c, pos1);
    }
    if(pos1 != s.length())
        v.push_back(s.substr(pos1));
}


int main() {

#ifdef USE_GPU
    int flag = cuda::getCudaEnabledDeviceCount();
    if (flag != 0) { cuda::setDevice(0); }
#endif // USE_GPU

    std::vector<std::string> fileList;
    std::vector<std::string> nameList;
    vector<pair<string,vector<cv::Mat> > >mat_pairs;
    string mainPath = "/home/spple/CLionProjects/PointCloudFusion/dataset/";
    string middPath = "_OtherSampleFrame_IMG_";
    string endPathGray = "Texture_8Bit.png";
    string endPathDep = "DepthMap.tif";
    string endPathNx = "NormalMap_X.tif";
    string endPathNy = "NormalMap_Y.tif";
    string endPathNz = "NormalMap_Z.tif";
    int numFile = findfileinfolder(mainPath.c_str(), "png", fileList, nameList);
    if (numFile==0)
        return 0;
    for (int f = 0; f < nameList.size(); ++f) {
        vector<cv::Mat> temp;
        vector<string> strtemp;
        SplitString(nameList[f], strtemp, middPath);
        temp.push_back(cv::imread(mainPath+strtemp[0]+middPath+endPathGray, -1));
        temp.push_back(cv::imread(mainPath+strtemp[0]+middPath+endPathDep, -1));
        temp.push_back(cv::imread(mainPath+strtemp[0]+middPath+endPathNx, -1));
        temp.push_back(cv::imread(mainPath+strtemp[0]+middPath+endPathNy, -1));
        temp.push_back(cv::imread(mainPath+strtemp[0]+middPath+endPathNz, -1));
        mat_pairs.push_back(make_pair(nameList[f], temp));
    }

    vector<cv::Mat>Rt_pairs;
    Mat R_ICPInit, t_ICPInit, T_ICPInit;
    for (int gms = 0; gms < mat_pairs.size()-1; ++gms) {
        runImagePair(mat_pairs[gms].second[0], mat_pairs[gms+1].second[0], mat_pairs[gms].second[1], mat_pairs[gms+1].second[1], R_ICPInit, t_ICPInit, T_ICPInit);
        Rt_pairs.push_back(T_ICPInit);
    }
}

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!