Ask Your Question
0

Display multiple images on multiple windows

asked 2017-10-11 05:39:46 -0600

AbdulWahab gravatar image

I have Open CV 2.4.9 with visual studio 12. I would like to display multiple images on multiple windows using for loop. Currently, I am doing this with waitkey() function. This showed me multiple images on a single window after specific time which I mentioned in the waitkey(100) function. I want that every image should be displayed on different windows like if there are four images so four images should be displayed on four windows. How could I do this kindly tell me. I am really thankful

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
2

answered 2017-10-11 08:30:55 -0600

updated 2017-10-11 08:43:27 -0600

A much better way of doing this would be you keeping your frames in a vector then iterating through it and displaying each frame. With this method, for every new frame(s) that you might want to add, you simply add them to this vector without having to change anything else with regards to displaying them.

#include <vector>
#include <opencv2/opencv.hpp>

using namespace cv;

using std::vector;
using std::to_string;

void displayWindows(vector<Mat>& frames)
{
    // iterate through the frames
    for(int index = 0; index < frames.size(); ++index)
    {
        // index allows us to assign a unique name to each window
        // windows will be titled 'Window 0', 'Window 1', 'Window 2'... until frames.size()-1
        String windowTitle = "Window " + to_string(index);

        imshow(windowTitle, frames[index]);
    }
}

int main(int argc, const char * argv[])
{
    // ........... (do your processing)

    // Add them afterwards

    // Method 1
    vector<Mat> images;
    images.push_back(frame1)
    images.push_back(frame2)
    images.push_back(frame3) // and so forth

    // Method 2
    vector<Mat>images{frame1, frame2, frame3} // and so forth

    // call the function
    displayWindows(images);

    // this waits until ESC is pressed.
    if(waitKey() == 27) destroyAllWindows();
}

You can read more on vectors here

edit flag offensive delete link more

Comments

1

Thank you so much for your reply

AbdulWahab gravatar imageAbdulWahab ( 2017-10-12 04:59:55 -0600 )edit

You're welcome

eshirima gravatar imageeshirima ( 2017-10-12 07:51:15 -0600 )edit
2

answered 2017-10-11 06:46:50 -0600

John_OpenCVdev gravatar image

updated 2017-10-11 06:48:53 -0600

Name your imshow window based on the counter in the for loop, such that new window is created for every loop instance.

Example:

stringstream ss;
char wks;
for(int i = 0; i < n; i++)  
{
      Mat InputFrame;
      ss << i;
      imshow(ss.str(), InputFrame);
      ss.str();
      wks = waitkey(1);

      if(wks == 27)
          break;
}
edit flag offensive delete link more

Comments

Thank you so much for your reply

AbdulWahab gravatar imageAbdulWahab ( 2017-10-12 06:23:41 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-10-11 05:39:46 -0600

Seen: 8,227 times

Last updated: Oct 11 '17