OpenCV人形检测Hog

本文展示了如何使用OpenCV库检测图像中的人形,并通过算法去除嵌套的矩形框以实现去重。包括检测、去重过程及最终展示结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include "iostream"
#include "queue"
using namespace std;
#include "opencv2/opencv.hpp"
#include "Windows.h"
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/objdetect/objdetect.hpp"
using namespace cv;

int main()
{
    try{

        IplImage *pFrame = NULL;
        CvCapture *pCapture = NULL;
        //pCapture = cvCreateCameraCapture(-1);
        //pCapture = cvCaptureFromCAM(0);
        pCapture = cvCaptureFromFile("C:\\C_C++ code\\Photo and video\\TextVideo2.flv");
        //pCapture = cvCaptureFromFile("C:\\C_C++ code\\Photo and video\\TextVideo1.flv");
        if (!pCapture)
        {
            cout << "File opened fail..." << endl;
            return -1;
        }

        Mat img;
        HOGDescriptor hog;
        Rect r;
        int nNum = 0;
        hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());
        vector<Rect> found,found1;
        int i, j;
        char str[100];
        while (pFrame = cvQueryFrame(pCapture))
        {
            nNum++;
            Mat img = cvarrToMat(pFrame, 0); //IplImage turn into Mat
            
            if (nNum >= 3)
            {
                //进行检测
                hog.detectMultiScale(img, found);

                found1.clear();
                //-------------------去除嵌套的矩形框------------------------
                for (i = 0; i < found.size(); i++)
                {
                    r = found[i];
                    for (j = 0; j < found.size(); j++)
                    {
                        if ( i != j && ((r&found[j]) == r) )
                        {
                            break;
                        }
                    }
                    if (j == found.size())
                    {
                        found1.push_back(r);
                    }
                }
                //画长方形 框出行人

                for (i = 0; i < found1.size(); i++)
                {
                    r = found1[i];
                    rectangle(img, r, Scalar(0, 255, 0), 1);
                }
                nNum = 0;
            }
            
            for (int i = 0; i < found1.size(); i++)
            {
                r = found1[i];
                rectangle(img, r, Scalar(0, 255, 0), 1);
            }
            sprintf(str, "The track count is: %d", found1.size());
            
            putText(img, str, cvPoint(30, 30), CV_FONT_HERSHEY_PLAIN, 0.8,CV_RGB(0, 0, 250),1,8);
        
            imshow("Track People", img);
            if (cvWaitKey(35) >= 0)
                break;
        }
    }
    catch (exception &e)
    {
        cout << e.what() << endl;
    }

    return 1;
}

效果:

图片人形测试:

#include "iostream"
#include "queue"
using namespace std;
#include "opencv2/opencv.hpp"
#include "Windows.h"
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/objdetect/objdetect.hpp"
int main(int argc, char** argv){
    Mat img;
    vector<Rect> found;

    img = imread("C:\\C_C++ code\\Photo and video\\text006.jpg");

    HOGDescriptor defaultHog;
    defaultHog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());

    //进行检测
    defaultHog.detectMultiScale(img, found);

    //画长方形,框出行人
    for (int i = 0; i < found.size(); i++){
        Rect r = found[i];
        rectangle(img, r, Scalar(0, 255, 0), 1);
    }
    namedWindow("检测行人", CV_WINDOW_AUTOSIZE);
    imshow("检测行人", img);

    waitKey(0);

    return 0;
}

边框嵌套去重:

int main(int argc, char** argv){
    Mat img;
    vector<Rect> found, foundRect;

    img = imread("C:\\C_C++ code\\Photo and video\\text007.jpg");

    HOGDescriptor defaultHog;
    defaultHog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());

    //进行检测
    defaultHog.detectMultiScale(img, found);

    //遍历found寻找没有被嵌套的长方形
    for (int i = 0; i < found.size(); i++){
        Rect r = found[i];

        int j = 0;
        for (; j < found.size(); j++){
            //如果时嵌套的就推出循环
            if (j != i && (r & found[j]) == r)
                break;
        }
        if (j == found.size()){
            foundRect.push_back(r);
        }
    }

    //画长方形,圈出行人
    for (int i = 0; i < foundRect.size(); i++){
        Rect r = foundRect[i];
        rectangle(img, r.tl(), r.br(), Scalar(0, 0, 255), 3);
    }

    namedWindow("检测行人", CV_WINDOW_AUTOSIZE);
    imshow("检测行人", img);

    waitKey(0);
    return 0;
}
int main()
{
    Mat image = imread("C:\\C_C++ code\\Photo and video\\text007jpg");
    imshow("hog", image);
    if (image.empty())
    {
        cout << "read image failed" << endl;
    }
    // 1. 定义HOG对象    
    HOGDescriptor hog(Size(48,96), Size(16, 16), Size(8, 8), Size(8, 8), 9);


    // 2. 设置SVM分类器    
    hog.setSVMDetector(HOGDescriptor::getDaimlerPeopleDetector());   // 采用已经训练好的行人检测分类器    

    // 3. 在测试图像上检测行人区域    
    std::vector<cv::Rect> regions;
    hog.detectMultiScale(image, regions, 0, cv::Size(8, 8), cv::Size(32, 32), 1.05, 1);

    // 显示    
    for (size_t i = 0; i < regions.size(); i++)
    {
        cv::rectangle(image, regions[i], cv::Scalar(0, 0, 255), 2);
    }

    cv::imshow("hog", image);
    cv::waitKey(0);

    return 0;
}

 

转载于:https://www.cnblogs.com/mypsq/p/4992547.html

### 使用 OpenCV 中的 HOG 和 SVM 进行行人目标检测 #### 方法概述 为了利用HOG和SVM进行行人检测,可以采用预训练好的模型来简化开发过程。OpenCV提供了内置的支持工具,特别是`cv::HOGDescriptor`类,它不仅封装了方向梯度直方图(Histogram of Oriented Gradients, HOG)特征提取的功能,还集成了支持向量机(Support Vector Machine, SVM)用于分类决策。 对于行人检测任务而言,有两种预先定义好的检测器可供选择:一种是由Daimler公司提供的行人模板`getDaimlerPeopleDetector()`;另一种则是默认的人体轮廓模板`getDefaultPeopleDetector()`[^3]。 当选择了合适的检测器之后,通过调用`setSVMDetector(InputArray svmdetector)`方法设置好SVM参数,就可以准备执行实际的目标识别工作了[^2]。 下面是一份简单的Python代码示例,展示了如何使用上述提到的技术组合来进行基本的行人检测: ```python import cv2 # 初始化HOG描述符对象并加载默认的人形探测器 hog = cv2.HOGDescriptor() hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector()) # 加载测试图片 image_path = 'path_to_your_image.jpg' img = cv2.imread(image_path) # 执行多尺度滑动窗口检测 found_locations, _ = hog.detectMultiScale(img) for (x, y, w, h) in found_locations: # 绘制矩形框标记出被发现的对象位置 cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2) # 显示结果图像 cv2.imshow('Detected People', img) cv2.waitKey(0) cv2.destroyAllWindows() ``` 这段程序首先创建了一个新的HOG描述符实例,并设置了其内部使用的SVM权重矩阵为默认配置下的行人模式识别数据[^4]。接着读取了一张待分析的照片文件作为输入源,最后运用`detectMultiScale()`函数完成整个场景内的行人查找操作,并将结果显示出来。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值