HOG检测器:检测到的roi大小与训练样本大小之间的关系 [英] HOG detector: relation between detected roi size and training sample size

查看:129
本文介绍了HOG检测器:检测到的roi大小与训练样本大小之间的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用opencv和HOGDescriptor c ++对象试验人检测器: HOGDescriptor :: getDefaultPeopleDetector().使用Opencv 2.4.3存储库的sample/cpp目录中的示例程序peopledetect.cpp并针对某些

I'm experimenting people detector with opencv and HOGDescriptor c++ object: HOGDescriptor::getDefaultPeopleDetector(). Using the sample program peopledetect.cpp in the sample/cpp directory of the Opencv 2.4.3 repository and testing it against some of the INRIA dataset images.. it works quite well.

现在,即使我尝试更改参数,我也想尝试一些我必须使用的图像..

Now I want to try with some images I have to work with and, even if I try to change parameters.. it doesn't find anything.

我想是因为图像中的行人比INRIA小得多.因此,最好在训练新探测器之前进行训练.

I suppose it is because of the pedestrian in the image I have are much more smaller then the INRIA ones. So it should be better to train a new detector but before doing it..

这是我的问题:

对吗?用于训练的图像与检测到的图像之间是否存在严格的关系?这意味着HOG检测器并不是真正的尺度不变方法. 特别是默认HOGDescriptor::getDefaultPeopleDetector()的最佳大小是多少?我必须训练一个新的探测器来探测小得多的人吗?

Is it right? Is there a strict relationship between the images used for training and the detected ones? That means that HOG detector is not really scale invariant method.. In particular, what is the best size of the default HOGDescriptor::getDefaultPeopleDetector() ? Do I have to train a new detector for detect much smaller people?

这是我正在使用的peopledetect.cpp:

Here is the peopledetect.cpp I'm using:

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

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

#include <iostream>

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)
{

    std::cout << "OPENCV version: " << CV_MAJOR_VERSION << " " << CV_MINOR_VERSION << std::endl; 

    Mat img;
    FILE* f = 0;
    char _filename[1024];

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

    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;
        }
    }

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

    for(;;)
    {
        char* filename = _filename;
        if(f)
        {
            if(!fgets(filename, (int)sizeof(_filename)-2, f))
                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);
        t = (double)getTickCount() - t;
        printf("tdetection time = %gms\n", t*1000./cv::getTickFrequency());

        std::cout << "found: " << found.size() << std::endl;

        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);
        }
        imshow("people detector", img);
        int c = waitKey(0) & 255;
        if( c == 'q' || c == 'Q' || !f)
            break;
    }
    if(f)
        fclose(f);
    return 0;
}

推荐答案

blackibiza答案所示,我有2个主要选择:找到一个已经受过训练的分类器,或者为我自己做.

As in blackibiza answer, I had 2 main choices: find an already trained classifier, or do it for my self.

因此,最后,我设法通过svmlight和opencv中包含的svm来训练Hog分类器.

So, in the end, I managed to train a Hog classifier both with svmlight and with svm included in opencv.

答案是肯定的:检测取决于训练所用的样本量.如果分类器具有64x128像素的样本,而您正尝试检测较小的对象,则该分类器将不起作用.但事实恰恰相反:您可以检测到更大的物体(尽管在图像上向下金字塔并进行多尺度检测,这也是在opencv中实现的.)

The answer is yes: the detection depends on the sample size used for the training. If the classifier got samples of 64x128 pixel and you are trying to detect smaller object, it doesn't work. But the opposite is true: you can detect bigger object (though pyramid down the image and do a multi-scale-detection, also implemented in opencv).

即使未在CPU部件中进行记录,您也可以在网上找到某个地方,也可以使用最后一个(版本2.4.8)opencv并查看gpu模块,然后您会看到以下方法:gpu::HOGDescriptor::getPeopleDetector48x96gpu::HOGDescriptor::getPeopleDetector64x128,这是两个已经训练好的生猪探测器.

Even if not documented in the CPU part you can find somewhere in the net, or you can youse the last (version 2.4.8) opencv and look at gpu module and you'll see those methods: gpu::HOGDescriptor::getPeopleDetector48x96 and gpu::HOGDescriptor::getPeopleDetector64x128, that are the two already trained hog detector.

最后一句话,我对培训时间有所警惕,但是使用一台普通笔记本电脑进行500次采样(或多或少)后,培训过程将花费几分钟.

As the last remark, I was warred about training time, but with 500 samples (more or less) the training process takes few minutes with a normal laptop.

这篇关于HOG检测器:检测到的roi大小与训练样本大小之间的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆