displaying m images one window opencv c++
#include <opencv2/opencv.hpp>
int main() {
std::vector<cv::Mat> images; // Create a vector to hold multiple images
// Load multiple images into the vector
images.push_back(cv::imread("image1.jpg"));
images.push_back(cv::imread("image2.jpg"));
// Create a window to display the images
cv::namedWindow("Display", cv::WINDOW_NORMAL);
// Loop through the vector of images
for (const auto& image : images) {
// Display each image in the window
cv::imshow("Display", image);
// Wait for a key press or delay
cv::waitKey(0);
}
// Destroy the window
cv::destroyWindow("Display");
return 0;
}
Explanation:
- #include <opencv2/opencv.hpp>
: Includes the OpenCV library.
- std::vector<cv::Mat> images;
: Creates a vector to store multiple images using OpenCV's Mat
data structure.
- images.push_back(cv::imread("image1.jpg"));
and images.push_back(cv::imread("image2.jpg"));
: Reads two images ("image1.jpg" and "image2.jpg") and adds them to the images
vector.
- cv::namedWindow("Display", cv::WINDOW_NORMAL);
: Creates a window named "Display" to show the images. cv::WINDOW_NORMAL
specifies the window to be resizable.
- for (const auto& image : images) { ... }
: Loops through each image in the images
vector.
- cv::imshow("Display", image);
: Displays each image in the "Display" window.
- cv::waitKey(0);
: Waits for a key press infinitely (0
as argument means waiting indefinitely).
- cv::destroyWindow("Display");
: Closes the window named "Display" after displaying all the images.
- return 0;
: Ends the program execution.