Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV: How to use HOGDescriptor::detect method?

I have succeeded in tracking moving objects in a video. http://ww1.sinaimg.cn/mw690/63a9e398jw1ecn39e3togj20jv0g1gnv.jpg

However I want to decide if an object is person or not. I have tried the HOGDescriptor in OpenCV. HOGDescriptor have two methods for detecting people: HOGDescriptor::detect, and HOGDescriptor::detectMultiScale. OpenCV "sources\samples\cpp\peopledetect.cpp" demonstrates how to use HOGDescriptor::detectMultiScale , which search around the image at different scale and is very slow.

In my case, I have tracked the objects in a rectangle. I think using HOGDescriptor::detect to detect the inside of the rectangle will be much more quickly. But the OpenCV document only have the gpu::HOGDescriptor::detect (I still can't guess how to use it) and missed HOGDescriptor::detect. I want to use HOGDescriptor::detect.

Could anyone provide me with some c++ code snippet demonstrating the usage of HOGDescriptor::detect?

thanks.

like image 410
gouchaoer Avatar asked Jan 17 '14 18:01

gouchaoer


2 Answers

Since you already have a list of objects, you can call the HOGDescriptor::detect method for all objects and check the output foundLocations array. If it is not empty the object was classified as a person. The only thing is that HOG works with 64x128 windows by default, so you need to rescale your objects:

std::vector<cv::Rect> movingObjects = ...;

cv::HOGDescriptor hog;
hog.setSVMDetector(cv::HOGDescriptor::getDefaultPeopleDetector());
std::vector<cv::Point> foundLocations;
for (size_t i = 0; i < movingObjects.size(); ++i)
{
    cv::Mat roi = image(movingObjects[i]);
    cv::Mat window;
    cv::resize(roi, window, cv::Size(64, 128));
    hog.detect(window, foundLocations);
    if (!foundLocations.empty())
    {
        // movingObjects[i] is a person
    }
}
like image 197
jet47 Avatar answered Nov 11 '22 21:11

jet47


  • If you don't cmake OpenCV with CUDA enabled, calling gpu::HOGDescriptor::detect will be equal to call HOGDescriptor::detect. No GPU is called.

  • Also for code, you can use

    GpuMat img;
    vector<Point> found_locations;
    gpu::HOGDescriptor::detect(img, found_locations);
    if(!found_locations.empty())
    {
        // img contains/is a real person 
    }
    

Edit:

However I want to decide if an object is person or not.

I don't think that you need this step. HOGDescriptor::detect itself is used to detect people, so you don't need to verify them as they are supposed to be people according to your setup. On the other hand, you can setup its threshold to control its detected quality.

like image 1
herohuyongtao Avatar answered Nov 11 '22 21:11

herohuyongtao