opencv中批量读取图片并保存

本文汇总了使用C++和OpenCV批量读取与保存图片的四种方法,包括按图片编号、从txt文件读取、利用dirent.h遍历文件夹以及使用opencv的Directory库。适合需要进行图像处理的C++开发者参考。

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

之前一直在写python,突然要用C++,因为涉及到图像处理,需要批量读取并存储图片,这里对图片的批量处理代码做一下汇总:

方法1:比较简单,唯一的要求就是文件夹下的图片名称是有规律的,比如1,2,3。

#include "opencv2/opencv.hpp"  
#include "iostream"  

using namespace std;  
using namespace cv;  

#define  NUM  100     //读取image的个数  
int main()  
{  
    Mat image;  
    string ImgName;  
    int n=1;  
    while(n<=NUM)   //100  
    {   
        ImgName="woman";  
        //int 转换string  
        stringstream ss;  
        string str;  
        ss<<n;  
        ss>>str;  

        ImgName=ImgName+" ("+str+")";    //图像文件明格式:ImgName(n)  
        ImgName = "D:\\Mycode\\imagebank\\woman\\" + ImgName+".png";  
        cout<<"处理:"<<ImgName<<endl;  
        image= imread(ImgName);//读取图片  
        if(image.data ==0)  
        { printf("[error] 没有图片\n");}  
        n++;  
   }  

    waitKey(0);  
    system("pause");  
    return 4;  
}  

方法二:需要一个 .txt文件存放文件中待读取图像的名称,每行为一条图像名。

#include "opencv2/opencv.hpp"  
#include "iostream"  
#include <fstream>  
using namespace std;  
using namespace cv;  

int main()  
{  
    Mat image;  
    string ImgName;  
    ifstream fin("woman.txt");//打开原始样本图片文件列表  
    while(getline(fin,ImgName)) //一行一行读取文件列表  
    {    
      cout<<"处理:"<<ImgName<<endl;  
      ImgName = "D:\\Mycode\\woman\\" + ImgName+".png";  
      image= imread(ImgName);//读取图片  

     if(image.data ==0)  
      {
  
  printf("[error] 没有图片\n");return -5;}  
    }  
        waitKey(0);   
return 4;  
}  

方法三:最简单,只需要一个“dirent.h”头文件便可以遍历指定文件夹的所有文件。

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

using namespace std;  
using namespace cv;  

int main()  
{  
    DIR *dir;   
    struct dirent *entry;   
    if((dir=opendir("D:\\Mycode\\VS2010work\\10Image_cut\\Image_cut\\noperson"))==NULL)   
        printf( "Error opening \n ");   
    else {   
        while((entry=readdir(dir))!=NULL) {   
          cout<<entry->d_name<<endl;  
        }   
    }  
    closedir(dir);    
    system("pause");  
    return 0;  
} 

下面给出dirent.h头文件(文件要放到VS安装目录中的include文件夹下,我的路径为C:\vs2015\VC\include)

/* 
 * Dirent interface for Microsoft Visual Studio 
 * Version 1.21 
 * 
 * Copyright (C) 2006-2012 Toni Ronkko 
 * This file is part of dirent.  Dirent may be freely distributed 
 * under the MIT license.  For all details and documentation, see 
 * https://github.com/tronkko/dirent 
 * http://www.softagalleria.net/dirent.php 
 * http://blog.youkuaiyun.com/thefinals/article/details/7174854 
 * https://zhidao.baidu.com/question/939784399849992372.html 
 * http://blog.chinaunix.net/uid-27213819-id-3810699.html 
 */  
#ifndef DIRENT_H  
#define DIRENT_H  

/* 
 * Define architecture flags so we don't need to include windows.h. 
 * Avoiding windows.h makes it simpler to use windows sockets in conjunction 
 * with dirent.h. 
 */  
#if !defined(_68K_) && !defined(_MPPC_) && !defined(_X86_) && !defined(_IA64_) && !defined(_AMD64_) && defined(_M_IX86)  
#   define _X86_  
#endif  
#if !defined(_68K_) && !defined(_MPPC_) && !defined(_X86_) && !defined(_IA64_) && !defined(_AMD64_) && defined(_M_AMD64)  
#define _AMD64_  
#endif  

#include <stdio.h>  
#include <stdarg.h>  
#include <windef.h>  
#include <winbase.h>  
#include <wchar.h>  
#include <string.h>  
#include <stdlib.h>  
#include <malloc.h>  
#include <sys/types.h>  
#include <sys/stat.h>  
#include <errno.h>  

/* Indicates that d_type field is available in dirent structure */  
#define _DIRENT_HAVE_D_TYPE  

/* Indicates that d_namlen field is available in dirent structure */  
#define _DIRENT_HAVE_D_NAMLEN  

/* Entries missing from MSVC 6.0 */  
#if !defined(FILE_ATTRIBUTE_DEVICE)  
#   define FILE_ATTRIBUTE_DEVICE 0x40  
#endif  

/* File type and permission flags for stat(), general mask */  
#if !defined(S_IFMT)  
#   define S_IFMT _S_IFMT  
#endif  

/* Directory bit */  
#if !defined(S_IFDIR)  
#   define S_IFDIR _S_IFDIR  
#endif  

/* Character device bit */  
#if !defined(S_IFCHR)  
#   define S_IFCHR _S_IFCHR  
#endif  

/* Pipe bit */  
#if !defined(S_IFFIFO)  
#   define S_IFFIFO _S_IFFIFO  
#endif  

/* Regular file bit */  
#if !defined(S_IFREG)  
#   define S_IFREG _S_IFREG  
#endif  

/* Read permission */  
#if !defined(S_IREAD)  
#   define S_IREAD _S_IREAD  
#endif  

/* Write permission */  
#
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值