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.
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++.
To install OpenCV, follow these steps:
BUILD_EXAMPLES
and BUILD_opencv_world
options during the CMake configuration.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})
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;
}
OpenCV provides a variety of functions to manipulate images, such as resizing, cropping, and rotating. Let’s take a look at a few examples.
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));
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));
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());
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;
}
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;
}
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.