Ask Your Question

Revision history [back]

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

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> 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