histogram_of_oriented_gradients.cpp
Go to the documentation of this file.
1 
3 
5  const int cell_size, const int block_per_cell,
6  const int n_bins, const float angle) :
7  CELL(cell_size),
8  BLOCK(block_per_cell),
9  ANGLE(angle),
10  N_BINS(n_bins) {
11  this->BINS_ANGLE = this->ANGLE / this->N_BINS;
12 }
13 
15  const float &angle, int &lower_index, int &higher_index) {
16  float nearest_lower = FLT_MAX;
17  float nearest_higher = FLT_MAX;
18  lower_index = 0;
19  higher_index = 0;
20  for (int i = BINS_ANGLE/2; i < ANGLE; i += BINS_ANGLE) {
21  float distance = abs(angle - i);
22  if (i < angle) {
23  if (distance < nearest_lower) {
24  nearest_lower = distance;
25  lower_index = i;
26  }
27  } else {
28  if (distance < nearest_higher) {
29  nearest_higher = distance;
30  higher_index = i;
31  }
32  }
33  }
34 }
35 
37  const cv::Mat &image, cv::Mat &hog_bins) {
38  cv::Mat xsobel;
39  cv::Mat ysobel;
40  cv::Sobel(image, xsobel, CV_32F, 1, 0, 7);
41  cv::Sobel(image, ysobel, CV_32F, 0, 1, 7);
42  cv::Mat Imag;
43  cv::Mat Iang;
44  cv::cartToPolar(xsobel, ysobel, Imag, Iang, true);
45  cv::add(Iang, cv::Scalar(ANGLE), Iang, Iang < 0);
46  cv::add(Iang, cv::Scalar(-ANGLE), Iang, Iang >= ANGLE);
47  cv::Mat orientation_histogram;
48  for (int j = 0; j < image.rows; j += CELL) {
49  for (int i = 0; i < image.cols; i += CELL) {
50  cv::Rect rect = cv::Rect(i, j, CELL, CELL);
51  if ((rect.x + rect.width <= image.cols) &&
52  (rect.y + rect.height <= image.rows)) {
53  cv::Mat bin = cv::Mat::zeros(1, N_BINS, CV_32F);
54  for (int y = rect.y; y < (rect.y + rect.height); y++) {
55  for (int x = rect.x; x < (rect.x + rect.width); x++) {
56  float angle = static_cast<float>(Iang.at<float>(y, x));
57  int l_bin;
58  int h_bin;
59  this->bilinearBinVoting(angle, l_bin, h_bin);
60  float l_ratio = 1 - (angle - l_bin)/BINS_ANGLE;
61  float h_ratio = 1 + (angle - h_bin)/BINS_ANGLE;
62  int l_index = (l_bin-(BINS_ANGLE/2))/BINS_ANGLE;
63  int h_index = (h_bin-(BINS_ANGLE/2))/BINS_ANGLE;
64  bin.at<float>(0, l_index) +=
65  (Imag.at<float>(y, x) * l_ratio);
66  bin.at<float>(0, h_index) +=
67  (Imag.at<float>(y, x) * h_ratio);
68  }
69  }
70  orientation_histogram.push_back(bin);
71  }
72  }
73  }
74  hog_bins = orientation_histogram.clone();
75 }
76 
78  const int col, const int row, cv::Mat &bins) {
79  cv::Mat block_hogMD = cv::Mat(cv::Size(N_BINS * BLOCK * BLOCK, 1), CV_32F);
80  int icounter = 0;
81  for (int j = row; j < (row + BLOCK); j++) {
82  for (int i = col; i < (col + BLOCK); i++) {
83  int index = (j * CELL) + i;
84  for (int k = 0; k < N_BINS; k++) {
85  block_hogMD.at<float>(0, icounter++) = bins.at<float>(index);
86  }
87  }
88  }
89  return block_hogMD;
90 }
91 
93  const cv::Mat &image, cv::Mat &bins, cv::Mat &featureMD) {
94  for (int j = 0; j < image.rows; j += CELL) {
95  for (int i = 0; i < image.cols; i += CELL) {
96  cv::Rect rect = cv::Rect(i, j, CELL*BLOCK, CELL*BLOCK);
97  if ((rect.x + rect.width <= image.cols) &&
98  (rect.y + rect.height <= image.rows)) {
99  cv::Mat hogMD = this->blockGradient(rect.x, rect.y, bins);
100  normalize(hogMD, hogMD, 1, 0, CV_L2);
101  featureMD.push_back(hogMD);
102  }
103  }
104  }
105 }
106 
108  const cv::Mat &img) {
109  cv::Mat image = img.clone();
110  if (image.type() != CV_8U) {
111  cv::cvtColor(image, image, CV_BGR2GRAY);
112  }
113  cv::Mat bins;
114  this->imageGradient(image, bins);
115  cv::Mat featureMD;
116  getHOG(image, bins, featureMD);
117  featureMD = featureMD.reshape(1, 1);
118  return featureMD;
119 }
120 
121 template<typename T>
123  const cv::Mat &patch, std::vector<cv::Mat> &imageHOG,
124  const int distance_type) {
125  T sum = 0.0;
126  T argMinDistance = FLT_MAX;
127  for (int i = 0; i < imageHOG.size(); i++) {
128  cv::Mat img_hog = imageHOG[i];
129  T d = compareHist(patch, img_hog, distance_type);
130  if (d < argMinDistance) {
131  argMinDistance = static_cast<double>(d);
132  }
133  }
134  sum = static_cast<T>(argMinDistance);
135  return static_cast<T>(sum);
136 }
virtual void bilinearBinVoting(const float &, int &, int &)
d
Definition: setup.py:6
virtual cv::Mat blockGradient(const int, const int, cv::Mat &)
virtual void getHOG(const cv::Mat &, cv::Mat &, cv::Mat &)
T computeHOGHistogramDistances(const cv::Mat &, std::vector< cv::Mat > &, const int=CV_COMP_BHATTACHARYYA)
virtual void imageGradient(const cv::Mat &, cv::Mat &)
pointer T
TFSIMD_FORCE_INLINE tfScalar distance(const Vector3 &v) const
bins
virtual cv::Mat computeHOG(const cv::Mat &)
GLfloat angle
x
y
pointer BLOCK(context *, pointer)
TFSIMD_FORCE_INLINE Vector3 & normalize()
INLINE Rall1d< T, V, S > abs(const Rall1d< T, V, S > &x)
char * index(char *sp, char c)
T sum(T *pf, int length)
HOGFeatureDescriptor(const int=8, const int=2, const int=9, const float=180.0f)


jsk_perception
Author(s): Manabu Saito, Ryohei Ueda
autogenerated on Mon May 3 2021 03:03:27