David's Blog

Using OpenCV in C++ A Comprehensive Guide

By David Li on 2023-05-03T14:16:44.846Z

Using OpenCV in C++: A Comprehensive Guide

OpenCV (Open Source Computer Vision) is an open-source computer vision and machine learning software library. It provides a collection of powerful tools and functions for image and video processing, feature extraction, and object detection. In this article, we will discuss how to use OpenCV in C++ to perform various computer vision tasks.

Table of Contents

  1. Setting Up OpenCV in C++
  2. Reading and Displaying Images
  3. Manipulating Images
  4. Working with Videos
  5. Object Detection
  6. Conclusion

1. Setting Up OpenCV in C++

Before we dive into using OpenCV with C++, we first need to set up the development environment. This includes installing OpenCV and configuring it for use with C++.

1.1 Installing OpenCV

To install OpenCV, follow these steps:

  1. Download the OpenCV source code from the official repository or the releases page.
  2. Extract the downloaded archive.
  3. Follow the official guide for building OpenCV from the source. Make sure to enable the BUILD_EXAMPLES and BUILD_opencv_world options during the CMake configuration.

1.2 Configuring OpenCV with C++

To configure OpenCV with C++, we need to link the OpenCV library to our C++ project. The process varies depending on the IDE or build system you are using. For instance, if you are using CMake, you can include the following lines in your CMakeLists.txt file:

find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
target_link_libraries(your_target_name ${OpenCV_LIBS})

2. Reading and Displaying Images

Now that we have OpenCV set up, let’s start by loading and displaying an image using the library. To do this, we need to include the necessary OpenCV headers and use the cv::imread() and cv::imshow() functions.

#include <iostream>
#include <opencv2/opencv.hpp>

int main() {
    // Read the image
    cv::Mat img = cv::imread("path/to/your/image.jpg", cv::IMREAD_COLOR);

    // Check if the image is loaded successfully
    if (img.empty()) {
        std::cerr << "Error: Could not load the image.\n";
        return 1;
    }

    // Display the image
    cv::imshow("My Image", img);
    
    // Wait for a key press and close the window
    cv::waitKey(0);
    cv::destroyAllWindows();

    return 0;
}

3. Manipulating Images

OpenCV provides a variety of functions to manipulate images, such as resizing, cropping, and rotating. Let’s take a look at a few examples.

3.1 Resizing Images

To resize an image, use the cv::resize() function.

cv::Mat resized;
int new_width = 300;
int new_height = 200;
cv::resize(img, resized, cv::Size(new_width, new_height));

3.2 Cropping Images

To crop an image, simply create a new cv::Mat object referencing the region of interest (ROI).

int x = 100, y = 50, width = 200, height = 150;
cv::Mat cropped = img(cv::Rect(x, y, width, height));

3.3 Rotating Images

To rotate an image, use the cv::getRotationMatrix2D() and cv::warpAffine() functions.

double angle = 45.0;
cv::Point2f center(img.cols / 2.0, img.rows / 2.0);
cv::Mat rotation_matrix = cv::getRotationMatrix2D(center, angle, 1.0);
cv::Mat rotated;
cv::warpAffine(img, rotated, rotation_matrix, img.size());

4. Working with Videos

OpenCV also provides tools for working with videos. To read and display a video, we use the cv::VideoCapture class and loop through the frames.

#include <iostream>
#include <opencv2/opencv.hpp>

int main() {
    // Open the video file
    cv::VideoCapture video("path/to/your/video.mp4");

    // Check if the video is opened successfully
    if (!video.isOpened()) {
        std::cerr << "Error: Could not open the video.\n";
        return 1;
    }

    // Loop through the frames
    cv::Mat frame;
    while (video.read(frame)) {
        // Display the frame
        cv::imshow("My Video", frame);

        // Exit the loop if 'q' is pressed
        if (cv::waitKey(25) == 'q') {
            break;
        }
    }

    // Release the video and close the window
    video.release();
    cv::destroyAllWindows();

    return 0;
}

5. Object Detection

One of the most common applications of computer vision is object detection. OpenCV provides pre-trained models and tools to perform object detection using deep learning algorithms like YOLO, SSD, and Faster R-CNN. In this example, we’ll use the MobileNet-SSD model.

#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/dnn/dnn.hpp>

int main() {
    // Load the pre-trained model
    cv::dnn::Net net = cv::dnn::readNetFromCaffe("path/to/MobileNetSSD_deploy.prototxt",
                                                 "path/to/MobileNetSSD_deploy.caffemodel");

    // Load the image
    cv::Mat img = cv::imread("path/to/your/image.jpg", cv::IMREAD_COLOR);

    // Prepare the input for the neural network
    cv::Mat blob = cv::dnn::blobFromImage(img, 0.007843, cv::Size(300, 300), cv::Scalar(127.5, 127.5, 127.5), false, false);
    net.setInput(blob);

    // Perform the forward pass (object detection)
    cv::Mat detections = net.forward();

    // Process the detections
    int num_detections = detections.size[2];
    float confidence_threshold = 0.5;
    for (int i = 0; i < num_detections; ++i) {
        float confidence = detections.at<float>(0, 0, i, 2);

        if (confidence > confidence_threshold) {
            int object_class = static_cast<int>(detections.at<float>(0, 0, i, 1));

            // Get the bounding box coordinates
            int x1 = static_cast<int>(detections.at<float>(0, 0, i, 3) * img.cols);
            int y1 = static_cast<int>(detections.at<float>(0, 0, i, 4) * img.rows);
            int x2 = static_cast<int>(detections.at<float>(0, 0, i, 5) * img.cols);
            int y2 = static_cast<int>(detections.at<float>(0, 0, i, 6) * img.rows);

            // Draw the bounding box on the image
            cv::rectangle(img, cv::Point(x1, y1), cv::Point(x2, y2), cv::Scalar(0, 255, 0), 2);
        }
    }

    // Display the image with the bounding boxes
    cv::imshow("Object Detection", img);
    cv::waitKey(0);
    cv::destroyAllWindows();

    return 0;
}

6. Conclusion

In this article, we covered the basics of using OpenCV in C++ for various computer vision tasks, such as reading and displaying images, manipulating images, working with videos, and performing object detection. OpenCV provides a wealth of functionality for computer vision applications, and this guide only scratches the surface. We encourage you to explore the official OpenCV documentation to discover more capabilities and tailor them to your specific needs.

© Copyright 2024 by FriendlyUsers Tech Blog. Built with ♥ by FriendlyUser. Last updated on 2024-12-19.