1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
#include <opencv2/opencv.hpp> #include <iostream> #include <vector> #include <filesystem> namespace fs = std::filesystem; void showImage(const cv::Mat& image, const std::string& windowName) { cv::imshow(windowName, image); cv::waitKey(0); // Wait for a key press } std::vector<std::string> loadImagePaths(const std::string& directory) { std::vector<std::string> imagePaths; for (const auto& entry : fs::directory_iterator(directory)) { if (entry.is_regular_file() && (entry.path().extension() == ".jpg" || entry.path().extension() == ".png" || entry.path().extension() == ".bmp")) { imagePaths.push_back(entry.path().string()); } } return imagePaths; } int main() { std::string directory; std::cout << "Enter the directory path containing images: "; std::cin >> directory; std::vector<std::string> imagePaths = loadImagePaths(directory); if (imagePaths.empty()) { std::cout << "No images found in the directory.\n"; return 1; } int index = 0; while (true) { cv::Mat image = cv::imread(imagePaths[index]); if (image.empty()) { std::cerr << "Error loading image: " << imagePaths[index] << std::endl; return 1; } showImage(image, "Photo Viewer"); std::cout << "Press 'n' for next image, 'p' for previous image, or 'q' to quit.\n"; char key = static_cast<char>(cv::waitKey(0)); if (key == 'n') { index = (index + 1) % imagePaths.size(); } else if (key == 'p') { index = (index - 1 + imagePaths.size()) % imagePaths.size(); } else if (key == 'q') { break; } else { std::cout << "Invalid key. Use 'n', 'p', or 'q'.\n"; } } cv::destroyAllWindows(); return 0; } |
Explanation:
- Include OpenCV and Standard Libraries:
#include <opencv2/opencv.hpp>
: Includes the OpenCV core functionalities.#include <iostream>
: For input and output operations.#include <vector>
: For using thestd::vector
container.#include <filesystem>
: For directory and file handling in C++17.
Advertisement - Show Image Function:
showImage(const cv::Mat& image, const std::string& windowName)
: Displays the image in a window namedwindowName
. Thecv::waitKey(0)
function waits for a key press indefinitely.
- Load Image Paths Function:
loadImagePaths(const std::string& directory)
: Scans the specified directory for image files with extensions.jpg
,.png
, or.bmp
. It returns a vector of file paths.
- Main Function:
- Input Directory: Asks the user to input the path of the directory containing images.
- Load Image Paths: Calls
loadImagePaths
to get all image file paths in the directory. - Image Viewing Loop: Displays images in a loop and allows navigation with key presses:
'n'
: Next image.'p'
: Previous image.'q'
: Quit the viewer.
- Key Handling: Uses
cv::waitKey(0)
to capture key presses and navigate through the images. - Window Cleanup:
cv::destroyAllWindows();
closes all OpenCV windows when done.
Prerequisites:
- OpenCV Library: Ensure OpenCV is installed and properly configured with your C++ project.
- C++17 Support: The
std::filesystem
library requires C++17 or later.
Compilation and Execution:
Compile with OpenCV:
1 |
g++ -std=c++17 -o photo_viewer photo_viewer.cpp `pkg-config --cflags --libs opencv4` |
Run the Program:
1 |
./photo_viewer |