前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >图像处理中处理批量图片和文件的小程序

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

作者头像
用户9831583
发布2022-06-16 14:28:05
3620
发布2022-06-16 14:28:05
举报
文章被收录于专栏:码出名企路码出名企路

1 批量图片重新命名

代码语言:javascript
复制
#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. 查找文件中批量数据

代码语言:javascript
复制
#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. 在文件首行添加列名称

代码语言:javascript
复制
#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. 修改文件中特定变量

代码语言:javascript
复制
#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;
}

博主:菜鸟程序员

初衷:学习资料,程序设计,视觉算法,求职经验,工作心得

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-02-06,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 码出名企路 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档