Object Detection

gpu::HOGDescriptor

class gpu::HOGDescriptor

The class implements Histogram of Oriented Gradients ([Dalal2005]) object detector.

struct CV_EXPORTS HOGDescriptor
{
    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
}

Interfaces of all methods are kept similar to the CPU HOG descriptor and detector analogues as much as possible.

gpu::HOGDescriptor::HOGDescriptor

Creates the HOG descriptor and detector.

C++: 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)
Parameters:
  • win_size – Detection window size. Align to block size and block stride.
  • block_size – Block size in pixels. Align to cell size. Only (16,16) is supported for now.
  • block_stride – Block stride. It must be a multiple of cell size.
  • cell_size – Cell size. Only (8, 8) is supported for now.
  • nbins – Number of bins. Only 9 bins per cell are supported for now.
  • win_sigma – Gaussian smoothing window parameter.
  • threshold_L2hys – L2-Hys normalization method shrinkage.
  • gamma_correction – Flag to specify whether the gamma correction preprocessing is required or not.
  • nlevels – Maximum number of detection window increases.

gpu::HOGDescriptor::getDescriptorSize

Returns the number of coefficients required for the classification.

C++: size_t gpu::HOGDescriptor::getDescriptorSize() const

gpu::HOGDescriptor::getBlockHistogramSize

Returns the block histogram size.

C++: size_t gpu::HOGDescriptor::getBlockHistogramSize() const

gpu::HOGDescriptor::setSVMDetector

Sets coefficients for the linear SVM classifier.

C++: void gpu::HOGDescriptor::setSVMDetector(const vector<float>& detector)

gpu::HOGDescriptor::getDefaultPeopleDetector

Returns coefficients of the classifier trained for people detection (for default window size).

C++: static vector<float> gpu::HOGDescriptor::getDefaultPeopleDetector()

gpu::HOGDescriptor::getPeopleDetector48x96

Returns coefficients of the classifier trained for people detection (for 48x96 windows).

C++: static vector<float> gpu::HOGDescriptor::getPeopleDetector48x96()

gpu::HOGDescriptor::getPeopleDetector64x128

Returns coefficients of the classifier trained for people detection (for 64x128 windows).

C++: static vector<float> gpu::HOGDescriptor::getPeopleDetector64x128()

gpu::HOGDescriptor::detect

Performs object detection without a multi-scale window.

C++: void gpu::HOGDescriptor::detect(const GpuMat& img, vector<Point>& found_locations, double hit_threshold=0, Size win_stride=Size(), Size padding=Size())
Parameters:
  • img – Source image. CV_8UC1 and CV_8UC4 types are supported for now.
  • found_locations – Left-top corner points of detected objects boundaries.
  • 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.
  • win_stride – Window stride. It must be a multiple of block stride.
  • padding – Mock parameter to keep the CPU interface compatibility. It must be (0,0).

gpu::HOGDescriptor::detectMultiScale

Performs object detection with a multi-scale window.

C++: 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)
Parameters:
  • img – Source image. See gpu::HOGDescriptor::detect() for type limitations.
  • found_locations – Detected objects boundaries.
  • hit_threshold – Threshold for the distance between features and SVM classifying plane. See gpu::HOGDescriptor::detect() for details.
  • win_stride – Window stride. It must be a multiple of block stride.
  • padding – Mock parameter to keep the CPU interface compatibility. It must be (0,0).
  • scale0 – Coefficient of the detection window increase.
  • 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 groupRectangles() .

gpu::HOGDescriptor::getDescriptors

Returns block descriptors computed for the whole image.

C++: void gpu::HOGDescriptor::getDescriptors(const GpuMat& img, Size win_stride, GpuMat& descriptors, int descr_format=DESCR_FORMAT_COL_BY_COL)
Parameters:
  • img – Source image. See gpu::HOGDescriptor::detect() for type limitations.
  • win_stride – Window stride. It must be a multiple of block stride.
  • descriptors – 2D array of descriptors.
  • descr_format

    Descriptor storage format:

    • DESCR_FORMAT_ROW_BY_ROW - Row-major order.
    • DESCR_FORMAT_COL_BY_COL - Column-major order.

The function is mainly used to learn the classifier.

gpu::CascadeClassifier_GPU

class gpu::CascadeClassifier_GPU

Cascade classifier class used for object detection.

class CV_EXPORTS CascadeClassifier_GPU
{
public:
        CascadeClassifier_GPU();
        CascadeClassifier_GPU(const string& filename);
        ~CascadeClassifier_GPU();

        bool empty() const;
        bool load(const string& filename);
        void release();

        /* Returns number of detected objects */
        int detectMultiScale( const GpuMat& image, GpuMat& objectsBuf, double scaleFactor=1.2, int minNeighbors=4, Size minSize=Size());

        /* Finds only the largest object. Special mode if training is required.*/
        bool findLargestObject;

        /* Draws rectangles in input image */
        bool visualizeInPlace;

        Size getClassifierSize() const;
};

gpu::CascadeClassifier_GPU::CascadeClassifier_GPU

Loads the classifier from a file.

C++: gpu::CascadeClassifier_GPU(const string& filename)
Parameters:
  • 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.

gpu::CascadeClassifier_GPU::empty

Checks whether the classifier is loaded or not.

C++: bool gpu::CascadeClassifier_GPU::empty() const

gpu::CascadeClassifier_GPU::load

Loads the classifier from a file. The previous content is destroyed.

C++: bool gpu::CascadeClassifier_GPU::load(const string& filename)
Parameters:
  • 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.

gpu::CascadeClassifier_GPU::release

Destroys the loaded classifier.

C++: void gpu::CascadeClassifier_GPU::release()

gpu::CascadeClassifier_GPU::detectMultiScale

Detects objects of different sizes in the input image.

C++: int gpu::CascadeClassifier_GPU::detectMultiScale(const GpuMat& image, GpuMat& objectsBuf, double scaleFactor=1.2, int minNeighbors=4, Size minSize=Size())
Parameters:
  • image – Matrix of type CV_8U containing an image where objects should be detected.
  • 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).
  • scaleFactor – Value to specify how much the image size is reduced at each image scale.
  • minNeighbors – Value to specify how many neighbours each candidate rectangle has to retain.
  • minSize – Minimum possible object size. Objects smaller than that are ignored.

The detected objects are returned as a list of rectangles.

The function returns the number of detected objects, so you can retrieve them as in the following example:

gpu::CascadeClassifier_GPU cascade_gpu(...);

Mat image_cpu = imread(...)
GpuMat image_gpu(image_cpu);

GpuMat objbuf;
int detections_number = cascade_gpu.detectMultiScale( image_gpu,
          objbuf, 1.2, minNeighbors);

Mat obj_host;
// download only detected number of rectangles
objbuf.colRange(0, detections_number).download(obj_host);

Rect* faces = obj_host.ptr<Rect>();
for(int i = 0; i < detections_num; ++i)
   cv::rectangle(image_cpu, faces[i], Scalar(255));

imshow("Faces", image_cpu);
[Dalal2005]Navneet Dalal and Bill Triggs. Histogram of oriented gradients for human detection. 2005.