gpt4 book ai didi

c++ - 如何使用命令行参数在 opencv 中读取多个图像文件?

转载 作者:太空宇宙 更新时间:2023-11-03 22:14:53 25 4
gpt4 key购买 nike

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

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)

是否可以从命令行读取多个图像并将它们作为参数?为什么会崩溃?

最佳答案

我不认为这编译。您必须在循环外分配一个 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

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

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。有成千上万的例子,像这样 How To Parse String File Txt Into Array With C++

关于c++ - 如何使用命令行参数在 opencv 中读取多个图像文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17441080/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com