Image OpenCV:加载多个图像

Image OpenCV:加载多个图像,image,opencv,Image,Opencv,我正在更新一些旧的OpenCV代码,这些代码是(我猜)以OpenCV 1.1的方式编写的(即使用IplImages) 我现在想要完成的是将一系列图像(作为命令行参数传递)作为MAT加载。这是一项更大任务的一部分。下面的第一个代码示例是旧代码的图像加载方法。它从命令行加载5个图像,并按顺序显示它们,每次单击一个键后暂停,然后退出 第二个代码示例是我使用Mat的更新版本。到目前为止效果不错,但这是最好的方法吗?我用了一系列的垫子。我应该使用指向垫子的指针数组吗?是否有一种方法可以做到这一点,即在运行

我正在更新一些旧的OpenCV代码,这些代码是(我猜)以OpenCV 1.1的方式编写的(即使用IplImages)

我现在想要完成的是将一系列图像(作为命令行参数传递)作为MAT加载。这是一项更大任务的一部分。下面的第一个代码示例是旧代码的图像加载方法。它从命令行加载5个图像,并按顺序显示它们,每次单击一个键后暂停,然后退出

第二个代码示例是我使用Mat的更新版本。到目前为止效果不错,但这是最好的方法吗?我用了一系列的垫子。我应该使用指向垫子的指针数组吗?是否有一种方法可以做到这一点,即在运行时从
argc
确定图像的数量,而不需要使用
IMAGE\u NUM
提前设置

基本上,我希望能够将任意数量(合理范围内)的图像作为命令行参数传递,并将它们加载到某个方便的数组或其他类似存储中,以供以后参考

谢谢

旧代码:

#include <iostream>
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
using namespace std;
using namespace cv;

// the number of input images
#define IMAGE_NUM 5

int main(int argc, char **argv)
{
    uchar **imgdata;
    IplImage **img;
    int index = 0;
    char *img_file[IMAGE_NUM];

    cout << "Loading files" << endl;
    while(++index < argc)
        if (index <= IMAGE_NUM)
            img_file[index-1] = argv[index];

    // malloc memory for images
    img = (IplImage **)malloc(IMAGE_NUM * sizeof(IplImage *)); // Allocates memory to store just an IplImage pointer for each image loaded
    imgdata = (uchar **)malloc(IMAGE_NUM * sizeof(uchar *));

    // load images. Note: cvLoadImage actually allocates the memory for the images
    for (index = 0; index < IMAGE_NUM; index++) {
        img[index] = cvLoadImage(img_file[index], 1);
        if (!img[index]->imageData){
            cout << "Image data not loaded properly" << endl;
            return -1;
        }
        imgdata[index] = (uchar *)img[index]->imageData;
    }

    for (index = 0; index < IMAGE_NUM; index++){
        imshow("myWin", img[index]);
        waitKey(0);
    }

    cvDestroyWindow("myWin");
    cvReleaseImage(img);

    return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
使用名称空间cv;
//输入图像的数量
#定义图像_NUM 5
int main(int argc,字符**argv)
{
乌查尔**imgdata;
IplImage**img;
int指数=0;
char*img_文件[IMAGE_NUM];

cout您可以使用向量而不是数组:

比如说

#include <iostream>
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
#include <time.h>
#include <vector>
using namespace std;
using namespace cv;

int main(int argc, char **argv)
{
    vector<Mat> img;
    //Mat img[IMAGE_NUM];
    int index = 0;

    for (index = 0; index < IMAGE_NUM; index++) {

        //img[index] = imread(argv[index+1]);
        img.push_back(imread(argy[index+1]));

        if (!img[index].data){
            cout << "Image data not loaded properly" << endl;
            cin.get();
            return -1;
        }
    }

    vector<Mat>::iterator it;

    for (it = img.begin(); it != img.end() ; it++) {
        imshow("myWin", (*it));
        waitKey(0);
    }
    cvDestroyWindow("myWin");
    return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
使用名称空间cv;
int main(int argc,字符**argv)
{
矢量img;
//Mat img[图像数量];
int指数=0;
对于(索引=0;索引cout我花了一段时间才回到这一点,但我最后做的是,这在功能上可能与Gootik的建议相同。这对我来说效果很好。请注意,对于采用
Mat&
(即单个
cv::Mat
)的函数,您只需取消对Mats数组的引用并传递它,在Matlab中进行了大量图像处理工作后,我更习惯使用这种表示法

#include <iostream>
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
using namespace std;
using namespace cv;

int main(int argc, char **argv)
{
    if (argc==1){
        cout << "No images to load!" << endl;
        cin.get();
        return 0;
    }

    int index = 0;
    int image_num = argc-1;

    Mat *img = new Mat[image_num]; // allocates table on heap instead of stack

    // Load the images from command line:
    for (index = 0; index < image_num; index++) {
        img[index] = imread(argv[index+1]);
        if (!img[index].data){
            cout << "Image data not loaded properly" << endl;
            cin.get();
            return -1;
        }
    }

    for (index = 0; index < image_num; index++) {
        imshow("myWin", img[index]);
        waitKey(0);
    }
    cvDestroyWindow("myWin");

    delete [] img; // notice the [] when deleting an array.
    return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
使用名称空间cv;
int main(int argc,字符**argv)
{
如果(argc==1){

cout Quick update:在Gootik回复时,我不熟悉Standad模板库的std::vector,也没有看到他的建议的价值。现在我更熟悉vector,我更喜欢他的建议,尽管我的建议在我看来效果不错。
#include <iostream>
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
using namespace std;
using namespace cv;

int main(int argc, char **argv)
{
    if (argc==1){
        cout << "No images to load!" << endl;
        cin.get();
        return 0;
    }

    int index = 0;
    int image_num = argc-1;

    Mat *img = new Mat[image_num]; // allocates table on heap instead of stack

    // Load the images from command line:
    for (index = 0; index < image_num; index++) {
        img[index] = imread(argv[index+1]);
        if (!img[index].data){
            cout << "Image data not loaded properly" << endl;
            cin.get();
            return -1;
        }
    }

    for (index = 0; index < image_num; index++) {
        imshow("myWin", img[index]);
        waitKey(0);
    }
    cvDestroyWindow("myWin");

    delete [] img; // notice the [] when deleting an array.
    return 0;
}