Darwin  1.10(beta)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
drwnHOGFeatures.h
1 /*****************************************************************************
2 ** DARWIN: A FRAMEWORK FOR MACHINE LEARNING RESEARCH AND DEVELOPMENT
3 ** Distributed under the terms of the BSD license (see the LICENSE file)
4 ** Copyright (c) 2007-2015, Stephen Gould
5 ** All rights reserved.
6 **
7 ******************************************************************************
8 ** FILENAME: drwnHOGFeatures.h
9 ** AUTHOR(S): Stephen Gould <stephen.gould@anu.edu.au>
10 **
11 *****************************************************************************/
12 
13 #pragma once
14 
15 #include <vector>
16 
17 #include "cv.h"
18 
19 // drwnHOGNormalization -----------------------------------------------------
21 
22 typedef enum _drwnHOGNormalization {
23  DRWN_HOG_L2_NORM,
24  DRWN_HOG_L1_NORM,
25  DRWN_HOG_L1_SQRT
26 } drwnHOGNormalization;
27 
28 // drwnHOGFeatures ----------------------------------------------------------
38 
40  public:
41  static int DEFAULT_CELL_SIZE;
42  static int DEFAULT_BLOCK_SIZE;
43  static int DEFAULT_BLOCK_STEP;
44  static int DEFAULT_ORIENTATIONS;
45  static drwnHOGNormalization DEFAULT_NORMALIZATION;
46  static double DEFAULT_CLIPPING_LB;
47  static double DEFAULT_CLIPPING_UB;
48  static bool DEFAULT_DIM_REDUCTION;
49 
50  protected:
51  int _cellSize;
52  int _blockSize;
53  int _blockStep;
55  drwnHOGNormalization _normalization;
56  pair<double, double> _clipping;
58 
59  public:
61  virtual ~drwnHOGFeatures();
62 
64  inline int numFeatures() const;
66  inline cv::Size numCells(const cv::Size& imgSize) const;
68  inline cv::Size numBlocks(const cv::Size& imgSize) const;
70  inline cv::Size padImageSize(const cv::Size& imgSize) const;
71 
73  pair<cv::Mat, cv::Mat> gradientMagnitudeAndOrientation(const cv::Mat& img) const;
74 
77  void computeFeatures(const cv::Mat& img, std::vector<cv::Mat>& features);
80  void computeFeatures(const pair<cv::Mat, cv::Mat>& gradMagAndOri, std::vector<cv::Mat>& features);
81 
84  void computeDenseFeatures(const cv::Mat& img, std::vector<cv::Mat>& features);
85 
87  cv::Mat visualizeCells(const cv::Mat& img, int scale = 2);
88 
89  protected:
90  void computeCanonicalOrientations(vector<float>& x, vector<float>& y) const;
91  void computeCellHistograms(const pair<cv::Mat, cv::Mat>& gradMagAndOri, vector<cv::Mat>& cellHistorgams) const;
92  void computeBlockFeatures(const vector<cv::Mat>& cellHistograms, vector<cv::Mat>& features) const;
93  void normalizeFeatureVectors(std::vector<cv::Mat>& features) const;
94  void clipFeatureVectors(std::vector<cv::Mat>& features) const;
95 };
96 
97 // drwnHOGFeatures inline functions -----------------------------------------
98 
99 inline int drwnHOGFeatures::numFeatures() const
100 {
103 }
104 
105 inline cv::Size drwnHOGFeatures::numCells(const cv::Size& imgSize) const
106 {
107  return cv::Size((int)((imgSize.width + _cellSize - 1) / _cellSize),
108  (int)((imgSize.height + _cellSize - 1) / _cellSize));
109 }
110 
111 inline cv::Size drwnHOGFeatures::numBlocks(const cv::Size& imgSize) const
112 {
113  cv::Size nCells = numCells(imgSize);
114  return cv::Size(nCells.width - _blockSize + 1, nCells.height - _blockSize + 1);
115 }
116 
117 inline cv::Size drwnHOGFeatures::padImageSize(const cv::Size& imgSize) const
118 {
119  return cv::Size((int)((imgSize.width + _cellSize - 1) / _cellSize) * _cellSize,
120  (int)((imgSize.height + _cellSize - 1) / _cellSize) * _cellSize);
121 }
static int DEFAULT_ORIENTATIONS
default number of quantized orientations
Definition: drwnHOGFeatures.h:44
int numFeatures() const
returns the number of features (numBlocks times _numOrientations)
Definition: drwnHOGFeatures.h:99
void computeFeatures(const cv::Mat &img, std::vector< cv::Mat > &features)
feature calculation from greyscale image returns features as a vector of matrices of size numBlocks ...
Definition: drwnHOGFeatures.cpp:96
void computeDenseFeatures(const cv::Mat &img, std::vector< cv::Mat > &features)
compute features at each pixel location returns features as a vector of images the same size as the o...
Definition: drwnHOGFeatures.cpp:172
cv::Size padImageSize(const cv::Size &imgSize) const
returns the size of the padded (enlarged) image over which features are computed
Definition: drwnHOGFeatures.h:117
static double DEFAULT_CLIPPING_UB
default upper-bound clipping in (0, 1]
Definition: drwnHOGFeatures.h:47
int _cellSize
size of each cell in pixels
Definition: drwnHOGFeatures.h:51
pair< cv::Mat, cv::Mat > gradientMagnitudeAndOrientation(const cv::Mat &img) const
pre-process gradient magnitude and orientation (can be provided to computeFeatures) ...
Definition: drwnHOGFeatures.cpp:52
static drwnHOGNormalization DEFAULT_NORMALIZATION
default normalization method
Definition: drwnHOGFeatures.h:45
cv::Size numBlocks(const cv::Size &imgSize) const
returns the size of the feature maps in terms of blocks
Definition: drwnHOGFeatures.h:111
cv::Size numCells(const cv::Size &imgSize) const
returns the size of the feature maps in terms of cells
Definition: drwnHOGFeatures.h:105
int _numOrientations
number of orientations in histogram
Definition: drwnHOGFeatures.h:54
static int DEFAULT_CELL_SIZE
default cell size (in pixels)
Definition: drwnHOGFeatures.h:41
static bool DEFAULT_DIM_REDUCTION
true for analytic dimensionality reduction
Definition: drwnHOGFeatures.h:48
bool _bDimReduction
use dimensionality reduction trick of Felzenszwalb et al, PAMI 2010
Definition: drwnHOGFeatures.h:57
Encapsulates histogram-of-gradient (HOG) feature computation.
Definition: drwnHOGFeatures.h:39
int _blockSize
number of cells in a block
Definition: drwnHOGFeatures.h:52
static int DEFAULT_BLOCK_STEP
default block increment (in cells)
Definition: drwnHOGFeatures.h:43
pair< double, double > _clipping
clipping for renormalization (0.0, 1.0 means none)
Definition: drwnHOGFeatures.h:56
static int DEFAULT_BLOCK_SIZE
default block size (in cells)
Definition: drwnHOGFeatures.h:42
cv::Mat visualizeCells(const cv::Mat &img, int scale=2)
visualization
Definition: drwnHOGFeatures.cpp:229
drwnHOGNormalization _normalization
normalization method
Definition: drwnHOGFeatures.h:55
int _blockStep
step to next block in cells
Definition: drwnHOGFeatures.h:53
static double DEFAULT_CLIPPING_LB
default lower-bound clipping in [0, 1)
Definition: drwnHOGFeatures.h:46