2 - c++ wrapper for the piotr toolbox
3 Created by Tomas Vojir, 2014
7 #ifndef FHOG_HEADER_7813784354687
8 #define FHOG_HEADER_7813784354687
11 #include <opencv2/opencv.hpp>
13 #include "gradientMex.h"
19 //description: extract hist. of gradients(use_hog == 0), hog(use_hog == 1) or fhog(use_hog == 2)
20 //input: float one channel image as input, hog type
21 //return: computed descriptor
22 static std::vector<cv::Mat> extract(const cv::Mat & img, int use_hog = 2, int bin_size = 4, int n_orients = 9, int soft_bin = -1, float clip = 0.2)
24 // d image dimension -> gray image d = 1
25 // h, w -> height, width of image
27 // I -> input image, M, O -> mag, orientation OUTPUT
28 int h = img.rows, w = img.cols, d = 1;
31 std::cerr << "I must be at least 2x2." << std::endl;
32 return std::vector<cv::Mat>();
35 // //image rows-by-rows
36 // float * I = new float[h*w];
37 // for (int y = 0; y < h; ++y) {
38 // const float * row_ptr = img.ptr<float>(y);
39 // for (int x = 0; x < w; ++x) {
40 // I[y*w + x] = row_ptr[x];
45 float * I = new float[h*w];
46 for (int x = 0; x < w; ++x) {
47 for (int y = 0; y < h; ++y) {
48 I[x*h + y] = img.at<float>(y, x)/255.f;
52 float *M = new float[h*w], *O = new float[h*w];
53 gradMag(I, M, O, h, w, d, full);
55 int n_chns = (use_hog == 0) ? n_orients : (use_hog==1 ? n_orients*4 : n_orients*3+5);
56 int hb = h/bin_size, wb = w/bin_size;
58 float *H = new float[hb*wb*n_chns];
59 memset(H, 0, hb*wb*n_chns*sizeof(float));
62 full = false; //by default
63 gradHist( M, O, H, h, w, bin_size, n_orients, soft_bin, full );
64 } else if (use_hog == 1) {
65 full = false; //by default
66 hog( M, O, H, h, w, bin_size, n_orients, soft_bin, full, clip );
68 fhog( M, O, H, h, w, bin_size, n_orients, soft_bin, clip );
71 //convert, assuming row-by-row-by-channel storage
72 std::vector<cv::Mat> res;
73 int n_res_channels = (use_hog == 2) ? n_chns-1 : n_chns; //last channel all zeros for fhog
74 res.reserve(n_res_channels);
75 for (int i = 0; i < n_res_channels; ++i) {
77 // cv::Mat desc(hb, wb, CV_32F, (H+hb*wb*i));
80 cv::Mat desc(hb, wb, CV_32F);
81 for (int x = 0; x < wb; ++x) {
82 for (int y = 0; y < hb; ++y) {
83 desc.at<float>(y,x) = H[i*hb*wb + x*hb + y];
87 res.push_back(desc.clone());
101 #endif //FHOG_HEADER_7813784354687