1 ///////////// see LICENSE.txt in the OpenCV root directory ////////////// 2 3 #ifndef __OPENCV_XFEATURES2D_SURF_HPP__ 4 #define __OPENCV_XFEATURES2D_SURF_HPP__ 5 6 namespace cv 7 { 8 namespace xfeatures2d 9 { 10 11 //! Speeded up robust features, port from CUDA module. 12 ////////////////////////////////// SURF ////////////////////////////////////////// 13 /*! 14 SURF implementation. 15 16 The class implements SURF algorithm by H. Bay et al. 17 */ 18 class SURF_Impl : public SURF 19 { 20 public: 21 //! the full constructor taking all the necessary parameters 22 explicit CV_WRAP SURF_Impl(double hessianThreshold, 23 int nOctaves = 4, int nOctaveLayers = 2, 24 bool extended = true, bool upright = false); 25 26 //! returns the descriptor size in float's (64 or 128) 27 CV_WRAP int descriptorSize() const CV_OVERRIDE; 28 29 //! returns the descriptor type 30 CV_WRAP int descriptorType() const CV_OVERRIDE; 31 32 //! returns the descriptor type 33 CV_WRAP int defaultNorm() const CV_OVERRIDE; 34 35 void set(int, double); 36 double get(int) const; 37 38 //! finds the keypoints and computes their descriptors. 39 // Optionally it can compute descriptors for the user-provided keypoints 40 void detectAndCompute(InputArray img, InputArray mask, 41 CV_OUT std::vector<KeyPoint>& keypoints, 42 OutputArray descriptors, 43 bool useProvidedKeypoints = false) CV_OVERRIDE; 44 setHessianThreshold(double hessianThreshold_)45 void setHessianThreshold(double hessianThreshold_) CV_OVERRIDE { hessianThreshold = hessianThreshold_; } getHessianThreshold() const46 double getHessianThreshold() const CV_OVERRIDE { return hessianThreshold; } 47 setNOctaves(int nOctaves_)48 void setNOctaves(int nOctaves_) CV_OVERRIDE { nOctaves = nOctaves_; } getNOctaves() const49 int getNOctaves() const CV_OVERRIDE { return nOctaves; } 50 setNOctaveLayers(int nOctaveLayers_)51 void setNOctaveLayers(int nOctaveLayers_) CV_OVERRIDE { nOctaveLayers = nOctaveLayers_; } getNOctaveLayers() const52 int getNOctaveLayers() const CV_OVERRIDE { return nOctaveLayers; } 53 setExtended(bool extended_)54 void setExtended(bool extended_) CV_OVERRIDE { extended = extended_; } getExtended() const55 bool getExtended() const CV_OVERRIDE { return extended; } 56 setUpright(bool upright_)57 void setUpright(bool upright_) CV_OVERRIDE { upright = upright_; } getUpright() const58 bool getUpright() const CV_OVERRIDE { return upright; } 59 60 double hessianThreshold; 61 int nOctaves; 62 int nOctaveLayers; 63 bool extended; 64 bool upright; 65 }; 66 67 #ifdef HAVE_OPENCL 68 class SURF_OCL 69 { 70 public: 71 enum KeypointLayout 72 { 73 X_ROW = 0, 74 Y_ROW, 75 LAPLACIAN_ROW, 76 OCTAVE_ROW, 77 SIZE_ROW, 78 ANGLE_ROW, 79 HESSIAN_ROW, 80 ROWS_COUNT 81 }; 82 83 //! the full constructor taking all the necessary parameters 84 SURF_OCL(); 85 86 bool init(const SURF_Impl* params); 87 88 //! returns the descriptor size in float's (64 or 128) descriptorSize() const89 int descriptorSize() const { return params->extended ? 128 : 64; } 90 91 void uploadKeypoints(const std::vector<KeyPoint> &keypoints, UMat &keypointsGPU); 92 void downloadKeypoints(const UMat &keypointsGPU, std::vector<KeyPoint> &keypoints); 93 94 //! finds the keypoints using fast hessian detector used in SURF 95 //! supports CV_8UC1 images 96 //! keypoints will have nFeature cols and 6 rows 97 //! keypoints.ptr<float>(X_ROW)[i] will contain x coordinate of i'th feature 98 //! keypoints.ptr<float>(Y_ROW)[i] will contain y coordinate of i'th feature 99 //! keypoints.ptr<float>(LAPLACIAN_ROW)[i] will contain laplacian sign of i'th feature 100 //! keypoints.ptr<float>(OCTAVE_ROW)[i] will contain octave of i'th feature 101 //! keypoints.ptr<float>(SIZE_ROW)[i] will contain size of i'th feature 102 //! keypoints.ptr<float>(ANGLE_ROW)[i] will contain orientation of i'th feature 103 //! keypoints.ptr<float>(HESSIAN_ROW)[i] will contain response of i'th feature 104 bool detect(InputArray img, InputArray mask, UMat& keypoints); 105 //! finds the keypoints and computes their descriptors. 106 //! Optionally it can compute descriptors for the user-provided keypoints and recompute keypoints direction 107 bool detectAndCompute(InputArray img, InputArray mask, UMat& keypoints, 108 OutputArray descriptors, bool useProvidedKeypoints = false); 109 110 protected: 111 bool setImage(InputArray img, InputArray mask); 112 113 // kernel callers declarations 114 bool calcLayerDetAndTrace(int octave, int layer_rows); 115 116 bool findMaximaInLayer(int counterOffset, int octave, int layer_rows, int layer_cols); 117 118 bool interpolateKeypoint(int maxCounter, UMat &keypoints, int octave, int layer_rows, int maxFeatures); 119 120 bool calcOrientation(UMat &keypoints); 121 122 bool setUpRight(UMat &keypoints); 123 124 bool computeDescriptors(const UMat &keypoints, OutputArray descriptors); 125 126 bool detectKeypoints(UMat &keypoints); 127 128 const SURF_Impl* params; 129 130 //! max keypoints = min(keypointsRatio * img.size().area(), 65535) 131 UMat sum, intBuffer; 132 UMat det, trace; 133 UMat maxPosBuffer; 134 135 int img_cols, img_rows; 136 137 int maxCandidates; 138 int maxFeatures; 139 140 UMat img, counters; 141 142 // texture buffers 143 ocl::Image2D imgTex, sumTex; 144 bool haveImageSupport; 145 String kerOpts; 146 147 int status; 148 }; 149 #endif // HAVE_OPENCL 150 151 /* 152 template<typename _Tp> void copyVectorToUMat(const std::vector<_Tp>& v, UMat& um) 153 { 154 if(v.empty()) 155 um.release(); 156 else 157 Mat(1, (int)(v.size()*sizeof(v[0])), CV_8U, (void*)&v[0]).copyTo(um); 158 } 159 160 template<typename _Tp> void copyUMatToVector(const UMat& um, std::vector<_Tp>& v) 161 { 162 if(um.empty()) 163 v.clear(); 164 else 165 { 166 size_t sz = um.total()*um.elemSize(); 167 CV_Assert(um.isContinuous() && (sz % sizeof(_Tp) == 0)); 168 v.resize(sz/sizeof(_Tp)); 169 Mat m(um.size(), um.type(), &v[0]); 170 um.copyTo(m); 171 } 172 }*/ 173 174 } 175 } 176 177 #endif 178