]> rtime.felk.cvut.cz Git - hercules2020/kcf.git/blob - src/piotr_fhog/fhog.hpp
Work done so far on making Scale_var struct
[hercules2020/kcf.git] / src / piotr_fhog / fhog.hpp
1 /*
2     - c++ wrapper for the piotr toolbox
3     Created by Tomas Vojir, 2014
4 */
5
6
7 #ifndef FHOG_HEADER_7813784354687
8 #define FHOG_HEADER_7813784354687
9
10 #include <vector>
11 #include <opencv2/opencv.hpp>
12
13 #include "gradientMex.h"
14 #include "scale_vars.hpp"
15
16 struct Scale_vars;
17
18 class FHoG
19 {
20 public:
21     //description: extract hist. of gradients(use_hog == 0), hog(use_hog == 1) or fhog(use_hog == 2)
22     //input: float one channel image as input, hog type
23     //return: computed descriptor
24     static void extract(const cv::Mat & img, Scale_vars & vars,int use_hog = 2, int bin_size = 4, int n_orients = 9, int soft_bin = -1, float clip = 0.2)
25     {
26         // d image dimension -> gray image d = 1
27         // h, w -> height, width of image
28         // full -> ??
29         // I -> input image, M, O -> mag, orientation OUTPUT
30         int h = img.rows, w = img.cols, d = 1;
31         bool full = true;
32         if (h < 2 || w < 2) {
33             std::cerr << "I must be at least 2x2." << std::endl;
34             return;
35         }
36
37 //        //image rows-by-rows
38 //        float * I = new float[h*w];
39 //        for (int y = 0; y < h; ++y) {
40 //            const float * row_ptr = img.ptr<float>(y);
41 //            for (int x = 0; x < w; ++x) {
42 //                I[y*w + x] = row_ptr[x];
43 //            }
44 //        }
45
46         //image cols-by-cols
47         float * I = new float[h*w];
48         for (int x = 0; x < w; ++x) {
49             for (int y = 0; y < h; ++y) {
50                 I[x*h + y] = img.at<float>(y, x)/255.f;
51             }
52         }
53
54         float *M = new float[h*w], *O = new float[h*w];
55         gradMag(I, M, O, h, w, d, full);
56
57         int n_chns = (use_hog == 0) ? n_orients : (use_hog==1 ? n_orients*4 : n_orients*3+5);
58         int hb = h/bin_size, wb = w/bin_size;
59
60         float *H = new float[hb*wb*n_chns];
61         memset(H, 0, hb*wb*n_chns*sizeof(float));
62
63         if (use_hog == 0) {
64             full = false;   //by default
65             gradHist( M, O, H, h, w, bin_size, n_orients, soft_bin, full );
66         } else if (use_hog == 1) {
67             full = false;   //by default
68             hog( M, O, H, h, w, bin_size, n_orients, soft_bin, full, clip );
69         } else {
70             fhog( M, O, H, h, w, bin_size, n_orients, soft_bin, clip );
71         }
72
73         //convert, assuming row-by-row-by-channel storage
74         int n_res_channels = (use_hog == 2) ? n_chns-1 : n_chns;    //last channel all zeros for fhog
75         vars.patch_feats.clear();
76         for (int i = 0; i < n_res_channels; ++i) {
77             //output rows-by-rows
78 //            cv::Mat desc(hb, wb, CV_32F, (H+hb*wb*i));
79
80             //output cols-by-cols
81             cv::Mat desc(hb, wb, CV_32F);
82             for (int x = 0; x < wb; ++x) {
83                 for (int y = 0; y < hb; ++y) {
84                     desc.at<float>(y,x) = H[i*hb*wb + x*hb + y];
85                 }
86             }
87
88             vars.patch_feats.push_back(desc.clone());
89         }
90
91         //clean
92         delete [] I;
93         delete [] M;
94         delete [] O;
95         delete [] H;
96
97         return;
98     }
99
100 };
101
102 #endif //FHOG_HEADER_7813784354687