Ask Your Question

HoG detectMultiScale API not able to detect exact object

asked 2017-11-27 00:51:54 -0600

Mark gravatar image

updated 2017-11-28 04:53:55 -0600

Hello Sir,

I want detect object from image. Study Hog from

Source image with different scale

C:\fakepath\AppService.png C:\fakepath\filename=4075.png C:\fakepath\filename=12068.png C:\fakepath\filename=12936.png C:\fakepath\filename=31908.png

2)Create 30 negative image. Please see below images as a some negative sample.


3)Using this example for train and test HOG I modify main function only to give fix path for above image .

int main()
    const char* keys =
        "{help h|     | show help message}"
        "{pd    |     | path of directory contains positive images}"
        "{nd    |     | path of directory contains negative images}"
        "{td    |     | path of directory contains test images}"
        "{tv    |     | test video file name}"
        "{dw    |     | width of the detector}"
        "{dh    |     | height of the detector}"
        "{d     |false| train twice}"
        "{t     |false| test a trained detector}"
        "{v     |false| visualize training steps}"
        "{fn    |my_detector.yml| file name of trained SVM}"

    string pos_dir = "D:\\HogImage\\Positive ";
    string neg_dir = "D:\\HogImage\\Negative";

    //String pos_dir = parser.get< String >("pd");
    //String neg_dir = parser.get< String >("nd");
    String test_dir = "D:\\1\\P";// parser.get< String >("td");
    String obj_det_filename = "D:\\1\\de.ylm";// parser.get< String >("fn");
    int detector_width = 80;// 83;
    int detector_height = 16;
    bool test_detector = false;  //parser.get< bool >("t");
    bool train_twice = true;// parser.get< bool >("d");
    bool visualization = true;// parser.get< bool >("v");
    string videofilename = "D:\\1\\sa.png";
    if (test_detector)
        test_trained_detector(obj_det_filename, test_dir, videofilename);
        return 0;

    if (pos_dir.empty() || neg_dir.empty())

    vector< Mat > pos_lst, full_neg_lst, neg_lst, gradient_lst;
    vector< int > labels;

    clog << "Positive images are being loaded...";
    load_images(pos_dir, pos_lst, visualization);

    if (pos_lst.size() > 0)
        clog << "...[done]" << endl;
        clog << "no image in " << pos_dir << endl;
        return 1;

    Size pos_image_size = pos_lst[0].size();

    for (size_t i = 0; i < pos_lst.size(); ++i)
        if (pos_lst[i].size() != pos_image_size)
            cout << "All positive images should be same size!" << endl;

    pos_image_size = pos_image_size / 8 * 8;

    if (detector_width && detector_height)
        pos_image_size = Size(detector_width, detector_height);

    labels.assign(pos_lst.size(), +1);
    const unsigned int old = (unsigned int)labels.size();

    clog << "Negative images are being loaded...";
    load_images(neg_dir, full_neg_lst, false);
    sample_negex(full_neg_lst, neg_lst, pos_image_size);
    clog << "...[done]" << endl;

    labels.insert(labels.end(), neg_lst.size(), -1);
    CV_Assert(old < labels.size());

    clog << "Histogram of Gradients are being calculated for positive images...";
    computeHOGs(pos_image_size, pos_lst, gradient_lst);
    clog << "...[done]" << endl;

    clog << "Histogram of Gradients are being calculated for negative images...";
    computeHOGs(pos_image_size, neg_lst, gradient_lst);
    clog << "...[done]" << endl;

    Mat train_data;
    convert_to_mlex(gradient_lst, train_data);

    clog << "Training SVM...";
    Ptr< SVM > svm = SVM::create();
    /* Default ...
edit retag flag offensive close merge delete



you probably misunderstood, that "negative" means not showing the object, yours are just inverted positives.

also, this would need like 10x the data for successful training (and i have some doubt, if it can be trained on text like this at all)

berak gravatar imageberak ( 2017-11-27 01:00:08 -0600 )edit

i'd rather try to upscale your template by a factor of 2.5, and use plain templateMatching

berak gravatar imageberak ( 2017-11-27 04:07:59 -0600 )edit

templateMatching found only matching one image and it is time consuming

Mark gravatar imageMark ( 2017-11-28 05:01:57 -0600 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2017-11-27 11:25:00 -0600

i am not sure the HOG detector is the right tool for your desired detection but i made a trial training (you can find details in my test repo)

in beginning i tried to use your images but i can't get a successful detector i think positive images should be larger

test result

image description

edit flag offensive delete link more


Thanks It working with match Weights >0.80 and able to give correct result. It is not working different scaling like 50%,75%,125%,150%,175% scale. Screen attached in my question Source image with different scale . It may be chance to multiple occurrence of same image as you shown in your above example. Please let me know how to detect image from scaled image?

Mark gravatar imageMark ( 2017-11-28 04:41:50 -0600 )edit

i did only a simple trial training with a few positive images. to increase accuracy you should prepare more positive images.could you explain your scenario. do you need to detect only "App Service" text ?

sturkmen gravatar imagesturkmen ( 2017-11-28 05:10:46 -0600 )edit

Thanks Sir, Yes I want to capture only "App Service" image. My aim is that my selected ("App Service") image should be detected from any image containing it, irrespective of its scaling or resolution.

Mark gravatar imageMark ( 2017-11-28 05:52:55 -0600 )edit

Can you guide me at where i am missing?

Mark gravatar imageMark ( 2017-11-28 23:03:23 -0600 )edit

Please give me some hint,how to get correct result?

Mark gravatar imageMark ( 2018-01-03 05:58:14 -0600 )edit

Question Tools

1 follower


Asked: 2017-11-27 00:51:54 -0600

Seen: 379 times

Last updated: Nov 28 '17