Introduction
The goal of this program is to create an image viewer that allows users to view images in a window and navigate through them using simple keyboard or mouse controls. The app will load and display images from a directory, and users can easily switch between images.
Objective
Our objective is to implement a C++ image viewer program that:
- Loads and displays images from a specific folder.
- Allows users to navigate between images using keyboard shortcuts.
- Supports basic image viewing functionalities such as zooming and scaling.
Code Implementation
#include
#include
#include
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;
int main() {
// Vector to store image file paths
vector imagePaths;
string imageFolder = "images/"; // Folder containing images
// Get all image file paths in the folder
for (int i = 1; i <= 5; i++) { // assuming there are 5 images for simplicity
imagePaths.push_back(imageFolder + "image" + to_string(i) + ".jpg");
}
int currentImageIndex = 0;
while (true) {
// Load and display the current image
Mat img = imread(imagePaths[currentImageIndex]);
if (img.empty()) {
cerr << "Error: Could not load image" << endl;
return -1;
}
imshow("Image Viewer", img);
char key = waitKey(0); // Wait indefinitely for a key press
// Handle user input to navigate images
if (key == 27) { // ESC key to exit
break;
} else if (key == 81 || key == 82 || key == 83) { // Arrow keys to navigate
if (key == 81) {
// Left Arrow - Show previous image
currentImageIndex = (currentImageIndex - 1 + imagePaths.size()) % imagePaths.size();
} else if (key == 83) {
// Right Arrow - Show next image
currentImageIndex = (currentImageIndex + 1) % imagePaths.size();
}
}
}
return 0;
}
Explanation of the Program
The program utilizes OpenCV, a powerful library for computer vision, to load and display images. Here’s how the program works:
- The program first defines a folder (`”images/”`) that contains the images to be viewed. In this case, we assume there are 5 images named “image1.jpg”, “image2.jpg”, etc.
- The `imread()` function is used to load the current image into a `Mat` object. If the image is not loaded properly, the program will display an error message.
- The `imshow()` function is used to display the current image in a window titled “Image Viewer”.
- The program uses the `waitKey()` function to capture user input. If the ESC key (ASCII code 27) is pressed, the program exits. If the left or right arrow keys (ASCII codes 81 and 83) are pressed, the program will navigate to the previous or next image, respectively.
How to Run the Program
- Install OpenCV: Make sure you have OpenCV installed in your C++ development environment. You can follow the instructions on the official OpenCV website to install it.
- Prepare Images: Place your images in a folder named `images/` and ensure the images are named correctly (e.g., image1.jpg, image2.jpg, etc.).
- Compile the Code: Use a C++ compiler (like g++) to compile the code. For example:
g++ -o image_viewer image_viewer.cpp `pkg-config --cflags --libs opencv4`
- Run the Program: After compiling the program, run it from the terminal:
./image_viewer