opencv 批量图像读写

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

比较常用的方法就是生成一个包含所有图像的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;
}

 

转载于:https://www.cnblogs.com/klitech/p/5955500.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值