In this tutorial, we will create a simple image viewer application using the C++ programming language. This app will allow you to view and navigate through images efficiently.

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

  1. 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.
  2. Prepare Images: Place your images in a folder named `images/` and ensure the images are named correctly (e.g., image1.jpg, image2.jpg, etc.).
  3. 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`
  4. Run the Program: After compiling the program, run it from the terminal:
    ./image_viewer
© 2025 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)