2011-02-22 21:43:26 +01:00
Object Detection
================
.. highlight :: cpp
2011-03-23 11:56:20 +01:00
.. index :: gpu::HOGDescriptor
2011-02-22 21:43:26 +01:00
gpu::HOGDescriptor
------------------
2011-03-23 11:56:20 +01:00
.. cpp:class :: gpu::HOGDescriptor
2011-02-22 21:43:26 +01:00
2011-03-23 11:56:20 +01:00
Histogram of Oriented Gradients [Navneet Dalal and Bill Triggs. Histogram of oriented gradients for human detection. 2005.] descriptor and detector. ::
2011-02-26 12:05:10 +01:00
2011-03-23 11:56:20 +01:00
struct HOGDescriptor
2011-02-26 12:05:10 +01: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-02-28 22:26:43 +01:00
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
Interfaces of all methods are kept similar to CPU HOG descriptor and detector analogues as much as possible.
2011-02-22 21:43:26 +01:00
2011-03-23 11:56:20 +01:00
2011-02-22 21:43:26 +01:00
.. index :: gpu::HOGDescriptor::HOGDescriptor
2011-03-23 11:56:20 +01:00
gpu::HOGDescriptor::HOGDescriptor
2011-02-22 21:43:26 +01:00
-------------------------------------
2011-03-23 11:56:20 +01:00
.. cpp:function :: gpu::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-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
Creates HOG descriptor and detector.
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
:param win_size: Detection window size. Must be aligned to block size and block stride.
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
:param block_size: Block size in pixels. Must be aligned to cell size. Only (16,16) is supported for now.
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
:param block_stride: Block stride. Must be a multiple of cell size.
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
:param cell_size: Cell size. Only (8, 8) is supported for now.
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
:param nbins: Number of bins. Only 9 bins per cell is supported for now.
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
:param win_sigma: Gaussian smoothing window parameter.
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
:param threshold_L2Hys: L2-Hys normalization method shrinkage.
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
:param gamma_correction: Do gamma correction preprocessing or not.
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
:param nlevels: Maximum number of detection window increases.
2011-02-22 21:43:26 +01:00
2011-03-23 11:56:20 +01:00
2011-02-26 12:05:10 +01:00
.. index :: gpu::HOGDescriptor::getDescriptorSize
2011-02-22 21:43:26 +01:00
2011-03-23 11:56:20 +01:00
gpu::HOGDescriptor::getDescriptorSize
2011-02-26 12:05:10 +01:00
-----------------------------------------
2011-03-23 11:56:20 +01:00
.. cpp:function :: size_t gpu::HOGDescriptor::getDescriptorSize() const
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
Returns number of coefficients required for the classification.
2011-02-22 21:43:26 +01:00
2011-03-23 11:56:20 +01:00
2011-02-22 21:43:26 +01:00
.. index :: gpu::HOGDescriptor::getBlockHistogramSize
2011-03-23 11:56:20 +01:00
gpu::HOGDescriptor::getBlockHistogramSize
2011-02-22 21:43:26 +01:00
---------------------------------------------
2011-03-23 11:56:20 +01:00
.. cpp:function :: size_t gpu::HOGDescriptor::getBlockHistogramSize() const
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
Returns block histogram size.
2011-02-22 21:43:26 +01:00
2011-03-23 11:56:20 +01:00
2011-02-22 21:43:26 +01:00
.. index :: gpu::HOGDescriptor::setSVMDetector
2011-03-23 11:56:20 +01:00
gpu::HOGDescriptor::setSVMDetector
2011-02-22 21:43:26 +01:00
--------------------------------------
2011-03-23 11:56:20 +01:00
.. cpp:function :: void gpu::HOGDescriptor::setSVMDetector(const vector<float>& detector)
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
Sets coefficients for the linear SVM classifier.
2011-02-22 21:43:26 +01:00
2011-03-23 11:56:20 +01:00
2011-02-22 21:43:26 +01:00
.. index :: gpu::HOGDescriptor::getDefaultPeopleDetector
2011-03-23 11:56:20 +01:00
gpu::HOGDescriptor::getDefaultPeopleDetector
2011-02-22 21:43:26 +01:00
------------------------------------------------
2011-03-23 11:56:20 +01:00
.. cpp:function :: static vector<float> gpu::HOGDescriptor::getDefaultPeopleDetector()
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
Returns coefficients of the classifier trained for people detection (for default window size).
2011-02-22 21:43:26 +01:00
2011-03-23 11:56:20 +01:00
2011-02-22 21:43:26 +01:00
.. index :: gpu::HOGDescriptor::getPeopleDetector48x96
2011-03-23 11:56:20 +01:00
gpu::HOGDescriptor::getPeopleDetector48x96
2011-02-22 21:43:26 +01:00
----------------------------------------------
2011-03-23 11:56:20 +01:00
.. cpp:function :: static vector<float> gpu::HOGDescriptor::getPeopleDetector48x96()
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
Returns coefficients of the classifier trained for people detection (for 48x96 windows).
2011-02-22 21:43:26 +01:00
2011-03-23 11:56:20 +01:00
2011-02-22 21:43:26 +01:00
.. index :: gpu::HOGDescriptor::getPeopleDetector64x128
2011-03-23 11:56:20 +01:00
gpu::HOGDescriptor::getPeopleDetector64x128
2011-02-22 21:43:26 +01:00
-----------------------------------------------
2011-03-23 11:56:20 +01:00
.. cpp:function :: static vector<float> gpu::HOGDescriptor::getPeopleDetector64x128()
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
Returns coefficients of the classifier trained for people detection (for 64x128 windows).
2011-02-22 21:43:26 +01:00
2011-03-23 11:56:20 +01:00
2011-02-22 21:43:26 +01:00
.. index :: gpu::HOGDescriptor::detect
2011-03-23 11:56:20 +01:00
gpu::HOGDescriptor::detect
2011-02-22 21:43:26 +01:00
------------------------------
2011-03-23 11:56:20 +01:00
.. cpp:function :: void gpu::HOGDescriptor::detect(const GpuMat& img, vector<Point>& found_locations, double hit_threshold=0, Size win_stride=Size(), Size padding=Size())
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
Perfroms object detection without multiscale window.
2011-02-22 21:43:26 +01:00
2011-03-23 11:56:20 +01:00
:param img: Source image. ``CV_8UC1`` and ``CV_8UC4`` types are supported for now.
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
:param found_locations: Will contain left-top corner points of detected objects boundaries.
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
:param hit_threshold: Threshold for the distance between features and SVM classifying plane. Usually it's 0 and should be specfied in the detector coefficients (as the last free coefficient), but if the free coefficient is omitted (it's allowed) you can specify it manually here.
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
:param win_stride: Window stride. Must be a multiple of block stride.
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
:param padding: Mock parameter to keep CPU interface compatibility. Must be (0,0).
2011-02-22 21:43:26 +01:00
2011-03-23 11:56:20 +01:00
2011-02-26 12:05:10 +01:00
.. index :: gpu::HOGDescriptor::detectMultiScale
2011-02-22 21:43:26 +01:00
2011-03-23 11:56:20 +01:00
gpu::HOGDescriptor::detectMultiScale
2011-02-22 21:43:26 +01:00
----------------------------------------
2011-03-23 11:56:20 +01:00
.. cpp:function :: void gpu::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-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
Perfroms object detection with multiscale window.
2011-02-22 21:43:26 +01:00
2011-03-23 11:56:20 +01:00
:param img: Source image. See :cpp:func:`gpu::HOGDescriptor::detect` for type limitations.
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
:param found_locations: Will contain detected objects boundaries.
2011-02-22 21:43:26 +01:00
2011-03-23 11:56:20 +01:00
:param hit_threshold: The threshold for the distance between features and SVM classifying plane. See :cpp:func:`gpu::HOGDescriptor::detect` for details.
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
:param win_stride: Window stride. Must be a multiple of block stride.
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
:param padding: Mock parameter to keep CPU interface compatibility. Must be (0,0).
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
:param scale0: Coefficient of the detection window increase.
2011-02-22 21:43:26 +01:00
2011-03-23 11:56:20 +01:00
:param group_threshold: After detection some objects could be covered by many rectangles. This coefficient regulates similarity threshold. 0 means don't perform grouping. See :c:func:`groupRectangles`.
2011-02-26 12:05:10 +01:00
.. index :: gpu::HOGDescriptor::getDescriptors
2011-02-22 21:43:26 +01:00
2011-03-23 11:56:20 +01:00
gpu::HOGDescriptor::getDescriptors
2011-02-22 21:43:26 +01:00
--------------------------------------
2011-03-23 11:56:20 +01:00
.. cpp:function :: void gpu::HOGDescriptor::getDescriptors(const GpuMat& img, Size win_stride, GpuMat& descriptors, int descr_format=DESCR_FORMAT_COL_BY_COL)
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
Returns block descriptors computed for the whole image. It's mainly used for classifier learning purposes.
2011-02-22 21:43:26 +01:00
2011-03-23 11:56:20 +01:00
:param img: Source image. See :cpp:func:`gpu::HOGDescriptor::detect` for type limitations.
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
:param win_stride: Window stride. Must be a multiple of block stride.
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
:param descriptors: 2D array of descriptors.
2011-02-22 21:43:26 +01:00
2011-02-28 22:26:43 +01:00
:param descr_format: Descriptor storage format:
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
* **DESCR_FORMAT_ROW_BY_ROW** Row-major order.
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
* **DESCR_FORMAT_COL_BY_COL** Column-major order.
2011-02-28 22:26:43 +01:00
2011-02-22 21:43:26 +01:00
2011-03-23 11:56:20 +01:00
.. index :: gpu::CascadeClassifier_GPU
2011-02-22 21:43:26 +01:00
gpu::CascadeClassifier_GPU
--------------------------
2011-03-23 11:56:20 +01:00
.. cpp:class :: gpu::CascadeClassifier_GPU
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
The cascade classifier class for object detection. ::
2011-02-22 21:43:26 +01:00
2011-03-23 11:56:20 +01:00
class CascadeClassifier_GPU
2011-02-26 12:05:10 +01:00
{
public:
2011-03-23 11:56:20 +01:00
CascadeClassifier_GPU();
CascadeClassifier_GPU(const string& filename);
~CascadeClassifier_GPU();
2011-02-22 21:43:26 +01:00
2011-03-23 11:56:20 +01:00
bool empty() const;
bool load(const string& filename);
void release();
2011-02-22 21:43:26 +01:00
2011-03-23 11:56:20 +01:00
/* returns number of detected objects * /
int detectMultiScale( const GpuMat& image, GpuMat& objectsBuf, double scaleFactor=1.2, int minNeighbors=4, Size minSize=Size());
2011-02-22 21:43:26 +01:00
2011-03-23 11:56:20 +01:00
/* Finds only the largest object. Special mode for need to training* /
bool findLargestObject;
2011-02-22 21:43:26 +01:00
2011-03-23 11:56:20 +01:00
/* Draws rectangles in input image * /
bool visualizeInPlace;
2011-02-22 21:43:26 +01:00
2011-03-23 11:56:20 +01:00
Size getClassifierSize() const;
2011-02-26 12:05:10 +01:00
};
2011-02-28 22:26:43 +01:00
2011-02-22 21:43:26 +01:00
2011-03-23 11:56:20 +01:00
.. index :: gpu::CascadeClassifier_GPU::CascadeClassifier_GPU
2011-02-22 21:43:26 +01:00
2011-03-23 11:56:20 +01:00
gpu::CascadeClassifier_GPU::CascadeClassifier_GPU
2011-02-22 21:43:26 +01:00
-----------------------------------------------------
2011-03-23 11:56:20 +01:00
.. cpp:function :: gpu::CascadeClassifier_GPU::CascadeClassifier_GPU(const string& filename)
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
Loads the classifier from file.
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
:param filename: Name of file from which classifier will be load. Only old haar classifier (trained by haartraining application) and NVidia's nvbin are supported.
2011-02-22 21:43:26 +01:00
2011-03-23 11:56:20 +01:00
.. index :: gpu::CascadeClassifier_GPU::empty
gpu::CascadeClassifier_GPU::empty
2011-02-22 21:43:26 +01:00
-------------------------------------
2011-03-23 11:56:20 +01:00
.. cpp:function :: bool gpu::CascadeClassifier_GPU::empty() const
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
Checks if the classifier has been loaded or not.
2011-02-22 21:43:26 +01:00
2011-03-23 11:56:20 +01:00
.. index :: cv::gpu::CascadeClassifier_GPU::load
gpu::CascadeClassifier_GPU::load
2011-02-22 21:43:26 +01:00
------------------------------------
2011-03-23 11:56:20 +01:00
.. cpp:function :: bool gpu::CascadeClassifier_GPU::load(const string\& filename)
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
Loads the classifier from file. The previous content is destroyed.
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
:param filename: Name of file from which classifier will be load. Only old haar classifier (trained by haartraining application) and NVidia's nvbin are supported.
2011-02-22 21:43:26 +01:00
2011-03-23 11:56:20 +01:00
.. index :: gpu::CascadeClassifier_GPU::release
gpu::CascadeClassifier_GPU::release
2011-02-22 21:43:26 +01:00
---------------------------------------
2011-03-23 11:56:20 +01:00
.. cpp:function :: void gpu::CascadeClassifier_GPU::release()
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
Destroys loaded classifier.
2011-02-22 21:43:26 +01:00
2011-03-23 11:56:20 +01:00
.. index :: gpu::CascadeClassifier_GPU::detectMultiScale
gpu::CascadeClassifier_GPU::detectMultiScale
2011-02-26 12:05:10 +01:00
------------------------------------------------
2011-03-23 11:56:20 +01:00
.. cpp:function :: int gpu::CascadeClassifier_GPU::detectMultiScale(const GpuMat& image, GpuMat& objectsBuf, double scaleFactor=1.2, int minNeighbors=4, Size minSize=Size())
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
Detects objects of different sizes in the input image. The detected objects are returned as a list of rectangles.
2011-02-22 21:43:26 +01:00
2011-03-23 11:56:20 +01:00
:param image: Matrix of type ``CV_8U`` containing the image in which to detect objects.
2011-02-22 21:43:26 +01:00
2011-03-23 11:56:20 +01:00
:param objects: Buffer to store detected objects (rectangles). If it is empty, it will be allocated with default size. If not empty, function will search not more than N objects, where ``N = sizeof(objectsBufer's data)/sizeof(cv::Rect)``.
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
:param scaleFactor: Specifies how much the image size is reduced at each image scale.
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
:param minNeighbors: Specifies how many neighbors should each candidate rectangle have to retain it.
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
:param minSize: The minimum possible object size. Objects smaller than that are ignored.
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
The function returns number of detected objects, so you can retrieve them as in following example: ::
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
cv::gpu::CascadeClassifier_GPU cascade_gpu(...);
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
Mat image_cpu = imread(...)
GpuMat image_gpu(image_cpu);
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
GpuMat objbuf;
int detections_number = cascade_gpu.detectMultiScale( image_gpu,
objbuf, 1.2, minNeighbors);
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
Mat obj_host;
// download only detected number of rectangles
objbuf.colRange(0, detections_number).download(obj_host);
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01: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-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
imshow("Faces", image_cpu);
2011-02-28 22:26:43 +01:00
2011-03-23 11:56:20 +01:00
See also: :c:func: `CascadeClassifier::detectMultiScale` .
2011-02-26 12:05:10 +01:00