编写函数对TUM数据集内,associate.txt没有用到的RGB图片进行过滤

大致思路:

1. 得到路径内rgb/所有的照片名称,存入file容器里面

2. 遍历associate.txt 得到我们实际用的到的图片, 放入字符串str

3. 经过对比,删除掉file里面用得到照片,便可以得到没有用到的照片,相对于 求了一个用到的图片的补集

#include <iostream>
#include <fstream>
#include <cstdio>
#include <dirent.h>
#include <string.h>
#include <vector>
using namespace std;
void remove_img(string str);
void getFiles(std::string path, std::vector<std::string> &files);
int main()
{
    ifstream infile;
    infile.open("../associate.txt");
    if (!infile.is_open())
    {
        cout<<"open file failed !"<<endl;
    }

    std::string path = "../rgb";
    std::vector<std::string> files;
    getFiles(path, files);


    string s;
    char img_path[30];   // 一个一个写的话,字符数组后面没有可能会随机生成
    int flag =0,cnt=0;

    while(!infile.eof())
    {
        getline(infile,s);
        //cout<<s<<endl;

        for (char ch :s)
        {

            if (ch == '/')
            {
                flag =1;
                continue;
            }

            if (flag)
            {
                if (ch == ' ')
                {
                    *(img_path+cnt) = '\0';   //  字符数组结尾是一个 0
                    flag = 0;
                    break;
                }
                *(img_path+cnt) = ch;

                //cout<<*(img_path+cnt);
                cnt ++;
            }

        }

        string str(img_path);
        str ="../rgb/"+str;
         cout<<str;

        for (int i = 0; i < files.size(); ++i)
        {
            if (str.compare(files[i]) == 0) // 如果相同就跳出,把相同的找出来,剩下的就是不同的了
            {
                files.erase(files.begin()+i);
                break;
            }
        }

        //remove_img(str);
        cnt =0;  //每次读取一行,cnt 应该清零
        cout<<endl;
    }

// 遍历所有照片,遇到associate.txt有的照片 ,则擦除,剩下的就是 没有用到的照片
    for (int j = 0; j <files.size() ; ++j)
    {
        remove_img(files[j]);
    }


    return 0;
}


// 得到所有的照片
void getFiles(std::string path, std::vector<std::string> &files) {
    DIR *dir;
    struct dirent *ptr;
    if ((dir = opendir(path.c_str())) == NULL) {
        perror("Open dir error...");
        return;
    }

    while ((ptr = readdir(dir)) != NULL) {
        if (strcmp(ptr->d_name, ".") == 0 || strcmp(ptr->d_name, "..") == 0)    ///current dir OR parrent dir
            continue;
        else if (ptr->d_type == 8)    ///file
        {
            std::string strFile;
            strFile = path;
            strFile += "/";
            strFile += ptr->d_name;
            files.push_back(strFile);
        } else {
            continue;
        }
    }
    closedir(dir);
}

// 删除照片
void remove_img(string str)
{

    const char *savePath = str.c_str();   // 字符串转char *

    if(remove(savePath)==0){
             cout<<"删除成功"<<endl;
    }
     else{
             cout<<"删除失败"<<endl;
     }
    printf("%s\n", savePath);
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值