0012-用OpenCV批量读取图片的三种方法

有时我们需要批量读取图片,所以我们有必要知道怎么在OpenCV开源环境下批量读取图片!

批量读取图片的关键是如何让程序知道文件夹下图片的名字!

第一种方法
这种方法只针对图片名字有规律的情况,比如:
***(0).jpg
***(1).jpg
***(2).jpg
***(3).jpg
..................

源代码如下
代码中用到的图片下载链接为:http://pan.baidu.com/s/1hr7GUtI 密码:gf7w

//opencv版本:OpenCV3.0
//VS版本:VS2013
//Author:qxsf321.net

#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>    
#include <opencv2/imgproc/types_c.h>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/highgui/highgui_c.h>
#include <iostream>

using namespace cv;
using namespace std;

#define  NUM  13    //读取image的个数  
int main()
{
        Mat image;
        string ImgName;
        int n = 1;
        while (n <= NUM)   //13
        {
                //while循环中的完整代码请搜索公众号"qxsf321",关注后回复0012即可获取
                //while循环中的完整代码请搜索公众号"qxsf321",关注后回复0012即可获取
                //while循环中的完整代码请搜索公众号"qxsf321",关注后回复0012即可获取
        }

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

代码说明
上面的代码实现的关键是把int类型转化为字符串类型,在这里使用类stringstream来进行转换,有人要问为什么不用sprintf来实现,具体的原因和对类stringstream的使用说明大家可下面这个网页!
http://www.cppblog.com/Sandywin/archive/2007/07/13/27984.html

在上面的代码中,我们想在路径 D:\\Hubble 想读取13张图片,但是我只在文件夹中放了10张图片,所以前10张应该是读取成功,而后三张是读取失败的。
相关截果如下



第二种方法
将图像的名字放在一个txt文件中,每一行是一幅图像的名字!
源代码如下
代码中用到的图片下载链接:http://pan.baidu.com/s/1c1DoSa0 密码:h2n0
代码中所需的txt文件下载链接:http://pan.baidu.com/s/1c2q4zW8 密码:0zhl

//opencv版本:OpenCV3.0
//VS版本:VS2013
//Author:qxsf321.net

#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>    
#include <opencv2/imgproc/types_c.h>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/highgui/highgui_c.h>
#include <iostream>
#include <fstream>  

using namespace cv;
using namespace std;

int main()
{
        Mat image;
        string ImgName;
        ifstream fin("Hubble_name_list.txt");//打开原始样本图片文件列表  
        while (getline(fin, ImgName)) //一行一行读取文件列表  
        {
                //while循环中的完整代码请搜索公众号"qxsf321",关注后回复0012即可获取
                //while循环中的完整代码请搜索公众号"qxsf321",关注后回复0012即可获取
                //while循环中的完整代码请搜索公众号"qxsf321",关注后回复0012即可获取
        }
        printf("本程序由qxsf321.net提供\n");
        printf("本程序由qxsf321.net提供\n");
        printf("本程序由qxsf321.net提供\n");
        waitKey(0);
        return 0;
}


代码说明

上面的程序用类ifstream实现一行一行的读取txt文件,具体的使用方法这里就不再赘述了,看上面的代码很容易明白。
相关截图如下

上图中之所以最后三幅图像读取失败,是因为文件夹下没有这三个名字的图像,文件夹下有的图片如下面截图所示



第三种方法
前两种方法都需要知道图片的名字,第三种方法则不需要,这种方法能自动遍历读取文件下的所有图片文件!
源代码如下
代码中用到的图片下载链接:http://pan.baidu.com/s/1qYhsAwW 密码:3see
代码中用到的头文件<dirent.h>下载链接:http://pan.baidu.com/s/1i4WH0HZ 密码:bwas

//opencv版本:OpenCV3.0
//VS版本:VS2013
//Author:qxsf321.net

#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>    
#include <opencv2/imgproc/types_c.h>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/highgui/highgui_c.h>
#include <iostream>
#include <dirent.h>  

using namespace cv;
using namespace std;

int main()
{
        DIR *dir;
        struct dirent *entry;
        
        //这个程序的完整代码请搜索公众号"qxsf321",关注后回复0012即可获取
        //这个程序的完整代码请搜索公众号"qxsf321",关注后回复0012即可获取
        //这个程序的完整代码请搜索公众号"qxsf321",关注后回复0012即可获取

        return 0;
}

代码说明

本程序用dirent.h中定义的DIR类实现对目录中所有文件的遍历读取!具体的使用方法这里就不再赘述了,看上面的代码很容易明白。
上面的代码需要包含头文件<dirent.h>,这个头文件下载链接:链接:http://pan.baidu.com/s/1i4WH0HZ  分享密码请搜索公众号"qxsf321",关注后回复0012即可获取
程序运行截图如下

猜你喜欢

转载自blog.csdn.net/lehuoziyuan/article/details/84064056