SoFunction
Updated on 2025-04-14

Three methods of object recognition and detection using OpenCV in C++

1. Review of OpenCV environment configuration

Before object recognition and detection, you need to ensure that the OpenCV library is installed and configured. If you have not configured OpenCV, you can refer to the following steps:

  1. Install OpenCV: You can download the installation package through the official website, or usevcpkgorconanSuch as package management tools to install OpenCV.

  2. Configure IDE: Make sure that the IDE (such as Visual Studio or CLion) correctly sets the path to OpenCV, including the header file path and the library file path.

2. Haar Feature Classifier

Haar feature classifier is a machine learning-based object detection method, which was first used for face detection. OpenCV provides a pre-trained Haar classifier model that can quickly detect objects such as faces, eyes, pedestrians, etc.

Face detection

OpenCV provides multiple pre-trained classifiers, such ashaarcascade_frontalface_default.xmlUsed for face detection. We can directly load this classifier for face detection.

#include <opencv2/>
using namespace cv;
 
int main() {
    // Read the input image    Mat image = imread("");
 
    if (()) {
        std::cout << "Error loading image!" << std::endl;
        return -1;
    }
 
    // Convert the image to grayscale    Mat grayImage;
    cvtColor(image, grayImage, COLOR_BGR2GRAY);
 
    // Load the pre-trained face classifier    CascadeClassifier faceCascade;
    if (!("haarcascade_frontalface_default.xml")) {
        std::cout << "Error loading cascade file!" << std::endl;
        return -1;
    }
 
    // Detect faces    std::vector<Rect> faces;
    (grayImage, faces);
 
    // Draw a rectangular box to mark the face    for (size_t i = 0; i < (); i++) {
        rectangle(image, faces[i], Scalar(0, 255, 0), 2);
    }
 
    // Show detection results    imshow("Detected Faces", image);
    waitKey(0);
 
    return 0;
}
  • CascadeClassifier::load()Methods are used to load pretrained Haar classifier files.

  • detectMultiScale()The method is used to detect faces in the image, and the return value is aRectVector, containing the detected face rectangle area.

  • rectangle()Method is used to draw rectangular boxes on images.

Other Classifiers

OpenCV provides a variety of Haar classifiers for detecting objects such as eyes, upper body, lower body, etc. For example:

CascadeClassifier eyeCascade;
("haarcascade_eye.xml");
(grayImage, eyes);

3. HOG Features and Pedestrian Detection

HOG (Histogram of Oriented Gradients) is another technology commonly used in object detection, especially suitable for pedestrian detection. AHOGDescriptorClass, used to extract HOG features and perform pedestrian detection in combination with support vector machine (SVM).

Pedestrian testing

#include <opencv2/>
#include <opencv2/>
using namespace cv;
 
int main() {
    // Read the input image    Mat image = imread("");
 
    if (()) {
        std::cout << "Error loading image!" << std::endl;
        return -1;
    }
 
    // Create HOG descriptor    HOGDescriptor hog;
    (HOGDescriptor::getDefaultPeopleDetector());
 
    // Detect pedestrians in the image    std::vector<Rect> foundLocations;
    (image, foundLocations);
 
    // Draw detected pedestrian location in the image    for (size_t i = 0; i < (); i++) {
        rectangle(image, foundLocations[i], Scalar(0, 255, 0), 2);
    }
 
    // Show detection results    imshow("Detected People", image);
    waitKey(0);
 
    return 0;
}
  • HOGDescriptorIt is an OpenCV class used to extract HOG features.detectMultiScale()Methods are used to conduct pedestrian detection.

  • getDefaultPeopleDetector()It is a pre-trained pedestrian detector.

4. Object detection based on deep learning

In addition to traditional machine learning methods, deep learning-based object detection has become the mainstream. OpenCV supports object detection through deep learning frameworks (such as Caffe, TensorFlow, ONNX) and using pre-trained deep learning models.

Object detection using pretrained models

byYOLO(You Only Look Once) As an example, YOLO is a very popular real-time object detection model, which can detect multiple objects in an image at the same time and return categories and positions. OpenCV supports loading and using YOLO models for object detection.

  1. Download YOLO model

    • Download YOLOv3 weight file () and configuration files ()。

    • Download COCO category files ()。

  2. Object detection using YOLO model

#include <opencv2/>
#include <opencv2/>
using namespace cv;
using namespace cv::dnn;
 
int main() {
    // Load the YOLO model    Net net = readNet("", "");
    std::vector<String> outNames = ();
 
    // Load the category name    std::ifstream ifs("");
    std::vector<std::string> classes;
    std::string line;
    while (getline(ifs, line)) {
        classes.push_back(line);
    }
 
    // Read the input image    Mat image = imread("");
 
    // Convert the image to YOLO input format    Mat blob = blobFromImage(image, 1 / 255.0, Size(416, 416), Scalar(), true, false);
 
    // Set network input    (blob);
 
    // Get YOLO output    std::vector<Mat> outs;
    (outs, outNames);
 
    // Process output    for (size_t i = 0; i < (); i++) {
        Mat detectionMat = outs[i];
        for (int j = 0; j < ; j++) {
            // Extract detection box information (confidence, class label, bounding box)            float confidence = <float>(j, 4);
            if (confidence > 0.5) {  // Only test results with confidence greater than 0.5 are displayed                int classId = -1;
                float maxClassConf = -1;
                for (int k = 5; k < ; k++) {
                    if (<float>(j, k) > maxClassConf) {
                        maxClassConf = <float>(j, k);
                        classId = k - 5;
                    }
                }
 
                // Calculate bounding box                int x = int(<float>(j, 0) * );
                int y = int(<float>(j, 1) * );
                int width = int(<float>(j, 2) * );
                int height = int(<float>(j, 3) * );
 
                // Draw bounding box                rectangle(image, Rect(x, y, width, height), Scalar(0, 255, 0), 2);
                putText(image, classes[classId], Point(x, y - 5), FONT_HERSHEY_SIMPLEX, 1, Scalar(0, 255, 0), 2);
            }
        }
    }
 
    // Show detection results    imshow("YOLO Object Detection", image);
    waitKey(0);
 
    return 0;
}
  • readNet()Method loads the weights and configuration files of the YOLO model.

  • blobFromImage()Method converts the input image into an input format suitable for the YOLO model.

  • forward()The method performs forward reasoning and obtains detection results.

  • Based on the bounding box, confidence and category information output by YOLO, we can draw the bounding box on the image and display the object category.

5. Summary

In this article, we describe how to use C++ and OpenCV for object recognition and detection. We cover the following common methods:

  • Haar Feature Classifier: used for detection of objects such as face and eyes, suitable for real-time detection tasks.

  • HOG Features and Pedestrian Detection: Use HOG features and SVM for pedestrian detection.

  • Object detection based on deep learning: Use deep learning models such as YOLO for object detection, which can identify multiple objects and return to their categories and locations.

These methods are suitable for different application scenarios, and appropriate algorithms and models can be selected according to specific needs. In future development, object detection technology will continue to play an important role and help the further development of artificial intelligence.

The above is the detailed content of three methods of C++ using OpenCV for object recognition and detection. For more information about C++ OpenCV object recognition and detection, please pay attention to my other related articles!