Opencv extends latent SVM discriminatively trained Part Based Models for Object Detection

Source: Internet
Author: User

Opencv 2.4 implements the DPM program in C ++. The main difference between it and the previous C version is that it can detect multiple targets at the same time. During use, you can put the trained model in a folder, and put the image to be detected in another folder for detection.

Unfortunately, the accelerated content is not considered.

 

Latent SVM regression ¶

Discriminatively trained Part Based Models for object detection Interval ¶

The object detector described below has been initially proposed by p.f. felzenszwalb in [felzenszwalb2010]. It is based
On a Dalal-triggs detector that uses a single filter on histogram of Oriented gradients (hog) Features to represent an object category. this detector uses a Sliding Window approach, where a filter is applied at all positions and scales of an image. the first
Innovation is enriching the Dalal-triggs model using a star-structured part-based model defined by a "root" filter (analogous to the Dalal-triggs filter) plus a set of parts filters and associated deformation models. the score of one of star models at a participant
Position and scale within an image is the score of the root filter at the given location plus the sum over parts of the maximum, over placements of that part, of the part filter score on its location minus a deformation cost easuring the deviation of the part
From its ideal location relative to the root. both root and part filter scores are defined by the dot product between a filter (a set of weights) and a subwindow of a feature pyramid computed from the input image. another improvement is a representation
The class of models by a mixture of star models. the score of a mixture model at a participating position and scale is the maximum over components, of the score of that component model at the given location.

In opencv there are c Implementation of latent SVM and C ++ wrapper of it. C version is the structureCvobjectdetection
And a set of functions working with this structure (seeCvloadlatentsvmdetector (),Cvreleaselatentsvmdetector (),Cvlatentsvmdetectobjects ()).
C ++ version is the classLatentsvmdetector
And has slightly different functionality in contrast with C version-it supports loading and detection of several models.

There are two examples of latent SVM usage:
Samples/C/latentsvmdetect. cppAndSamples/CPP/latentsvm_multidetect.cpp.

 

The Code is as follows:

#include <iostream>#include "opencv2/objdetect/objdetect.hpp"#include "opencv2/highgui/highgui.hpp"#include "opencv2/contrib/contrib.hpp"#ifdef WIN32#include <io.h>#else#include <dirent.h>#endif#ifdef HAVE_CVCONFIG_H#include <cvconfig.h>#endif#ifdef HAVE_TBB#include "tbb/task_scheduler_init.h"#endifusing namespace std;using namespace cv;static void help(){    cout << "This program demonstrated the use of the latentSVM detector." << endl <<            "It reads in a trained object models and then uses them to detect the objects in an images." << endl <<             endl <<            "Call:" << endl <<            "./latentsvm_multidetect <imagesFolder> <modelsFolder> [<overlapThreshold>][<threadsNumber>]" << endl <<            "<overlapThreshold> - threshold for the non-maximum suppression algorithm." << endl <<            "Example of <modelsFolder> is opencv_extra/testdata/cv/latentsvmdetector/models_VOC2007" << endl <<             endl <<            "Keys:" << endl <<            "'n' - to go to the next image;" << endl <<            "'esc' - to quit." << endl <<            endl;}static void detectAndDrawObjects( Mat& image, LatentSvmDetector& detector, const vector<Scalar>& colors, float overlapThreshold, int numThreads ){    vector<LatentSvmDetector::ObjectDetection> detections;    TickMeter tm;    tm.start();    detector.detect( image, detections, overlapThreshold, numThreads);    tm.stop();    cout << "Detection time = " << tm.getTimeSec() << " sec" << endl;    const vector<string> classNames = detector.getClassNames();    CV_Assert( colors.size() == classNames.size() );    for( size_t i = 0; i < detections.size(); i++ )    {        const LatentSvmDetector::ObjectDetection& od = detections[i];        rectangle( image, od.rect, colors[od.classID], 3 );    }    // put text over the all rectangles    for( size_t i = 0; i < detections.size(); i++ )    {        const LatentSvmDetector::ObjectDetection& od = detections[i];        putText( image, classNames[od.classID], Point(od.rect.x+4,od.rect.y+13), FONT_HERSHEY_SIMPLEX, 0.55, colors[od.classID], 2 );    }}static void readDirectory( const string& directoryName, vector<string>& filenames, bool addDirectoryName=true ){    filenames.clear();#ifdef WIN32    struct _finddata_t s_file;    string str = directoryName + "\\*.*";    intptr_t h_file = _findfirst( str.c_str(), &s_file );    if( h_file != static_cast<intptr_t>(-1.0) )    {        do        {            if( addDirectoryName )                filenames.push_back(directoryName + "\\" + s_file.name);            else                filenames.push_back((string)s_file.name);        }        while( _findnext( h_file, &s_file ) == 0 );    }    _findclose( h_file );#else    DIR* dir = opendir( directoryName.c_str() );    if( dir != NULL )    {        struct dirent* dent;        while( (dent = readdir(dir)) != NULL )        {            if( addDirectoryName )                filenames.push_back( directoryName + "/" + string(dent->d_name) );            else                filenames.push_back( string(dent->d_name) );        }    }#endif    sort( filenames.begin(), filenames.end() );}int main(int argc, char* argv[]){    help();    string images_folder, models_folder;    float overlapThreshold = 0.2f;    int numThreads = -1;    if( argc > 2 )    {        images_folder = argv[1];        models_folder = argv[2];        if( argc > 3 ) overlapThreshold = (float)atof(argv[3]);        if( overlapThreshold < 0 || overlapThreshold > 1)        {            cout << "overlapThreshold must be in interval (0,1)." << endl;            exit(-1);        }        if( argc > 4 ) numThreads = atoi(argv[4]);    }    vector<string> images_filenames, models_filenames;    readDirectory( images_folder, images_filenames );    readDirectory( models_folder, models_filenames );    LatentSvmDetector detector( models_filenames );    if( detector.empty() )    {        cout << "Models cann't be loaded" << endl;        exit(-1);    }    const vector<string>& classNames = detector.getClassNames();    cout << "Loaded " << classNames.size() << " models:" << endl;    for( size_t i = 0; i < classNames.size(); i++ )    {        cout << i << ") " << classNames[i] << "; ";    }    cout << endl;    cout << "overlapThreshold = " << overlapThreshold << endl;    vector<Scalar> colors;    generateColors( colors, detector.getClassNames().size() );    for( size_t i = 0; i < images_filenames.size(); i++ )    {        Mat image = imread( images_filenames[i] );        if( image.empty() )  continue;        cout << "Process image " << images_filenames[i] << endl;        detectAndDrawObjects( image, detector, colors, overlapThreshold, numThreads );        imshow( "result", image );        for(;;)        {            int c = waitKey();            if( (char)c == 'n')                break;            else if( (char)c == '\x1b' )                exit(0);        }    }    return 0;}

Upload a result. The picture is copyrighted. Do not reprint it.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.