在OpenCV中,有时候可能需要处理多张图片,花了一些时间,查找资料,写了下面这样一段程序,可以读取连续的图片(比如1到99)这样;
代码如下:
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <cv.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;
int _tmain(int argc, _TCHAR* argv[])
{
Mat img1,img2,temp,gray1,gray2;
for (int i = 1; i <= 2; i++)
{
char left_img[100],right_img[100];
char windowname1[100],windowname2[100];
sprintf(left_img, "left%d.jpg", i);
sprintf(right_img, "right%d.jpg",i);
sprintf(windowname1,"window%d.jpg",i);
sprintf(windowname2,"window%d.jpg",i);
namedWindow(windowname1);
namedWindow(windowname2);
img1 = imread(left_img);
img2 = imread(right_img);
if (!img1.data)
{
cout << "Don't load data!" << endl;
return -1;
}
imshow(windowname1,img1);
imshow(windowname2,img2);
waitKey(0);
}
return 0;
}
最近又发现一种更简单的方法,是一个国外小哥给出的,发现可以实现对多张图片的读取和显示:
vector<Mat> images; // another thing. use english for variable names, nothing else...
for (int a=1; a<10;a++) // a <=Count would do one too many...
{
string name = format("E:\\ImageEffect\\FusionImages\\FusionImages_%d.bmp", a);
Mat img = imread(name); // pgm implies grayscale, maybe even: imread(name,0); to return CV_8U
if ( img.empty() ) // please, *always check* resource-loading.
{
cerr << "whaa " << name << " can't be loaded!" << endl;
continue;
}
images.push_back(img);
// show result:
imshow("test",img);
waitKey(); // yes, you need the waitKey()
}
return 0;
链接: OpenCV读取多幅图片
在OpenCV的官网上,也找到一个方法,但是没有证实,附上链接: 点击打开链接!