备份:C++读取指定目录下的文件重命名后另存为

某批图像中的名字同时包含了时间戳和其他前缀,想要批量进行处理,图像名称只保留时间戳
吐槽:使用python更快更方便,但是python环境死活用不了了

前提:要有记录图像路径的txt文件./data.txt

changeimgname.cpp文件

#include
#include
#include
#include

using namespace std;

int main(int argc, char **argv)
{
    vector<string> vImgPath;
    ifstream fin;
    string imageDir = "./data.txt";
    fin.open(imageDir.c_str());
    if(!fin.is_open())
    {
        cerr<<"imageDir "<<imageDir<<"is wrong."<<endl;
        exit(1);
    }

    while(!fin.eof())
    {
        string path;
        getline(fin, path);
        if(!path.empty())
            vImgPath.push_back(path);
    }

    for(size_t i=0;i<vImgPath.size();i++)
    {
        cv::Mat img_src = cv::imread(string(vImgPath[i]));
        int pos = vImgPath[i].rfind("bev_");
        string img_name = vImgPath[i].substr(pos+4,17);
        cout<<"img_name["<<i<<"] = "<<img_name<<endl;
        string img_dst_dir = "./imgdata/" + img_name + ".jpg";
        cv::imwrite(img_dst_dir.c_str(),img_src);
    }

    return 0;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.2)

project(imgprocess)

find_package(OpenCV 3 REQUIRED)

include_directories(${OpenCV_INCLUDE_DIR})

add_executable(${PROJECT_NAME} changeimgname.cpp)
target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS})

你可能感兴趣的:(C++学习笔记,c++,opencv,计算机视觉)