Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 936703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:21:26+00:00 2026-05-15T21:21:26+00:00

I am updating some older OpenCV code that was written in (I guess) an

  • 0

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T21:21:26+00:00Added an answer on May 15, 2026 at 9:21 pm

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm updating some code that formats a date to a string using MM/dd/yy. I
I'm updating some older code that used the v2 API for Google Maps. On
I'm updating some old code that has several POD structs that were getting zero'd
I am updating some older code to be integrated with SQL. I work for
I am updating an older project, written in Flex 3 with some new functionality
Today I had to fix some older VB.NET 1.0 code which is using threads.
I'm updating some of our legacy C++ code to use the MFC feature pack
I am updating some code from the old mysql_* functions to PDO. It connects
I want to be able to make an HTTP call updating some select boxes
Imagine the scene, you're updating some legacy Sybase code and come across a cursor.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.