图像处理中处理批量图片和文件的小程序

微信公众号:码出名企路—图像处理技巧

1 批量图片重新命名

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/opencv.hpp>
#include <iostream>
#include <fstream>

using namespace std;
using namespace cv;

int main()
{
    cv::String path="/home/lyy/from_0_to_1_for_slam/homework_1/data/";//待处理图片路径
    cv::String dest="/home/lyy/from_0_to_1_for_slam/homework_1/dst/";//保存处理后的图片路径
    cv::String savefilename;
    vector<cv::String> filenames;
    Mat srcImg,dstImg;

    cv::glob(path,filenames);//glob 寻找与模式匹配的文件路径
   for(int i=0;i<filenames.size();++i)
    {
        srcImg=cv::imread(filenames[i]);
        srcImg.copyTo(dstImg);
        if(i<10)
        {
        savefilename=dest+"000"+to_string(i)+".png";
        cv::imwrite(savefilename,dstImg);
        }
        if(i>=10&&i<99)
        {
        savefilename=dest+"00"+to_string(i)+".png";
        cv::imwrite(savefilename,dstImg);
        }
        if(i>=100&&i<999)
        {
        savefilename=dest+"0"+to_string(i)+".png";
        cv::imwrite(savefilename,dstImg);
        }
        if(i>=1000&&i<9999)
        {
        savefilename=dest+to_string(i)+".png";
        cv::imwrite(savefilename,dstImg);
        }
     
    }
  
return 0;
  }

2. 查找文件中批量数据

#include <fstream>
#include <iostream>
#include <vector>
#include <sstream>

using namespace std;

vector<pair<int,string>> data;

void read_txt(string input_file)
{
    ifstream file_open;
    file_open.open(input_file.c_str());
    if(!file_open.is_open())
    {
        cout<<"cannot open file"<<endl;
        exit(1);
    }

    std::string file_line;
    int num;
    int people;
    string city;
    string note;

    bool not_first_line=false;
    bool first_line=true;

    while(std::getline(file_open,file_line) && !file_line.empty())
    {   
        //第一行为标题的情况下,且各列之间是空格分隔符
      if(first_line)
        {
            not_first_line=true;
            first_line=false;
        }

      else if(not_first_line)
       {
          std::istringstream data_(file_line);
          data_>>num>>people>>city>>note;

       cout<<num<<" "<<people<<" "<<city<<" "<<note<<endl;;
       
        pair<int,string> _data=std::make_pair(people,city);
        data.push_back(_data);
       }
      
    }
    file_open.close();
     
    for(int i=0;i<data.size();i++)
    {
        if(data[i].first > 600)
            cout<<"人流量大于600的城市:"<<data[i].second<<endl;
    }

}

int main()
{
    string input="./1.txt";
    read_txt(input);

    return 0;
}

3. 在文件首行添加列名称

#include <iostream>
#include <fstream>
using namespace std;

//在第一行插入各列的名字

int main()
{
    ifstream ifs("1.txt");
    ofstream ofs("output.txt");
    //ofs<<"x1\ty1\tx2\ty2\tth"<<endl;
    ofs<<"序号\t人流量\t城市\t其他\t"<<endl;
    char str[1024];
    while(ifs.getline(str, 1024)){
        ofs<<str<<endl;
    }
    ifs.close();
    ofs.close();
    return 0;
}

4. 修改文件中特定变量


```cpp
#include <fstream>
#include <iostream>
#include <cstring>
using namespace std;

struct Data
{
    char name[20];
    int people;
};

int main()
{
    FILE *fp=fopen("./2.txt","r+");
    Data data={"宁波",200};//修改目标
    char name[100];
    int people;
    while(fscanf(fp,"%s%d",name,&people)!=EOF)
    {
        if(strcmp(name,data.name)==0 && people==data.people)//找到目标
        {
            fseek(fp,-3,SEEK_CUR);//回滚指针到200前
            cout<<"Please input new people"<<endl;
            cin>>people;
            fprintf(fp,"%d",people);//覆盖写入
            break;
        }
    }
    fclose(fp);

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值