2011-02-23 04:43:26 +08:00
Object Detection
================
.. highlight :: cpp
2011-08-30 16:27:23 +08:00
2011-02-23 04:43:26 +08:00
2013-08-20 19:05:49 +08:00
cuda::HOGDescriptor
-------------------
.. ocv:struct :: cuda::HOGDescriptor
2011-02-23 04:43:26 +08:00
2011-08-30 16:27:23 +08:00
The class implements Histogram of Oriented Gradients ([Dalal2005]_ ) object detector. ::
2011-02-26 19:05:10 +08:00
2011-03-29 07:05:42 +08:00
struct CV_EXPORTS HOGDescriptor
2011-02-26 19:05:10 +08:00
{
enum { DEFAULT_WIN_SIGMA = -1 };
enum { DEFAULT_NLEVELS = 64 };
enum { DESCR_FORMAT_ROW_BY_ROW, DESCR_FORMAT_COL_BY_COL };
HOGDescriptor(Size win_size=Size(64, 128), Size block_size=Size(16, 16),
Size block_stride=Size(8, 8), Size cell_size=Size(8, 8),
int nbins=9, double win_sigma=DEFAULT_WIN_SIGMA,
double threshold_L2hys=0.2, bool gamma_correction=true,
int nlevels=DEFAULT_NLEVELS);
size_t getDescriptorSize() const;
size_t getBlockHistogramSize() const;
void setSVMDetector(const vector<float>& detector);
static vector<float> getDefaultPeopleDetector();
static vector<float> getPeopleDetector48x96();
static vector<float> getPeopleDetector64x128();
void detect(const GpuMat& img, vector<Point>& found_locations,
double hit_threshold=0, Size win_stride=Size(),
Size padding=Size());
void detectMultiScale(const GpuMat& img, vector<Rect>& found_locations,
double hit_threshold=0, Size win_stride=Size(),
Size padding=Size(), double scale0=1.05,
int group_threshold=2);
void getDescriptors(const GpuMat& img, Size win_stride,
GpuMat& descriptors,
int descr_format=DESCR_FORMAT_COL_BY_COL);
Size win_size;
Size block_size;
Size block_stride;
Size cell_size;
int nbins;
double win_sigma;
double threshold_L2hys;
bool gamma_correction;
int nlevels;
private:
// Hidden
}
2011-03-01 05:26:43 +08:00
2011-02-23 04:43:26 +08:00
2011-04-01 06:07:17 +08:00
Interfaces of all methods are kept similar to the `` CPU HOG `` descriptor and detector analogues as much as possible.
2011-03-23 18:56:20 +08:00
2013-08-06 22:24:09 +08:00
.. note ::
2011-08-30 16:27:23 +08:00
2013-08-06 22:24:09 +08:00
* An example applying the HOG descriptor for people detection can be found at opencv_source_code/samples/cpp/peopledetect.cpp
2013-08-20 19:05:49 +08:00
* A CUDA example applying the HOG descriptor for people detection can be found at opencv_source_code/samples/gpu/hog.cpp
2013-08-02 20:05:08 +08:00
2013-08-06 22:24:09 +08:00
* (Python) An example applying the HOG descriptor for people detection can be found at opencv_source_code/samples/python2/peopledetect.py
2011-02-23 04:43:26 +08:00
2013-08-20 19:05:49 +08:00
cuda::HOGDescriptor::HOGDescriptor
----------------------------------
2011-08-30 16:27:23 +08:00
Creates the `` HOG `` descriptor and detector.
2011-02-23 04:43:26 +08:00
2013-08-20 19:05:49 +08:00
.. ocv:function :: cuda::HOGDescriptor::HOGDescriptor(Size win_size=Size(64, 128), Size block_size=Size(16, 16), Size block_stride=Size(8, 8), Size cell_size=Size(8, 8), int nbins=9, double win_sigma=DEFAULT_WIN_SIGMA, double threshold_L2hys=0.2, bool gamma_correction=true, int nlevels=DEFAULT_NLEVELS)
2011-02-23 04:43:26 +08:00
2011-06-19 04:37:50 +08:00
:param win_size: Detection window size. Align to block size and block stride.
2011-02-23 04:43:26 +08:00
2011-06-19 04:37:50 +08:00
:param block_size: Block size in pixels. Align to cell size. Only (16,16) is supported for now.
2011-02-23 04:43:26 +08:00
2011-06-19 04:37:50 +08:00
:param block_stride: Block stride. It must be a multiple of cell size.
2011-02-23 04:43:26 +08:00
2011-06-19 04:37:50 +08:00
:param cell_size: Cell size. Only (8, 8) is supported for now.
2011-02-23 04:43:26 +08:00
2011-06-19 04:37:50 +08:00
:param nbins: Number of bins. Only 9 bins per cell are supported for now.
2011-02-23 04:43:26 +08:00
2011-06-19 04:37:50 +08:00
:param win_sigma: Gaussian smoothing window parameter.
2011-02-23 04:43:26 +08:00
2012-03-29 14:50:05 +08:00
:param threshold_L2hys: L2-Hys normalization method shrinkage.
2011-02-23 04:43:26 +08:00
2011-06-19 04:37:50 +08:00
:param gamma_correction: Flag to specify whether the gamma correction preprocessing is required or not.
2011-02-23 04:43:26 +08:00
2011-06-19 04:37:50 +08:00
:param nlevels: Maximum number of detection window increases.
2011-02-23 04:43:26 +08:00
2011-08-30 16:27:23 +08:00
2011-02-23 04:43:26 +08:00
2013-08-20 19:05:49 +08:00
cuda::HOGDescriptor::getDescriptorSize
--------------------------------------
2011-08-30 16:27:23 +08:00
Returns the number of coefficients required for the classification.
2013-08-20 19:05:49 +08:00
.. ocv:function :: size_t cuda::HOGDescriptor::getDescriptorSize() const
2011-02-23 04:43:26 +08:00
2011-03-23 18:56:20 +08:00
2011-02-23 04:43:26 +08:00
2013-08-20 19:05:49 +08:00
cuda::HOGDescriptor::getBlockHistogramSize
------------------------------------------
2011-08-30 16:27:23 +08:00
Returns the block histogram size.
2013-08-20 19:05:49 +08:00
.. ocv:function :: size_t cuda::HOGDescriptor::getBlockHistogramSize() const
2011-02-23 04:43:26 +08:00
2011-03-23 18:56:20 +08:00
2011-02-23 04:43:26 +08:00
2013-08-20 19:05:49 +08:00
cuda::HOGDescriptor::setSVMDetector
-----------------------------------
2011-08-30 16:27:23 +08:00
Sets coefficients for the linear SVM classifier.
2013-08-20 19:05:49 +08:00
.. ocv:function :: void cuda::HOGDescriptor::setSVMDetector(const vector<float>& detector)
2011-02-23 04:43:26 +08:00
2013-08-20 19:05:49 +08:00
cuda::HOGDescriptor::getDefaultPeopleDetector
---------------------------------------------
2011-08-30 16:27:23 +08:00
Returns coefficients of the classifier trained for people detection (for default window size).
2013-08-20 19:05:49 +08:00
.. ocv:function :: static vector<float> cuda::HOGDescriptor::getDefaultPeopleDetector()
2011-02-23 04:43:26 +08:00
2013-08-20 19:05:49 +08:00
cuda::HOGDescriptor::getPeopleDetector48x96
-------------------------------------------
2011-08-30 16:27:23 +08:00
Returns coefficients of the classifier trained for people detection (for 48x96 windows).
2013-08-20 19:05:49 +08:00
.. ocv:function :: static vector<float> cuda::HOGDescriptor::getPeopleDetector48x96()
2011-02-23 04:43:26 +08:00
2013-08-20 19:05:49 +08:00
cuda::HOGDescriptor::getPeopleDetector64x128
--------------------------------------------
2011-08-30 16:27:23 +08:00
Returns coefficients of the classifier trained for people detection (for 64x128 windows).
2013-08-20 19:05:49 +08:00
.. ocv:function :: static vector<float> cuda::HOGDescriptor::getPeopleDetector64x128()
2011-02-23 04:43:26 +08:00
2013-08-20 19:05:49 +08:00
cuda::HOGDescriptor::detect
---------------------------
2011-08-30 16:27:23 +08:00
Performs object detection without a multi-scale window.
2011-02-23 04:43:26 +08:00
2013-08-20 19:05:49 +08:00
.. ocv:function :: void cuda::HOGDescriptor::detect(const GpuMat& img, vector<Point>& found_locations, double hit_threshold=0, Size win_stride=Size(), Size padding=Size())
2011-02-23 04:43:26 +08:00
2011-06-19 04:37:50 +08:00
:param img: Source image. ``CV_8UC1`` and ``CV_8UC4`` types are supported for now.
2011-02-23 04:43:26 +08:00
2011-06-19 04:37:50 +08:00
:param found_locations: Left-top corner points of detected objects boundaries.
2011-02-23 04:43:26 +08:00
2011-06-19 04:37:50 +08:00
:param hit_threshold: Threshold for the distance between features and SVM classifying plane. Usually it is 0 and should be specfied in the detector coefficients (as the last free coefficient). But if the free coefficient is omitted (which is allowed), you can specify it manually here.
2011-02-23 04:43:26 +08:00
2011-06-19 04:37:50 +08:00
:param win_stride: Window stride. It must be a multiple of block stride.
2011-03-23 18:56:20 +08:00
2011-06-19 04:37:50 +08:00
:param padding: Mock parameter to keep the CPU interface compatibility. It must be (0,0).
2011-03-23 18:56:20 +08:00
2011-08-30 16:27:23 +08:00
2011-02-23 04:43:26 +08:00
2013-08-20 19:05:49 +08:00
cuda::HOGDescriptor::detectMultiScale
-------------------------------------
2011-08-30 16:27:23 +08:00
Performs object detection with a multi-scale window.
2011-02-23 04:43:26 +08:00
2013-08-20 19:05:49 +08:00
.. ocv:function :: void cuda::HOGDescriptor::detectMultiScale(const GpuMat& img, vector<Rect>& found_locations, double hit_threshold=0, Size win_stride=Size(), Size padding=Size(), double scale0=1.05, int group_threshold=2)
2011-02-23 04:43:26 +08:00
2013-08-20 19:05:49 +08:00
:param img: Source image. See :ocv:func:`cuda::HOGDescriptor::detect` for type limitations.
2011-02-23 04:43:26 +08:00
2011-06-19 04:37:50 +08:00
:param found_locations: Detected objects boundaries.
2011-02-23 04:43:26 +08:00
2013-08-20 19:05:49 +08:00
:param hit_threshold: Threshold for the distance between features and SVM classifying plane. See :ocv:func:`cuda::HOGDescriptor::detect` for details.
2011-02-23 04:43:26 +08:00
2011-06-19 04:37:50 +08:00
:param win_stride: Window stride. It must be a multiple of block stride.
2011-02-23 04:43:26 +08:00
2011-06-19 04:37:50 +08:00
:param padding: Mock parameter to keep the CPU interface compatibility. It must be (0,0).
2011-02-23 04:43:26 +08:00
2011-06-19 04:37:50 +08:00
:param scale0: Coefficient of the detection window increase.
2011-02-23 04:43:26 +08:00
2011-06-19 04:37:50 +08:00
:param group_threshold: Coefficient to regulate the similarity threshold. When detected, some objects can be covered by many rectangles. 0 means not to perform grouping. See :ocv:func:`groupRectangles` .
2011-02-26 19:05:10 +08:00
2011-08-30 16:27:23 +08:00
2011-02-23 04:43:26 +08:00
2013-08-20 19:05:49 +08:00
cuda::HOGDescriptor::getDescriptors
-----------------------------------
2011-08-30 16:27:23 +08:00
Returns block descriptors computed for the whole image.
2011-02-23 04:43:26 +08:00
2013-08-20 19:05:49 +08:00
.. ocv:function :: void cuda::HOGDescriptor::getDescriptors(const GpuMat& img, Size win_stride, GpuMat& descriptors, int descr_format=DESCR_FORMAT_COL_BY_COL)
2011-02-23 04:43:26 +08:00
2013-08-20 19:05:49 +08:00
:param img: Source image. See :ocv:func:`cuda::HOGDescriptor::detect` for type limitations.
2011-02-23 04:43:26 +08:00
2011-06-19 04:37:50 +08:00
:param win_stride: Window stride. It must be a multiple of block stride.
2011-02-23 04:43:26 +08:00
2011-06-19 04:37:50 +08:00
:param descriptors: 2D array of descriptors.
2011-02-23 04:43:26 +08:00
2011-08-30 16:27:23 +08:00
:param descr_format: Descriptor storage format:
2011-02-23 04:43:26 +08:00
2011-06-19 04:37:50 +08:00
* **DESCR_FORMAT_ROW_BY_ROW** - Row-major order.
2011-02-23 04:43:26 +08:00
2011-06-19 04:37:50 +08:00
* **DESCR_FORMAT_COL_BY_COL** - Column-major order.
2011-02-23 04:43:26 +08:00
2011-08-30 16:27:23 +08:00
The function is mainly used to learn the classifier.
2013-08-20 19:05:49 +08:00
cuda::CascadeClassifier_CUDA
----------------------------
.. ocv:class :: cuda::CascadeClassifier_CUDA
2011-02-23 04:43:26 +08:00
2012-08-20 07:25:52 +08:00
Cascade classifier class used for object detection. Supports HAAR and LBP cascades. ::
2011-02-23 04:43:26 +08:00
2013-08-20 19:05:49 +08:00
class CV_EXPORTS CascadeClassifier_CUDA
2011-02-26 19:05:10 +08:00
{
public:
2013-08-20 19:05:49 +08:00
CascadeClassifier_CUDA();
CascadeClassifier_CUDA(const String& filename);
~CascadeClassifier_CUDA();
2011-02-23 04:43:26 +08:00
2011-03-29 07:05:42 +08:00
bool empty() const;
2013-03-23 00:51:45 +08:00
bool load(const String& filename);
2011-03-29 07:05:42 +08:00
void release();
2011-02-23 04:43:26 +08:00
2011-03-29 07:05:42 +08:00
/* Returns number of detected objects * /
int detectMultiScale( const GpuMat& image, GpuMat& objectsBuf, double scaleFactor=1.2, int minNeighbors=4, Size minSize=Size());
2012-08-20 07:25:52 +08:00
int detectMultiScale( const GpuMat& image, GpuMat& objectsBuf, Size maxObjectSize, Size minSize = Size(), double scaleFactor = 1.1, int minNeighbors = 4);
2011-02-23 04:43:26 +08:00
2011-03-29 07:05:42 +08:00
/* Finds only the largest object. Special mode if training is required.* /
bool findLargestObject;
2011-02-23 04:43:26 +08:00
2011-03-29 07:05:42 +08:00
/* Draws rectangles in input image * /
bool visualizeInPlace;
2011-02-23 04:43:26 +08:00
2011-03-29 07:05:42 +08:00
Size getClassifierSize() const;
2011-02-26 19:05:10 +08:00
};
2011-03-01 05:26:43 +08:00
2013-08-06 22:24:09 +08:00
.. note ::
2011-02-23 04:43:26 +08:00
2013-08-06 22:24:09 +08:00
* A cascade classifier example can be found at opencv_source_code/samples/gpu/cascadeclassifier.cpp
* A Nvidea API specific cascade classifier example can be found at opencv_source_code/samples/gpu/cascadeclassifier_nvidia_api.cpp
2011-02-23 04:43:26 +08:00
2013-08-20 19:05:49 +08:00
cuda::CascadeClassifier_CUDA::CascadeClassifier_CUDA
----------------------------------------------------
2012-08-20 07:25:52 +08:00
Loads the classifier from a file. Cascade type is detected automatically by constructor parameter.
2011-02-23 04:43:26 +08:00
2013-08-20 19:05:49 +08:00
.. ocv:function :: cuda::CascadeClassifier_CUDA::CascadeClassifier_CUDA(const String& filename)
2011-02-23 04:43:26 +08:00
2012-08-20 07:25:52 +08:00
:param filename: Name of the file from which the classifier is loaded. Only the old ``haar`` classifier (trained by the ``haar`` training application) and NVIDIA's ``nvbin`` are supported for HAAR and only new type of OpenCV XML cascade supported for LBP.
2011-02-23 04:43:26 +08:00
2011-03-23 18:56:20 +08:00
2011-03-29 07:05:42 +08:00
2013-08-20 19:05:49 +08:00
cuda::CascadeClassifier_CUDA::empty
-----------------------------------
2011-08-30 16:27:23 +08:00
Checks whether the classifier is loaded or not.
2011-02-23 04:43:26 +08:00
2013-08-20 19:05:49 +08:00
.. ocv:function :: bool cuda::CascadeClassifier_CUDA::empty() const
2011-02-23 04:43:26 +08:00
2011-03-23 18:56:20 +08:00
2013-08-20 19:05:49 +08:00
cuda::CascadeClassifier_CUDA::load
----------------------------------
2011-08-30 16:27:23 +08:00
Loads the classifier from a file. The previous content is destroyed.
2011-02-23 04:43:26 +08:00
2013-08-20 19:05:49 +08:00
.. ocv:function :: bool cuda::CascadeClassifier_CUDA::load(const String& filename)
2011-02-23 04:43:26 +08:00
2012-08-20 07:25:52 +08:00
:param filename: Name of the file from which the classifier is loaded. Only the old ``haar`` classifier (trained by the ``haar`` training application) and NVIDIA's ``nvbin`` are supported for HAAR and only new type of OpenCV XML cascade supported for LBP.
2011-08-30 16:27:23 +08:00
2011-03-23 18:56:20 +08:00
2013-08-20 19:05:49 +08:00
cuda::CascadeClassifier_CUDA::release
-------------------------------------
2011-08-30 16:27:23 +08:00
Destroys the loaded classifier.
2013-08-20 19:05:49 +08:00
.. ocv:function :: void cuda::CascadeClassifier_CUDA::release()
2011-02-23 04:43:26 +08:00
2011-03-23 18:56:20 +08:00
2013-08-20 19:05:49 +08:00
cuda::CascadeClassifier_CUDA::detectMultiScale
----------------------------------------------
2011-08-30 16:27:23 +08:00
Detects objects of different sizes in the input image.
2011-02-23 04:43:26 +08:00
2013-08-20 19:05:49 +08:00
.. ocv:function :: int cuda::CascadeClassifier_CUDA::detectMultiScale(const GpuMat& image, GpuMat& objectsBuf, double scaleFactor=1.2, int minNeighbors=4, Size minSize=Size())
2011-02-23 04:43:26 +08:00
2013-08-20 19:05:49 +08:00
.. ocv:function :: int cuda::CascadeClassifier_CUDA::detectMultiScale(const GpuMat& image, GpuMat& objectsBuf, Size maxObjectSize, Size minSize = Size(), double scaleFactor = 1.1, int minNeighbors = 4)
2012-08-20 07:25:52 +08:00
2011-03-29 07:05:42 +08:00
:param image: Matrix of type ``CV_8U`` containing an image where objects should be detected.
2011-02-23 04:43:26 +08:00
2012-03-29 14:50:05 +08:00
:param objectsBuf: Buffer to store detected objects (rectangles). If it is empty, it is allocated with the default size. If not empty, the function searches not more than N objects, where ``N = sizeof(objectsBufer's data)/sizeof(cv::Rect)``.
2011-02-23 04:43:26 +08:00
2012-08-20 07:25:52 +08:00
:param maxObjectSize: Maximum possible object size. Objects larger than that are ignored. Used for second signature and supported only for LBP cascades.
:param scaleFactor: Parameter specifying how much the image size is reduced at each image scale.
2011-02-23 04:43:26 +08:00
2012-08-20 07:25:52 +08:00
:param minNeighbors: Parameter specifying how many neighbors each candidate rectangle should have to retain it.
2011-02-23 04:43:26 +08:00
2011-03-29 07:05:42 +08:00
:param minSize: Minimum possible object size. Objects smaller than that are ignored.
2011-02-23 04:43:26 +08:00
2011-08-30 16:27:23 +08:00
The detected objects are returned as a list of rectangles.
2011-06-30 08:41:41 +08:00
2011-08-30 16:27:23 +08:00
The function returns the number of detected objects, so you can retrieve them as in the following example: ::
2011-02-23 04:43:26 +08:00
2013-08-20 19:05:49 +08:00
cuda::CascadeClassifier_CUDA cascade_gpu(...);
2011-02-23 04:43:26 +08:00
2011-02-26 19:05:10 +08:00
Mat image_cpu = imread(...)
GpuMat image_gpu(image_cpu);
2011-02-23 04:43:26 +08:00
2011-02-26 19:05:10 +08:00
GpuMat objbuf;
int detections_number = cascade_gpu.detectMultiScale( image_gpu,
objbuf, 1.2, minNeighbors);
2011-02-23 04:43:26 +08:00
2011-02-26 19:05:10 +08:00
Mat obj_host;
// download only detected number of rectangles
objbuf.colRange(0, detections_number).download(obj_host);
2011-02-23 04:43:26 +08:00
2011-02-26 19:05:10 +08:00
Rect* faces = obj_host.ptr<Rect>();
for(int i = 0; i < detections_num; ++i)
cv::rectangle(image_cpu, faces[i], Scalar(255));
2011-02-23 04:43:26 +08:00
2011-02-26 19:05:10 +08:00
imshow("Faces", image_cpu);
2011-03-01 05:26:43 +08:00
2011-03-29 07:05:42 +08:00
2011-08-30 16:27:23 +08:00
.. seealso :: :ocv:func: `CascadeClassifier::detectMultiScale`
2011-02-26 19:05:10 +08:00
2011-06-30 06:06:42 +08:00
.. [Dalal2005] Navneet Dalal and Bill Triggs. *Histogram of oriented gradients for human detection* . 2005.