我想在我的程序中加载大量图像,以便稍后将它们拼接在一起。我正在使用命令行来提及图像地址。如果我事先知道我将在输入中输入的图像数量,它就可以工作,但它不适用于可变数量的输入图像:
if (argc < 3)
{
cerr<<"Usage:" << argv[0] << "SOURCE DESTINATION" << endl;
return 1;
}
for (int i = 0;i <argc;i++)
{
Mat img[i] = imread(argv[i+1], 0);
imshow("image", img[i]);
}
在这种情况下是否有任何其他方法可以获取输入。我需要处理 20 到 25 张图像。我可以使用一些文件,存储地址并读取它吗?我认为这是可能的,但它是如何工作的?
编辑:
经过一些修改,我的程序现在看起来像这样:
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#include <opencv2/imgproc/imgproc.hpp>
#include "opencv2/calib3d/calib3d.hpp"
#include <math.h>
#include <cmath>
#define PI 3.14159
#define max_img 25
using namespace cv;
using namespace std;
//main function
int main(int argc, char** argv)
{const int MAX_NUM_OF_IMG = 1024;
cv::Mat img[MAX_NUM_OF_IMG];
for(int i=1;i<argc;i++)
{
std::cout << i << ": " << argv[i] << std::endl;
img[i-1] = imread(argv[i],0);
std::cout << "Finished reading images1" << std::endl;
imshow("image",img[i]);
//start stitching operations
}std::cout << "Finished reading images2" << std::endl;
cv::waitKey();
return 0;
}
我在命令行中给出了以下内容:
./img_many camera3/imageb-032.jpgcamera3/imageb-033.jpgcamera3/imageb-034.jpgcamera3/imageb-035.jpgcamera3/imageb-036.jpgcamera3/imageb-037.jpgcamera3/imageb-038.jpgcamera3/imageb-039.jpg
我得到的输出是
1: camera3/imageb-032.jpgcamera3/imageb-033.jpgcamera3/imageb-034.jpgcamera3/imageb-035.jpgcamera3/imageb-036.jpgcamera3/imageb-037.jpgcamera3/imageb-038.jpgcamera3/imageb-039.jpg
Finished reading images1
OpenCV Error: Bad flag (parameter or structure field) (Unrecognized or unsupported array type) in cvGetMat, file /build/buildd/opencv-2.3.1/modules/core/src/array.cpp, line 2482
terminate called after throwing an instance of 'cv::Exception'
what(): /build/buildd/opencv-2.3.1/modules/core/src/array.cpp:2482: error: (-206) Unrecognized or unsupported array type in function cvGetMat
Aborted (core dumped)
是否可以从命令行读取多个图像,并将它们作为参数?为什么会崩溃?