如何使用命令行参数在opencv中读取多个图像文件? [英] how to read multiple image files in opencv using commandline arguments?

查看:543
本文介绍了如何使用命令行参数在opencv中读取多个图像文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的程序中加载大量图片,以便以后将它们拼接在一起。我使用命令行提及图像地址。如果我提前知道我将在输入的图像数量,但它不适用于可变数量的输入图像工作:

I want to load a large number of images in my program, to later stitch them together. I am using commandline to mention image address. It works if I know in advance the number of images I will have in input but it doesn't works for variable number of input images:

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的图像。我可以使用一些文件,存储地址和读取。我认为这是可能的,但它是如何工作的?

Is there any other method to get inputs in this case. I need to work on 20 to 25 images. Can I use some file, storing address and read that. I think it is possible, but how does it work?

编辑

经过一些修改,我的程序现在看起来像这样:

After some modifications, my program now look like this:

#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;
}

我在命令行中提供了以下命令:

I gave the following in the commandline:

./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

输出I get是

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)

可以从命令行读取多个映像给他们作为参数?为什么会崩溃?

Is it possible to read multiple images from commandline giving them as arguments? Why is it crashing?

推荐答案

我不认为这会编译。
你必须从循环中分配一个数组cv :: Mat

I don't think this compiles. You have to allocate out of the loop an array of cv::Mat

 const int MAX_NUM_OF_IMG = 1024;
 cv::Mat img[MAX_NUM_OF_IMG]; //You could also allocate this dynamically based on the number of arguments/the value of argc

在循环中,你正在打破边界,你必须这样改变:

Then, in the loop, you are breaking the boundaries, you have to change it like this:

for(int i=1;i<argc;i++) //You should also check you have not more than MAX_NUM_OF_IMG
  {
  img[i-1] = imread(argv[i],0);  ///What is zero by the way
  imshow("image",img[i]);
  //Probably here you want some short sleep, or a "press a key" event
  }

您获得的图片数量为 argc-1

有你的文件的名称存储在一些txt文件,你可以使用像std :: ifstream来解析它,因为它是std :: cin。有成千上万个示例,像这样如何解析字符串使用C ++将文件Txt转换为数组

Then, if you want to have the name of your files stored in some txt file, you can use something like std::ifstream to parse it, as it was std::cin. There are thousands of examples, like this How To Parse String File Txt Into Array With C++

这篇关于如何使用命令行参数在opencv中读取多个图像文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆