opencv 批量图像读写

本文介绍了一种使用C++和OpenCV批量读写图像的方法。通过生成包含所有图像路径的文本列表,可以方便地处理整个文件夹中的图像。该方法首先创建一个包含图像路径的列表文件,然后使用C++和OpenCV读取这些图像,并将它们保存到新的位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

处理图像数据集时通常要读写整个文件夹里的图像,这时就会用的图像的批量读写。

比较常用的方法就是生成一个包含所有图像的txt列表

生成txt文件的方法如下:

利用cmd进入dos

利用路径进入指定文件夹后生成txt文件

然后可以利用txt列表读入图像并做处理。

#include "opencv2/opencv.hpp"  
#include "iostream"  
#include <fstream>  
#include <windows.h>
#include <string>

using namespace std;
using namespace cv;



int main()
{
    Mat image;
    string ImgName;
    string savefile;
    int count = 1;
    ifstream fin("D:/opencv pro/readfiles/readfiles/English1.txt");//打开原始样本图片文件列表  
    while (getline(fin, ImgName)) //逐行读取文件列表  
    {

        cout << "processing:" << ImgName << endl;
        ImgName = "D:/opencv pro/readfiles/readfiles/" + ImgName;
        savefile = "D:/opencv pro/readfiles/readfiles/saved/" + to_string(count) + ".jpg"; 指定存储路径
        image = imread(ImgName);//读取图片  
        //imshow("1", image);
        if (image.data == 0)
        {
            printf("[error] 没有图片\n"); 
            return -1;
        }
        count++;
        imwrite(savefile, image);

    }

    waitKey(600000); //存储图像
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

春哥111

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值