Good day, forumites. Since my c++ skills are very rusty, I managed to entangle myself in the following mess:
I am using OpenCV to read and manipulate a set of images, which I need to store in an array to work with them. Here is a snippet of the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
#define MAX_IMAGES 8
typedef Mat* MatPtr;
int main(int argc, char** argv) {
char imageName[] = "./res/imageX.tiff";
MatPtr datacube[MAX_IMAGES];
for(unsigned int i = 0; i < MAX_IMAGES; i++) {
imageName[11] = 49 + i;
*datacube[i] = imread(imageName, -1);
}
return 0;
}
|
As you can see, I have an array of pointers to Mat objects (an OpenCV class used to hold info and data about an image), which I will use to store the images. The function
imread reads an image and returns a Mat object loaded with the relevant data about the image.
However, this gives me a nice segfault when the assignment takes place. Of course, I can swap it with the following code, but since I'm working with big images (2048x2048 and upwards), it's really inefficient:
1 2 3 4
|
for(unsigned int i = 0; i < MAX_IMAGES; i++) {
imageName[11] = 49 + i;
datacube[i] = new Mat(imread(imageName, -1));
}
|
Is there any way to do this elegantly and without much hassle?
Again, excuse my rustiness and anything completely stupid I might have said. It's been a long time since I worked with C++.
-- EDIT --
Managed to circumvent the problem by using a STD vector instead of an array. I'd still like to know the answer to this riddle...