2

I am wondering if there is any way to hold (or store) images of different sizes in a single data structure using OpenCV (C++). For example, in MATLAB I can do it by using "cell".

Specifically, I am generating my results which are images of different sizes and it would be grate for me if I can store them in a single data structure. So that, I can use it late on. Please note, this has to be done with C++ and OpenCV.

I am thinking to give a try with: std::vector. Thanks a lot.

2
  • 2
    Give it a try with std::vector!
    – Peter Wood
    Apr 22, 2013 at 7:41
  • +1 lots of functions in OpenCV use std::vector<cv::Mat>
    – Amro
    Apr 22, 2013 at 8:59

2 Answers 2

3
Yeah you can try this

    std::vector<cv::Mat> ImageDataBase;

for(int i=0;i<length_of_imageDataBase;i++)
{
  cv::Mat img = cv::imread("Address of the images");
  ImageDataBase.pushback(img);
}
2

I think the problem lays in the way You think about objects in c++ generally. Matlab requires objects to be of the same size in one vector/array/matrix/however it should be called, because it is optimised to operate on matrices, and those operations are very dependent on dimensions of a matrix.

In c++ the main entity is an object. The most similar thing to matlab vector is an array, like cv::Mat potatoes[30]. Yet, even this demands only to be filled with objects of the same class, disregarding the size of those cv::Mat contents.

So, to wrap it all up, You have a couple of choices:

  • an array, like cv::Mat crazySocks[42] - You need to be carefull here, because You need to know how many socks there will be, and You might a segmentation error if You go out of array bounds
  • a vector, as suggested by Vinoj John Hosan, like std::vector<cv::Mat> jaguars - this is a fine idea, because stl containers can do some nice tricks with their content, and You may easily modify size of the vector.
  • a list, like std::list<cv::Mat> toFind - this is better than vector if You plan to modify the size of Your container often.
  • any of previously mentioned, but with pointers, like cv::Mat *crazyPointers[33] - when You have some big objects to move, it's better to move only informations about where they are, than the object.cv::Mat does some tricks internally with it's data, so it shouldn't be the case.
2
  • Thanks a lot all of you. I have already tried with std::vector<cv::Mat> and it works perfectly good for my current situation. @morynicz thanks again for the kind suggestions. However, I can not take fixed size array beforehand. But, I like a list idea (std::list<cv::Mat> also. I am going to a give try to it.
    – Sanchit
    Apr 23, 2013 at 6:52
  • @morynicz: just to be fair, the MATLAB equivalent of general containers are cell arrays
    – Amro
    Apr 23, 2013 at 9:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.