From fba71a8ecf0966fb17760e8c6ca12dc12e80ceae Mon Sep 17 00:00:00 2001 From: biagio montesano Date: Sun, 8 Jun 2014 17:34:02 +0200 Subject: [PATCH] Added methods for derivation from FeatureDetector --- .../opencv2/line_descriptor/descriptor.hpp | 85 +++++-- .../line_descriptor/src/BinaryDescriptor.cpp | 211 ++++++++++++++---- 2 files changed, 233 insertions(+), 63 deletions(-) diff --git a/modules/line_descriptor/include/opencv2/line_descriptor/descriptor.hpp b/modules/line_descriptor/include/opencv2/line_descriptor/descriptor.hpp index 3df0b18d8..867a6318e 100644 --- a/modules/line_descriptor/include/opencv2/line_descriptor/descriptor.hpp +++ b/modules/line_descriptor/include/opencv2/line_descriptor/descriptor.hpp @@ -53,18 +53,32 @@ namespace cv { - class CV_EXPORTS_W LineDescriptor : public virtual Algorithm + class CV_EXPORTS_W KeyLine: public KeyPoint { public: - virtual ~LineDescriptor(); - void getLineBinaryDescriptors(cv::Mat &oct_binaryDescMat); + /* lines's extremes in original image */ + float startPointX; + float startPointY; + float endPointX; + float endPointY; + + /* line's extremes in image it was extracted from */ + float sPointInOctaveX; + float sPointInOctaveY; + float ePointInOctaveX; + float ePointInOctaveY; + + /* the length of line */ + float lineLength; + + /* number of pixels covered by the line */ + unsigned int numOfPixels; + - protected: - virtual void getLineBinaryDescriptorsImpl(cv::Mat &oct_binaryDescMat); }; - class CV_EXPORTS_W BinaryDescriptor : public LineDescriptor + class CV_EXPORTS_W BinaryDescriptor : public DescriptorExtractor { public: @@ -92,7 +106,10 @@ namespace cv /* image's reduction ratio in construction of Gaussian pyramids */ CV_PROP_RW int reductionRatio; + /* read parameters from a FileNode object and store them (struct function) */ void read( const FileNode& fn ); + + /* store parameters to a FileStorage object (struct function) */ void write( FileStorage& fs ) const; }; @@ -100,29 +117,63 @@ namespace cv CV_WRAP BinaryDescriptor(const BinaryDescriptor::Params ¶meters = BinaryDescriptor::Params()); + /* read parameters from a FileNode object and store them (class function ) */ virtual void read( const cv::FileNode& fn ); + + /* store parameters to a FileStorage object (class function) */ virtual void write( cv::FileStorage& fs ) const; - void getLineBinaryDescriptors(cv::Mat &oct_binaryDescMat); + /* requires line detection (only one image) */ + CV_WRAP void detect( const Mat& image, + CV_OUT std::vector& keypoints, + const Mat& mask=Mat() ); - protected: - virtual void getLineBinaryDescriptorsImpl(cv::Mat &oct_binaryDescMat); - AlgorithmInfo* info() const; + /* requires line detection (more than one image) */ + void detect( const std::vector& images, + std::vector >& keypoints, + const std::vector& masks=std::vector() ) const; - Params params; + /*return descriptor size */ + int descriptorSize() const = 0; + + /* return data type */ + int descriptorType() const = 0; + + /* return norm mode */ + int defaultNorm() const = 0; + /* check whether Gaussian pyramids were created */ + bool empty() const; + + + protected: + virtual void detectImpl( const Mat& image, + std::vector& keypoints, + const Mat& mask=Mat() ) const = 0; + + AlgorithmInfo* info() const; private: + /* conversion of an LBD descriptor to the decimal equivalent of its binary representation */ unsigned char binaryTest(float* f1, float* f2); + + /* compute LBD descriptors */ int ComputeLBD_(ScaleLines &keyLines); - int OctaveKeyLines(std::vector & octaveImages, ScaleLines &keyLines); + + /* gather lines in groups. + Each group contains the same line, detected in different octaves */ + int OctaveKeyLines(ScaleLines &keyLines); + + /* get coefficients of line passing by two points (in line_extremes) */ void getLineParameters(cv::Vec4i &line_extremes, cv::Vec3i &lineParams); + + /* compute the angle between line and X axis */ float getLineDirection(cv::Vec3i &lineParams); - /* the local gaussian coefficient apply to the orthogonal line direction within each band */ + /* the local gaussian coefficient applied to the orthogonal line direction within each band */ std::vector gaussCoefL_; - /* the global gaussian coefficient apply to each Row within line support region */ + /* the global gaussian coefficient applied to each Row within line support region */ std::vector gaussCoefG_; /* vector to store horizontal and vertical derivatives of octave images */ @@ -134,6 +185,12 @@ namespace cv /* structure to store lines extracted from each octave image */ std::vector > extractedLines; + /* descriptor parameters */ + Params params; + + /* vector to store the Gaussian pyramid od an input image */ + std::vector octaveImages; + }; } diff --git a/modules/line_descriptor/src/BinaryDescriptor.cpp b/modules/line_descriptor/src/BinaryDescriptor.cpp index 399434f32..9bd2918ce 100644 --- a/modules/line_descriptor/src/BinaryDescriptor.cpp +++ b/modules/line_descriptor/src/BinaryDescriptor.cpp @@ -132,7 +132,31 @@ void BinaryDescriptor::write( cv::FileStorage& fs ) const params.write(fs); } -/* get coefficients of line passing by two points in line_extremes */ +/* return norm mode */ +int BinaryDescriptor::defaultNorm() const +{ + return NORM_HAMMING; +} + +/* return data type */ +int BinaryDescriptor::descriptorType() const +{ + return CV_8U; +} + +/*return descriptor size */ +int BinaryDescriptor::descriptorSize() const +{ + return 1; +} + +/* check whether Gaussian pyramids were created */ +bool BinaryDescriptor::empty() const +{ + return true; +} + +/* get coefficients of line passing by two points in (line_extremes) */ void BinaryDescriptor::getLineParameters(cv::Vec4i &line_extremes, cv::Vec3i &lineParams) { int x1 = line_extremes[0]; @@ -185,73 +209,162 @@ float BinaryDescriptor::getLineDirection(cv::Vec3i &lineParams) return atan(m); else - return M_PI - atan(m); + return -atan(m); } } -/* extract lines from an image and compute their descriptors */ -void BinaryDescriptor::getLineBinaryDescriptors(cv::Mat &oct_binaryDescMat) +/* requires line detection (only one image) */ +void BinaryDescriptor::detect( const Mat& image, + CV_OUT std::vector& keypoints, + const Mat& mask) { - /* start function that actually implements descriptors' computation */ - getLineBinaryDescriptorsImpl(oct_binaryDescMat); + /* invoke KeyLines detection */ + detectImpl(image, keypoints, mask); } -/* compute descriptors */ -void BinaryDescriptor::getLineBinaryDescriptorsImpl(cv::Mat &oct_binaryDescMat) +/* requires line detection (more than one image) */ +void BinaryDescriptor::detect( const std::vector& images, + std::vector >& keypoints, + const std::vector& masks ) const { - /* prepare a matrix to store Gaussian pyramid of input matrix */ - std::vector matVec(params.numOfOctave_); + /* detect lines from each image */ + for(size_t counter = 0; counter& keypoints, + const Mat& mask) const +{ + /* reinitialize structures for hosting images, images' derivatives and sizes (they may have been used in the past) */ - dxImg_vector.clear(); - dyImg_vector.clear(); - images_sizes.clear(); - - dxImg_vector.resize(params.numOfOctave_); - dyImg_vector.resize(params.numOfOctave_); - images_sizes.resize(params.numOfOctave_); + BinaryDescriptor *bn = const_cast(this); + bn->dxImg_vector.clear(); + bn->dyImg_vector.clear(); + bn->images_sizes.clear(); + bn->octaveImages.clear(); /* insert input image into pyramid */ - cv::Mat currentMat = oct_binaryDescMat.clone(); - matVec.push_back(currentMat); - images_sizes.push_back(currentMat.size()); - - /* compute and store derivatives of input image */ - cv:Mat currentDx, currentDy; - cv::Sobel( currentMat, currentDx, CV_16SC1, 1, 0, 3); - cv::Sobel( currentMat, currentDy, CV_16SC1, 0, 1, 3); - - dxImg_vector.push_back(currentDx); - dyImg_vector.push_back(currentDy); + cv::Mat currentMat = image.clone(); + bn->octaveImages.push_back(currentMat); + bn->images_sizes.push_back(currentMat.size()); /* fill Gaussian pyramid */ - for(int i = 1; ioctaveImages.push_back(currentMat); + bn->images_sizes.push_back(currentMat.size()); + } - /* compute and store derivatives of new image */ - cv::Sobel( currentMat, currentDx, CV_16SC1, 1, 0, 3); - cv::Sobel( currentMat, currentDy, CV_16SC1, 0, 1, 3); + /* detect and arrange lines across octaves */ + ScaleLines sl; + bn->OctaveKeyLines(sl); - dxImg_vector.push_back(currentDx); - dyImg_vector.push_back(currentDy); + /* fill KeyLines vector */ + for(int i = 0; i<(int)sl.size(); i++) + { + for(size_t j = 0; j matVec(params.numOfOctave_); -} +// /* reinitialize structures for hosting images' derivatives and sizes +// (they may have been used in the past) */ +// dxImg_vector.clear(); +// dyImg_vector.clear(); +// images_sizes.clear(); + +// dxImg_vector.resize(params.numOfOctave_); +// dyImg_vector.resize(params.numOfOctave_); +// images_sizes.resize(params.numOfOctave_); + +// /* insert input image into pyramid */ +// cv::Mat currentMat = oct_binaryDescMat.clone(); +// matVec.push_back(currentMat); +// images_sizes.push_back(currentMat.size()); + +// /* compute and store derivatives of input image */ +// cv:Mat currentDx, currentDy; +// cv::Sobel( currentMat, currentDx, CV_16SC1, 1, 0, 3); +// cv::Sobel( currentMat, currentDy, CV_16SC1, 0, 1, 3); + +// dxImg_vector.push_back(currentDx); +// dyImg_vector.push_back(currentDy); + +// /* fill Gaussian pyramid */ +// for(int i = 1; i & octaveImages, ScaleLines &keyLines) +int BinaryDescriptor::OctaveKeyLines(ScaleLines &keyLines) { /* final number of extracted lines */ @@ -345,7 +458,7 @@ int BinaryDescriptor::OctaveKeyLines(std::vector & octaveImages, ScaleL /* create and fill an array to store scale factors */ float *scale = new float[params.numOfOctave_]; scale[0] = 1; - for(unsigned int octaveCount = 1; octaveCount & octaveImages, ScaleL float lp0,lp1, lp2, lp3, np0,np1, np2, np3; /* loop over list of octaves */ - for(unsigned int octaveCount = 1; octaveCount & octaveImages, ScaleL maxLocalDis = (endPointDis>maxLocalDis)?endPointDis:maxLocalDis; /* check whether conditions for considering line to be compared - worth to be inserted in the same LineVec are satisfied */ + wremoveInvalidPointsorth to be inserted in the same LineVec are satisfied */ if((maxLocalDis<0.8*(length+octaveLines[lineNextId].lineLength)) &&(minLocalDis