1 // This file is part of OpenCV project. 2 // It is subject to the license terms in the LICENSE file found in the top-level directory 3 // of this distribution and at http://opencv.org/license.html. 4 5 #ifndef OPENCV_VIDEO_DETAIL_TRACKING_FEATURE_HPP 6 #define OPENCV_VIDEO_DETAIL_TRACKING_FEATURE_HPP 7 8 #include "opencv2/core.hpp" 9 #include "opencv2/imgproc.hpp" 10 11 /* 12 * TODO This implementation is based on apps/traincascade/ 13 * TODO Changed CvHaarEvaluator based on ADABOOSTING implementation (Grabner et al.) 14 */ 15 16 namespace cv { 17 namespace detail { 18 inline namespace tracking { 19 20 //! @addtogroup tracking_detail 21 //! @{ 22 23 inline namespace feature { 24 25 class CvParams 26 { 27 public: 28 CvParams(); ~CvParams()29 virtual ~CvParams() 30 { 31 } 32 }; 33 34 class CvFeatureParams : public CvParams 35 { 36 public: 37 enum FeatureType 38 { 39 HAAR = 0, 40 LBP = 1, 41 HOG = 2 42 }; 43 44 CvFeatureParams(); 45 static Ptr<CvFeatureParams> create(CvFeatureParams::FeatureType featureType); 46 int maxCatCount; // 0 in case of numerical features 47 int featSize; // 1 in case of simple features (HAAR, LBP) and N_BINS(9)*N_CELLS(4) in case of Dalal's HOG features 48 int numFeatures; 49 }; 50 51 class CvFeatureEvaluator 52 { 53 public: ~CvFeatureEvaluator()54 virtual ~CvFeatureEvaluator() 55 { 56 } 57 virtual void init(const CvFeatureParams* _featureParams, int _maxSampleCount, Size _winSize); 58 virtual void setImage(const Mat& img, uchar clsLabel, int idx); 59 static Ptr<CvFeatureEvaluator> create(CvFeatureParams::FeatureType type); 60 getNumFeatures() const61 int getNumFeatures() const 62 { 63 return numFeatures; 64 } getMaxCatCount() const65 int getMaxCatCount() const 66 { 67 return featureParams->maxCatCount; 68 } getFeatureSize() const69 int getFeatureSize() const 70 { 71 return featureParams->featSize; 72 } getCls() const73 const Mat& getCls() const 74 { 75 return cls; 76 } getCls(int si) const77 float getCls(int si) const 78 { 79 return cls.at<float>(si, 0); 80 } 81 82 protected: 83 virtual void generateFeatures() = 0; 84 85 int npos, nneg; 86 int numFeatures; 87 Size winSize; 88 CvFeatureParams* featureParams; 89 Mat cls; 90 }; 91 92 class CvHaarFeatureParams : public CvFeatureParams 93 { 94 public: 95 CvHaarFeatureParams(); 96 bool isIntegral; 97 }; 98 99 class CvHaarEvaluator : public CvFeatureEvaluator 100 { 101 public: 102 class FeatureHaar 103 { 104 105 public: 106 FeatureHaar(Size patchSize); 107 bool eval(const Mat& image, Rect ROI, float* result) const; getNumAreas() const108 inline int getNumAreas() const { return m_numAreas; } getWeights() const109 inline const std::vector<float>& getWeights() const { return m_weights; } getAreas() const110 inline const std::vector<Rect>& getAreas() const { return m_areas; } 111 112 private: 113 int m_type; 114 int m_numAreas; 115 std::vector<float> m_weights; 116 float m_initMean; 117 float m_initSigma; 118 void generateRandomFeature(Size imageSize); 119 float getSum(const Mat& image, Rect imgROI) const; 120 std::vector<Rect> m_areas; // areas within the patch over which to compute the feature 121 cv::Size m_initSize; // size of the patch used during training 122 cv::Size m_curSize; // size of the patches currently under investigation 123 float m_scaleFactorHeight; // scaling factor in vertical direction 124 float m_scaleFactorWidth; // scaling factor in horizontal direction 125 std::vector<Rect> m_scaleAreas; // areas after scaling 126 std::vector<float> m_scaleWeights; // weights after scaling 127 }; 128 129 virtual void init(const CvFeatureParams* _featureParams, int _maxSampleCount, Size _winSize) CV_OVERRIDE; 130 virtual void setImage(const Mat& img, uchar clsLabel = 0, int idx = 1) CV_OVERRIDE; getFeatures() const131 inline const std::vector<CvHaarEvaluator::FeatureHaar>& getFeatures() const { return features; } getFeatures(int idx)132 inline CvHaarEvaluator::FeatureHaar& getFeatures(int idx) 133 { 134 return features[idx]; 135 } setWinSize(Size patchSize)136 inline void setWinSize(Size patchSize) { winSize = patchSize; } getWinSize() const137 inline Size getWinSize() const { return winSize; } 138 virtual void generateFeatures() CV_OVERRIDE; 139 140 /** 141 * \brief Overload the original generateFeatures in order to limit the number of the features 142 * @param numFeatures Number of the features 143 */ 144 virtual void generateFeatures(int numFeatures); 145 146 protected: 147 bool isIntegral; 148 149 /* TODO Added from MIL implementation */ 150 Mat _ii_img; compute_integral(const cv::Mat & img,std::vector<cv::Mat_<float>> & ii_imgs)151 void compute_integral(const cv::Mat& img, std::vector<cv::Mat_<float>>& ii_imgs) 152 { 153 Mat ii_img; 154 integral(img, ii_img, CV_32F); 155 split(ii_img, ii_imgs); 156 } 157 158 std::vector<FeatureHaar> features; 159 Mat sum; /* sum images (each row represents image) */ 160 }; 161 162 } // namespace feature 163 164 //! @} 165 166 }}} // namespace cv::detail::tracking 167 168 #endif 169