【问题标题】:HOG detector: relation between detected roi size and training sample sizeHOG 检测器:检测到的 roi 大小与训练样本大小之间的关系
【发布时间】:2014-02-13 01:17:00
【问题描述】:

我正在用 opencv 和 HOGDescriptor c++ 对象试验人员检测器:HOGDescriptor::getDefaultPeopleDetector()。使用 Opencv 2.4.3 存储库的 sample/cpp 目录中的示例程序 peopledetect.cpp 并针对一些 INRIA dataset 图像对其进行测试。它运行良好。

现在我想尝试一些我必须使用的图像,即使我尝试更改参数.. 它也找不到任何东西。

我想这是因为图像中的行人比 INRIA 的要小得多。所以最好先训练一个新的检测器,但在做之前..

这是我的问题:

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

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

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

【问题讨论】:

  • afaik,默认的 hogdescriptors 是 48x96。 detectMultiScale() 将找到较大的实例,但不会找到较小的实例。你可能需要自己训练。
  • @berak:考虑发布答案,因为您的评论实际上告诉了我我想知道的内容

标签: opencv svm object-detection


【解决方案1】:

blackibiza answer 一样,我有两个主要选择:找到一个已经训练好的分类器,或者自己做。

所以,最后,我设法用 svmlight 和 opencv 中包含的 svm 训练了一个 Hog 分类器。

答案是肯定的:检测取决于用于训练的样本大小。如果分类器获得了 64x128 像素的样本并且您尝试检测较小的对象,则它不起作用。但事实恰恰相反:您可以检测到更大的物体(尽管在图像中使用金字塔并进行多尺度检测,也在 opencv 中实现)。

即使 CPU 部分没有记录,你也可以在网上找到某个地方,或者你可以使用最后一个(2.4.8 版)opencv 并查看 gpu 模块,你会看到这些方法:gpu::HOGDescriptor::getPeopleDetector48x96 和 @ 987654323@,那是两个已经训练好的生猪检测器。

作为最后一句话,我对训练时间很担心,但是对于 500 个样本(或多或少),使用普通笔记本电脑的训练过程需要几分钟。

【讨论】:

    【解决方案2】:

    HOG 使用经过训练的数据。为了有效地使用它,您有 3 种可能性:

    1. 使用与训练数据相同/接近的数据类型(例如 INRIA 数据集镜头)使用您的图像(简单的方法)

    2. 构建您自己的训练数据以用于 HOG。 (艰难的路)

    3. 找到一个非常通用的 SVM 集,它可以应用到任何地方(很难找到)

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-18
    • 2016-12-21
    • 1970-01-01
    • 1970-01-01
    • 2015-10-06
    • 1970-01-01
    • 1970-01-01
    • 2013-06-18
    相关资源
    最近更新 更多