-1

I have an array of images as follows:

int width = 5, height = 4, n = 3; // example --> 3 images of size 5x4
int sz[] = {width, height, n};
cv::Mat array(3, sz, CV_8UC1, cv::Scalar::all(0));

Now I would like to display the images. I tried something like this:

char winName[20];
for (int i = 0; i < n; i++)
{
    sprintf(winName, "image %d", i);
    cv::imshow(winName, array.data[i]);
}

which I got from here: http://answers.opencv.org/question/28184/show-many-images-in-different-windows-in-a-loop-using-one-imshow-command/

And I thought about using a for each, in expression, but couldn't get that running. I'm not quite sure on how to manage 3 dimensions of the array. Could you please provide me with some information on how to solve that problem? Many thanks in advance.

Vinay Shukla
  • 1,818
  • 13
  • 41
jkl
  • 93
  • 2
  • 11

1 Answers1

1

Try something like this:

// create a vector of Mats
std::vector<cv::Mat> images(3);

// initialize each image somehow, for example with black images
for(size_t i = 0; i < images.size(); ++i)
  images[i] = cv::Mat::zeros(4, 5, CV_8U);

// show each image
for(size_t i = 0; i < images.size(); ++i) {
  char winName[20];
  sprintf(winName, "image %d", i);
  cv::imshow(winName, images[i]);
}

// wait until a key is pressed before exiting 
cv::waitKey();
ChronoTrigger
  • 8,459
  • 1
  • 36
  • 57
  • I will try that out. I just have to change some other parts of the code then. Thanks. – jkl Apr 21 '15 at 12:32
  • I tried it out and I can visualise the images, but now I have problems with writing something inside the images. My code looks like this: `images[index].at(x,y) = whatever();`. Is that in principle correct? (If yes, then I have to change `x`and `y`) – jkl Apr 21 '15 at 12:45
  • Ok. I solved it. The code was fine, just `x`and `y` where incorrect. It must be `(y, x)`. Thank you very much for your support! :) – jkl Apr 21 '15 at 13:26