tags:

views:

56

answers:

2

Hi All,

I am updating some older OpenCV code that was written in (I guess) an OpenCV 1.1 manner (i.e. using IplImages).

What I want to accomplish right now is to simply load a series of images (passed as command line arguments) as Mats. This is part of a larger task. The first code sample below is the old code's image loading method. It loads 5 images from the command line and displays them in sequence, pausing for a key hit after each, then exits.

The second code sample is my updated version using Mat. It works fine so far, but is this the best way to do this? I've used an array of Mats. Should I use an array of pointers to Mats instead? And is there a way to do this such that the number of images is determined at run time from argc and does not need to be set ahead of time with IMAGE_NUM.

Basically, I'd like to be able to pass any number (within reason) of images as command line arguments, and have them loaded into some convenient array or other similar storage for later reference.

Thanks.

Old code:

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

New code:

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

// the number of input images
#define IMAGE_NUM 5

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

    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");
    return 0;
}
A: 

you can use a vector instead of an array:

for example

#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;
}
Gootik
A: 

Hi again,

It took me a while to get back around to this, but what I've ended up doing is as follows, which is probably functionally the same as Gootik's suggestion. This has worked well for me. Notice that for functions that take Mat& (i.e. a single cv::Mat), you can just de-ref the array of Mats and pass that, which is a notation I'm more comfortable with after doing a lot of image processing work in 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;
}
SSilk