Direct Graphical Models  v.1.7.0
HOG.cpp
1 #include "HOG.h"
2 #include "Gradient.h"
3 #include "macroses.h"
4 
5 namespace DirectGraphicalModels { namespace fex
6 {
7 Mat CHOG::get(const Mat &img, int nBins, SqNeighbourhood nbhd)
8 {
9  DGM_ASSERT_MSG(nBins < CV_CN_MAX, "Number of bins (%d) exceeds the maximum allowed number (%d)", nBins, CV_CN_MAX);
10 
11  int i; // bins index
12  int x, y;
13  int width = img.cols;
14  int height = img.rows;
15 
16  // Converting to one channel image
17  Mat I;
18  if (img.channels() != 1) cvtColor(img, I, cv::ColorConversionCodes::COLOR_RGB2GRAY);
19  else img.copyTo(I);
20 
21  // Derivatives
22  Mat Ix = CGradient::getDerivativeX(I);
23  Mat Iy = CGradient::getDerivativeY(I);
24 
25  // Initializing bins and integrals
26  vec_mat_t vTemp(nBins);
27  vec_mat_t vBins(nBins);
28  vec_mat_t vInts(nBins);
29 
30  for (i = 0; i < nBins; i++) {
31  vTemp[i].create(img.size(), CV_8UC1);
32  vBins[i].create(img.size(), CV_32FC1);
33  vBins[i].setTo(0);
34  }
35 
36  std::vector<float *> pBins(nBins);
37  std::vector<double *> pInts0(nBins);
38  std::vector<double *> pInts1(nBins);
39  std::vector<byte *> pTemp(nBins);
40 
41  // Caclculating the bins
42  for (y = 0; y < height; y++) {
43  float *pIx = Ix.ptr<float>(y);
44  float *pIy = Iy.ptr<float>(y);
45  for (i = 0; i < nBins; i++) pBins[i] = vBins[i].ptr<float>(y);
46  for (x = 0; x < width; x++) {
47  float ix = pIx[x];
48  float iy = pIy[x];
49 
50  // gradient Magnitude
51  float gMgn = sqrtf(ix*ix + iy*iy);
52 
53  // gradient Orientation
54  if (fabs(ix) < FLT_EPSILON) ix = SIGN(ix) * FLT_EPSILON;
55  float tg = iy / ix;
56  float gOrt = (0.5f + atanf(tg) / (float)Pi) * 180.0f; // [0°; 180°]
57 
58  // filling in the bins
59  float gOrtStep = 180.0f / nBins;
60  for (i = 0; i < nBins; i++)
61  if (gOrt <= (i + 1) * gOrtStep) {
62  pBins[i][x] = gMgn;
63  break;
64  }
65  }
66  }
67 
68  // Calculating the integrals
69  for (i = 0; i < nBins; i++) integral(vBins[i], vInts[i]);
70 
71  for (y = 0; y < height; y++) {
72  int y0 = MAX(0, y - nbhd.upperGap);
73  int y1 = MIN(y + nbhd.lowerGap, height - 1);
74  for (i = 0; i < nBins; i++) pInts0[i] = vInts[i].ptr<double>(y0);
75  for (i = 0; i < nBins; i++) pInts1[i] = vInts[i].ptr<double>(y1 + 1);
76  for (i = 0; i < nBins; i++) pTemp[i] = vTemp[i].ptr<byte>(y);
77  for (x = 0; x < width; x++) {
78  int x0 = MAX(0, x - nbhd.leftGap);
79  int x1 = MIN(x + nbhd.rightGap, width - 1);
80 
81  Mat HOGcell(cv::Size(nBins, 1), CV_64FC1);
82  double *pHOGcell = HOGcell.ptr<double>(0);
83  for (i = 0; i < nBins; i++) pHOGcell[i] = pInts1[i][x1 + 1] - pInts1[i][x0] - pInts0[i][x1 + 1] + pInts0[i][x0];
84  normalize(HOGcell, HOGcell, 255, 0, cv::NormTypes::NORM_MINMAX);
85  for (i = 0; i < nBins; i++) pTemp[i][x] = static_cast<byte>(pHOGcell[i]);
86  HOGcell.release();
87  } // x
88  } // y
89 
90  Mat res;
91  merge(vTemp, res);
92 
93  return res;
94 }
95 } }
int rightGap
Distance from the base point to the neighborhood&#39;s right boundary.
int leftGap
Distance from the base point to the neighborhood&#39;s left boundary.
int lowerGap
Distance from the base point to the neighborhood&#39;s lower boundary.
int upperGap
Distance from the base point to the neighborhood&#39;s upper boundary.
virtual Mat get(void) const
Extracts and returns the required feature.
Definition: HOG.h:25
static Mat getDerivativeY(const Mat &img)
Definition: Gradient.cpp:49
static Mat getDerivativeX(const Mat &img)
Definition: Gradient.cpp:35