Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions modules/latentsvm/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
set(the_description "Object Detection")
ocv_define_module(latentsvm opencv_core opencv_imgproc opencv_objdetect opencv_ts OPTIONAL opencv_highgui)
113 changes: 113 additions & 0 deletions modules/latentsvm/doc/latent_svm_cascade.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
Latent SVM
===============================================================

Discriminatively Trained Part Based Models for Object Detection
---------------------------------------------------------------

The object detector described below has been initially proposed by
P.F. Felzenszwalb in [Felzenszwalb2010a]_. 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 particular 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 of the
class of models by a mixture of star models. The score of a mixture
model at a particular position and scale is the maximum over
components, of the score of that component model at the given
location.

The detector was dramatically speeded-up with cascade algorithm
proposed by P.F. Felzenszwalb in [Felzenszwalb2010b]_. The algorithm
prunes partial hypotheses using thresholds on their scores.The basic
idea of the algorithm is to use a hierarchy of models defined by an
ordering of the original model's parts. For a model with (n+1)
parts, including the root, a sequence of (n+1) models is obtained.
The i-th model in this sequence is defined by the first i parts from
the original model. Using this hierarchy, low scoring hypotheses can be
pruned after looking at the best configuration of a subset of the parts.
Hypotheses that score high under a weak model are evaluated further using
a richer model.

In OpenCV there is an C++ implementation of Latent SVM.

.. highlight:: cpp

LSVMDetector
-----------------
.. ocv:class:: LSVMDetector

This is a C++ abstract class, it provides external user API to work with Latent SVM.

LSVMDetector::ObjectDetection
----------------------------------
.. ocv:struct:: LSVMDetector::ObjectDetection

Structure contains the detection information.

.. ocv:member:: Rect rect

bounding box for a detected object

.. ocv:member:: float score

confidence level

.. ocv:member:: int classID

class (model or detector) ID that detect an object

LSVMDetector::~LSVMDetector
-------------------------------------
Destructor.

.. ocv:function:: LSVMDetector::~LSVMDetector()

LSVMDetector::create
-----------------------
Load the trained models from given ``.xml`` files and return ``cv::Ptr<LSVMDetector>``.

.. ocv:function:: static cv::Ptr<LSVMDetector> LSVMDetector::create( const vector<string>& filenames, const vector<string>& classNames=vector<string>() )

: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/.

: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".

LSVMDetector::detect
-------------------------
Find rectangular regions in the given image that are likely to contain objects of loaded classes (models)
and corresponding confidence levels.

.. ocv:function:: void LSVMDetector::detect( const Mat& image, vector<ObjectDetection>& objectDetections, float overlapThreshold=0.5f, int numThreads=-1 )

:param image: An image.
:param objectDetections: The detections: rectangulars, scores and class IDs.
:param overlapThreshold: Threshold for the non-maximum suppression algorithm.
:param numThreads: Number of threads used in parallel version of the algorithm.

LSVMDetector::getClassNames
--------------------------------
Return the class (model) names that were passed in constructor or method ``load`` or extracted from models filenames in those methods.

.. ocv:function:: const vector<string>& LSVMDetector::getClassNames() const

LSVMDetector::getClassCount
--------------------------------
Return a count of loaded models (classes).

.. ocv:function:: size_t LSVMDetector::getClassCount() const


.. [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
.. [Felzenszwalb2010b] Felzenszwalb, P. F. and Girshick, R. B. and McAllester, D. *Cascade Object Detection with Deformable Part Models*. CVPR 2010, pp. 2241-2248

93 changes: 93 additions & 0 deletions modules/latentsvm/include/opencv2/latentsvm.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2010-2013, University of Nizhny Novgorod, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of the copyright holders may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
// SVM implementation authors:
// Evgeniy Kozinov - [email protected]
// Valentina Kustikova - [email protected]
// Nikolai Zolotykh - [email protected]
// Iosif Meyerov - [email protected]
// Alexey Polovinkin - [email protected]
//
//M*/

#ifndef __OPENCV_LATENTSVM_HPP__
#define __OPENCV_LATENTSVM_HPP__

#include "opencv2/core.hpp"

#include <map>
#include <vector>
#include <string>

namespace cv
{

namespace lsvm
{

class CV_EXPORTS_W LSVMDetector
{
public:

struct CV_EXPORTS_W ObjectDetection
{
ObjectDetection();
ObjectDetection( const Rect& rect, float score, int classID=-1 );
Rect rect;
float score;
int classID;
};

virtual bool isEmpty() const = 0;
virtual void detect(cv::Mat const &image, CV_OUT std::vector<ObjectDetection> &objects,
float overlapThreshold=0.5f ) = 0;

virtual std::vector<std::string> const& getClassNames() const = 0;
virtual size_t getClassCount() const = 0;

static cv::Ptr<LSVMDetector> create(std::vector<std::string> const &filenames,
std::vector<std::string> const &classNames = std::vector<std::string>());

virtual ~LSVMDetector(){}
};

} // namespace lsvm
} // namespace cv

#endif
50 changes: 50 additions & 0 deletions modules/latentsvm/perf/perf_cascadeclassifier.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include "perf_precomp.hpp"
#include <opencv2/imgproc.hpp>

using namespace std;
using namespace cv;
using namespace perf;
using std::tr1::make_tuple;
using std::tr1::get;

typedef std::tr1::tuple<std::string, int> ImageName_MinSize_t;
typedef perf::TestBaseWithParam<ImageName_MinSize_t> ImageName_MinSize;

PERF_TEST_P(ImageName_MinSize, CascadeClassifierLBPFrontalFace,
testing::Combine(testing::Values( std::string("cv/shared/lena.png"),
std::string("cv/shared/1_itseez-0000289.png"),
std::string("cv/shared/1_itseez-0000492.png"),
std::string("cv/shared/1_itseez-0000573.png")),
testing::Values(24, 30, 40, 50, 60, 70, 80, 90)
)
)
{
const string filename = get<0>(GetParam());
int min_size = get<1>(GetParam());
Size minSize(min_size, min_size);

CascadeClassifier cc(getDataPath("cv/cascadeandhog/cascades/lbpcascade_frontalface.xml"));
if (cc.empty())
FAIL() << "Can't load cascade file";

Mat img = imread(getDataPath(filename), 0);
if (img.empty())
FAIL() << "Can't load source image";

vector<Rect> faces;

equalizeHist(img, img);
declare.in(img);

while(next())
{
faces.clear();

startTimer();
cc.detectMultiScale(img, faces, 1.1, 3, 0, minSize);
stopTimer();
}

std::sort(faces.begin(), faces.end(), comparators::RectLess());
SANITY_CHECK(faces, 3.001 * faces.size());
}
3 changes: 3 additions & 0 deletions modules/latentsvm/perf/perf_main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "perf_precomp.hpp"

CV_PERF_TEST_MAIN(objdetect)
1 change: 1 addition & 0 deletions modules/latentsvm/perf/perf_precomp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "perf_precomp.hpp"
20 changes: 20 additions & 0 deletions modules/latentsvm/perf/perf_precomp.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifdef __GNUC__
# pragma GCC diagnostic ignored "-Wmissing-declarations"
# if defined __clang__ || defined __APPLE__
# pragma GCC diagnostic ignored "-Wmissing-prototypes"
# pragma GCC diagnostic ignored "-Wextra"
# endif
#endif

#ifndef __OPENCV_PERF_PRECOMP_HPP__
#define __OPENCV_PERF_PRECOMP_HPP__

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

#ifdef GTEST_CREATE_SHARED_LIBRARY
#error no modules except ts should have GTEST_CREATE_SHARED_LIBRARY defined
#endif

#endif
61 changes: 61 additions & 0 deletions modules/latentsvm/src/_lsvmc_error.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2010-2013, University of Nizhny Novgorod, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of the copyright holders may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/

#ifndef LSVM_ERROR
#define LSVM_ERROR

#define LATENT_SVM_OK 0
#define LATENT_SVM_MEM_NULL 2
#define DISTANCE_TRANSFORM_OK 1
#define DISTANCE_TRANSFORM_GET_INTERSECTION_ERROR -1
#define DISTANCE_TRANSFORM_ERROR -2
#define DISTANCE_TRANSFORM_EQUAL_POINTS -3
#define LATENT_SVM_GET_FEATURE_PYRAMID_FAILED -4
#define LATENT_SVM_SEARCH_OBJECT_FAILED -5
#define LATENT_SVM_FAILED_SUPERPOSITION -6
#define FILTER_OUT_OF_BOUNDARIES -7
#define LATENT_SVM_TBB_SCHEDULE_CREATION_FAILED -8
#define LATENT_SVM_TBB_NUMTHREADS_NOT_CORRECT -9
#define FFT_OK 2
#define FFT_ERROR -10
#define LSVM_PARSER_FILE_NOT_FOUND -11

#endif
Loading