Ask Your Question

Display multiple images on multiple windows

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

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

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

eshirima gravatar image

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

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(frame3) // and so forth

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

    // call the function

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

You can read more on vectors here

edit flag offensive delete link more



Thank you so much for your reply

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

You're welcome

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

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

John_OpenCVdev gravatar image

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

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


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

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


Thank you so much for your reply

AbdulWahab gravatar imageAbdulWahab ( 2017-10-12 06:23:41 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


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

Seen: 52 times

Last updated: Oct 11