Skip to content

Commit 9a0cc98

Browse files
committed
Merge pull request #127 from BKNio/lsvm_develop
latentsvm caskade
2 parents cb3eebc + df2f650 commit 9a0cc98

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+5514
-0
lines changed

modules/latentsvm/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
set(the_description "Object Detection")
2+
ocv_define_module(latentsvm opencv_core opencv_imgproc opencv_objdetect opencv_ts OPTIONAL opencv_highgui)
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
Latent SVM
2+
===============================================================
3+
4+
Discriminatively Trained Part Based Models for Object Detection
5+
---------------------------------------------------------------
6+
7+
The object detector described below has been initially proposed by
8+
P.F. Felzenszwalb in [Felzenszwalb2010a]_. It is based on a
9+
Dalal-Triggs detector that uses a single filter on histogram of
10+
oriented gradients (HOG) features to represent an object category.
11+
This detector uses a sliding window approach, where a filter is
12+
applied at all positions and scales of an image. The first
13+
innovation is enriching the Dalal-Triggs model using a
14+
star-structured part-based model defined by a "root" filter
15+
(analogous to the Dalal-Triggs filter) plus a set of parts filters
16+
and associated deformation models. The score of one of star models
17+
at a particular position and scale within an image is the score of
18+
the root filter at the given location plus the sum over parts of the
19+
maximum, over placements of that part, of the part filter score on
20+
its location minus a deformation cost easuring the deviation of the
21+
part from its ideal location relative to the root. Both root and
22+
part filter scores are defined by the dot product between a filter
23+
(a set of weights) and a subwindow of a feature pyramid computed
24+
from the input image. Another improvement is a representation of the
25+
class of models by a mixture of star models. The score of a mixture
26+
model at a particular position and scale is the maximum over
27+
components, of the score of that component model at the given
28+
location.
29+
30+
The detector was dramatically speeded-up with cascade algorithm
31+
proposed by P.F. Felzenszwalb in [Felzenszwalb2010b]_. The algorithm
32+
prunes partial hypotheses using thresholds on their scores.The basic
33+
idea of the algorithm is to use a hierarchy of models defined by an
34+
ordering of the original model's parts. For a model with (n+1)
35+
parts, including the root, a sequence of (n+1) models is obtained.
36+
The i-th model in this sequence is defined by the first i parts from
37+
the original model. Using this hierarchy, low scoring hypotheses can be
38+
pruned after looking at the best configuration of a subset of the parts.
39+
Hypotheses that score high under a weak model are evaluated further using
40+
a richer model.
41+
42+
In OpenCV there is an C++ implementation of Latent SVM.
43+
44+
.. highlight:: cpp
45+
46+
LSVMDetector
47+
-----------------
48+
.. ocv:class:: LSVMDetector
49+
50+
This is a C++ abstract class, it provides external user API to work with Latent SVM.
51+
52+
LSVMDetector::ObjectDetection
53+
----------------------------------
54+
.. ocv:struct:: LSVMDetector::ObjectDetection
55+
56+
Structure contains the detection information.
57+
58+
.. ocv:member:: Rect rect
59+
60+
bounding box for a detected object
61+
62+
.. ocv:member:: float score
63+
64+
confidence level
65+
66+
.. ocv:member:: int classID
67+
68+
class (model or detector) ID that detect an object
69+
70+
LSVMDetector::~LSVMDetector
71+
-------------------------------------
72+
Destructor.
73+
74+
.. ocv:function:: LSVMDetector::~LSVMDetector()
75+
76+
LSVMDetector::create
77+
-----------------------
78+
Load the trained models from given ``.xml`` files and return ``cv::Ptr<LSVMDetector>``.
79+
80+
.. ocv:function:: static cv::Ptr<LSVMDetector> LSVMDetector::create( const vector<string>& filenames, const vector<string>& classNames=vector<string>() )
81+
82+
:param filenames: A set of filenames storing the trained detectors (models). Each file contains one model. See examples of such files here /opencv_extra/testdata/cv/LSVMDetector/models_VOC2007/.
83+
84+
:param classNames: A set of trained models names. If it's empty then the name of each model will be constructed from the name of file containing the model. E.g. the model stored in "/home/user/cat.xml" will get the name "cat".
85+
86+
LSVMDetector::detect
87+
-------------------------
88+
Find rectangular regions in the given image that are likely to contain objects of loaded classes (models)
89+
and corresponding confidence levels.
90+
91+
.. ocv:function:: void LSVMDetector::detect( const Mat& image, vector<ObjectDetection>& objectDetections, float overlapThreshold=0.5f, int numThreads=-1 )
92+
93+
:param image: An image.
94+
:param objectDetections: The detections: rectangulars, scores and class IDs.
95+
:param overlapThreshold: Threshold for the non-maximum suppression algorithm.
96+
:param numThreads: Number of threads used in parallel version of the algorithm.
97+
98+
LSVMDetector::getClassNames
99+
--------------------------------
100+
Return the class (model) names that were passed in constructor or method ``load`` or extracted from models filenames in those methods.
101+
102+
.. ocv:function:: const vector<string>& LSVMDetector::getClassNames() const
103+
104+
LSVMDetector::getClassCount
105+
--------------------------------
106+
Return a count of loaded models (classes).
107+
108+
.. ocv:function:: size_t LSVMDetector::getClassCount() const
109+
110+
111+
.. [Felzenszwalb2010a] Felzenszwalb, P. F. and Girshick, R. B. and McAllester, D. and Ramanan, D. *Object Detection with Discriminatively Trained Part Based Models*. PAMI, vol. 32, no. 9, pp. 1627-1645, September 2010
112+
.. [Felzenszwalb2010b] Felzenszwalb, P. F. and Girshick, R. B. and McAllester, D. *Cascade Object Detection with Deformable Part Models*. CVPR 2010, pp. 2241-2248
113+
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*M///////////////////////////////////////////////////////////////////////////////////////
2+
//
3+
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4+
//
5+
// By downloading, copying, installing or using the software you agree to this license.
6+
// If you do not agree to this license, do not download, install,
7+
// copy or use the software.
8+
//
9+
//
10+
// License Agreement
11+
// For Open Source Computer Vision Library
12+
//
13+
// Copyright (C) 2010-2013, University of Nizhny Novgorod, all rights reserved.
14+
// Third party copyrights are property of their respective owners.
15+
//
16+
// Redistribution and use in source and binary forms, with or without modification,
17+
// are permitted provided that the following conditions are met:
18+
//
19+
// * Redistribution's of source code must retain the above copyright notice,
20+
// this list of conditions and the following disclaimer.
21+
//
22+
// * Redistribution's in binary form must reproduce the above copyright notice,
23+
// this list of conditions and the following disclaimer in the documentation
24+
// and/or other materials provided with the distribution.
25+
//
26+
// * The name of the copyright holders may not be used to endorse or promote products
27+
// derived from this software without specific prior written permission.
28+
//
29+
// This software is provided by the copyright holders and contributors "as is" and
30+
// any express or implied warranties, including, but not limited to, the implied
31+
// warranties of merchantability and fitness for a particular purpose are disclaimed.
32+
// In no event shall the Intel Corporation or contributors be liable for any direct,
33+
// indirect, incidental, special, exemplary, or consequential damages
34+
// (including, but not limited to, procurement of substitute goods or services;
35+
// loss of use, data, or profits; or business interruption) however caused
36+
// and on any theory of liability, whether in contract, strict liability,
37+
// or tort (including negligence or otherwise) arising in any way out of
38+
// the use of this software, even if advised of the possibility of such damage.
39+
//
40+
// SVM implementation authors:
41+
// Evgeniy Kozinov - [email protected]
42+
// Valentina Kustikova - [email protected]
43+
// Nikolai Zolotykh - [email protected]
44+
// Iosif Meyerov - [email protected]
45+
// Alexey Polovinkin - [email protected]
46+
//
47+
//M*/
48+
49+
#ifndef __OPENCV_LATENTSVM_HPP__
50+
#define __OPENCV_LATENTSVM_HPP__
51+
52+
#include "opencv2/core.hpp"
53+
54+
#include <map>
55+
#include <vector>
56+
#include <string>
57+
58+
namespace cv
59+
{
60+
61+
namespace lsvm
62+
{
63+
64+
class CV_EXPORTS_W LSVMDetector
65+
{
66+
public:
67+
68+
struct CV_EXPORTS_W ObjectDetection
69+
{
70+
ObjectDetection();
71+
ObjectDetection( const Rect& rect, float score, int classID=-1 );
72+
Rect rect;
73+
float score;
74+
int classID;
75+
};
76+
77+
virtual bool isEmpty() const = 0;
78+
virtual void detect(cv::Mat const &image, CV_OUT std::vector<ObjectDetection> &objects,
79+
float overlapThreshold=0.5f ) = 0;
80+
81+
virtual std::vector<std::string> const& getClassNames() const = 0;
82+
virtual size_t getClassCount() const = 0;
83+
84+
static cv::Ptr<LSVMDetector> create(std::vector<std::string> const &filenames,
85+
std::vector<std::string> const &classNames = std::vector<std::string>());
86+
87+
virtual ~LSVMDetector(){}
88+
};
89+
90+
} // namespace lsvm
91+
} // namespace cv
92+
93+
#endif
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include "perf_precomp.hpp"
2+
#include <opencv2/imgproc.hpp>
3+
4+
using namespace std;
5+
using namespace cv;
6+
using namespace perf;
7+
using std::tr1::make_tuple;
8+
using std::tr1::get;
9+
10+
typedef std::tr1::tuple<std::string, int> ImageName_MinSize_t;
11+
typedef perf::TestBaseWithParam<ImageName_MinSize_t> ImageName_MinSize;
12+
13+
PERF_TEST_P(ImageName_MinSize, CascadeClassifierLBPFrontalFace,
14+
testing::Combine(testing::Values( std::string("cv/shared/lena.png"),
15+
std::string("cv/shared/1_itseez-0000289.png"),
16+
std::string("cv/shared/1_itseez-0000492.png"),
17+
std::string("cv/shared/1_itseez-0000573.png")),
18+
testing::Values(24, 30, 40, 50, 60, 70, 80, 90)
19+
)
20+
)
21+
{
22+
const string filename = get<0>(GetParam());
23+
int min_size = get<1>(GetParam());
24+
Size minSize(min_size, min_size);
25+
26+
CascadeClassifier cc(getDataPath("cv/cascadeandhog/cascades/lbpcascade_frontalface.xml"));
27+
if (cc.empty())
28+
FAIL() << "Can't load cascade file";
29+
30+
Mat img = imread(getDataPath(filename), 0);
31+
if (img.empty())
32+
FAIL() << "Can't load source image";
33+
34+
vector<Rect> faces;
35+
36+
equalizeHist(img, img);
37+
declare.in(img);
38+
39+
while(next())
40+
{
41+
faces.clear();
42+
43+
startTimer();
44+
cc.detectMultiScale(img, faces, 1.1, 3, 0, minSize);
45+
stopTimer();
46+
}
47+
48+
std::sort(faces.begin(), faces.end(), comparators::RectLess());
49+
SANITY_CHECK(faces, 3.001 * faces.size());
50+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#include "perf_precomp.hpp"
2+
3+
CV_PERF_TEST_MAIN(objdetect)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include "perf_precomp.hpp"
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#ifdef __GNUC__
2+
# pragma GCC diagnostic ignored "-Wmissing-declarations"
3+
# if defined __clang__ || defined __APPLE__
4+
# pragma GCC diagnostic ignored "-Wmissing-prototypes"
5+
# pragma GCC diagnostic ignored "-Wextra"
6+
# endif
7+
#endif
8+
9+
#ifndef __OPENCV_PERF_PRECOMP_HPP__
10+
#define __OPENCV_PERF_PRECOMP_HPP__
11+
12+
#include "opencv2/ts.hpp"
13+
#include "opencv2/objdetect.hpp"
14+
#include "opencv2/highgui.hpp"
15+
16+
#ifdef GTEST_CREATE_SHARED_LIBRARY
17+
#error no modules except ts should have GTEST_CREATE_SHARED_LIBRARY defined
18+
#endif
19+
20+
#endif
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*M///////////////////////////////////////////////////////////////////////////////////////
2+
//
3+
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4+
//
5+
// By downloading, copying, installing or using the software you agree to this license.
6+
// If you do not agree to this license, do not download, install,
7+
// copy or use the software.
8+
//
9+
//
10+
// License Agreement
11+
// For Open Source Computer Vision Library
12+
//
13+
// Copyright (C) 2010-2013, University of Nizhny Novgorod, all rights reserved.
14+
// Third party copyrights are property of their respective owners.
15+
//
16+
// Redistribution and use in source and binary forms, with or without modification,
17+
// are permitted provided that the following conditions are met:
18+
//
19+
// * Redistribution's of source code must retain the above copyright notice,
20+
// this list of conditions and the following disclaimer.
21+
//
22+
// * Redistribution's in binary form must reproduce the above copyright notice,
23+
// this list of conditions and the following disclaimer in the documentation
24+
// and/or other materials provided with the distribution.
25+
//
26+
// * The name of the copyright holders may not be used to endorse or promote products
27+
// derived from this software without specific prior written permission.
28+
//
29+
// This software is provided by the copyright holders and contributors "as is" and
30+
// any express or implied warranties, including, but not limited to, the implied
31+
// warranties of merchantability and fitness for a particular purpose are disclaimed.
32+
// In no event shall the Intel Corporation or contributors be liable for any direct,
33+
// indirect, incidental, special, exemplary, or consequential damages
34+
// (including, but not limited to, procurement of substitute goods or services;
35+
// loss of use, data, or profits; or business interruption) however caused
36+
// and on any theory of liability, whether in contract, strict liability,
37+
// or tort (including negligence or otherwise) arising in any way out of
38+
// the use of this software, even if advised of the possibility of such damage.
39+
//
40+
//M*/
41+
42+
#ifndef LSVM_ERROR
43+
#define LSVM_ERROR
44+
45+
#define LATENT_SVM_OK 0
46+
#define LATENT_SVM_MEM_NULL 2
47+
#define DISTANCE_TRANSFORM_OK 1
48+
#define DISTANCE_TRANSFORM_GET_INTERSECTION_ERROR -1
49+
#define DISTANCE_TRANSFORM_ERROR -2
50+
#define DISTANCE_TRANSFORM_EQUAL_POINTS -3
51+
#define LATENT_SVM_GET_FEATURE_PYRAMID_FAILED -4
52+
#define LATENT_SVM_SEARCH_OBJECT_FAILED -5
53+
#define LATENT_SVM_FAILED_SUPERPOSITION -6
54+
#define FILTER_OUT_OF_BOUNDARIES -7
55+
#define LATENT_SVM_TBB_SCHEDULE_CREATION_FAILED -8
56+
#define LATENT_SVM_TBB_NUMTHREADS_NOT_CORRECT -9
57+
#define FFT_OK 2
58+
#define FFT_ERROR -10
59+
#define LSVM_PARSER_FILE_NOT_FOUND -11
60+
61+
#endif

0 commit comments

Comments
 (0)