OpenCV Demo :行人检测(HOG+SVM)

HOG算法的基本思想是统计梯度方向,形成梯度方向直方图,然后让SVM训练,分类。是一种针对静态图片的行人检测经典方法

首先看下效果

HOG算法对于直立的行人有很好的检测效果,但是要是来个弯腰驼背那就难说了。

一、数据库下载

本文选择有挑战性的INRIA数据库,下载链接http://pascal.inrialpes.fr/data/human/,也可选择比较理想的MIT数据库

其余数据库参见我转载的一篇博客:行人检测资源汇总 http://blog.csdn.net/soidnhp/article/details/11540327

二、创建图像列表

因为我想运行一次程序能测试多张图片,因为没运行一次只测试一张实在不是人干的活……这样的话就得把一堆图像的路径保存到一个List文件中。如果像我这样懒得选图片的可以直接从INRIA数据库中目录\Test的pos.lst中选取一部分,然后保存成一个.txt文件。

比如我这里新建一个posListINRIA.txt文件,文件内容如下:

E:\MachineVision\Reference\ObjectDtetect\数据库\INRIAPerson\Train/pos/crop001815.png
E:\MachineVision\Reference\ObjectDtetect\数据库\INRIAPerson\Test/pos/crop_000001.png

E:\MachineVision\Reference\ObjectDtetect\数据库\INRIAPerson\Test/pos/crop_000002.png

E:\MachineVision\Reference\ObjectDtetect\数据库\INRIAPerson\Test/pos/crop_000003.png

E:\MachineVision\Reference\ObjectDtetect\数据库\INRIAPerson\Test/pos/crop_000004.png

E:\MachineVision\Reference\ObjectDtetect\数据库\INRIAPerson\Test/pos/crop_000005.png

E:\MachineVision\Reference\ObjectDtetect\数据库\INRIAPerson\Test/pos/crop_000006.png

E:\MachineVision\Reference\ObjectDtetect\数据库\INRIAPerson\Test/pos/crop_000007.png

E:\MachineVision\Reference\ObjectDtetect\数据库\INRIAPerson\Test/pos/crop_000008.png

E:\MachineVision\Reference\ObjectDtetect\数据库\INRIAPerson\Test/pos/crop_000009.png

 三、创建工程

源码如下: 

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"

#include <stdio.h>
#include <string.h>
#include <ctype.h>

using namespace cv;
using namespace std;

// static void help()
// {
//     printf(
//             "\nDemonstrate the use of the HoG descriptor using\n"
//             "  HOGDescriptor::hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());\n"
//             "Usage:\n"
//             "./peopledetect (<image_filename> | <image_list>.txt)\n\n");
// }

int main(int argc, char** argv)
{
    Mat img;
    FILE* f = 0;
    char _filename[1024];
	//char *_filename="posListINRIA.txt";

    //if( argc == 1 )
    //{
    //    printf("Usage: peopledetect (<image_filename> | <image_list>.txt)\n");
    //    return 0;
    //}
    //img = imread(argv[1]);

	//img = imread("negListINRIA.txt");

    //if( img.data )
    //{
    //    strcpy(_filename, argv[1]);
    //}
    //else
    //{
    //    f = fopen(argv[1], "rt");
    //    if(!f)
    //    {
    //        fprintf( stderr, "ERROR: the specified file could not be loaded\n");
    //        return -1;
    //    }
    //}

	 f = fopen("posListINRIA.txt", "rt");

    HOGDescriptor hog;
    hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());
    namedWindow("people detector", 1);

    for(;;)
    {
        char* filename = _filename;
        if(f)
        {
            if(!fgets(filename, (int)sizeof(_filename)-2, f))	//每次读取一行,即一张图片的路径
			//destination,maxnumber,souce,返回读取的字符串,遇到换行或EOF结束
		    //最多能读取maxnumber-1个字符,因为默认会加上‘\0’,要给它预留位置
                break;
            //while(*filename && isspace(*filename))
            //  ++filename;
            if(filename[0] == '#')
                continue;
            int l = (int)strlen(filename);
            while(l > 0 && isspace(filename[l-1]))
                --l;
            filename[l] = '\0';
            img = imread(filename);	//每次循环都会读取一张图片
        }
        printf("%s:\n", filename);
        if(!img.data)
            continue;

        fflush(stdout);//清除文件缓冲区。以写方式打开文件时,将缓冲区内容写入文件
        vector<Rect> found, found_filtered;
        double t = (double)getTickCount();
        // run the detector with default parameters. to get a higher hit-rate
        // (and more false alarms, respectively), decrease the hitThreshold and
        // groupThreshold (set groupThreshold to 0 to turn off the grouping completely).
        hog.detectMultiScale(img, found, 0, Size(8,8), Size(32,32), 1.05, 2);
		/*
		img				– Source image. 
		found_locations – objects boundaries(x,y,width,height).
		hit_threshold	– Threshold for the distance between features and SVM classifying plane.
		win_stride		– Window stride. It must be a multiple of block stride.
		padding			– Mock parameter to keep the CPU interface compatibility. It must be (0,0).  ????
		scale0			– Coefficient of the detection window increase.
		group_threshold	– Coefficient to regulate the similarity threshold. When detected, some
		objects can be covered by many rectangles. 0 means not to perform grouping.  原本能检测一个人,当设为4的时候居然没了
		*/
        t = (double)getTickCount() - t;
        printf("tdetection time = %gms\n", t*1000./cv::getTickFrequency());
        size_t i, j;
        for( i = 0; i < found.size(); i++ )//去掉空间中具有内外包含关系的区域,保留大的
        {
            Rect r = found[i];
            for( j = 0; j < found.size(); j++ )
                if( j != i && (r & found[j]) == r)
                    break;
            if( j == found.size() )
                found_filtered.push_back(r);
        }
        for( i = 0; i < found_filtered.size(); i++ )
        {
            Rect r = found_filtered[i];
            // the HOG detector returns slightly larger rectangles than the real objects.
            // so we slightly shrink the rectangles to get a nicer output.
            r.x += cvRound(r.width*0.1);
            r.width = cvRound(r.width*0.8);
            r.y += cvRound(r.height*0.07);
            r.height = cvRound(r.height*0.8);
            rectangle(img, r.tl(), r.br(), cv::Scalar(0,255,0), 3);// tl:the top-left corner,br: the bottom-right corner
        }
        imshow("people detector", img);
        int c = waitKey(5000) & 255;
        if( c == 'q' || c == 'Q' || !f)
            break;
    }
    if(f)
        fclose(f);
    return 0;
}


记得运行的时候,要把图像列表文件放在工程文件下。

我这里改成5秒钟之后自动处理下一张图片,你也可以将鼠标停留在图像窗口,按下任意键手动激活处理下一张图片

enjoy it!

 

 

 

 

 

很好,下面我来为你介绍一下 OpenCV-Python 实现目标检测的过程。 OpenCV-Python 是一款基于 Python 的计算机视觉,它可以帮助我们完成各种图像处理任务,包括图像的读取、处理、显示等。在使用它进行目标检测时,我们可以使用传统方法 HOG+SVM 实现。 HOG(Histogram of Oriented Gradient)特征是一种常用的图像特征,它可以描述图像中的边缘、角点等信息,其中梯度方向直方图是 HOG 特征的核心部分。在目标检测中,我们需要先提取出图像中的 HOG 特征,然后再使用 SVM(Support Vector Machine)进行分类,从而实现目标检测。 下面是一个简单的 OpenCV-Python 实现目标检测的示例代码: ```python import cv2 # 加载 SVM 分类器 svm = cv2.ml.SVM_load('svm.xml') # 加载测试图片 img = cv2.imread('test.jpg') # 创建 HOG 描述符 hog = cv2.HOGDescriptor() # 设置 SVM 分类器 hog.setSVMDetector(svm) # 检测目标并绘制矩形框 rects, weights = hog.detectMultiScale(img, winStride=(8, 8), padding=(32, 32), scale=1.05) for (x, y, w, h) in rects: cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2) # 显示结果 cv2.imshow('result', img) cv2.waitKey(0) cv2.destroyAllWindows() ``` 在代码中,我们首先加载了训练好的 SVM 分类器,并加载了测试图片。然后创建了 HOG 描述符,并设置 SVM 分类器。最后使用 detectMultiScale 函数检测目标,并绘制矩形框,最终在窗口中显示检测结果。 当然,这仅仅是一个简单的示例,实际的目标检测过程还需要根据具体的应用场景进行调整和优化。
评论 24
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值