From 44c359db57b9910d469a95af57cae906b2ffe24d Mon Sep 17 00:00:00 2001 From: "steffen.ehrle" Date: Tue, 13 Oct 2020 11:03:04 +0200 Subject: [PATCH 01/22] initial commit --- modules/tbmr/CMakeLists.txt | 2 + modules/tbmr/README.md | 54 +++ modules/tbmr/doc/tbmr.bib | 13 + modules/tbmr/include/opencv2/tbmr.hpp | 65 ++++ modules/tbmr/src/tbmr.cpp | 485 ++++++++++++++++++++++++++ modules/tbmr/test/test_main.cpp | 45 +++ modules/tbmr/test/test_precomp.hpp | 48 +++ modules/tbmr/test/test_tbmr.cpp | 105 ++++++ 8 files changed, 817 insertions(+) create mode 100644 modules/tbmr/CMakeLists.txt create mode 100644 modules/tbmr/README.md create mode 100644 modules/tbmr/doc/tbmr.bib create mode 100644 modules/tbmr/include/opencv2/tbmr.hpp create mode 100644 modules/tbmr/src/tbmr.cpp create mode 100644 modules/tbmr/test/test_main.cpp create mode 100644 modules/tbmr/test/test_precomp.hpp create mode 100644 modules/tbmr/test/test_tbmr.cpp diff --git a/modules/tbmr/CMakeLists.txt b/modules/tbmr/CMakeLists.txt new file mode 100644 index 00000000000..dd425c1722e --- /dev/null +++ b/modules/tbmr/CMakeLists.txt @@ -0,0 +1,2 @@ +set(the_description "Tree-Based Morse Regions") +ocv_define_module(tbmr opencv_core opencv_imgproc WRAP python) \ No newline at end of file diff --git a/modules/tbmr/README.md b/modules/tbmr/README.md new file mode 100644 index 00000000000..06d51bbcb48 --- /dev/null +++ b/modules/tbmr/README.md @@ -0,0 +1,54 @@ +## OpenCV Tree-Based Morse REgions + +Author and maintainers: Steffen Ehrle (steffen.ehrle@myestro.de). + +Hierachical Feature Selection (HFS) is a real-time system for image segmentation. It was originally proposed in [1]. Here is the original project website: http://mmcheng.net/zh/hfs/ + +The algorithm is executed in 3 stages. In the first stage, it obtains an over-segmented image using SLIC(simple linear iterative clustering). In the last 2 stages, it iteratively merges the over-segmented image with merging method mentioned in EGB(Efficient Graph-based Image Segmentation) and learned SVM parameters. + +In our implementation, we wrapped these stages into one single member function of the interface class. + +Since this module used cuda in some part of the implementation, it has to be compiled with cuda support + +For more details about the algorithm, please refer to the original paper: [6940260] + +### usage + +c++ interface: + +```c++ +// read a image +Mat img = imread(image_path), res; +int _h = img.rows, _w = img.cols; + +// create engine +Ptr seg = HfsSegment::create( _h, _w ); + +// perform segmentation +// now "res" is a matrix of indices +// change the second parameter to "True" to get a rgb image for "res" +res = seg->performSegmentGpu(img, false); +``` + +python interface: + +```python +import cv2 +import numpy as np + +img = cv2.imread(image_path) + +# create engine +engine = cv2.hfs.HfsSegment_create(img.shape[0], img.shape[1]) + +# perform segmentation +# now "res" is a matrix of indices +# change the second parameter to "True" to get a rgb image for "res" +res = engine.performSegmentGpu(img, False) +``` + + + +### Reference + +[6940260]: Y. Xu and P. Monasse and T. Géraud and L. Najman Tree-Based Morse Regions: A Topological Approach to Local Feature Detection. \ No newline at end of file diff --git a/modules/tbmr/doc/tbmr.bib b/modules/tbmr/doc/tbmr.bib new file mode 100644 index 00000000000..2466babf7a8 --- /dev/null +++ b/modules/tbmr/doc/tbmr.bib @@ -0,0 +1,13 @@ +@ARTICLE{6940260, + author={Y. {Xu} and P. {Monasse} and T. {Géraud} and L. {Najman}}, + journal={IEEE Transactions on Image Processing}, + title={Tree-Based Morse Regions: A Topological Approach to Local Feature Detection}, + year={2014}, + volume={23}, + number={12}, + pages={5612-5625}, + abstract={This paper introduces a topological approach to local invariant feature detection motivated by Morse theory. We use the critical points of the graph of the intensity image, revealing directly the topology information as initial interest points. Critical points are selected from what we call a tree-based shape-space. In particular, they are selected from both the connected components of the upper level sets of the image (the Max-tree) and those of the lower level sets (the Min-tree). They correspond to specific nodes on those two trees: 1) to the leaves (extrema) and 2) to the nodes having bifurcation (saddle points). We then associate to each critical point the largest region that contains it and is topologically equivalent in its tree. We call such largest regions the tree-based Morse regions (TBMRs). The TBMR can be seen as a variant of maximally stable extremal region (MSER), which are contrasted regions. Contrarily to MSER, TBMR relies only on topological information and thus fully inherit the invariance properties of the space of shapes (e.g., invariance to affine contrast changes and covariance to continuous transformations). In particular, TBMR extracts the regions independently of the contrast, which makes it truly contrast invariant. Furthermore, it is quasi-parameter free. TBMR extraction is fast, having the same complexity as MSER. Experimentally, TBMR achieves a repeatability on par with state-of-the-art methods, but obtains a significantly higher number of features. Both the accuracy and robustness of TBMR are demonstrated by applications to image registration and 3D reconstruction.}, + keywords={feature extraction;image reconstruction;image registration;trees (mathematics);tree-based Morse regions;topological approach;local invariant feature detection;Morse theory;intensity image;initial interest points;critical points;tree-based shape-space;upper level image sets;Max-tree;lower level sets;Min-tree;saddle points;bifurcation;maximally stable extremal region variant;MSER;topological information;TBMR extraction;3D reconstruction;image registration;Feature extraction;Detectors;Shape;Time complexity;Level set;Three-dimensional displays;Image registration;Min/Max tree;local features;affine region detectors;image registration;3D reconstruction;Min/Max tree;local features;affine region detectors;image registration;3D reconstruction}, + doi={10.1109/TIP.2014.2364127}, + ISSN={1941-0042}, + month={Dec},} \ No newline at end of file diff --git a/modules/tbmr/include/opencv2/tbmr.hpp b/modules/tbmr/include/opencv2/tbmr.hpp new file mode 100644 index 00000000000..ce4447cbeee --- /dev/null +++ b/modules/tbmr/include/opencv2/tbmr.hpp @@ -0,0 +1,65 @@ +// This file is part of OpenCV project. +// It is subject to the license terms in the LICENSE file found in the top-level directory +// of this distribution and at http://opencv.org/license.html. + +#include "opencv2/core.hpp" +#include "opencv2/features2d.hpp" + +namespace cv { + namespace tbmr { + + /** @defgroup tbmr Tree Based Morse Features + + The opencv tbmr module contains an algorithm to ... + This module is implemented based on the paper Tree-Based Morse Regions: A Topological Approach to Local + Feature Detection, IEEE 2014. + + + Introduction to Tree-Based Morse Regions + ---------------------------------------------- + + + This algorithm is executed in 2 stages: + + In the first stage, the algorithm computes Component trees (Min-tree and Max-tree) based on the input image. + + In the second stage, the Min- and Max-trees are used to extract TBMR candidates. The extraction can be compared to MSER, + but uses a different criterion: Instead of calculating a stable path along the tree, we look for nodes in the tree, + that have one child while their parent has more than one child. + + The Component tree calculation is based on union-find [Berger 2007 ICIP] + rank. + + */ + + //! @addtogroup tbmr + //! @{ + class CV_EXPORTS_W TBMR : public Feature2D { + public: + + /** @brief Full constructor for %TBMR detector + + @param _min_area prune the area which smaller than minArea + @param _max_area_relative prune the area which bigger than maxArea (max_area = _max_area_relative * image_size) + */ + CV_WRAP static Ptr create(int _min_area = 60, float _max_area_relative = 0.01); + + + /** @brief Detect %MSER regions + + @param image input image (8UC1) + @param tbmrs resulting list of point sets + */ + CV_WRAP virtual void detectRegions(InputArray image, CV_OUT std::vector& tbmrs) = 0; + + CV_WRAP virtual void setMinArea(int minArea) = 0; + CV_WRAP virtual int getMinArea() const = 0; + + CV_WRAP virtual void setMaxAreaRelative(float maxArea) = 0; + CV_WRAP virtual float getMaxAreaRelative() const = 0; + CV_WRAP virtual String getDefaultName() const CV_OVERRIDE; + }; + + //! @} + + } +} // namespace cv { namespace hfs { diff --git a/modules/tbmr/src/tbmr.cpp b/modules/tbmr/src/tbmr.cpp new file mode 100644 index 00000000000..eb3eaf070a4 --- /dev/null +++ b/modules/tbmr/src/tbmr.cpp @@ -0,0 +1,485 @@ +// This file is part of OpenCV project. +// It is subject to the license terms in the LICENSE file found in the top-level directory +// of this distribution and at http://opencv.org/license.html. + + +#include "opencv2/tbmr.hpp" + +namespace cv { + namespace tbmr { + class TBMR_Impl CV_FINAL : public TBMR + { + public: + struct Params + { + Params(int _min_area = 60, float _max_area_relative = 0.01) + { + CV_Assert(_min_area >= 0); + CV_Assert(_max_area_relative >= std::numeric_limits::epsilon()); + + minArea = _min_area; + maxAreaRelative = _max_area_relative; + } + + + uint minArea; + float maxAreaRelative; + }; + + explicit TBMR_Impl(const Params& _params) : params(_params) {} + + virtual ~TBMR_Impl() CV_OVERRIDE {} + + void setMinArea(int minArea) CV_OVERRIDE + { + params.minArea = minArea; + } + int getMinArea() const CV_OVERRIDE + { + return params.minArea; + } + + void setMaxAreaRelative(float maxAreaRelative) CV_OVERRIDE + { + params.maxAreaRelative = maxAreaRelative; + } + float getMaxAreaRelative() const CV_OVERRIDE + { + return params.maxAreaRelative; + } + + void detectRegions(InputArray image, CV_OUT std::vector& tbmrs) CV_OVERRIDE; + void detect(InputArray image, CV_OUT std::vector& keypoints, InputArray mask = noArray()) CV_OVERRIDE; + + + // radix sort images -> indexes + template + cv::Mat sort_indexes(const cv::Mat& input) + { + const size_t bucket_count = 1 << 8; + const uint mask = bucket_count - 1; + uint N = input.cols * input.rows; + cv::Mat indexes_sorted(input.rows, input.cols, CV_32S); // unsigned + + + const uint8_t* input_ptr = input.ptr(); + uint* lutSorted = indexes_sorted.ptr(); + + uint bucket_offsets[bucket_count + 1]; + memset(&bucket_offsets, 0, sizeof(int) * (bucket_count + 1)); + + // count occurences + for (uint i = 0; i < N; ++i) + { + uint8_t key = input_ptr[i]; + uint bucket = key & mask; + bucket_offsets[bucket + 1]++; + } + + // make cumulative + for (uint i = 0; i < bucket_count - 1; ++i) + bucket_offsets[i + 1] += bucket_offsets[i]; + + + // actual step + for (uint i = 0; i < N; ++i) + { + uint8_t key = input_ptr[i]; + uint bucket = key & mask; + + uint pos = bucket_offsets[bucket]++; + + if (sort_order_up) + { + lutSorted[pos] = i; + } + else + { + lutSorted[N - 1 - pos] = i; // reverse + } + } + + return indexes_sorted; + } + + template + void calc_min_max_tree(cv::Mat ima, cv::Mat& parent, cv::Mat& S, std::array* imaAttribute) + { + int rs = ima.rows; + int cs = ima.cols; + uint imSize = (uint)rs * cs; + + std::array offsets = { -ima.cols, -1, 1, ima.cols }; // {-1,0}, {0,-1}, {0,1}, {1,0} yx + std::array offsetsv + = { cv::Vec2i(0, -1), cv::Vec2i(-1, 0), cv::Vec2i(1, 0), cv::Vec2i(0, 1) }; // xy + + + uint* zpar = (uint*)malloc(imSize * sizeof(uint)); + uint* root = (uint*)malloc(imSize * sizeof(uint)); + uint* rank = (uint*)calloc(imSize, sizeof(uint)); + parent = cv::Mat(rs, cs, CV_32S); // unsigned + bool* dejaVu = (bool*)calloc(imSize, sizeof(bool)); + S = sort_indexes(ima); + + const uint8_t* ima_ptr = ima.ptr(); + const uint* S_ptr = S.ptr(); + uint* parent_ptr = parent.ptr(); + + + for (int i = imSize - 1; i >= 0; --i) + { + uint p = S_ptr[i]; + + cv::Vec2i idx_p(p % cs, p / cs); + // make set + { + parent_ptr[p] = p; + zpar[p] = p; + root[p] = p; + dejaVu[p] = true; + imaAttribute[p][0] = 1; // area + imaAttribute[p][1] = idx_p[0]; // sum_x + imaAttribute[p][2] = idx_p[1]; // sum_y + imaAttribute[p][3] = idx_p[0] * idx_p[1]; // sum_xy + imaAttribute[p][4] = idx_p[0] * idx_p[0]; // sum_xx + imaAttribute[p][5] = idx_p[1] * idx_p[1]; // sum_yy + } + + uint x = p; // zpar of p + for (unsigned k = 0; k < offsets.size(); ++k) + { + uint q = p + offsets[k]; + + cv::Vec2i q_idx = idx_p + offsetsv[k]; + bool inBorder + = q_idx[0] >= 0 && q_idx[0] < ima.cols && q_idx[1] >= 0 && q_idx[1] < ima.rows; // filter out border cases + + if (inBorder && dejaVu[q]) // remove first check obsolete + { + uint r = zfindroot(zpar, q); + if (r != x) // make union + { + parent_ptr[root[r]] = p; + // accumulate information + imaAttribute[p][0] += imaAttribute[root[r]][0]; // area + imaAttribute[p][1] += imaAttribute[root[r]][1]; // sum_x + imaAttribute[p][2] += imaAttribute[root[r]][2]; // sum_y + imaAttribute[p][3] += imaAttribute[root[r]][3]; // sum_xy + imaAttribute[p][4] += imaAttribute[root[r]][4]; // sum_xx + imaAttribute[p][5] += imaAttribute[root[r]][5]; // sum_yy + + if (rank[x] < rank[r]) + { + // we merge p to r + zpar[x] = r; + root[r] = p; + x = r; + } + else if (rank[r] < rank[p]) + { + // merge r to p + zpar[r] = p; + } + else + { + // same height + zpar[r] = p; + rank[p] += 1; + } + } + } + } + } + + free(zpar); + free(root); + free(rank); + free(dejaVu); + } + + template + void calculateTBMRs(const cv::Mat& image, std::vector& tbmrs) + { + + uint imSize = image.cols * image.rows; + uint maxArea = static_cast(params.maxAreaRelative * imSize); + + + cv::Mat parentMat, SMat; + + // calculate moments during tree construction: compound type of: (area, x, y, xy, xx, yy) + std::array* imaAttribute = (std::array*)malloc(imSize * sizeof(uint) * 6); + + calc_min_max_tree(image, parentMat, SMat, imaAttribute); + + const uint8_t* ima_ptr = image.ptr(); + const uint* S = SMat.ptr(); + uint* parent = parentMat.ptr(); + + // canonization + for (uint i = 0; i < imSize; ++i) + { + uint p = S[i]; + uint q = parent[p]; + if (ima_ptr[parent[q]] == ima_ptr[q]) + parent[p] = parent[q]; + } + + // TBMRs extraction + //------------------------------------------------------------------------ + // small variant of the given algorithm in the paper. For each critical node having more than one child, we + // check if the largest region containing this node without any change of topology is above its parent, if not, + // discard this critical node. + // + // note also that we do not select the critical nodes themselves as final TBMRs + //-------------------------------------------------------------------------- + + + uint* numSons = (uint*)calloc(imSize, sizeof(uint)); + uint vecNodesSize = imaAttribute[S[0]][0]; // area + uint* vecNodes = (uint*)calloc(vecNodesSize, sizeof(uint)); // area + uint numNodes = 0; + + // leaf to root propagation to select the canonized nodes + for (int i = imSize - 1; i >= 0; --i) + { + uint p = S[i]; + if (parent[p] == p || ima_ptr[p] != ima_ptr[parent[p]]) + { + vecNodes[numNodes++] = p; + if (imaAttribute[p][0] >= params.minArea) // area + numSons[parent[p]]++; + } + } + + bool* isSeen = (bool*)calloc(imSize, sizeof(bool)); + + // parent of critical leaf node + bool* isParentofLeaf = (bool*)calloc(imSize, sizeof(bool)); + + for (uint i = 0; i < vecNodesSize; i++) + { + uint p = vecNodes[i]; + if (numSons[p] == 0 && numSons[parent[p]] == 1) + isParentofLeaf[parent[p]] = true; + } + + uint numTbmrs = 0; + uint* vecTbmrs = (uint*)malloc(numNodes * sizeof(uint)); + for (uint i = 0; i < vecNodesSize; i++) + { + std::size_t p = vecNodes[i]; + if (numSons[p] == 1 && !isSeen[p] && imaAttribute[p][0] <= maxArea) + { + unsigned num_ancestors = 0; + std::size_t pt = p; + std::size_t po = pt; + while (numSons[pt] == 1 && imaAttribute[pt][0] <= maxArea) + { + isSeen[pt] = true; + num_ancestors++; + po = pt; + pt = parent[pt]; + } + if (!isParentofLeaf[p] || num_ancestors > 1) + { + vecTbmrs[numTbmrs++] = po; + } + } + } + // end of TBMRs extraction + //------------------------------------------------------------------------ + + // compute best fitting ellipses + //------------------------------------------------------------------------ + unsigned num_valid_TBMRs = 0; + for (int i = 0; i < numTbmrs; i++) + { + uint p = vecTbmrs[i]; + double x = (double)imaAttribute[p][1] / (double)imaAttribute[p][0]; // sum_x / area + double y = (double)imaAttribute[p][2] / (double)imaAttribute[p][0]; // sum_y / area + double i20 = imaAttribute[p][4] - imaAttribute[p][0] * x * x; // sum_xx - area + double i02 = imaAttribute[p][5] - imaAttribute[p][0] * y * y; // sum_yy - area + double i11 = imaAttribute[p][3] - imaAttribute[p][0] * x * y; // sum_xy - area + double n = i20 * i02 - i11 * i11; + if (n != 0) + { + double a = i02 / n; + a = a * (imaAttribute[p][0] - 1) / 4; + double b = -i11 / n; + b = b * (imaAttribute[p][0] - 1) / 4; + double c = i20 / n; + c = c * (imaAttribute[p][0] - 1) / 4; + + // filter out some non meaingful ellipses + double a1 = a; + double b1 = b; + double c1 = c; + unsigned ai = 0; + unsigned bi = 0; + unsigned ci = 0; + if (a > 0) + { + ai = 100000 * a; + if (a < 0.00005) + a1 = 0; + else if (a < 0.0001) + { + a1 = 0.0001; + } + else + { + ai = 10000 * a; + a1 = (double)ai / 10000; + } + } + else + { + if (a > -0.00005) + a1 = 0; + else if (a > -0.0001) + a1 = -0.0001; + else + { + ai = 10000 * (-a); + a1 = -(double)ai / 10000; + } + } + + if (b > 0) + { + bi = 100000 * b; + if (b < 0.00005) + b1 = 0; + else if (b < 0.0001) + { + b1 = 0.0001; + } + else + { + bi = 10000 * b; + b1 = (double)bi / 10000; + } + } + else + { + if (b > -0.00005) + b1 = 0; + else if (b > -0.0001) + b1 = -0.0001; + else + { + bi = 10000 * (-b); + b1 = -(double)bi / 10000; + } + } + + if (c > 0) + { + ci = 100000 * c; + if (c < 0.00005) + c1 = 0; + else if (c < 0.0001) + { + c1 = 0.0001; + } + else + { + ci = 10000 * c; + c1 = (double)ci / 10000; + } + } + else + { + if (c > -0.00005) + c1 = 0; + else if (c > -0.0001) + c1 = -0.0001; + else + { + ci = 10000 * (-c); + c1 = -(double)ci / 10000; + } + } + double v = (a1 + c1 - std::sqrt(a1 * a1 + c1 * c1 + 4 * b1 * b1 - 2 * a1 * c1)) / 2; + + double l1 = (a + c + std::sqrt(a * a + c * c + 4 * b * b - 2 * a * c)) / 2; + double l2 = (a + c - std::sqrt(a * a + c * c + 4 * b * b - 2 * a * c)) / 2; + l1 = std::sqrt(l1); + l2 = std::sqrt(l2); + l1 = 1 / l1; + l2 = 1 / l2; + double l = std::min(l1, l2); + + if (l >= 1.5 && v != 0) + { + tbmrs.push_back(cv::KeyPoint(cv::Point2f(x, y), c, b, a)); + // num_valid_TBMRs++; + // outputFile << y << " " << x << " " << c << " " << b << " " << a << std::endl; + } + } + } + + free(imaAttribute); + free(numSons); + free(vecNodes); + free(isSeen); + free(isParentofLeaf); + free(vecTbmrs); + //--------------------------------------------- + } + + Mat tempsrc; + + Params params; + }; + + static inline uint zfindroot(uint* parent, uint p) + { + if (parent[p] == p) + return p; + else + return parent[p] = zfindroot(parent, parent[p]); + } + + void TBMR_Impl::detectRegions(InputArray _src, std::vector& tbmrs) + { + Mat src = _src.getMat(); + + tbmrs.clear(); + + CV_Assert(!src.empty()); + CV_Assert(src.type() == CV_8UC1); + + if (!src.isContinuous()) + { + src.copyTo(tempsrc); + src = tempsrc; + } + + // append max-tree tbmrs + calculateTBMRs(src, tbmrs); + // append min-tree tbmrs + calculateTBMRs(src, tbmrs); + } + + void TBMR_Impl::detect(InputArray _image, std::vector& keypoints, InputArray _mask) + { + Mat mask = _mask.getMat(); + + detectRegions(_image, keypoints); + + // int i, ncomps = (int)keypoints.size(); + // keypoints.clear(); + } + CV_WRAP Ptr TBMR::create(int _min_area, float _max_area_relative) + { + return cv::makePtr(TBMR_Impl::Params(_min_area, _max_area_relative)); + } + + String TBMR::getDefaultName() const + { + return (Feature2D::getDefaultName() + ".TBMR"); + } + } +} diff --git a/modules/tbmr/test/test_main.cpp b/modules/tbmr/test/test_main.cpp new file mode 100644 index 00000000000..5af98d89d06 --- /dev/null +++ b/modules/tbmr/test/test_main.cpp @@ -0,0 +1,45 @@ +/*M/////////////////////////////////////////////////////////////////////////////////////// +// +// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. +// +// By downloading, copying, installing or using the software you agree to this license. +// If you do not agree to this license, do not download, install, +// copy or use the software. +// +// +// License Agreement +// For Open Source Computer Vision Library +// +// Copyright (C) 2000-2008, Intel Corporation, all rights reserved. +// Copyright (C) 2009, Willow Garage Inc., all rights reserved. +// Third party copyrights are property of their respective owners. +// +// Redistribution and use in source and binary forms, with or without modification, +// are permitted provided that the following conditions are met: +// +// * Redistribution's of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// * Redistribution's in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// * The name of the copyright holders may not be used to endorse or promote products +// derived from this software without specific prior written permission. +// +// This software is provided by the copyright holders and contributors "as is" and +// any express or implied warranties, including, but not limited to, the implied +// warranties of merchantability and fitness for a particular purpose are disclaimed. +// In no event shall the Intel Corporation or contributors be liable for any direct, +// indirect, incidental, special, exemplary, or consequential damages +// (including, but not limited to, procurement of substitute goods or services; +// loss of use, data, or profits; or business interruption) however caused +// and on any theory of liability, whether in contract, strict liability, +// or tort (including negligence or otherwise) arising in any way out of +// the use of this software, even if advised of the possibility of such damage. +// +//M*/ + +#include "test_precomp.hpp" + +CV_TEST_MAIN("tbmr") diff --git a/modules/tbmr/test/test_precomp.hpp b/modules/tbmr/test/test_precomp.hpp new file mode 100644 index 00000000000..fb15c09a3fe --- /dev/null +++ b/modules/tbmr/test/test_precomp.hpp @@ -0,0 +1,48 @@ +/*M/////////////////////////////////////////////////////////////////////////////////////// +// +// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. +// +// By downloading, copying, installing or using the software you agree to this license. +// If you do not agree to this license, do not download, install, +// copy or use the software. +// +// +// License Agreement +// For Open Source Computer Vision Library +// +// Copyright (C) 2000-2008, Intel Corporation, all rights reserved. +// Copyright (C) 2009, Willow Garage Inc., all rights reserved. +// Third party copyrights are property of their respective owners. +// +// Redistribution and use in source and binary forms, with or without modification, +// are permitted provided that the following conditions are met: +// +// * Redistribution's of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// * Redistribution's in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// * The name of the copyright holders may not be used to endorse or promote products +// derived from this software without specific prior written permission. +// +// This software is provided by the copyright holders and contributors "as is" and +// any express or implied warranties, including, but not limited to, the implied +// warranties of merchantability and fitness for a particular purpose are disclaimed. +// In no event shall the Intel Corporation or contributors be liable for any direct, +// indirect, incidental, special, exemplary, or consequential damages +// (including, but not limited to, procurement of substitute goods or services; +// loss of use, data, or profits; or business interruption) however caused +// and on any theory of liability, whether in contract, strict liability, +// or tort (including negligence or otherwise) arising in any way out of +// the use of this software, even if advised of the possibility of such damage. +// +//M*/ +#ifndef __OPENCV_TEST_PRECOMP_HPP__ +#define __OPENCV_TEST_PRECOMP_HPP__ + +#include "opencv2/ts.hpp" +#include "opencv2/tbmr.hpp" + +#endif diff --git a/modules/tbmr/test/test_tbmr.cpp b/modules/tbmr/test/test_tbmr.cpp new file mode 100644 index 00000000000..70f9854664f --- /dev/null +++ b/modules/tbmr/test/test_tbmr.cpp @@ -0,0 +1,105 @@ +/*M/////////////////////////////////////////////////////////////////////////////////////// +// +// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. +// +// By downloading, copying, installing or using the software you agree to this license. +// If you do not agree to this license, do not download, install, +// copy or use the software. +// +// +// License Agreement +// For Open Source Computer Vision Library +// +// Copyright (C) 2000-2008, Intel Corporation, all rights reserved. +// Copyright (C) 2009, Willow Garage Inc., all rights reserved. +// Third party copyrights are property of their respective owners. +// +// Redistribution and use in source and binary forms, with or without modification, +// are permitted provided that the following conditions are met: +// +// * Redistribution's of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// * Redistribution's in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// * The name of the copyright holders may not be used to endorse or promote products +// derived from this software without specific prior written permission. +// +// This software is provided by the copyright holders and contributors "as is" and +// any express or implied warranties, including, but not limited to, the implied +// warranties of merchantability and fitness for a particular purpose are disclaimed. +// In no event shall the Intel Corporation or contributors be liable for any direct, +// indirect, incidental, special, exemplary, or consequential damages +// (including, but not limited to, procurement of substitute goods or services; +// loss of use, data, or profits; or business interruption) however caused +// and on any theory of liability, whether in contract, strict liability, +// or tort (including negligence or otherwise) arising in any way out of +// the use of this software, even if advised of the possibility of such damage. +// +//M*/ + +#include "test_precomp.hpp" +#include "cvconfig.h" +#include "opencv2/ts/ocl_test.hpp" + +namespace opencv_test { + + class CV_TBMRTest : public cvtest::BaseTest + { + public: + CV_TBMRTest(); + ~CV_TBMRTest(); + protected: + void run(int /* idx */); + }; + + CV_TBMRTest::CV_TBMRTest() {} + CV_TBMRTest::~CV_TBMRTest() {} + + void CV_TBMRTest::run(int) + { + using namespace tbmr; + Mat image1; + ts->printf(cvtest::TS::CONSOLE, "Looking in %s \n", ts->get_data_path().c_str()); + image1 = imread(ts->get_data_path() + "graf1.pgm", IMREAD_GRAYSCALE); + + if (image1.empty()) + { + ts->printf(cvtest::TS::LOG, "Wrong input data \n"); + ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA); + return; + } + + Ptr tbmr = TBMR::create(30, 0.01); + // set the corresponding parameters + tbmr->setMinArea(30); + tbmr->setMaxAreaRelative(0.01); + + std::vector tbmrs; + tbmr->detectRegions(image1, tbmrs); + + if (tbmrs.size() != 1221) + { + ts->printf(cvtest::TS::LOG, "Invalid result \n"); + ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_OUTPUT); + return; + } + } + + TEST(tbmr_simple_test, accuracy) { CV_TBMRTest test; test.safe_run(); } +#ifdef HAVE_OPENCL + + namespace ocl { + + //OCL_TEST_F(cv::tbmr::TBMR, ABC1) + //{ + // // RunTest(cv::superres::createSuperResolution_BTVL1()); + //} + + } // namespace opencv_test::ocl + +#endif + +} // namespace From a2ee31b7687c50c9805bfe2458d19834f5103b10 Mon Sep 17 00:00:00 2001 From: "steffen.ehrle" Date: Tue, 13 Oct 2020 11:13:30 +0200 Subject: [PATCH 02/22] fix test data reusing stereomatching testdata. --- modules/tbmr/test/test_tbmr.cpp | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/modules/tbmr/test/test_tbmr.cpp b/modules/tbmr/test/test_tbmr.cpp index 70f9854664f..69ab4494296 100644 --- a/modules/tbmr/test/test_tbmr.cpp +++ b/modules/tbmr/test/test_tbmr.cpp @@ -61,11 +61,10 @@ namespace opencv_test { void CV_TBMRTest::run(int) { using namespace tbmr; - Mat image1; - ts->printf(cvtest::TS::CONSOLE, "Looking in %s \n", ts->get_data_path().c_str()); - image1 = imread(ts->get_data_path() + "graf1.pgm", IMREAD_GRAYSCALE); + Mat image; + image = imread(ts->get_data_path() + "../cv/stereomatching/datasets/tsukuba/im2.png", IMREAD_GRAYSCALE); - if (image1.empty()) + if (image.empty()) { ts->printf(cvtest::TS::LOG, "Wrong input data \n"); ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA); @@ -78,9 +77,9 @@ namespace opencv_test { tbmr->setMaxAreaRelative(0.01); std::vector tbmrs; - tbmr->detectRegions(image1, tbmrs); + tbmr->detectRegions(image, tbmrs); - if (tbmrs.size() != 1221) + if (tbmrs.size() != 351) { ts->printf(cvtest::TS::LOG, "Invalid result \n"); ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_OUTPUT); From dfc9cf9b4c35ee2a3dbc4c55087a9ef7fb47837e Mon Sep 17 00:00:00 2001 From: "steffen.ehrle" Date: Tue, 13 Oct 2020 13:26:59 +0200 Subject: [PATCH 03/22] fix incorrect function, ellipse notation, types and comments. --- modules/tbmr/include/opencv2/tbmr.hpp | 29 +++---- modules/tbmr/src/tbmr.cpp | 108 +++++++++++++------------- modules/tbmr/test/test_tbmr.cpp | 2 +- 3 files changed, 63 insertions(+), 76 deletions(-) diff --git a/modules/tbmr/include/opencv2/tbmr.hpp b/modules/tbmr/include/opencv2/tbmr.hpp index ce4447cbeee..e8f8d3b0ef1 100644 --- a/modules/tbmr/include/opencv2/tbmr.hpp +++ b/modules/tbmr/include/opencv2/tbmr.hpp @@ -23,9 +23,9 @@ namespace cv { In the first stage, the algorithm computes Component trees (Min-tree and Max-tree) based on the input image. - In the second stage, the Min- and Max-trees are used to extract TBMR candidates. The extraction can be compared to MSER, - but uses a different criterion: Instead of calculating a stable path along the tree, we look for nodes in the tree, - that have one child while their parent has more than one child. + In the second stage, the Min- and Max-trees are used to extract TBMR candidates. The extraction can be compared to MSER + but uses different criteria: Instead of calculating a stable path along the tree, we look for nodes in the tree, + that have one child while their parent has more than one child (Morse regions). The Component tree calculation is based on union-find [Berger 2007 ICIP] + rank. @@ -38,28 +38,19 @@ namespace cv { /** @brief Full constructor for %TBMR detector - @param _min_area prune the area which smaller than minArea - @param _max_area_relative prune the area which bigger than maxArea (max_area = _max_area_relative * image_size) + @param _min_area prune areas smaller than minArea + @param _max_area_relative prune areas bigger than maxArea = _max_area_relative * input_image_size */ - CV_WRAP static Ptr create(int _min_area = 60, float _max_area_relative = 0.01); - - - /** @brief Detect %MSER regions - - @param image input image (8UC1) - @param tbmrs resulting list of point sets - */ - CV_WRAP virtual void detectRegions(InputArray image, CV_OUT std::vector& tbmrs) = 0; + CV_WRAP static Ptr create(int _min_area = 60, double _max_area_relative = 0.01); CV_WRAP virtual void setMinArea(int minArea) = 0; CV_WRAP virtual int getMinArea() const = 0; - - CV_WRAP virtual void setMaxAreaRelative(float maxArea) = 0; - CV_WRAP virtual float getMaxAreaRelative() const = 0; + CV_WRAP virtual void setMaxAreaRelative(double maxArea) = 0; + CV_WRAP virtual double getMaxAreaRelative() const = 0; CV_WRAP virtual String getDefaultName() const CV_OVERRIDE; }; - //! @} + //! @} tbmr } -} // namespace cv { namespace hfs { +} // namespace cv { namespace tbmr { diff --git a/modules/tbmr/src/tbmr.cpp b/modules/tbmr/src/tbmr.cpp index eb3eaf070a4..4142bbf6962 100644 --- a/modules/tbmr/src/tbmr.cpp +++ b/modules/tbmr/src/tbmr.cpp @@ -12,10 +12,10 @@ namespace cv { public: struct Params { - Params(int _min_area = 60, float _max_area_relative = 0.01) + Params(int _min_area = 60, double _max_area_relative = 0.01) { CV_Assert(_min_area >= 0); - CV_Assert(_max_area_relative >= std::numeric_limits::epsilon()); + CV_Assert(_max_area_relative >= std::numeric_limits::epsilon()); minArea = _min_area; maxAreaRelative = _max_area_relative; @@ -23,7 +23,7 @@ namespace cv { uint minArea; - float maxAreaRelative; + double maxAreaRelative; }; explicit TBMR_Impl(const Params& _params) : params(_params) {} @@ -39,19 +39,17 @@ namespace cv { return params.minArea; } - void setMaxAreaRelative(float maxAreaRelative) CV_OVERRIDE + void setMaxAreaRelative(double maxAreaRelative) CV_OVERRIDE { params.maxAreaRelative = maxAreaRelative; } - float getMaxAreaRelative() const CV_OVERRIDE + double getMaxAreaRelative() const CV_OVERRIDE { return params.maxAreaRelative; } - void detectRegions(InputArray image, CV_OUT std::vector& tbmrs) CV_OVERRIDE; void detect(InputArray image, CV_OUT std::vector& keypoints, InputArray mask = noArray()) CV_OVERRIDE; - // radix sort images -> indexes template cv::Mat sort_indexes(const cv::Mat& input) @@ -102,6 +100,15 @@ namespace cv { return indexes_sorted; } + inline uint zfindroot(uint* parent, uint p) + { + if (parent[p] == p) + return p; + else + return parent[p] = zfindroot(parent, parent[p]); + } + + template void calc_min_max_tree(cv::Mat ima, cv::Mat& parent, cv::Mat& S, std::array* imaAttribute) { @@ -198,7 +205,7 @@ namespace cv { } template - void calculateTBMRs(const cv::Mat& image, std::vector& tbmrs) + void calculateTBMRs(const cv::Mat& image, std::vector& tbmrs, const cv::Mat& mask) { uint imSize = image.cols * image.rows; @@ -271,7 +278,7 @@ namespace cv { std::size_t p = vecNodes[i]; if (numSons[p] == 1 && !isSeen[p] && imaAttribute[p][0] <= maxArea) { - unsigned num_ancestors = 0; + uint num_ancestors = 0; std::size_t pt = p; std::size_t po = pt; while (numSons[pt] == 1 && imaAttribute[pt][0] <= maxArea) @@ -292,26 +299,35 @@ namespace cv { // compute best fitting ellipses //------------------------------------------------------------------------ - unsigned num_valid_TBMRs = 0; for (int i = 0; i < numTbmrs; i++) { uint p = vecTbmrs[i]; - double x = (double)imaAttribute[p][1] / (double)imaAttribute[p][0]; // sum_x / area - double y = (double)imaAttribute[p][2] / (double)imaAttribute[p][0]; // sum_y / area - double i20 = imaAttribute[p][4] - imaAttribute[p][0] * x * x; // sum_xx - area - double i02 = imaAttribute[p][5] - imaAttribute[p][0] * y * y; // sum_yy - area - double i11 = imaAttribute[p][3] - imaAttribute[p][0] * x * y; // sum_xy - area + double area = static_cast(imaAttribute[p][0]); + double sum_x = static_cast(imaAttribute[p][1]); + double sum_y = static_cast(imaAttribute[p][2]); + double sum_xy = static_cast(imaAttribute[p][3]); + double sum_xx = static_cast(imaAttribute[p][4]); + double sum_yy = static_cast(imaAttribute[p][5]); + + // Barycenter: + double x = sum_x / area; + double y = sum_y / area; + // Second order moments + //double X2 = sum_xx / area - x * x; + //double Y2 = sum_yy / area - y * y; + //double XY = sum_xy / area - x * y; + + double i20 = sum_xx - area * x * x; + double i02 = sum_yy - area * y * y; + double i11 = sum_xy - area * x * y; double n = i20 * i02 - i11 * i11; if (n != 0) { - double a = i02 / n; - a = a * (imaAttribute[p][0] - 1) / 4; - double b = -i11 / n; - b = b * (imaAttribute[p][0] - 1) / 4; - double c = i20 / n; - c = c * (imaAttribute[p][0] - 1) / 4; - - // filter out some non meaingful ellipses + double a = (i02 / n) * (area - 1) / 4; + double b = (-i11 / n) * (area - 1) / 4; + double c = (i20 / n) * (area - 1) / 4; + + // filter out some non meaningful ellipses double a1 = a; double b1 = b; double c1 = c; @@ -403,19 +419,15 @@ namespace cv { } double v = (a1 + c1 - std::sqrt(a1 * a1 + c1 * c1 + 4 * b1 * b1 - 2 * a1 * c1)) / 2; - double l1 = (a + c + std::sqrt(a * a + c * c + 4 * b * b - 2 * a * c)) / 2; - double l2 = (a + c - std::sqrt(a * a + c * c + 4 * b * b - 2 * a * c)) / 2; - l1 = std::sqrt(l1); - l2 = std::sqrt(l2); - l1 = 1 / l1; - l2 = 1 / l2; + double l1 = 1. / std::sqrt((a + c + std::sqrt(a * a + c * c + 4 * b * b - 2 * a * c)) / 2); + double l2 = 1. / std::sqrt((a + c - std::sqrt(a * a + c * c + 4 * b * b - 2 * a * c)) / 2); double l = std::min(l1, l2); - if (l >= 1.5 && v != 0) + if (l >= 1.5 && v != 0 && (mask.empty() || mask.at(cvRound(y), cvRound(x)) != 0)) { - tbmrs.push_back(cv::KeyPoint(cv::Point2f(x, y), c, b, a)); - // num_valid_TBMRs++; - // outputFile << y << " " << x << " " << c << " " << b << " " << a << std::endl; + float diam = 2 * a; // Major axis + // float angle = std::atan((a / b) * (y / x)); + tbmrs.push_back(cv::KeyPoint(cv::Point2f(x, y), diam)); } } } @@ -434,19 +446,12 @@ namespace cv { Params params; }; - static inline uint zfindroot(uint* parent, uint p) - { - if (parent[p] == p) - return p; - else - return parent[p] = zfindroot(parent, parent[p]); - } - - void TBMR_Impl::detectRegions(InputArray _src, std::vector& tbmrs) + void TBMR_Impl::detect(InputArray _image, std::vector& keypoints, InputArray _mask) { - Mat src = _src.getMat(); + Mat mask = _mask.getMat(); + Mat src = _image.getMat(); - tbmrs.clear(); + keypoints.clear(); CV_Assert(!src.empty()); CV_Assert(src.type() == CV_8UC1); @@ -458,21 +463,12 @@ namespace cv { } // append max-tree tbmrs - calculateTBMRs(src, tbmrs); + calculateTBMRs(src, keypoints, mask); // append min-tree tbmrs - calculateTBMRs(src, tbmrs); + calculateTBMRs(src, keypoints, mask); } - void TBMR_Impl::detect(InputArray _image, std::vector& keypoints, InputArray _mask) - { - Mat mask = _mask.getMat(); - - detectRegions(_image, keypoints); - - // int i, ncomps = (int)keypoints.size(); - // keypoints.clear(); - } - CV_WRAP Ptr TBMR::create(int _min_area, float _max_area_relative) + CV_WRAP Ptr TBMR::create(int _min_area, double _max_area_relative) { return cv::makePtr(TBMR_Impl::Params(_min_area, _max_area_relative)); } diff --git a/modules/tbmr/test/test_tbmr.cpp b/modules/tbmr/test/test_tbmr.cpp index 69ab4494296..f1b35aee4c6 100644 --- a/modules/tbmr/test/test_tbmr.cpp +++ b/modules/tbmr/test/test_tbmr.cpp @@ -77,7 +77,7 @@ namespace opencv_test { tbmr->setMaxAreaRelative(0.01); std::vector tbmrs; - tbmr->detectRegions(image, tbmrs); + tbmr->detect(image, tbmrs); if (tbmrs.size() != 351) { From f34c6a6d22b6690c404556cbac8c79ba3a5bfab0 Mon Sep 17 00:00:00 2001 From: "steffen.ehrle" Date: Tue, 13 Oct 2020 13:43:45 +0200 Subject: [PATCH 04/22] add required precomp.hpp, fix warnings. --- modules/tbmr/include/opencv2/tbmr.hpp | 6 +++- modules/tbmr/src/precomp.hpp | 52 +++++++++++++++++++++++++++ modules/tbmr/src/tbmr.cpp | 37 +++++++++---------- 3 files changed, 73 insertions(+), 22 deletions(-) create mode 100644 modules/tbmr/src/precomp.hpp diff --git a/modules/tbmr/include/opencv2/tbmr.hpp b/modules/tbmr/include/opencv2/tbmr.hpp index e8f8d3b0ef1..28a9f7ca5e2 100644 --- a/modules/tbmr/include/opencv2/tbmr.hpp +++ b/modules/tbmr/include/opencv2/tbmr.hpp @@ -2,7 +2,9 @@ // It is subject to the license terms in the LICENSE file found in the top-level directory // of this distribution and at http://opencv.org/license.html. -#include "opencv2/core.hpp" +#ifndef OPENCV_TBMR_HPP +#define OPENCV_TBMR_HPP + #include "opencv2/features2d.hpp" namespace cv { @@ -54,3 +56,5 @@ namespace cv { } } // namespace cv { namespace tbmr { + +#endif diff --git a/modules/tbmr/src/precomp.hpp b/modules/tbmr/src/precomp.hpp new file mode 100644 index 00000000000..ec9e3ca59bc --- /dev/null +++ b/modules/tbmr/src/precomp.hpp @@ -0,0 +1,52 @@ +/*M/////////////////////////////////////////////////////////////////////////////////////// +// +// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. +// +// By downloading, copying, installing or using the software you agree to this license. +// If you do not agree to this license, do not download, install, +// copy or use the software. +// +// +// License Agreement +// For Open Source Computer Vision Library +// +// Copyright (C) 2000-2008, Intel Corporation, all rights reserved. +// Copyright (C) 2009, Willow Garage Inc., all rights reserved. +// Third party copyrights are property of their respective owners. +// +// Redistribution and use in source and binary forms, with or without modification, +// are permitted provided that the following conditions are met: +// +// * Redistribution's of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// * Redistribution's in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// * The name of the copyright holders may not be used to endorse or promote products +// derived from this software without specific prior written permission. +// +// This software is provided by the copyright holders and contributors "as is" and +// any express or implied warranties, including, but not limited to, the implied +// warranties of merchantability and fitness for a particular purpose are disclaimed. +// In no event shall the Intel Corporation or contributors be liable for any direct, +// indirect, incidental, special, exemplary, or consequential damages +// (including, but not limited to, procurement of substitute goods or services; +// loss of use, data, or profits; or business interruption) however caused +// and on any theory of liability, whether in contract, strict liability, +// or tort (including negligence or otherwise) arising in any way out of +// the use of this software, even if advised of the possibility of such damage. +// +//M*/ + +#ifndef __OPENCV_PRECOMP_H__ +#define __OPENCV_PRECOMP_H__ + +#include "opencv2/features2d.hpp" +#include "opencv2/tbmr.hpp" + +//#include "opencv2/core/ocl.hpp" +#include + +#endif diff --git a/modules/tbmr/src/tbmr.cpp b/modules/tbmr/src/tbmr.cpp index 4142bbf6962..aef94c2561b 100644 --- a/modules/tbmr/src/tbmr.cpp +++ b/modules/tbmr/src/tbmr.cpp @@ -2,8 +2,7 @@ // It is subject to the license terms in the LICENSE file found in the top-level directory // of this distribution and at http://opencv.org/license.html. - -#include "opencv2/tbmr.hpp" +#include "precomp.hpp" namespace cv { namespace tbmr { @@ -128,7 +127,6 @@ namespace cv { bool* dejaVu = (bool*)calloc(imSize, sizeof(bool)); S = sort_indexes(ima); - const uint8_t* ima_ptr = ima.ptr(); const uint* S_ptr = S.ptr(); uint* parent_ptr = parent.ptr(); @@ -275,12 +273,12 @@ namespace cv { uint* vecTbmrs = (uint*)malloc(numNodes * sizeof(uint)); for (uint i = 0; i < vecNodesSize; i++) { - std::size_t p = vecNodes[i]; + uint p = vecNodes[i]; if (numSons[p] == 1 && !isSeen[p] && imaAttribute[p][0] <= maxArea) { uint num_ancestors = 0; - std::size_t pt = p; - std::size_t po = pt; + uint pt = p; + uint po = pt; while (numSons[pt] == 1 && imaAttribute[pt][0] <= maxArea) { isSeen[pt] = true; @@ -299,7 +297,7 @@ namespace cv { // compute best fitting ellipses //------------------------------------------------------------------------ - for (int i = 0; i < numTbmrs; i++) + for (uint i = 0; i < numTbmrs; i++) { uint p = vecTbmrs[i]; double area = static_cast(imaAttribute[p][0]); @@ -331,12 +329,11 @@ namespace cv { double a1 = a; double b1 = b; double c1 = c; - unsigned ai = 0; - unsigned bi = 0; - unsigned ci = 0; + uint ai = 0; + uint bi = 0; + uint ci = 0; if (a > 0) { - ai = 100000 * a; if (a < 0.00005) a1 = 0; else if (a < 0.0001) @@ -345,7 +342,7 @@ namespace cv { } else { - ai = 10000 * a; + ai = (uint)(10000 * a); a1 = (double)ai / 10000; } } @@ -357,14 +354,13 @@ namespace cv { a1 = -0.0001; else { - ai = 10000 * (-a); + ai = (uint)(10000 * (-a)); a1 = -(double)ai / 10000; } } if (b > 0) { - bi = 100000 * b; if (b < 0.00005) b1 = 0; else if (b < 0.0001) @@ -373,7 +369,7 @@ namespace cv { } else { - bi = 10000 * b; + bi = (uint)(10000 * b); b1 = (double)bi / 10000; } } @@ -385,14 +381,13 @@ namespace cv { b1 = -0.0001; else { - bi = 10000 * (-b); + bi = (uint)(10000 * (-b)); b1 = -(double)bi / 10000; } } if (c > 0) { - ci = 100000 * c; if (c < 0.00005) c1 = 0; else if (c < 0.0001) @@ -401,7 +396,7 @@ namespace cv { } else { - ci = 10000 * c; + ci = (uint)(10000 * c); c1 = (double)ci / 10000; } } @@ -413,7 +408,7 @@ namespace cv { c1 = -0.0001; else { - ci = 10000 * (-c); + ci = (uint)(10000 * (-c)); c1 = -(double)ci / 10000; } } @@ -425,9 +420,9 @@ namespace cv { if (l >= 1.5 && v != 0 && (mask.empty() || mask.at(cvRound(y), cvRound(x)) != 0)) { - float diam = 2 * a; // Major axis + float diam = (float)(2 * a); // Major axis // float angle = std::atan((a / b) * (y / x)); - tbmrs.push_back(cv::KeyPoint(cv::Point2f(x, y), diam)); + tbmrs.push_back(cv::KeyPoint(cv::Point2f((float)x, (float)y), diam)); } } } From 4f2191c5a6f5cb052dfeeb36cb88642378c0061c Mon Sep 17 00:00:00 2001 From: "steffen.ehrle" Date: Tue, 13 Oct 2020 13:54:03 +0200 Subject: [PATCH 05/22] fix naming --- modules/tbmr/src/tbmr.cpp | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/modules/tbmr/src/tbmr.cpp b/modules/tbmr/src/tbmr.cpp index aef94c2561b..13dc2fd3d55 100644 --- a/modules/tbmr/src/tbmr.cpp +++ b/modules/tbmr/src/tbmr.cpp @@ -49,12 +49,12 @@ namespace cv { void detect(InputArray image, CV_OUT std::vector& keypoints, InputArray mask = noArray()) CV_OVERRIDE; - // radix sort images -> indexes + // counting sort indexes (can we use cv::SortIdx instead?) template - cv::Mat sort_indexes(const cv::Mat& input) + cv::Mat sortIndexes(const cv::Mat& input) { - const size_t bucket_count = 1 << 8; - const uint mask = bucket_count - 1; + constexpr size_t bucket_count = 1 << 8; + constexpr uint mask = bucket_count - 1; uint N = input.cols * input.rows; cv::Mat indexes_sorted(input.rows, input.cols, CV_32S); // unsigned @@ -78,7 +78,7 @@ namespace cv { bucket_offsets[i + 1] += bucket_offsets[i]; - // actual step + // sort step for (uint i = 0; i < N; ++i) { uint8_t key = input_ptr[i]; @@ -108,8 +108,9 @@ namespace cv { } + // Calculate the Component tree template - void calc_min_max_tree(cv::Mat ima, cv::Mat& parent, cv::Mat& S, std::array* imaAttribute) + void calcMinMaxTree(cv::Mat ima, cv::Mat& parent, cv::Mat& S, std::array* imaAttribute) { int rs = ima.rows; int cs = ima.cols; @@ -125,7 +126,7 @@ namespace cv { uint* rank = (uint*)calloc(imSize, sizeof(uint)); parent = cv::Mat(rs, cs, CV_32S); // unsigned bool* dejaVu = (bool*)calloc(imSize, sizeof(bool)); - S = sort_indexes(ima); + S = sortIndexes(ima); const uint* S_ptr = S.ptr(); uint* parent_ptr = parent.ptr(); @@ -215,7 +216,7 @@ namespace cv { // calculate moments during tree construction: compound type of: (area, x, y, xy, xx, yy) std::array* imaAttribute = (std::array*)malloc(imSize * sizeof(uint) * 6); - calc_min_max_tree(image, parentMat, SMat, imaAttribute); + calcMinMaxTree(image, parentMat, SMat, imaAttribute); const uint8_t* ima_ptr = image.ptr(); const uint* S = SMat.ptr(); @@ -457,9 +458,9 @@ namespace cv { src = tempsrc; } - // append max-tree tbmrs + // append max tree tbmrs calculateTBMRs(src, keypoints, mask); - // append min-tree tbmrs + // append min tree tbmrs calculateTBMRs(src, keypoints, mask); } From c396037d646619c86f66889ef3e0a3d98ad0db68 Mon Sep 17 00:00:00 2001 From: "steffen.ehrle" Date: Tue, 13 Oct 2020 14:03:29 +0200 Subject: [PATCH 06/22] remove ocl for now. (we want to add opencl functionality later) --- modules/tbmr/test/test_tbmr.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/tbmr/test/test_tbmr.cpp b/modules/tbmr/test/test_tbmr.cpp index f1b35aee4c6..ab6a6854c7e 100644 --- a/modules/tbmr/test/test_tbmr.cpp +++ b/modules/tbmr/test/test_tbmr.cpp @@ -42,7 +42,7 @@ #include "test_precomp.hpp" #include "cvconfig.h" -#include "opencv2/ts/ocl_test.hpp" +//#include "opencv2/ts/ocl_test.hpp" namespace opencv_test { From 810eecb383bf411418779a96b9e730ce9ce4e508 Mon Sep 17 00:00:00 2001 From: "steffen.ehrle" Date: Tue, 13 Oct 2020 14:28:49 +0200 Subject: [PATCH 07/22] update readme --- modules/tbmr/README.md | 45 ++++++++++++------------------------------ 1 file changed, 13 insertions(+), 32 deletions(-) diff --git a/modules/tbmr/README.md b/modules/tbmr/README.md index 06d51bbcb48..340a02b5bc5 100644 --- a/modules/tbmr/README.md +++ b/modules/tbmr/README.md @@ -2,13 +2,10 @@ Author and maintainers: Steffen Ehrle (steffen.ehrle@myestro.de). -Hierachical Feature Selection (HFS) is a real-time system for image segmentation. It was originally proposed in [1]. Here is the original project website: http://mmcheng.net/zh/hfs/ +Tree Based Morse Regions (TBMR) is a topological approach to local invariant feature detection motivated by Morse theory. -The algorithm is executed in 3 stages. In the first stage, it obtains an over-segmented image using SLIC(simple linear iterative clustering). In the last 2 stages, it iteratively merges the over-segmented image with merging method mentioned in EGB(Efficient Graph-based Image Segmentation) and learned SVM parameters. - -In our implementation, we wrapped these stages into one single member function of the interface class. - -Since this module used cuda in some part of the implementation, it has to be compiled with cuda support +The algorithm can be seen as a variant of [MSER](https://docs.opencv.org/3.4.2/d3/d28/classcv_1_1MSER.html) as both algorithms rely on Min/Max-tree. +However TBMRs being purely topological are invariant to affine illumination change. For more details about the algorithm, please refer to the original paper: [6940260] @@ -17,38 +14,22 @@ For more details about the algorithm, please refer to the original paper: [69402 c++ interface: ```c++ +using namespace tbmr; // read a image Mat img = imread(image_path), res; -int _h = img.rows, _w = img.cols; // create engine -Ptr seg = HfsSegment::create( _h, _w ); - -// perform segmentation -// now "res" is a matrix of indices -// change the second parameter to "True" to get a rgb image for "res" -res = seg->performSegmentGpu(img, false); +// TBMR uses only 2 parameters: MinSize and MaxSizeRel. +// MinSize, pruning areas smaller than MinSize +// MaxSizeRel, pruning areas bigger than MaxSize = MaxSizeRel * img.size +Ptr alg = TBMR::create(30, 0.01); + +// perform Keypoint Extraction +// TBMR features provide Point, diameter and angle +std::vector tbmrs; +res = alg->detect(img, tbmrs); ``` -python interface: - -```python -import cv2 -import numpy as np - -img = cv2.imread(image_path) - -# create engine -engine = cv2.hfs.HfsSegment_create(img.shape[0], img.shape[1]) - -# perform segmentation -# now "res" is a matrix of indices -# change the second parameter to "True" to get a rgb image for "res" -res = engine.performSegmentGpu(img, False) -``` - - - ### Reference [6940260]: Y. Xu and P. Monasse and T. Géraud and L. Najman Tree-Based Morse Regions: A Topological Approach to Local Feature Detection. \ No newline at end of file From 4bfebc5d657c4308431a732bf4c4265bfc477ca2 Mon Sep 17 00:00:00 2001 From: "steffen.ehrle" Date: Tue, 13 Oct 2020 15:27:18 +0200 Subject: [PATCH 08/22] fix invalid module dependency. --- modules/tbmr/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/tbmr/CMakeLists.txt b/modules/tbmr/CMakeLists.txt index dd425c1722e..fb68d773ac6 100644 --- a/modules/tbmr/CMakeLists.txt +++ b/modules/tbmr/CMakeLists.txt @@ -1,2 +1,2 @@ set(the_description "Tree-Based Morse Regions") -ocv_define_module(tbmr opencv_core opencv_imgproc WRAP python) \ No newline at end of file +ocv_define_module(tbmr opencv_core opencv_features2d WRAP python) \ No newline at end of file From 70df97569a9038a17b754c813dc12fef17c3c083 Mon Sep 17 00:00:00 2001 From: "steffen.ehrle" Date: Thu, 15 Oct 2020 14:32:23 +0200 Subject: [PATCH 09/22] add angle, minAxis and majAxis calculation. --- modules/tbmr/src/tbmr.cpp | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/modules/tbmr/src/tbmr.cpp b/modules/tbmr/src/tbmr.cpp index 13dc2fd3d55..99565453f42 100644 --- a/modules/tbmr/src/tbmr.cpp +++ b/modules/tbmr/src/tbmr.cpp @@ -301,6 +301,7 @@ namespace cv { for (uint i = 0; i < numTbmrs; i++) { uint p = vecTbmrs[i]; + double area = static_cast(imaAttribute[p][0]); double sum_x = static_cast(imaAttribute[p][1]); double sum_y = static_cast(imaAttribute[p][2]); @@ -308,14 +309,9 @@ namespace cv { double sum_xx = static_cast(imaAttribute[p][4]); double sum_yy = static_cast(imaAttribute[p][5]); - // Barycenter: + double x = sum_x / area; double y = sum_y / area; - // Second order moments - //double X2 = sum_xx / area - x * x; - //double Y2 = sum_yy / area - y * y; - //double XY = sum_xy / area - x * y; - double i20 = sum_xx - area * x * x; double i02 = sum_yy - area * y * y; double i11 = sum_xy - area * x * y; @@ -417,13 +413,24 @@ namespace cv { double l1 = 1. / std::sqrt((a + c + std::sqrt(a * a + c * c + 4 * b * b - 2 * a * c)) / 2); double l2 = 1. / std::sqrt((a + c - std::sqrt(a * a + c * c + 4 * b * b - 2 * a * c)) / 2); - double l = std::min(l1, l2); + double minAxL = std::min(l1, l2); + double majAxL = std::max(l1, l2); - if (l >= 1.5 && v != 0 && (mask.empty() || mask.at(cvRound(y), cvRound(x)) != 0)) + if (minAxL >= 1.5 && v != 0 && (mask.empty() || mask.at(cvRound(y), cvRound(x)) != 0)) { - float diam = (float)(2 * a); // Major axis - // float angle = std::atan((a / b) * (y / x)); - tbmrs.push_back(cv::KeyPoint(cv::Point2f((float)x, (float)y), diam)); + float theta = 0; + if (b == 0) + if (a < c) + theta = 0; + else + theta = CV_PI / 2.; + else + theta = CV_PI / 2. + 0.5 * std::atan2(2 * b, (a - c)); + + // we have all ellipse informations, but KeyPoint does not store ellipses. Use Elliptic_Keypoint + // from xFeatures2D? + float diam = std::sqrt(majAxL * majAxL + minAxL * minAxL); + tbmrs.push_back(cv::KeyPoint(cv::Point2f((float)x, (float)y), majAxL, theta)); } } } From 592dd32d4111be1673f736c51c6d87757233bf1a Mon Sep 17 00:00:00 2001 From: "steffen.ehrle" Date: Thu, 15 Oct 2020 15:54:20 +0200 Subject: [PATCH 10/22] formatting fixes. restructure component tree calculation. remove sort_indexes by using cv::sortIdx. --- modules/tbmr/include/opencv2/tbmr.hpp | 82 +-- modules/tbmr/src/tbmr.cpp | 803 +++++++++++++------------- modules/tbmr/test/test_tbmr.cpp | 120 ++-- 3 files changed, 503 insertions(+), 502 deletions(-) diff --git a/modules/tbmr/include/opencv2/tbmr.hpp b/modules/tbmr/include/opencv2/tbmr.hpp index 28a9f7ca5e2..f8698c0a4a6 100644 --- a/modules/tbmr/include/opencv2/tbmr.hpp +++ b/modules/tbmr/include/opencv2/tbmr.hpp @@ -1,60 +1,68 @@ // This file is part of OpenCV project. -// It is subject to the license terms in the LICENSE file found in the top-level directory -// of this distribution and at http://opencv.org/license.html. +// It is subject to the license terms in the LICENSE file found in the top-level +// directory of this distribution and at http://opencv.org/license.html. #ifndef OPENCV_TBMR_HPP #define OPENCV_TBMR_HPP #include "opencv2/features2d.hpp" -namespace cv { - namespace tbmr { +namespace cv +{ +namespace tbmr +{ - /** @defgroup tbmr Tree Based Morse Features +/** @defgroup tbmr Tree Based Morse Features - The opencv tbmr module contains an algorithm to ... - This module is implemented based on the paper Tree-Based Morse Regions: A Topological Approach to Local - Feature Detection, IEEE 2014. +The opencv tbmr module contains an algorithm to ... +This module is implemented based on the paper Tree-Based Morse Regions: A +Topological Approach to Local Feature Detection, IEEE 2014. - Introduction to Tree-Based Morse Regions - ---------------------------------------------- +Introduction to Tree-Based Morse Regions +---------------------------------------------- - This algorithm is executed in 2 stages: +This algorithm is executed in 2 stages: - In the first stage, the algorithm computes Component trees (Min-tree and Max-tree) based on the input image. +In the first stage, the algorithm computes Component trees (Min-tree and +Max-tree) based on the input image. - In the second stage, the Min- and Max-trees are used to extract TBMR candidates. The extraction can be compared to MSER - but uses different criteria: Instead of calculating a stable path along the tree, we look for nodes in the tree, - that have one child while their parent has more than one child (Morse regions). +In the second stage, the Min- and Max-trees are used to extract TBMR candidates. +The extraction can be compared to MSER but uses different criteria: Instead of +calculating a stable path along the tree, we look for siblings (nodes whos +parent has more than one child node) that have only one child (Morse regions). +These candidates are filtered by their ellipse shape and size. - The Component tree calculation is based on union-find [Berger 2007 ICIP] + rank. +This TBMR implementation is adapting source code from +http://laurentnajman.org/index.php?page=tbmr. The Component tree calculation is +based on union-find [Berger 2007 ICIP] + rank. - */ +*/ - //! @addtogroup tbmr - //! @{ - class CV_EXPORTS_W TBMR : public Feature2D { - public: +//! @addtogroup tbmr +//! @{ +class CV_EXPORTS_W TBMR : public Feature2D +{ + public: + /** @brief Full constructor for %TBMR detector + @param _min_area prune areas smaller than minArea + @param _max_area_relative prune areas bigger than maxArea = + _max_area_relative * input_image_size + */ + CV_WRAP static Ptr create(int _min_area = 60, + double _max_area_relative = 0.01); - /** @brief Full constructor for %TBMR detector + CV_WRAP virtual void setMinArea(int minArea) = 0; + CV_WRAP virtual int getMinArea() const = 0; + CV_WRAP virtual void setMaxAreaRelative(double maxArea) = 0; + CV_WRAP virtual double getMaxAreaRelative() const = 0; + CV_WRAP virtual String getDefaultName() const CV_OVERRIDE; +}; - @param _min_area prune areas smaller than minArea - @param _max_area_relative prune areas bigger than maxArea = _max_area_relative * input_image_size - */ - CV_WRAP static Ptr create(int _min_area = 60, double _max_area_relative = 0.01); +//! @} tbmr - CV_WRAP virtual void setMinArea(int minArea) = 0; - CV_WRAP virtual int getMinArea() const = 0; - CV_WRAP virtual void setMaxAreaRelative(double maxArea) = 0; - CV_WRAP virtual double getMaxAreaRelative() const = 0; - CV_WRAP virtual String getDefaultName() const CV_OVERRIDE; - }; - - //! @} tbmr - - } -} // namespace cv { namespace tbmr { +} // namespace tbmr +} // namespace cv #endif diff --git a/modules/tbmr/src/tbmr.cpp b/modules/tbmr/src/tbmr.cpp index 99565453f42..503dc03d0e4 100644 --- a/modules/tbmr/src/tbmr.cpp +++ b/modules/tbmr/src/tbmr.cpp @@ -1,484 +1,461 @@ // This file is part of OpenCV project. -// It is subject to the license terms in the LICENSE file found in the top-level directory -// of this distribution and at http://opencv.org/license.html. +// It is subject to the license terms in the LICENSE file found in the top-level +// directory of this distribution and at http://opencv.org/license.html. #include "precomp.hpp" -namespace cv { - namespace tbmr { - class TBMR_Impl CV_FINAL : public TBMR +namespace cv +{ +namespace tbmr +{ +class TBMR_Impl CV_FINAL : public TBMR +{ + public: + struct Params + { + Params(int _min_area = 60, double _max_area_relative = 0.01) { - public: - struct Params - { - Params(int _min_area = 60, double _max_area_relative = 0.01) - { - CV_Assert(_min_area >= 0); - CV_Assert(_max_area_relative >= std::numeric_limits::epsilon()); + CV_Assert(_min_area >= 0); + CV_Assert(_max_area_relative >= + std::numeric_limits::epsilon()); - minArea = _min_area; - maxAreaRelative = _max_area_relative; - } + minArea = _min_area; + maxAreaRelative = _max_area_relative; + } + uint minArea; + double maxAreaRelative; + }; - uint minArea; - double maxAreaRelative; - }; + explicit TBMR_Impl(const Params &_params) : params(_params) {} - explicit TBMR_Impl(const Params& _params) : params(_params) {} + virtual ~TBMR_Impl() CV_OVERRIDE {} - virtual ~TBMR_Impl() CV_OVERRIDE {} + void setMinArea(int minArea) CV_OVERRIDE { params.minArea = minArea; } + int getMinArea() const CV_OVERRIDE { return params.minArea; } - void setMinArea(int minArea) CV_OVERRIDE - { - params.minArea = minArea; - } - int getMinArea() const CV_OVERRIDE - { - return params.minArea; - } + void setMaxAreaRelative(double maxAreaRelative) CV_OVERRIDE + { + params.maxAreaRelative = maxAreaRelative; + } + double getMaxAreaRelative() const CV_OVERRIDE + { + return params.maxAreaRelative; + } - void setMaxAreaRelative(double maxAreaRelative) CV_OVERRIDE - { - params.maxAreaRelative = maxAreaRelative; - } - double getMaxAreaRelative() const CV_OVERRIDE - { - return params.maxAreaRelative; - } + void detect(InputArray image, CV_OUT std::vector &keypoints, + InputArray mask = noArray()) CV_OVERRIDE; - void detect(InputArray image, CV_OUT std::vector& keypoints, InputArray mask = noArray()) CV_OVERRIDE; + CV_INLINE uint zfindroot(uint *parent, uint p) + { + if (parent[p] == p) + return p; + else + return parent[p] = zfindroot(parent, parent[p]); + } - // counting sort indexes (can we use cv::SortIdx instead?) - template - cv::Mat sortIndexes(const cv::Mat& input) - { - constexpr size_t bucket_count = 1 << 8; - constexpr uint mask = bucket_count - 1; - uint N = input.cols * input.rows; - cv::Mat indexes_sorted(input.rows, input.cols, CV_32S); // unsigned + // Calculate the Component tree. Based on the order of S, it will be a + // min or max tree. + void calcMinMaxTree(Mat ima) + { + int rs = ima.rows; + int cs = ima.cols; + uint imSize = (uint)rs * cs; + + std::array offsets = { + -ima.cols, -1, 1, ima.cols + }; // {-1,0}, {0,-1}, {0,1}, {1,0} yx + std::array offsetsv = { Vec2i(0, -1), Vec2i(-1, 0), + Vec2i(1, 0), Vec2i(0, 1) }; // xy + + uint *zpar = (uint *)malloc(imSize * sizeof(uint)); + uint *root = (uint *)malloc(imSize * sizeof(uint)); + uint *rank = (uint *)calloc(imSize, sizeof(uint)); + parent = Mat(rs, cs, CV_32S); // unsigned + bool *dejaVu = (bool *)calloc(imSize, sizeof(bool)); + + const uint *S_ptr = S.ptr(); + uint *parent_ptr = parent.ptr(); + Vec *imaAttribute = imaAttributes.ptr>(); + + for (int i = imSize - 1; i >= 0; --i) + { + uint p = S_ptr[i]; + Vec2i idx_p(p % cs, p / cs); + // make set + { + parent_ptr[p] = p; + zpar[p] = p; + root[p] = p; + dejaVu[p] = true; + imaAttribute[p][0] = 1; // area + imaAttribute[p][1] = idx_p[0]; // sum_x + imaAttribute[p][2] = idx_p[1]; // sum_y + imaAttribute[p][3] = idx_p[0] * idx_p[1]; // sum_xy + imaAttribute[p][4] = idx_p[0] * idx_p[0]; // sum_xx + imaAttribute[p][5] = idx_p[1] * idx_p[1]; // sum_yy + } - const uint8_t* input_ptr = input.ptr(); - uint* lutSorted = indexes_sorted.ptr(); + uint x = p; // zpar of p + for (unsigned k = 0; k < offsets.size(); ++k) + { + uint q = p + offsets[k]; - uint bucket_offsets[bucket_count + 1]; - memset(&bucket_offsets, 0, sizeof(int) * (bucket_count + 1)); + Vec2i q_idx = idx_p + offsetsv[k]; + bool inBorder = q_idx[0] >= 0 && q_idx[0] < ima.cols && + q_idx[1] >= 0 && + q_idx[1] < ima.rows; // filter out border cases - // count occurences - for (uint i = 0; i < N; ++i) + if (inBorder && dejaVu[q]) // remove first check + // obsolete { - uint8_t key = input_ptr[i]; - uint bucket = key & mask; - bucket_offsets[bucket + 1]++; + uint r = zfindroot(zpar, q); + if (r != x) // make union + { + parent_ptr[root[r]] = p; + // accumulate information + imaAttribute[p][0] += imaAttribute[root[r]][0]; // area + imaAttribute[p][1] += imaAttribute[root[r]][1]; // sum_x + imaAttribute[p][2] += imaAttribute[root[r]][2]; // sum_y + imaAttribute[p][3] += + imaAttribute[root[r]][3]; // sum_xy + imaAttribute[p][4] += + imaAttribute[root[r]][4]; // sum_xx + imaAttribute[p][5] += + imaAttribute[root[r]][5]; // sum_yy + + if (rank[x] < rank[r]) + { + // we merge p to r + zpar[x] = r; + root[r] = p; + x = r; + } + else if (rank[r] < rank[p]) + { + // merge r to p + zpar[r] = p; + } + else + { + // same height + zpar[r] = p; + rank[p] += 1; + } + } } + } + } - // make cumulative - for (uint i = 0; i < bucket_count - 1; ++i) - bucket_offsets[i + 1] += bucket_offsets[i]; + free(zpar); + free(root); + free(rank); + free(dejaVu); + } + void calculateTBMRs(const Mat &image, std::vector &tbmrs, + const Mat &mask) + { + uint imSize = image.cols * image.rows; + uint maxArea = static_cast(params.maxAreaRelative * imSize); - // sort step - for (uint i = 0; i < N; ++i) - { - uint8_t key = input_ptr[i]; - uint bucket = key & mask; + if (parent.empty() || parent.size != image.size) + parent = Mat(image.rows, image.cols, CV_32S); - uint pos = bucket_offsets[bucket]++; - - if (sort_order_up) - { - lutSorted[pos] = i; - } - else - { - lutSorted[N - 1 - pos] = i; // reverse - } - } + if (imaAttributes.empty() || imaAttributes.size != image.size) + imaAttributes = Mat(image.rows, image.cols, CV_32SC(6)); - return indexes_sorted; - } + calcMinMaxTree(image); - inline uint zfindroot(uint* parent, uint p) - { - if (parent[p] == p) - return p; - else - return parent[p] = zfindroot(parent, parent[p]); - } + const Vec *imaAttribute = + imaAttributes.ptr>(); + const uint8_t *ima_ptr = image.ptr(); + const uint *S_ptr = S.ptr(); + uint *parent_ptr = parent.ptr(); + // canonization + for (uint i = 0; i < imSize; ++i) + { + uint p = S_ptr[i]; + uint q = parent_ptr[p]; + if (ima_ptr[parent_ptr[q]] == ima_ptr[q]) + parent_ptr[p] = parent_ptr[q]; + } - // Calculate the Component tree - template - void calcMinMaxTree(cv::Mat ima, cv::Mat& parent, cv::Mat& S, std::array* imaAttribute) + // TBMRs extraction + //------------------------------------------------------------------------ + // small variant of the given algorithm in the paper. For each + // critical node having more than one child, we check if the + // largest region containing this node without any change of + // topology is above its parent, if not, discard this critical + // node. + // + // note also that we do not select the critical nodes themselves + // as final TBMRs + //-------------------------------------------------------------------------- + + uint *numSons = (uint *)calloc(imSize, sizeof(uint)); + uint vecNodesSize = imaAttribute[S_ptr[0]][0]; // area + uint *vecNodes = (uint *)calloc(vecNodesSize, sizeof(uint)); // area + uint numNodes = 0; + + // leaf to root propagation to select the canonized nodes + for (int i = imSize - 1; i >= 0; --i) + { + uint p = S_ptr[i]; + if (parent_ptr[p] == p || ima_ptr[p] != ima_ptr[parent_ptr[p]]) { - int rs = ima.rows; - int cs = ima.cols; - uint imSize = (uint)rs * cs; - - std::array offsets = { -ima.cols, -1, 1, ima.cols }; // {-1,0}, {0,-1}, {0,1}, {1,0} yx - std::array offsetsv - = { cv::Vec2i(0, -1), cv::Vec2i(-1, 0), cv::Vec2i(1, 0), cv::Vec2i(0, 1) }; // xy - + vecNodes[numNodes++] = p; + if (imaAttribute[p][0] >= params.minArea) // area + numSons[parent_ptr[p]]++; + } + } - uint* zpar = (uint*)malloc(imSize * sizeof(uint)); - uint* root = (uint*)malloc(imSize * sizeof(uint)); - uint* rank = (uint*)calloc(imSize, sizeof(uint)); - parent = cv::Mat(rs, cs, CV_32S); // unsigned - bool* dejaVu = (bool*)calloc(imSize, sizeof(bool)); - S = sortIndexes(ima); + bool *isSeen = (bool *)calloc(imSize, sizeof(bool)); - const uint* S_ptr = S.ptr(); - uint* parent_ptr = parent.ptr(); + // parent of critical leaf node + bool *isParentofLeaf = (bool *)calloc(imSize, sizeof(bool)); + for (uint i = 0; i < vecNodesSize; i++) + { + uint p = vecNodes[i]; + if (numSons[p] == 0 && numSons[parent_ptr[p]] == 1) + isParentofLeaf[parent_ptr[p]] = true; + } - for (int i = imSize - 1; i >= 0; --i) + uint numTbmrs = 0; + uint *vecTbmrs = (uint *)malloc(numNodes * sizeof(uint)); + for (uint i = 0; i < vecNodesSize; i++) + { + uint p = vecNodes[i]; + if (numSons[p] == 1 && !isSeen[p] && imaAttribute[p][0] <= maxArea) + { + uint num_ancestors = 0; + uint pt = p; + uint po = pt; + while (numSons[pt] == 1 && imaAttribute[pt][0] <= maxArea) { - uint p = S_ptr[i]; + isSeen[pt] = true; + num_ancestors++; + po = pt; + pt = parent_ptr[pt]; + } + if (!isParentofLeaf[p] || num_ancestors > 1) + { + vecTbmrs[numTbmrs++] = po; + } + } + } + // end of TBMRs extraction + //------------------------------------------------------------------------ - cv::Vec2i idx_p(p % cs, p / cs); - // make set + // compute best fitting ellipses + //------------------------------------------------------------------------ + for (uint i = 0; i < numTbmrs; i++) + { + uint p = vecTbmrs[i]; + double area = static_cast(imaAttribute[p][0]); + double sum_x = static_cast(imaAttribute[p][1]); + double sum_y = static_cast(imaAttribute[p][2]); + double sum_xy = static_cast(imaAttribute[p][3]); + double sum_xx = static_cast(imaAttribute[p][4]); + double sum_yy = static_cast(imaAttribute[p][5]); + + // Barycenter: + double x = sum_x / area; + double y = sum_y / area; + + double i20 = sum_xx - area * x * x; + double i02 = sum_yy - area * y * y; + double i11 = sum_xy - area * x * y; + double n = i20 * i02 - i11 * i11; + if (n != 0) + { + double a = (i02 / n) * (area - 1) / 4; + double b = (-i11 / n) * (area - 1) / 4; + double c = (i20 / n) * (area - 1) / 4; + + // filter out some non meaningful ellipses + double a1 = a; + double b1 = b; + double c1 = c; + uint ai = 0; + uint bi = 0; + uint ci = 0; + if (a > 0) + { + if (a < 0.00005) + a1 = 0; + else if (a < 0.0001) { - parent_ptr[p] = p; - zpar[p] = p; - root[p] = p; - dejaVu[p] = true; - imaAttribute[p][0] = 1; // area - imaAttribute[p][1] = idx_p[0]; // sum_x - imaAttribute[p][2] = idx_p[1]; // sum_y - imaAttribute[p][3] = idx_p[0] * idx_p[1]; // sum_xy - imaAttribute[p][4] = idx_p[0] * idx_p[0]; // sum_xx - imaAttribute[p][5] = idx_p[1] * idx_p[1]; // sum_yy + a1 = 0.0001; } - - uint x = p; // zpar of p - for (unsigned k = 0; k < offsets.size(); ++k) + else { - uint q = p + offsets[k]; - - cv::Vec2i q_idx = idx_p + offsetsv[k]; - bool inBorder - = q_idx[0] >= 0 && q_idx[0] < ima.cols && q_idx[1] >= 0 && q_idx[1] < ima.rows; // filter out border cases - - if (inBorder && dejaVu[q]) // remove first check obsolete - { - uint r = zfindroot(zpar, q); - if (r != x) // make union - { - parent_ptr[root[r]] = p; - // accumulate information - imaAttribute[p][0] += imaAttribute[root[r]][0]; // area - imaAttribute[p][1] += imaAttribute[root[r]][1]; // sum_x - imaAttribute[p][2] += imaAttribute[root[r]][2]; // sum_y - imaAttribute[p][3] += imaAttribute[root[r]][3]; // sum_xy - imaAttribute[p][4] += imaAttribute[root[r]][4]; // sum_xx - imaAttribute[p][5] += imaAttribute[root[r]][5]; // sum_yy - - if (rank[x] < rank[r]) - { - // we merge p to r - zpar[x] = r; - root[r] = p; - x = r; - } - else if (rank[r] < rank[p]) - { - // merge r to p - zpar[r] = p; - } - else - { - // same height - zpar[r] = p; - rank[p] += 1; - } - } - } + ai = (uint)(10000 * a); + a1 = (double)ai / 10000; } } - - free(zpar); - free(root); - free(rank); - free(dejaVu); - } - - template - void calculateTBMRs(const cv::Mat& image, std::vector& tbmrs, const cv::Mat& mask) - { - - uint imSize = image.cols * image.rows; - uint maxArea = static_cast(params.maxAreaRelative * imSize); - - - cv::Mat parentMat, SMat; - - // calculate moments during tree construction: compound type of: (area, x, y, xy, xx, yy) - std::array* imaAttribute = (std::array*)malloc(imSize * sizeof(uint) * 6); - - calcMinMaxTree(image, parentMat, SMat, imaAttribute); - - const uint8_t* ima_ptr = image.ptr(); - const uint* S = SMat.ptr(); - uint* parent = parentMat.ptr(); - - // canonization - for (uint i = 0; i < imSize; ++i) + else { - uint p = S[i]; - uint q = parent[p]; - if (ima_ptr[parent[q]] == ima_ptr[q]) - parent[p] = parent[q]; + if (a > -0.00005) + a1 = 0; + else if (a > -0.0001) + a1 = -0.0001; + else + { + ai = (uint)(10000 * (-a)); + a1 = -(double)ai / 10000; + } } - // TBMRs extraction - //------------------------------------------------------------------------ - // small variant of the given algorithm in the paper. For each critical node having more than one child, we - // check if the largest region containing this node without any change of topology is above its parent, if not, - // discard this critical node. - // - // note also that we do not select the critical nodes themselves as final TBMRs - //-------------------------------------------------------------------------- - - - uint* numSons = (uint*)calloc(imSize, sizeof(uint)); - uint vecNodesSize = imaAttribute[S[0]][0]; // area - uint* vecNodes = (uint*)calloc(vecNodesSize, sizeof(uint)); // area - uint numNodes = 0; - - // leaf to root propagation to select the canonized nodes - for (int i = imSize - 1; i >= 0; --i) + if (b > 0) { - uint p = S[i]; - if (parent[p] == p || ima_ptr[p] != ima_ptr[parent[p]]) + if (b < 0.00005) + b1 = 0; + else if (b < 0.0001) { - vecNodes[numNodes++] = p; - if (imaAttribute[p][0] >= params.minArea) // area - numSons[parent[p]]++; + b1 = 0.0001; + } + else + { + bi = (uint)(10000 * b); + b1 = (double)bi / 10000; } } - - bool* isSeen = (bool*)calloc(imSize, sizeof(bool)); - - // parent of critical leaf node - bool* isParentofLeaf = (bool*)calloc(imSize, sizeof(bool)); - - for (uint i = 0; i < vecNodesSize; i++) + else { - uint p = vecNodes[i]; - if (numSons[p] == 0 && numSons[parent[p]] == 1) - isParentofLeaf[parent[p]] = true; + if (b > -0.00005) + b1 = 0; + else if (b > -0.0001) + b1 = -0.0001; + else + { + bi = (uint)(10000 * (-b)); + b1 = -(double)bi / 10000; + } } - uint numTbmrs = 0; - uint* vecTbmrs = (uint*)malloc(numNodes * sizeof(uint)); - for (uint i = 0; i < vecNodesSize; i++) + if (c > 0) { - uint p = vecNodes[i]; - if (numSons[p] == 1 && !isSeen[p] && imaAttribute[p][0] <= maxArea) + if (c < 0.00005) + c1 = 0; + else if (c < 0.0001) { - uint num_ancestors = 0; - uint pt = p; - uint po = pt; - while (numSons[pt] == 1 && imaAttribute[pt][0] <= maxArea) - { - isSeen[pt] = true; - num_ancestors++; - po = pt; - pt = parent[pt]; - } - if (!isParentofLeaf[p] || num_ancestors > 1) - { - vecTbmrs[numTbmrs++] = po; - } + c1 = 0.0001; + } + else + { + ci = (uint)(10000 * c); + c1 = (double)ci / 10000; } } - // end of TBMRs extraction - //------------------------------------------------------------------------ - - // compute best fitting ellipses - //------------------------------------------------------------------------ - for (uint i = 0; i < numTbmrs; i++) + else { - uint p = vecTbmrs[i]; - - double area = static_cast(imaAttribute[p][0]); - double sum_x = static_cast(imaAttribute[p][1]); - double sum_y = static_cast(imaAttribute[p][2]); - double sum_xy = static_cast(imaAttribute[p][3]); - double sum_xx = static_cast(imaAttribute[p][4]); - double sum_yy = static_cast(imaAttribute[p][5]); - - - double x = sum_x / area; - double y = sum_y / area; - double i20 = sum_xx - area * x * x; - double i02 = sum_yy - area * y * y; - double i11 = sum_xy - area * x * y; - double n = i20 * i02 - i11 * i11; - if (n != 0) + if (c > -0.00005) + c1 = 0; + else if (c > -0.0001) + c1 = -0.0001; + else { - double a = (i02 / n) * (area - 1) / 4; - double b = (-i11 / n) * (area - 1) / 4; - double c = (i20 / n) * (area - 1) / 4; - - // filter out some non meaningful ellipses - double a1 = a; - double b1 = b; - double c1 = c; - uint ai = 0; - uint bi = 0; - uint ci = 0; - if (a > 0) - { - if (a < 0.00005) - a1 = 0; - else if (a < 0.0001) - { - a1 = 0.0001; - } - else - { - ai = (uint)(10000 * a); - a1 = (double)ai / 10000; - } - } - else - { - if (a > -0.00005) - a1 = 0; - else if (a > -0.0001) - a1 = -0.0001; - else - { - ai = (uint)(10000 * (-a)); - a1 = -(double)ai / 10000; - } - } - - if (b > 0) - { - if (b < 0.00005) - b1 = 0; - else if (b < 0.0001) - { - b1 = 0.0001; - } - else - { - bi = (uint)(10000 * b); - b1 = (double)bi / 10000; - } - } + ci = (uint)(10000 * (-c)); + c1 = -(double)ci / 10000; + } + } + double v = + (a1 + c1 - + std::sqrt(a1 * a1 + c1 * c1 + 4 * b1 * b1 - 2 * a1 * c1)) / + 2; + + double l1 = 1. / std::sqrt((a + c + + std::sqrt(a * a + c * c + + 4 * b * b - 2 * a * c)) / + 2); + double l2 = 1. / std::sqrt((a + c - + std::sqrt(a * a + c * c + + 4 * b * b - 2 * a * c)) / + 2); + double minAxL = std::min(l1, l2); + double majAxL = std::max(l1, l2); + if (minAxL >= 1.5 && v != 0 && + (mask.empty() || + mask.at(cvRound(y), cvRound(x)) != 0)) + { + double theta = 0; + if (b == 0) + if (a < c) + theta = 0; else - { - if (b > -0.00005) - b1 = 0; - else if (b > -0.0001) - b1 = -0.0001; - else - { - bi = (uint)(10000 * (-b)); - b1 = -(double)bi / 10000; - } - } + theta = CV_PI / 2.; + else + theta = CV_PI / 2. + 0.5 * std::atan2(2 * b, (a - c)); - if (c > 0) - { - if (c < 0.00005) - c1 = 0; - else if (c < 0.0001) - { - c1 = 0.0001; - } - else - { - ci = (uint)(10000 * c); - c1 = (double)ci / 10000; - } - } - else - { - if (c > -0.00005) - c1 = 0; - else if (c > -0.0001) - c1 = -0.0001; - else - { - ci = (uint)(10000 * (-c)); - c1 = -(double)ci / 10000; - } - } - double v = (a1 + c1 - std::sqrt(a1 * a1 + c1 * c1 + 4 * b1 * b1 - 2 * a1 * c1)) / 2; + // we have all ellipse informations, but + // KeyPoint does not store ellipses. Use + // Elliptic_Keypoint from xFeatures2D? + tbmrs.push_back(KeyPoint(Point2f((float)x, (float)y), + (float)majAxL, (float)theta)); + } + } + } - double l1 = 1. / std::sqrt((a + c + std::sqrt(a * a + c * c + 4 * b * b - 2 * a * c)) / 2); - double l2 = 1. / std::sqrt((a + c - std::sqrt(a * a + c * c + 4 * b * b - 2 * a * c)) / 2); - double minAxL = std::min(l1, l2); - double majAxL = std::max(l1, l2); + free(numSons); + free(vecNodes); + free(isSeen); + free(isParentofLeaf); + free(vecTbmrs); + //--------------------------------------------- + } - if (minAxL >= 1.5 && v != 0 && (mask.empty() || mask.at(cvRound(y), cvRound(x)) != 0)) - { - float theta = 0; - if (b == 0) - if (a < c) - theta = 0; - else - theta = CV_PI / 2.; - else - theta = CV_PI / 2. + 0.5 * std::atan2(2 * b, (a - c)); - - // we have all ellipse informations, but KeyPoint does not store ellipses. Use Elliptic_Keypoint - // from xFeatures2D? - float diam = std::sqrt(majAxL * majAxL + minAxL * minAxL); - tbmrs.push_back(cv::KeyPoint(cv::Point2f((float)x, (float)y), majAxL, theta)); - } - } - } + Mat tempsrc; - free(imaAttribute); - free(numSons); - free(vecNodes); - free(isSeen); - free(isParentofLeaf); - free(vecTbmrs); - //--------------------------------------------- - } + // component tree representation: see + // https://ieeexplore.ieee.org/document/6850018 + Mat parent; + Mat S; + // moments: compound type of: (area, x, y, xy, xx, yy) + Mat imaAttributes; - Mat tempsrc; + Params params; +}; - Params params; - }; +void TBMR_Impl::detect(InputArray _image, std::vector &keypoints, + InputArray _mask) +{ + Mat mask = _mask.getMat(); + Mat src = _image.getMat(); - void TBMR_Impl::detect(InputArray _image, std::vector& keypoints, InputArray _mask) - { - Mat mask = _mask.getMat(); - Mat src = _image.getMat(); + keypoints.clear(); - keypoints.clear(); + CV_Assert(!src.empty()); + CV_Assert(src.type() == CV_8UC1); - CV_Assert(!src.empty()); - CV_Assert(src.type() == CV_8UC1); + if (!src.isContinuous()) + { + src.copyTo(tempsrc); + src = tempsrc; + } - if (!src.isContinuous()) - { - src.copyTo(tempsrc); - src = tempsrc; - } + // append max tree tbmrs + sortIdx(src.reshape(1, 1), S, + SortFlags::SORT_ASCENDING | SortFlags::SORT_EVERY_ROW); + calculateTBMRs(src, keypoints, mask); - // append max tree tbmrs - calculateTBMRs(src, keypoints, mask); - // append min tree tbmrs - calculateTBMRs(src, keypoints, mask); - } + // reverse instead of sort + flip(S, S, -1); + calculateTBMRs(src, keypoints, mask); +} - CV_WRAP Ptr TBMR::create(int _min_area, double _max_area_relative) - { - return cv::makePtr(TBMR_Impl::Params(_min_area, _max_area_relative)); - } +CV_WRAP Ptr TBMR::create(int _min_area, double _max_area_relative) +{ + return cv::makePtr( + TBMR_Impl::Params(_min_area, _max_area_relative)); +} - String TBMR::getDefaultName() const - { - return (Feature2D::getDefaultName() + ".TBMR"); - } - } +String TBMR::getDefaultName() const +{ + return (Feature2D::getDefaultName() + ".TBMR"); } +} // namespace tbmr +} // namespace cv diff --git a/modules/tbmr/test/test_tbmr.cpp b/modules/tbmr/test/test_tbmr.cpp index ab6a6854c7e..fd337b4cac9 100644 --- a/modules/tbmr/test/test_tbmr.cpp +++ b/modules/tbmr/test/test_tbmr.cpp @@ -2,7 +2,8 @@ // // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. // -// By downloading, copying, installing or using the software you agree to this license. +// By downloading, copying, installing or using the software you agree to this +license. // If you do not agree to this license, do not download, install, // copy or use the software. // @@ -14,23 +15,29 @@ // Copyright (C) 2009, Willow Garage Inc., all rights reserved. // Third party copyrights are property of their respective owners. // -// Redistribution and use in source and binary forms, with or without modification, +// Redistribution and use in source and binary forms, with or without +modification, // are permitted provided that the following conditions are met: // // * Redistribution's of source code must retain the above copyright notice, // this list of conditions and the following disclaimer. // -// * Redistribution's in binary form must reproduce the above copyright notice, +// * Redistribution's in binary form must reproduce the above copyright +notice, // this list of conditions and the following disclaimer in the documentation // and/or other materials provided with the distribution. // -// * The name of the copyright holders may not be used to endorse or promote products +// * The name of the copyright holders may not be used to endorse or promote +products // derived from this software without specific prior written permission. // -// This software is provided by the copyright holders and contributors "as is" and +// This software is provided by the copyright holders and contributors "as is" +and // any express or implied warranties, including, but not limited to, the implied -// warranties of merchantability and fitness for a particular purpose are disclaimed. -// In no event shall the Intel Corporation or contributors be liable for any direct, +// warranties of merchantability and fitness for a particular purpose are +disclaimed. +// In no event shall the Intel Corporation or contributors be liable for any +direct, // indirect, incidental, special, exemplary, or consequential damages // (including, but not limited to, procurement of substitute goods or services; // loss of use, data, or profits; or business interruption) however caused @@ -40,65 +47,74 @@ // //M*/ -#include "test_precomp.hpp" #include "cvconfig.h" +#include "test_precomp.hpp" //#include "opencv2/ts/ocl_test.hpp" -namespace opencv_test { +namespace opencv_test +{ + +class CV_TBMRTest : public cvtest::BaseTest +{ + public: + CV_TBMRTest(); + ~CV_TBMRTest(); + + protected: + void run(int /* idx */); +}; - class CV_TBMRTest : public cvtest::BaseTest +CV_TBMRTest::CV_TBMRTest() {} +CV_TBMRTest::~CV_TBMRTest() {} + +void CV_TBMRTest::run(int) +{ + using namespace tbmr; + Mat image; + image = imread(ts->get_data_path() + + "../cv/stereomatching/datasets/tsukuba/im2.png", + IMREAD_GRAYSCALE); + + if (image.empty()) { - public: - CV_TBMRTest(); - ~CV_TBMRTest(); - protected: - void run(int /* idx */); - }; + ts->printf(cvtest::TS::LOG, "Wrong input data \n"); + ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA); + return; + } + + Ptr tbmr = TBMR::create(30, 0.01); + // set the corresponding parameters + tbmr->setMinArea(30); + tbmr->setMaxAreaRelative(0.01); - CV_TBMRTest::CV_TBMRTest() {} - CV_TBMRTest::~CV_TBMRTest() {} + std::vector tbmrs; + tbmr->detect(image, tbmrs); - void CV_TBMRTest::run(int) + if (tbmrs.size() != 351) { - using namespace tbmr; - Mat image; - image = imread(ts->get_data_path() + "../cv/stereomatching/datasets/tsukuba/im2.png", IMREAD_GRAYSCALE); - - if (image.empty()) - { - ts->printf(cvtest::TS::LOG, "Wrong input data \n"); - ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA); - return; - } - - Ptr tbmr = TBMR::create(30, 0.01); - // set the corresponding parameters - tbmr->setMinArea(30); - tbmr->setMaxAreaRelative(0.01); - - std::vector tbmrs; - tbmr->detect(image, tbmrs); - - if (tbmrs.size() != 351) - { - ts->printf(cvtest::TS::LOG, "Invalid result \n"); - ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_OUTPUT); - return; - } + ts->printf(cvtest::TS::LOG, "Invalid result \n"); + ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_OUTPUT); + return; } +} - TEST(tbmr_simple_test, accuracy) { CV_TBMRTest test; test.safe_run(); } +TEST(tbmr_simple_test, accuracy) +{ + CV_TBMRTest test; + test.safe_run(); +} #ifdef HAVE_OPENCL - namespace ocl { +namespace ocl +{ - //OCL_TEST_F(cv::tbmr::TBMR, ABC1) - //{ - // // RunTest(cv::superres::createSuperResolution_BTVL1()); - //} +// OCL_TEST_F(cv::tbmr::TBMR, ABC1) +//{ +// // RunTest(cv::superres::createSuperResolution_BTVL1()); +//} - } // namespace opencv_test::ocl +} // namespace ocl #endif -} // namespace +} // namespace opencv_test From fb73bcc630ca94e997b4220774358ab8043ba1dd Mon Sep 17 00:00:00 2001 From: "steffen.ehrle" Date: Thu, 15 Oct 2020 18:19:39 +0200 Subject: [PATCH 11/22] fix test using virtual data. --- modules/tbmr/test/test_tbmr.cpp | 53 +++++++++++++++++---------------- 1 file changed, 27 insertions(+), 26 deletions(-) diff --git a/modules/tbmr/test/test_tbmr.cpp b/modules/tbmr/test/test_tbmr.cpp index fd337b4cac9..be4fb45de38 100644 --- a/modules/tbmr/test/test_tbmr.cpp +++ b/modules/tbmr/test/test_tbmr.cpp @@ -49,7 +49,6 @@ direct, #include "cvconfig.h" #include "test_precomp.hpp" -//#include "opencv2/ts/ocl_test.hpp" namespace opencv_test { @@ -70,32 +69,47 @@ CV_TBMRTest::~CV_TBMRTest() {} void CV_TBMRTest::run(int) { using namespace tbmr; - Mat image; - image = imread(ts->get_data_path() + - "../cv/stereomatching/datasets/tsukuba/im2.png", - IMREAD_GRAYSCALE); - if (image.empty()) - { - ts->printf(cvtest::TS::LOG, "Wrong input data \n"); - ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA); - return; - } + Mat image(100, 100, CV_8UC1); + image.setTo(Scalar(0)); + Point2f feature(50, 50); + double feature_angle = 5 * CV_PI / 180.; + + ellipse(image, feature, Size2f(40, 30), feature_angle * 180. / CV_PI, 0, + 360, Scalar(255), -1); + ellipse(image, Point2f(40, 50), Size2f(10, 5), 0, 0, 360, Scalar(128), -1); + ellipse(image, Point2f(70, 50), Size2f(10, 5), 0, 0, 360, Scalar(98), -1); Ptr tbmr = TBMR::create(30, 0.01); + // set the corresponding parameters tbmr->setMinArea(30); - tbmr->setMaxAreaRelative(0.01); + + // For normal images we want to set maxAreaRelative to around 0.01. Here we + // have a feature bigger than half of the image. + tbmr->setMaxAreaRelative(0.9); std::vector tbmrs; tbmr->detect(image, tbmrs); - if (tbmrs.size() != 351) + if (tbmrs.size() != 1) + { ts->printf(cvtest::TS::LOG, "Invalid result \n"); ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_OUTPUT); return; } + + double feature_diff = cv::norm(tbmrs.at(0).pt - feature); + double angle_diff = (tbmrs.at(0).angle - feature_angle); + + if (feature_diff > 0.1 || angle_diff > 0.01) + + { + ts->printf(cvtest::TS::LOG, "Incorrect result \n"); + ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_OUTPUT); + return; + } } TEST(tbmr_simple_test, accuracy) @@ -103,18 +117,5 @@ TEST(tbmr_simple_test, accuracy) CV_TBMRTest test; test.safe_run(); } -#ifdef HAVE_OPENCL - -namespace ocl -{ - -// OCL_TEST_F(cv::tbmr::TBMR, ABC1) -//{ -// // RunTest(cv::superres::createSuperResolution_BTVL1()); -//} - -} // namespace ocl - -#endif } // namespace opencv_test From 3c59b3c2df0495a96b1029db0d088b9d8e8c010d Mon Sep 17 00:00:00 2001 From: "steffen.ehrle" Date: Thu, 5 Nov 2020 10:41:05 +0100 Subject: [PATCH 12/22] move tbmr to xfeatures2d. Add standard tests in xFeatures2d. --- modules/tbmr/CMakeLists.txt | 2 - modules/tbmr/README.md | 35 - modules/tbmr/doc/tbmr.bib | 13 - modules/tbmr/include/opencv2/tbmr.hpp | 68 - modules/tbmr/src/precomp.hpp | 52 - modules/tbmr/test/test_main.cpp | 45 - modules/tbmr/test/test_precomp.hpp | 48 - modules/tbmr/test/test_tbmr.cpp | 121 -- modules/xfeatures2d/doc/xfeatures2d.bib | 16 +- .../include/opencv2/xfeatures2d.hpp | 1207 +++++++++-------- modules/{tbmr => xfeatures2d}/src/tbmr.cpp | 62 +- modules/xfeatures2d/test/test_features2d.cpp | 310 +++-- modules/xfeatures2d/test/test_keypoints.cpp | 85 +- 13 files changed, 969 insertions(+), 1095 deletions(-) delete mode 100644 modules/tbmr/CMakeLists.txt delete mode 100644 modules/tbmr/README.md delete mode 100644 modules/tbmr/doc/tbmr.bib delete mode 100644 modules/tbmr/include/opencv2/tbmr.hpp delete mode 100644 modules/tbmr/src/precomp.hpp delete mode 100644 modules/tbmr/test/test_main.cpp delete mode 100644 modules/tbmr/test/test_precomp.hpp delete mode 100644 modules/tbmr/test/test_tbmr.cpp rename modules/{tbmr => xfeatures2d}/src/tbmr.cpp (89%) diff --git a/modules/tbmr/CMakeLists.txt b/modules/tbmr/CMakeLists.txt deleted file mode 100644 index fb68d773ac6..00000000000 --- a/modules/tbmr/CMakeLists.txt +++ /dev/null @@ -1,2 +0,0 @@ -set(the_description "Tree-Based Morse Regions") -ocv_define_module(tbmr opencv_core opencv_features2d WRAP python) \ No newline at end of file diff --git a/modules/tbmr/README.md b/modules/tbmr/README.md deleted file mode 100644 index 340a02b5bc5..00000000000 --- a/modules/tbmr/README.md +++ /dev/null @@ -1,35 +0,0 @@ -## OpenCV Tree-Based Morse REgions - -Author and maintainers: Steffen Ehrle (steffen.ehrle@myestro.de). - -Tree Based Morse Regions (TBMR) is a topological approach to local invariant feature detection motivated by Morse theory. - -The algorithm can be seen as a variant of [MSER](https://docs.opencv.org/3.4.2/d3/d28/classcv_1_1MSER.html) as both algorithms rely on Min/Max-tree. -However TBMRs being purely topological are invariant to affine illumination change. - -For more details about the algorithm, please refer to the original paper: [6940260] - -### usage - -c++ interface: - -```c++ -using namespace tbmr; -// read a image -Mat img = imread(image_path), res; - -// create engine -// TBMR uses only 2 parameters: MinSize and MaxSizeRel. -// MinSize, pruning areas smaller than MinSize -// MaxSizeRel, pruning areas bigger than MaxSize = MaxSizeRel * img.size -Ptr alg = TBMR::create(30, 0.01); - -// perform Keypoint Extraction -// TBMR features provide Point, diameter and angle -std::vector tbmrs; -res = alg->detect(img, tbmrs); -``` - -### Reference - -[6940260]: Y. Xu and P. Monasse and T. Géraud and L. Najman Tree-Based Morse Regions: A Topological Approach to Local Feature Detection. \ No newline at end of file diff --git a/modules/tbmr/doc/tbmr.bib b/modules/tbmr/doc/tbmr.bib deleted file mode 100644 index 2466babf7a8..00000000000 --- a/modules/tbmr/doc/tbmr.bib +++ /dev/null @@ -1,13 +0,0 @@ -@ARTICLE{6940260, - author={Y. {Xu} and P. {Monasse} and T. {Géraud} and L. {Najman}}, - journal={IEEE Transactions on Image Processing}, - title={Tree-Based Morse Regions: A Topological Approach to Local Feature Detection}, - year={2014}, - volume={23}, - number={12}, - pages={5612-5625}, - abstract={This paper introduces a topological approach to local invariant feature detection motivated by Morse theory. We use the critical points of the graph of the intensity image, revealing directly the topology information as initial interest points. Critical points are selected from what we call a tree-based shape-space. In particular, they are selected from both the connected components of the upper level sets of the image (the Max-tree) and those of the lower level sets (the Min-tree). They correspond to specific nodes on those two trees: 1) to the leaves (extrema) and 2) to the nodes having bifurcation (saddle points). We then associate to each critical point the largest region that contains it and is topologically equivalent in its tree. We call such largest regions the tree-based Morse regions (TBMRs). The TBMR can be seen as a variant of maximally stable extremal region (MSER), which are contrasted regions. Contrarily to MSER, TBMR relies only on topological information and thus fully inherit the invariance properties of the space of shapes (e.g., invariance to affine contrast changes and covariance to continuous transformations). In particular, TBMR extracts the regions independently of the contrast, which makes it truly contrast invariant. Furthermore, it is quasi-parameter free. TBMR extraction is fast, having the same complexity as MSER. Experimentally, TBMR achieves a repeatability on par with state-of-the-art methods, but obtains a significantly higher number of features. Both the accuracy and robustness of TBMR are demonstrated by applications to image registration and 3D reconstruction.}, - keywords={feature extraction;image reconstruction;image registration;trees (mathematics);tree-based Morse regions;topological approach;local invariant feature detection;Morse theory;intensity image;initial interest points;critical points;tree-based shape-space;upper level image sets;Max-tree;lower level sets;Min-tree;saddle points;bifurcation;maximally stable extremal region variant;MSER;topological information;TBMR extraction;3D reconstruction;image registration;Feature extraction;Detectors;Shape;Time complexity;Level set;Three-dimensional displays;Image registration;Min/Max tree;local features;affine region detectors;image registration;3D reconstruction;Min/Max tree;local features;affine region detectors;image registration;3D reconstruction}, - doi={10.1109/TIP.2014.2364127}, - ISSN={1941-0042}, - month={Dec},} \ No newline at end of file diff --git a/modules/tbmr/include/opencv2/tbmr.hpp b/modules/tbmr/include/opencv2/tbmr.hpp deleted file mode 100644 index f8698c0a4a6..00000000000 --- a/modules/tbmr/include/opencv2/tbmr.hpp +++ /dev/null @@ -1,68 +0,0 @@ -// This file is part of OpenCV project. -// It is subject to the license terms in the LICENSE file found in the top-level -// directory of this distribution and at http://opencv.org/license.html. - -#ifndef OPENCV_TBMR_HPP -#define OPENCV_TBMR_HPP - -#include "opencv2/features2d.hpp" - -namespace cv -{ -namespace tbmr -{ - -/** @defgroup tbmr Tree Based Morse Features - -The opencv tbmr module contains an algorithm to ... -This module is implemented based on the paper Tree-Based Morse Regions: A -Topological Approach to Local Feature Detection, IEEE 2014. - - -Introduction to Tree-Based Morse Regions ----------------------------------------------- - - -This algorithm is executed in 2 stages: - -In the first stage, the algorithm computes Component trees (Min-tree and -Max-tree) based on the input image. - -In the second stage, the Min- and Max-trees are used to extract TBMR candidates. -The extraction can be compared to MSER but uses different criteria: Instead of -calculating a stable path along the tree, we look for siblings (nodes whos -parent has more than one child node) that have only one child (Morse regions). -These candidates are filtered by their ellipse shape and size. - -This TBMR implementation is adapting source code from -http://laurentnajman.org/index.php?page=tbmr. The Component tree calculation is -based on union-find [Berger 2007 ICIP] + rank. - -*/ - -//! @addtogroup tbmr -//! @{ -class CV_EXPORTS_W TBMR : public Feature2D -{ - public: - /** @brief Full constructor for %TBMR detector - @param _min_area prune areas smaller than minArea - @param _max_area_relative prune areas bigger than maxArea = - _max_area_relative * input_image_size - */ - CV_WRAP static Ptr create(int _min_area = 60, - double _max_area_relative = 0.01); - - CV_WRAP virtual void setMinArea(int minArea) = 0; - CV_WRAP virtual int getMinArea() const = 0; - CV_WRAP virtual void setMaxAreaRelative(double maxArea) = 0; - CV_WRAP virtual double getMaxAreaRelative() const = 0; - CV_WRAP virtual String getDefaultName() const CV_OVERRIDE; -}; - -//! @} tbmr - -} // namespace tbmr -} // namespace cv - -#endif diff --git a/modules/tbmr/src/precomp.hpp b/modules/tbmr/src/precomp.hpp deleted file mode 100644 index ec9e3ca59bc..00000000000 --- a/modules/tbmr/src/precomp.hpp +++ /dev/null @@ -1,52 +0,0 @@ -/*M/////////////////////////////////////////////////////////////////////////////////////// -// -// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. -// -// By downloading, copying, installing or using the software you agree to this license. -// If you do not agree to this license, do not download, install, -// copy or use the software. -// -// -// License Agreement -// For Open Source Computer Vision Library -// -// Copyright (C) 2000-2008, Intel Corporation, all rights reserved. -// Copyright (C) 2009, Willow Garage Inc., all rights reserved. -// Third party copyrights are property of their respective owners. -// -// Redistribution and use in source and binary forms, with or without modification, -// are permitted provided that the following conditions are met: -// -// * Redistribution's of source code must retain the above copyright notice, -// this list of conditions and the following disclaimer. -// -// * Redistribution's in binary form must reproduce the above copyright notice, -// this list of conditions and the following disclaimer in the documentation -// and/or other materials provided with the distribution. -// -// * The name of the copyright holders may not be used to endorse or promote products -// derived from this software without specific prior written permission. -// -// This software is provided by the copyright holders and contributors "as is" and -// any express or implied warranties, including, but not limited to, the implied -// warranties of merchantability and fitness for a particular purpose are disclaimed. -// In no event shall the Intel Corporation or contributors be liable for any direct, -// indirect, incidental, special, exemplary, or consequential damages -// (including, but not limited to, procurement of substitute goods or services; -// loss of use, data, or profits; or business interruption) however caused -// and on any theory of liability, whether in contract, strict liability, -// or tort (including negligence or otherwise) arising in any way out of -// the use of this software, even if advised of the possibility of such damage. -// -//M*/ - -#ifndef __OPENCV_PRECOMP_H__ -#define __OPENCV_PRECOMP_H__ - -#include "opencv2/features2d.hpp" -#include "opencv2/tbmr.hpp" - -//#include "opencv2/core/ocl.hpp" -#include - -#endif diff --git a/modules/tbmr/test/test_main.cpp b/modules/tbmr/test/test_main.cpp deleted file mode 100644 index 5af98d89d06..00000000000 --- a/modules/tbmr/test/test_main.cpp +++ /dev/null @@ -1,45 +0,0 @@ -/*M/////////////////////////////////////////////////////////////////////////////////////// -// -// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. -// -// By downloading, copying, installing or using the software you agree to this license. -// If you do not agree to this license, do not download, install, -// copy or use the software. -// -// -// License Agreement -// For Open Source Computer Vision Library -// -// Copyright (C) 2000-2008, Intel Corporation, all rights reserved. -// Copyright (C) 2009, Willow Garage Inc., all rights reserved. -// Third party copyrights are property of their respective owners. -// -// Redistribution and use in source and binary forms, with or without modification, -// are permitted provided that the following conditions are met: -// -// * Redistribution's of source code must retain the above copyright notice, -// this list of conditions and the following disclaimer. -// -// * Redistribution's in binary form must reproduce the above copyright notice, -// this list of conditions and the following disclaimer in the documentation -// and/or other materials provided with the distribution. -// -// * The name of the copyright holders may not be used to endorse or promote products -// derived from this software without specific prior written permission. -// -// This software is provided by the copyright holders and contributors "as is" and -// any express or implied warranties, including, but not limited to, the implied -// warranties of merchantability and fitness for a particular purpose are disclaimed. -// In no event shall the Intel Corporation or contributors be liable for any direct, -// indirect, incidental, special, exemplary, or consequential damages -// (including, but not limited to, procurement of substitute goods or services; -// loss of use, data, or profits; or business interruption) however caused -// and on any theory of liability, whether in contract, strict liability, -// or tort (including negligence or otherwise) arising in any way out of -// the use of this software, even if advised of the possibility of such damage. -// -//M*/ - -#include "test_precomp.hpp" - -CV_TEST_MAIN("tbmr") diff --git a/modules/tbmr/test/test_precomp.hpp b/modules/tbmr/test/test_precomp.hpp deleted file mode 100644 index fb15c09a3fe..00000000000 --- a/modules/tbmr/test/test_precomp.hpp +++ /dev/null @@ -1,48 +0,0 @@ -/*M/////////////////////////////////////////////////////////////////////////////////////// -// -// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. -// -// By downloading, copying, installing or using the software you agree to this license. -// If you do not agree to this license, do not download, install, -// copy or use the software. -// -// -// License Agreement -// For Open Source Computer Vision Library -// -// Copyright (C) 2000-2008, Intel Corporation, all rights reserved. -// Copyright (C) 2009, Willow Garage Inc., all rights reserved. -// Third party copyrights are property of their respective owners. -// -// Redistribution and use in source and binary forms, with or without modification, -// are permitted provided that the following conditions are met: -// -// * Redistribution's of source code must retain the above copyright notice, -// this list of conditions and the following disclaimer. -// -// * Redistribution's in binary form must reproduce the above copyright notice, -// this list of conditions and the following disclaimer in the documentation -// and/or other materials provided with the distribution. -// -// * The name of the copyright holders may not be used to endorse or promote products -// derived from this software without specific prior written permission. -// -// This software is provided by the copyright holders and contributors "as is" and -// any express or implied warranties, including, but not limited to, the implied -// warranties of merchantability and fitness for a particular purpose are disclaimed. -// In no event shall the Intel Corporation or contributors be liable for any direct, -// indirect, incidental, special, exemplary, or consequential damages -// (including, but not limited to, procurement of substitute goods or services; -// loss of use, data, or profits; or business interruption) however caused -// and on any theory of liability, whether in contract, strict liability, -// or tort (including negligence or otherwise) arising in any way out of -// the use of this software, even if advised of the possibility of such damage. -// -//M*/ -#ifndef __OPENCV_TEST_PRECOMP_HPP__ -#define __OPENCV_TEST_PRECOMP_HPP__ - -#include "opencv2/ts.hpp" -#include "opencv2/tbmr.hpp" - -#endif diff --git a/modules/tbmr/test/test_tbmr.cpp b/modules/tbmr/test/test_tbmr.cpp deleted file mode 100644 index be4fb45de38..00000000000 --- a/modules/tbmr/test/test_tbmr.cpp +++ /dev/null @@ -1,121 +0,0 @@ -/*M/////////////////////////////////////////////////////////////////////////////////////// -// -// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. -// -// By downloading, copying, installing or using the software you agree to this -license. -// If you do not agree to this license, do not download, install, -// copy or use the software. -// -// -// License Agreement -// For Open Source Computer Vision Library -// -// Copyright (C) 2000-2008, Intel Corporation, all rights reserved. -// Copyright (C) 2009, Willow Garage Inc., all rights reserved. -// Third party copyrights are property of their respective owners. -// -// Redistribution and use in source and binary forms, with or without -modification, -// are permitted provided that the following conditions are met: -// -// * Redistribution's of source code must retain the above copyright notice, -// this list of conditions and the following disclaimer. -// -// * Redistribution's in binary form must reproduce the above copyright -notice, -// this list of conditions and the following disclaimer in the documentation -// and/or other materials provided with the distribution. -// -// * The name of the copyright holders may not be used to endorse or promote -products -// derived from this software without specific prior written permission. -// -// This software is provided by the copyright holders and contributors "as is" -and -// any express or implied warranties, including, but not limited to, the implied -// warranties of merchantability and fitness for a particular purpose are -disclaimed. -// In no event shall the Intel Corporation or contributors be liable for any -direct, -// indirect, incidental, special, exemplary, or consequential damages -// (including, but not limited to, procurement of substitute goods or services; -// loss of use, data, or profits; or business interruption) however caused -// and on any theory of liability, whether in contract, strict liability, -// or tort (including negligence or otherwise) arising in any way out of -// the use of this software, even if advised of the possibility of such damage. -// -//M*/ - -#include "cvconfig.h" -#include "test_precomp.hpp" - -namespace opencv_test -{ - -class CV_TBMRTest : public cvtest::BaseTest -{ - public: - CV_TBMRTest(); - ~CV_TBMRTest(); - - protected: - void run(int /* idx */); -}; - -CV_TBMRTest::CV_TBMRTest() {} -CV_TBMRTest::~CV_TBMRTest() {} - -void CV_TBMRTest::run(int) -{ - using namespace tbmr; - - Mat image(100, 100, CV_8UC1); - image.setTo(Scalar(0)); - Point2f feature(50, 50); - double feature_angle = 5 * CV_PI / 180.; - - ellipse(image, feature, Size2f(40, 30), feature_angle * 180. / CV_PI, 0, - 360, Scalar(255), -1); - ellipse(image, Point2f(40, 50), Size2f(10, 5), 0, 0, 360, Scalar(128), -1); - ellipse(image, Point2f(70, 50), Size2f(10, 5), 0, 0, 360, Scalar(98), -1); - - Ptr tbmr = TBMR::create(30, 0.01); - - // set the corresponding parameters - tbmr->setMinArea(30); - - // For normal images we want to set maxAreaRelative to around 0.01. Here we - // have a feature bigger than half of the image. - tbmr->setMaxAreaRelative(0.9); - - std::vector tbmrs; - tbmr->detect(image, tbmrs); - - if (tbmrs.size() != 1) - - { - ts->printf(cvtest::TS::LOG, "Invalid result \n"); - ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_OUTPUT); - return; - } - - double feature_diff = cv::norm(tbmrs.at(0).pt - feature); - double angle_diff = (tbmrs.at(0).angle - feature_angle); - - if (feature_diff > 0.1 || angle_diff > 0.01) - - { - ts->printf(cvtest::TS::LOG, "Incorrect result \n"); - ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_OUTPUT); - return; - } -} - -TEST(tbmr_simple_test, accuracy) -{ - CV_TBMRTest test; - test.safe_run(); -} - -} // namespace opencv_test diff --git a/modules/xfeatures2d/doc/xfeatures2d.bib b/modules/xfeatures2d/doc/xfeatures2d.bib index b88be8bfe1e..6337a606d17 100644 --- a/modules/xfeatures2d/doc/xfeatures2d.bib +++ b/modules/xfeatures2d/doc/xfeatures2d.bib @@ -80,6 +80,20 @@ @article{Mikolajczyk2004 publisher = {Springer} } +@ARTICLE{Najman2014, + author={Y. {Xu} and P. {Monasse} and T. {Géraud} and L. {Najman}}, + journal={IEEE Transactions on Image Processing}, + title={Tree-Based Morse Regions: A Topological Approach to Local Feature Detection}, + year={2014}, + volume={23}, + number={12}, + pages={5612-5625}, + abstract={This paper introduces a topological approach to local invariant feature detection motivated by Morse theory. We use the critical points of the graph of the intensity image, revealing directly the topology information as initial interest points. Critical points are selected from what we call a tree-based shape-space. In particular, they are selected from both the connected components of the upper level sets of the image (the Max-tree) and those of the lower level sets (the Min-tree). They correspond to specific nodes on those two trees: 1) to the leaves (extrema) and 2) to the nodes having bifurcation (saddle points). We then associate to each critical point the largest region that contains it and is topologically equivalent in its tree. We call such largest regions the tree-based Morse regions (TBMRs). The TBMR can be seen as a variant of maximally stable extremal region (MSER), which are contrasted regions. Contrarily to MSER, TBMR relies only on topological information and thus fully inherit the invariance properties of the space of shapes (e.g., invariance to affine contrast changes and covariance to continuous transformations). In particular, TBMR extracts the regions independently of the contrast, which makes it truly contrast invariant. Furthermore, it is quasi-parameter free. TBMR extraction is fast, having the same complexity as MSER. Experimentally, TBMR achieves a repeatability on par with state-of-the-art methods, but obtains a significantly higher number of features. Both the accuracy and robustness of TBMR are demonstrated by applications to image registration and 3D reconstruction.}, + keywords={feature extraction;image reconstruction;image registration;trees (mathematics);tree-based Morse regions;topological approach;local invariant feature detection;Morse theory;intensity image;initial interest points;critical points;tree-based shape-space;upper level image sets;Max-tree;lower level sets;Min-tree;saddle points;bifurcation;maximally stable extremal region variant;MSER;topological information;TBMR extraction;3D reconstruction;image registration;Feature extraction;Detectors;Shape;Time complexity;Level set;Three-dimensional displays;Image registration;Min/Max tree;local features;affine region detectors;image registration;3D reconstruction;Min/Max tree;local features;affine region detectors;image registration;3D reconstruction}, + doi={10.1109/TIP.2014.2364127}, + ISSN={1941-0042}, + month={Dec},} + @article{Simonyan14, author = {Simonyan, K. and Vedaldi, A. and Zisserman, A.}, title = {Learning Local Feature Descriptors Using Convex Optimisation}, @@ -126,4 +140,4 @@ @incollection{LUCID pages = {1--9} year = {2012} publisher = {NIPS} -} +} \ No newline at end of file diff --git a/modules/xfeatures2d/include/opencv2/xfeatures2d.hpp b/modules/xfeatures2d/include/opencv2/xfeatures2d.hpp index b2eda6f643e..8e486d66ec3 100644 --- a/modules/xfeatures2d/include/opencv2/xfeatures2d.hpp +++ b/modules/xfeatures2d/include/opencv2/xfeatures2d.hpp @@ -50,14 +50,16 @@ This section describes experimental algorithms for 2d feature detection. @defgroup xfeatures2d_nonfree Non-free 2D Features Algorithms -This section describes two popular algorithms for 2d feature detection, SIFT and SURF, that are -known to be patented. You need to set the OPENCV_ENABLE_NONFREE option in cmake to use those. Use them at your own risk. +This section describes two popular algorithms for 2d feature detection, SIFT and +SURF, that are known to be patented. You need to set the OPENCV_ENABLE_NONFREE +option in cmake to use those. Use them at your own risk. @defgroup xfeatures2d_match Experimental 2D Features Matching Algorithm This section describes the following matching strategies: - GMS: Grid-based Motion Statistics, @cite Bian2017gms - - LOGOS: Local geometric support for high-outlier spatial verification, @cite Lowry2018LOGOSLG + - LOGOS: Local geometric support for high-outlier spatial verification, +@cite Lowry2018LOGOSLG @} */ @@ -70,13 +72,16 @@ namespace xfeatures2d //! @addtogroup xfeatures2d_experiment //! @{ -/** @brief Class implementing the FREAK (*Fast Retina Keypoint*) keypoint descriptor, described in @cite AOV12 . +/** @brief Class implementing the FREAK (*Fast Retina Keypoint*) keypoint +descriptor, described in @cite AOV12 . -The algorithm propose a novel keypoint descriptor inspired by the human visual system and more -precisely the retina, coined Fast Retina Key- point (FREAK). A cascade of binary strings is -computed by efficiently comparing image intensities over a retinal sampling pattern. FREAKs are in -general faster to compute with lower memory load and also more robust than SIFT, SURF or BRISK. -They are competitive alternatives to existing keypoints in particular for embedded applications. +The algorithm propose a novel keypoint descriptor inspired by the human visual +system and more precisely the retina, coined Fast Retina Key- point (FREAK). A +cascade of binary strings is computed by efficiently comparing image intensities +over a retinal sampling pattern. FREAKs are in general faster to compute with +lower memory load and also more robust than SIFT, SURF or BRISK. They are +competitive alternatives to existing keypoints in particular for embedded +applications. @note - An example on how to use the FREAK descriptor can be found at @@ -84,11 +89,10 @@ They are competitive alternatives to existing keypoints in particular for embedd */ class CV_EXPORTS_W FREAK : public Feature2D { -public: - - static const int NB_SCALES = 64; - static const int NB_PAIRS = 512; - static const int NB_ORIENPAIRS = 45; + public: + static const int NB_SCALES = 64; + static const int NB_PAIRS = 512; + static const int NB_ORIENPAIRS = 45; /** @param orientationNormalized Enable orientation normalization. @@ -97,24 +101,24 @@ class CV_EXPORTS_W FREAK : public Feature2D @param nOctaves Number of octaves covered by the detected keypoints. @param selectedPairs (Optional) user defined selected pairs indexes, */ - CV_WRAP static Ptr create(bool orientationNormalized = true, - bool scaleNormalized = true, - float patternScale = 22.0f, - int nOctaves = 4, - const std::vector& selectedPairs = std::vector()); + CV_WRAP static Ptr + create(bool orientationNormalized = true, bool scaleNormalized = true, + float patternScale = 22.0f, int nOctaves = 4, + const std::vector &selectedPairs = std::vector()); }; - -/** @brief The class implements the keypoint detector introduced by @cite Agrawal08, synonym of StarDetector. : +/** @brief The class implements the keypoint detector introduced by @cite + * Agrawal08, synonym of StarDetector. : */ class CV_EXPORTS_W StarDetector : public Feature2D { -public: + public: //! the full constructor - CV_WRAP static Ptr create(int maxSize=45, int responseThreshold=30, - int lineThresholdProjected=10, - int lineThresholdBinarized=8, - int suppressNonmaxSize=5); + CV_WRAP static Ptr create(int maxSize = 45, + int responseThreshold = 30, + int lineThresholdProjected = 10, + int lineThresholdBinarized = 8, + int suppressNonmaxSize = 5); }; /* @@ -123,17 +127,21 @@ class CV_EXPORTS_W StarDetector : public Feature2D /** @brief Class for computing BRIEF descriptors described in @cite calon2010 . -@param bytes legth of the descriptor in bytes, valid values are: 16, 32 (default) or 64 . -@param use_orientation sample patterns using keypoints orientation, disabled by default. +@param bytes legth of the descriptor in bytes, valid values are: 16, 32 +(default) or 64 . +@param use_orientation sample patterns using keypoints orientation, disabled by +default. */ class CV_EXPORTS_W BriefDescriptorExtractor : public Feature2D { -public: - CV_WRAP static Ptr create( int bytes = 32, bool use_orientation = false ); + public: + CV_WRAP static Ptr + create(int bytes = 32, bool use_orientation = false); }; -/** @brief Class implementing the locally uniform comparison image descriptor, described in @cite LUCID +/** @brief Class implementing the locally uniform comparison image descriptor, +described in @cite LUCID An image descriptor that can be computed very fast, while being about as robust as, for example, SURF or BRIEF. @@ -142,41 +150,53 @@ about as robust as, for example, SURF or BRIEF. */ class CV_EXPORTS_W LUCID : public Feature2D { -public: + public: /** - * @param lucid_kernel kernel for descriptor construction, where 1=3x3, 2=5x5, 3=7x7 and so forth - * @param blur_kernel kernel for blurring image prior to descriptor construction, where 1=3x3, 2=5x5, 3=7x7 and so forth + * @param lucid_kernel kernel for descriptor construction, where 1=3x3, + * 2=5x5, 3=7x7 and so forth + * @param blur_kernel kernel for blurring image prior to descriptor + * construction, where 1=3x3, 2=5x5, 3=7x7 and so forth */ - CV_WRAP static Ptr create(const int lucid_kernel = 1, const int blur_kernel = 2); + CV_WRAP static Ptr create(const int lucid_kernel = 1, + const int blur_kernel = 2); }; - /* -* LATCH Descriptor -*/ + * LATCH Descriptor + */ /** latch Class for computing the LATCH descriptor. -If you find this code useful, please add a reference to the following paper in your work: -Gil Levi and Tal Hassner, "LATCH: Learned Arrangements of Three Patch Codes", arXiv preprint arXiv:1501.03719, 15 Jan. 2015 +If you find this code useful, please add a reference to the following paper in +your work: Gil Levi and Tal Hassner, "LATCH: Learned Arrangements of Three Patch +Codes", arXiv preprint arXiv:1501.03719, 15 Jan. 2015 -LATCH is a binary descriptor based on learned comparisons of triplets of image patches. +LATCH is a binary descriptor based on learned comparisons of triplets of image +patches. * bytes is the size of the descriptor - can be 64, 32, 16, 8, 4, 2 or 1 -* rotationInvariance - whether or not the descriptor should compansate for orientation changes. -* half_ssd_size - the size of half of the mini-patches size. For example, if we would like to compare triplets of patches of size 7x7x - then the half_ssd_size should be (7-1)/2 = 3. -* sigma - sigma value for GaussianBlur smoothing of the source image. Source image will be used without smoothing in case sigma value is 0. - -Note: the descriptor can be coupled with any keypoint extractor. The only demand is that if you use set rotationInvariance = True then - you will have to use an extractor which estimates the patch orientation (in degrees). Examples for such extractors are ORB and SIFT. - -Note: a complete example can be found under /samples/cpp/tutorial_code/xfeatures2D/latch_match.cpp +* rotationInvariance - whether or not the descriptor should compansate for +orientation changes. +* half_ssd_size - the size of half of the mini-patches size. For example, if we +would like to compare triplets of patches of size 7x7x then the half_ssd_size +should be (7-1)/2 = 3. +* sigma - sigma value for GaussianBlur smoothing of the source image. Source +image will be used without smoothing in case sigma value is 0. + +Note: the descriptor can be coupled with any keypoint extractor. The only demand +is that if you use set rotationInvariance = True then you will have to use an +extractor which estimates the patch orientation (in degrees). Examples for such +extractors are ORB and SIFT. + +Note: a complete example can be found under +/samples/cpp/tutorial_code/xfeatures2D/latch_match.cpp */ class CV_EXPORTS_W LATCH : public Feature2D { -public: - CV_WRAP static Ptr create(int bytes = 32, bool rotationInvariance = true, int half_ssd_size = 3, double sigma = 2.0); + public: + CV_WRAP static Ptr create(int bytes = 32, + bool rotationInvariance = true, + int half_ssd_size = 3, double sigma = 2.0); }; /** @brief Class implementing DAISY descriptor, described in @cite Tola10 @@ -187,48 +207,59 @@ class CV_EXPORTS_W LATCH : public Feature2D @param q_hist amount of gradient orientations range division quantity @param norm choose descriptors normalization type, where DAISY::NRM_NONE will not do any normalization (default), -DAISY::NRM_PARTIAL mean that histograms are normalized independently for L2 norm equal to 1.0, -DAISY::NRM_FULL mean that descriptors are normalized for L2 norm equal to 1.0, -DAISY::NRM_SIFT mean that descriptors are normalized for L2 norm equal to 1.0 but no individual one is bigger than 0.154 as in SIFT -@param H optional 3x3 homography matrix used to warp the grid of daisy but sampling keypoints remains unwarped on image -@param interpolation switch to disable interpolation for speed improvement at minor quality loss -@param use_orientation sample patterns using keypoints orientation, disabled by default. +DAISY::NRM_PARTIAL mean that histograms are normalized independently for L2 norm +equal to 1.0, DAISY::NRM_FULL mean that descriptors are normalized for L2 norm +equal to 1.0, DAISY::NRM_SIFT mean that descriptors are normalized for L2 norm +equal to 1.0 but no individual one is bigger than 0.154 as in SIFT +@param H optional 3x3 homography matrix used to warp the grid of daisy but +sampling keypoints remains unwarped on image +@param interpolation switch to disable interpolation for speed improvement at +minor quality loss +@param use_orientation sample patterns using keypoints orientation, disabled by +default. */ class CV_EXPORTS_W DAISY : public Feature2D { -public: + public: enum NormalizationType { - NRM_NONE = 100, NRM_PARTIAL = 101, NRM_FULL = 102, NRM_SIFT = 103, + NRM_NONE = 100, + NRM_PARTIAL = 101, + NRM_FULL = 102, + NRM_SIFT = 103, }; - CV_WRAP static Ptr create( float radius = 15, int q_radius = 3, int q_theta = 8, - int q_hist = 8, DAISY::NormalizationType norm = DAISY::NRM_NONE, InputArray H = noArray(), - bool interpolation = true, bool use_orientation = false ); + CV_WRAP static Ptr + create(float radius = 15, int q_radius = 3, int q_theta = 8, int q_hist = 8, + DAISY::NormalizationType norm = DAISY::NRM_NONE, + InputArray H = noArray(), bool interpolation = true, + bool use_orientation = false); /** @overload * @param image image to extract descriptors * @param keypoints of interest within image * @param descriptors resulted descriptors array */ - virtual void compute( InputArray image, std::vector& keypoints, OutputArray descriptors ) CV_OVERRIDE = 0; + virtual void compute(InputArray image, std::vector &keypoints, + OutputArray descriptors) CV_OVERRIDE = 0; - virtual void compute( InputArrayOfArrays images, - std::vector >& keypoints, - OutputArrayOfArrays descriptors ) CV_OVERRIDE; + virtual void compute(InputArrayOfArrays images, + std::vector> &keypoints, + OutputArrayOfArrays descriptors) CV_OVERRIDE; /** @overload * @param image image to extract descriptors * @param roi region of interest within image * @param descriptors resulted descriptors array for roi image pixels */ - virtual void compute( InputArray image, Rect roi, OutputArray descriptors ) = 0; + virtual void compute(InputArray image, Rect roi, + OutputArray descriptors) = 0; /**@overload * @param image image to extract descriptors * @param descriptors resulted descriptors array for all image pixels */ - virtual void compute( InputArray image, OutputArray descriptors ) = 0; + virtual void compute(InputArray image, OutputArray descriptors) = 0; /** * @param y position y on image @@ -236,7 +267,8 @@ class CV_EXPORTS_W DAISY : public Feature2D * @param orientation orientation on image (0->360) * @param descriptor supplied array for descriptor storage */ - virtual void GetDescriptor( double y, double x, int orientation, float* descriptor ) const = 0; + virtual void GetDescriptor(double y, double x, int orientation, + float *descriptor) const = 0; /** * @param y position y on image @@ -245,7 +277,8 @@ class CV_EXPORTS_W DAISY : public Feature2D * @param descriptor supplied array for descriptor storage * @param H homography matrix for warped grid */ - virtual bool GetDescriptor( double y, double x, int orientation, float* descriptor, double* H ) const = 0; + virtual bool GetDescriptor(double y, double x, int orientation, + float *descriptor, double *H) const = 0; /** * @param y position y on image @@ -253,7 +286,8 @@ class CV_EXPORTS_W DAISY : public Feature2D * @param orientation orientation on image (0->360) * @param descriptor supplied array for descriptor storage */ - virtual void GetUnnormalizedDescriptor( double y, double x, int orientation, float* descriptor ) const = 0; + virtual void GetUnnormalizedDescriptor(double y, double x, int orientation, + float *descriptor) const = 0; /** * @param y position y on image @@ -262,61 +296,75 @@ class CV_EXPORTS_W DAISY : public Feature2D * @param descriptor supplied array for descriptor storage * @param H homography matrix for warped grid */ - virtual bool GetUnnormalizedDescriptor( double y, double x, int orientation, float* descriptor , double *H ) const = 0; - + virtual bool GetUnnormalizedDescriptor(double y, double x, int orientation, + float *descriptor, + double *H) const = 0; }; -/** @brief Class implementing the MSD (*Maximal Self-Dissimilarity*) keypoint detector, described in @cite Tombari14. +/** @brief Class implementing the MSD (*Maximal Self-Dissimilarity*) keypoint +detector, described in @cite Tombari14. -The algorithm implements a novel interest point detector stemming from the intuition that image patches -which are highly dissimilar over a relatively large extent of their surroundings hold the property of -being repeatable and distinctive. This concept of "contextual self-dissimilarity" reverses the key -paradigm of recent successful techniques such as the Local Self-Similarity descriptor and the Non-Local -Means filter, which build upon the presence of similar - rather than dissimilar - patches. Moreover, -it extends to contextual information the local self-dissimilarity notion embedded in established -detectors of corner-like interest points, thereby achieving enhanced repeatability, distinctiveness and -localization accuracy. +The algorithm implements a novel interest point detector stemming from the +intuition that image patches which are highly dissimilar over a relatively large +extent of their surroundings hold the property of being repeatable and +distinctive. This concept of "contextual self-dissimilarity" reverses the key +paradigm of recent successful techniques such as the Local Self-Similarity +descriptor and the Non-Local Means filter, which build upon the presence of +similar - rather than dissimilar - patches. Moreover, it extends to contextual +information the local self-dissimilarity notion embedded in established +detectors of corner-like interest points, thereby achieving enhanced +repeatability, distinctiveness and localization accuracy. */ -class CV_EXPORTS_W MSDDetector : public Feature2D { - -public: +class CV_EXPORTS_W MSDDetector : public Feature2D +{ - static Ptr create(int m_patch_radius = 3, int m_search_area_radius = 5, - int m_nms_radius = 5, int m_nms_scale_radius = 0, float m_th_saliency = 250.0f, int m_kNN = 4, - float m_scale_factor = 1.25f, int m_n_scales = -1, bool m_compute_orientation = false); + public: + static Ptr + create(int m_patch_radius = 3, int m_search_area_radius = 5, + int m_nms_radius = 5, int m_nms_scale_radius = 0, + float m_th_saliency = 250.0f, int m_kNN = 4, + float m_scale_factor = 1.25f, int m_n_scales = -1, + bool m_compute_orientation = false); }; -/** @brief Class implementing VGG (Oxford Visual Geometry Group) descriptor trained end to end -using "Descriptor Learning Using Convex Optimisation" (DLCO) aparatus described in @cite Simonyan14. +/** @brief Class implementing VGG (Oxford Visual Geometry Group) descriptor +trained end to end using "Descriptor Learning Using Convex Optimisation" (DLCO) +aparatus described in @cite Simonyan14. -@param desc type of descriptor to use, VGG::VGG_120 is default (120 dimensions float) -Available types are VGG::VGG_120, VGG::VGG_80, VGG::VGG_64, VGG::VGG_48 +@param desc type of descriptor to use, VGG::VGG_120 is default (120 dimensions +float) Available types are VGG::VGG_120, VGG::VGG_80, VGG::VGG_64, VGG::VGG_48 @param isigma gaussian kernel value for image blur (default is 1.4f) -@param img_normalize use image sample intensity normalization (enabled by default) -@param use_orientation sample patterns using keypoints orientation, enabled by default -@param scale_factor adjust the sampling window of detected keypoints to 64.0f (VGG sampling window) -6.25f is default and fits for KAZE, SURF detected keypoints window ratio -6.75f should be the scale for SIFT detected keypoints window ratio -5.00f should be the scale for AKAZE, MSD, AGAST, FAST, BRISK keypoints window ratio -0.75f should be the scale for ORB keypoints ratio - -@param dsc_normalize clamp descriptors to 255 and convert to uchar CV_8UC1 (disabled by default) +@param img_normalize use image sample intensity normalization (enabled by +default) +@param use_orientation sample patterns using keypoints orientation, enabled by +default +@param scale_factor adjust the sampling window of detected keypoints to 64.0f +(VGG sampling window) 6.25f is default and fits for KAZE, SURF detected +keypoints window ratio 6.75f should be the scale for SIFT detected keypoints +window ratio 5.00f should be the scale for AKAZE, MSD, AGAST, FAST, BRISK +keypoints window ratio 0.75f should be the scale for ORB keypoints ratio + +@param dsc_normalize clamp descriptors to 255 and convert to uchar CV_8UC1 +(disabled by default) */ class CV_EXPORTS_W VGG : public Feature2D { -public: - - CV_WRAP enum - { - VGG_120 = 100, VGG_80 = 101, VGG_64 = 102, VGG_48 = 103, + public: + CV_WRAP enum { + VGG_120 = 100, + VGG_80 = 101, + VGG_64 = 102, + VGG_48 = 103, }; - CV_WRAP static Ptr create( int desc = VGG::VGG_120, float isigma = 1.4f, - bool img_normalize = true, bool use_scale_orientation = true, - float scale_factor = 6.25f, bool dsc_normalize = false ); + CV_WRAP static Ptr create(int desc = VGG::VGG_120, float isigma = 1.4f, + bool img_normalize = true, + bool use_scale_orientation = true, + float scale_factor = 6.25f, + bool dsc_normalize = false); CV_WRAP virtual void setSigma(const float isigma) = 0; CV_WRAP virtual float getSigma() const = 0; @@ -324,600 +372,636 @@ class CV_EXPORTS_W VGG : public Feature2D CV_WRAP virtual void setUseNormalizeImage(const bool img_normalize) = 0; CV_WRAP virtual bool getUseNormalizeImage() const = 0; - CV_WRAP virtual void setUseScaleOrientation(const bool use_scale_orientation) = 0; + CV_WRAP virtual void + setUseScaleOrientation(const bool use_scale_orientation) = 0; CV_WRAP virtual bool getUseScaleOrientation() const = 0; CV_WRAP virtual void setScaleFactor(const float scale_factor) = 0; CV_WRAP virtual float getScaleFactor() const = 0; - CV_WRAP virtual void setUseNormalizeDescriptor(const bool dsc_normalize) = 0; + CV_WRAP virtual void + setUseNormalizeDescriptor(const bool dsc_normalize) = 0; CV_WRAP virtual bool getUseNormalizeDescriptor() const = 0; }; -/** @brief Class implementing BoostDesc (Learning Image Descriptors with Boosting), described in +/** @brief Class implementing BoostDesc (Learning Image Descriptors with +Boosting), described in @cite Trzcinski13a and @cite Trzcinski13b. -@param desc type of descriptor to use, BoostDesc::BINBOOST_256 is default (256 bit long dimension) -Available types are: BoostDesc::BGM, BoostDesc::BGM_HARD, BoostDesc::BGM_BILINEAR, BoostDesc::LBGM, -BoostDesc::BINBOOST_64, BoostDesc::BINBOOST_128, BoostDesc::BINBOOST_256 -@param use_orientation sample patterns using keypoints orientation, enabled by default +@param desc type of descriptor to use, BoostDesc::BINBOOST_256 is default (256 +bit long dimension) Available types are: BoostDesc::BGM, BoostDesc::BGM_HARD, +BoostDesc::BGM_BILINEAR, BoostDesc::LBGM, BoostDesc::BINBOOST_64, +BoostDesc::BINBOOST_128, BoostDesc::BINBOOST_256 +@param use_orientation sample patterns using keypoints orientation, enabled by +default @param scale_factor adjust the sampling window of detected keypoints 6.25f is default and fits for KAZE, SURF detected keypoints window ratio 6.75f should be the scale for SIFT detected keypoints window ratio -5.00f should be the scale for AKAZE, MSD, AGAST, FAST, BRISK keypoints window ratio -0.75f should be the scale for ORB keypoints ratio -1.50f was the default in original implementation - -@note BGM is the base descriptor where each binary dimension is computed as the output of a single weak learner. -BGM_HARD and BGM_BILINEAR refers to same BGM but use different type of gradient binning. In the BGM_HARD that -use ASSIGN_HARD binning type the gradient is assigned to the nearest orientation bin. In the BGM_BILINEAR that use -ASSIGN_BILINEAR binning type the gradient is assigned to the two neighbouring bins. In the BGM and all other modes that use -ASSIGN_SOFT binning type the gradient is assigned to 8 nearest bins according to the cosine value between the gradient -angle and the bin center. LBGM (alias FP-Boost) is the floating point extension where each dimension is computed -as a linear combination of the weak learner responses. BINBOOST and subvariants are the binary extensions of LBGM -where each bit is computed as a thresholded linear combination of a set of weak learners. -BoostDesc header files (boostdesc_*.i) was exported from original binaries with export-boostdesc.py script from -samples subfolder. +5.00f should be the scale for AKAZE, MSD, AGAST, FAST, BRISK keypoints window +ratio 0.75f should be the scale for ORB keypoints ratio 1.50f was the default in +original implementation + +@note BGM is the base descriptor where each binary dimension is computed as the +output of a single weak learner. BGM_HARD and BGM_BILINEAR refers to same BGM +but use different type of gradient binning. In the BGM_HARD that use ASSIGN_HARD +binning type the gradient is assigned to the nearest orientation bin. In the +BGM_BILINEAR that use ASSIGN_BILINEAR binning type the gradient is assigned to +the two neighbouring bins. In the BGM and all other modes that use ASSIGN_SOFT +binning type the gradient is assigned to 8 nearest bins according to the cosine +value between the gradient angle and the bin center. LBGM (alias FP-Boost) is +the floating point extension where each dimension is computed as a linear +combination of the weak learner responses. BINBOOST and subvariants are the +binary extensions of LBGM where each bit is computed as a thresholded linear +combination of a set of weak learners. BoostDesc header files (boostdesc_*.i) +was exported from original binaries with export-boostdesc.py script from samples +subfolder. */ class CV_EXPORTS_W BoostDesc : public Feature2D { -public: - - CV_WRAP enum - { - BGM = 100, BGM_HARD = 101, BGM_BILINEAR = 102, LBGM = 200, - BINBOOST_64 = 300, BINBOOST_128 = 301, BINBOOST_256 = 302 + public: + CV_WRAP enum { + BGM = 100, + BGM_HARD = 101, + BGM_BILINEAR = 102, + LBGM = 200, + BINBOOST_64 = 300, + BINBOOST_128 = 301, + BINBOOST_256 = 302 }; - CV_WRAP static Ptr create( int desc = BoostDesc::BINBOOST_256, - bool use_scale_orientation = true, float scale_factor = 6.25f ); + CV_WRAP static Ptr create(int desc = BoostDesc::BINBOOST_256, + bool use_scale_orientation = true, + float scale_factor = 6.25f); - CV_WRAP virtual void setUseScaleOrientation(const bool use_scale_orientation) = 0; + CV_WRAP virtual void + setUseScaleOrientation(const bool use_scale_orientation) = 0; CV_WRAP virtual bool getUseScaleOrientation() const = 0; CV_WRAP virtual void setScaleFactor(const float scale_factor) = 0; CV_WRAP virtual float getScaleFactor() const = 0; }; - /* -* Position-Color-Texture signatures -*/ + * Position-Color-Texture signatures + */ /** -* @brief Class implementing PCT (position-color-texture) signature extraction -* as described in @cite KrulisLS16. -* The algorithm is divided to a feature sampler and a clusterizer. -* Feature sampler produces samples at given set of coordinates. -* Clusterizer then produces clusters of these samples using k-means algorithm. -* Resulting set of clusters is the signature of the input image. -* -* A signature is an array of SIGNATURE_DIMENSION-dimensional points. -* Used dimensions are: -* weight, x, y position; lab color, contrast, entropy. -* @cite KrulisLS16 -* @cite BeecksUS10 -*/ + * @brief Class implementing PCT (position-color-texture) signature extraction + * as described in @cite KrulisLS16. + * The algorithm is divided to a feature sampler and a clusterizer. + * Feature sampler produces samples at given set of coordinates. + * Clusterizer then produces clusters of these samples using k-means + * algorithm. Resulting set of clusters is the signature of the input image. + * + * A signature is an array of SIGNATURE_DIMENSION-dimensional points. + * Used dimensions are: + * weight, x, y position; lab color, contrast, entropy. + * @cite KrulisLS16 + * @cite BeecksUS10 + */ class CV_EXPORTS_W PCTSignatures : public Algorithm { -public: + public: /** - * @brief Lp distance function selector. - */ + * @brief Lp distance function selector. + */ enum DistanceFunction { - L0_25, L0_5, L1, L2, L2SQUARED, L5, L_INFINITY + L0_25, + L0_5, + L1, + L2, + L2SQUARED, + L5, + L_INFINITY }; /** - * @brief Point distributions supported by random point generator. - */ + * @brief Point distributions supported by random point generator. + */ enum PointDistribution { - UNIFORM, //!< Generate numbers uniformly. - REGULAR, //!< Generate points in a regular grid. - NORMAL //!< Generate points with normal (gaussian) distribution. + UNIFORM, //!< Generate numbers uniformly. + REGULAR, //!< Generate points in a regular grid. + NORMAL //!< Generate points with normal (gaussian) distribution. }; /** - * @brief Similarity function selector. - * @see - * Christian Beecks, Merih Seran Uysal, Thomas Seidl. - * Signature quadratic form distance. - * In Proceedings of the ACM International Conference on Image and Video Retrieval, pages 438-445. - * ACM, 2010. - * @cite BeecksUS10 - * @note For selected distance function: \f[ d(c_i, c_j) \f] and parameter: \f[ \alpha \f] - */ + * @brief Similarity function selector. + * @see + * Christian Beecks, Merih Seran Uysal, Thomas Seidl. + * Signature quadratic form distance. + * In Proceedings of the ACM International Conference on Image and + * Video Retrieval, pages 438-445. ACM, 2010. + * @cite BeecksUS10 + * @note For selected distance function: \f[ d(c_i, c_j) \f] and parameter: + * \f[ \alpha \f] + */ enum SimilarityFunction { - MINUS, //!< \f[ -d(c_i, c_j) \f] - GAUSSIAN, //!< \f[ e^{ -\alpha * d^2(c_i, c_j)} \f] - HEURISTIC //!< \f[ \frac{1}{\alpha + d(c_i, c_j)} \f] + MINUS, //!< \f[ -d(c_i, c_j) \f] + GAUSSIAN, //!< \f[ e^{ -\alpha * d^2(c_i, c_j)} \f] + HEURISTIC //!< \f[ \frac{1}{\alpha + d(c_i, c_j)} \f] }; - /** - * @brief Creates PCTSignatures algorithm using sample and seed count. - * It generates its own sets of sampling points and clusterization seed indexes. - * @param initSampleCount Number of points used for image sampling. - * @param initSeedCount Number of initial clusterization seeds. - * Must be lower or equal to initSampleCount - * @param pointDistribution Distribution of generated points. Default: UNIFORM. - * Available: UNIFORM, REGULAR, NORMAL. - * @return Created algorithm. - */ - CV_WRAP static Ptr create( - const int initSampleCount = 2000, - const int initSeedCount = 400, - const int pointDistribution = 0); - - /** - * @brief Creates PCTSignatures algorithm using pre-generated sampling points - * and number of clusterization seeds. It uses the provided - * sampling points and generates its own clusterization seed indexes. - * @param initSamplingPoints Sampling points used in image sampling. - * @param initSeedCount Number of initial clusterization seeds. - * Must be lower or equal to initSamplingPoints.size(). - * @return Created algorithm. - */ - CV_WRAP static Ptr create( - const std::vector& initSamplingPoints, - const int initSeedCount); + * @brief Creates PCTSignatures algorithm using sample and seed count. + * It generates its own sets of sampling points and clusterization + * seed indexes. + * @param initSampleCount Number of points used for image sampling. + * @param initSeedCount Number of initial clusterization seeds. + * Must be lower or equal to initSampleCount + * @param pointDistribution Distribution of generated points. Default: + * UNIFORM. Available: UNIFORM, REGULAR, NORMAL. + * @return Created algorithm. + */ + CV_WRAP static Ptr create(const int initSampleCount = 2000, + const int initSeedCount = 400, + const int pointDistribution = 0); + + /** + * @brief Creates PCTSignatures algorithm using pre-generated sampling + * points and number of clusterization seeds. It uses the provided sampling + * points and generates its own clusterization seed indexes. + * @param initSamplingPoints Sampling points used in image sampling. + * @param initSeedCount Number of initial clusterization seeds. + * Must be lower or equal to initSamplingPoints.size(). + * @return Created algorithm. + */ + CV_WRAP static Ptr + create(const std::vector &initSamplingPoints, + const int initSeedCount); /** - * @brief Creates PCTSignatures algorithm using pre-generated sampling points - * and clusterization seeds indexes. - * @param initSamplingPoints Sampling points used in image sampling. - * @param initClusterSeedIndexes Indexes of initial clusterization seeds. - * Its size must be lower or equal to initSamplingPoints.size(). - * @return Created algorithm. - */ - CV_WRAP static Ptr create( - const std::vector& initSamplingPoints, - const std::vector& initClusterSeedIndexes); - - + * @brief Creates PCTSignatures algorithm using pre-generated sampling + * points and clusterization seeds indexes. + * @param initSamplingPoints Sampling points used in image sampling. + * @param initClusterSeedIndexes Indexes of initial clusterization seeds. + * Its size must be lower or equal to initSamplingPoints.size(). + * @return Created algorithm. + */ + CV_WRAP static Ptr + create(const std::vector &initSamplingPoints, + const std::vector &initClusterSeedIndexes); /** - * @brief Computes signature of given image. - * @param image Input image of CV_8U type. - * @param signature Output computed signature. - */ - CV_WRAP virtual void computeSignature( - InputArray image, - OutputArray signature) const = 0; + * @brief Computes signature of given image. + * @param image Input image of CV_8U type. + * @param signature Output computed signature. + */ + CV_WRAP virtual void computeSignature(InputArray image, + OutputArray signature) const = 0; /** - * @brief Computes signatures for multiple images in parallel. - * @param images Vector of input images of CV_8U type. - * @param signatures Vector of computed signatures. - */ - CV_WRAP virtual void computeSignatures( - const std::vector& images, - std::vector& signatures) const = 0; - - /** - * @brief Draws signature in the source image and outputs the result. - * Signatures are visualized as a circle - * with radius based on signature weight - * and color based on signature color. - * Contrast and entropy are not visualized. - * @param source Source image. - * @param signature Image signature. - * @param result Output result. - * @param radiusToShorterSideRatio Determines maximal radius of signature in the output image. - * @param borderThickness Border thickness of the visualized signature. - */ - CV_WRAP static void drawSignature( - InputArray source, - InputArray signature, - OutputArray result, - float radiusToShorterSideRatio = 1.0 / 8, - int borderThickness = 1); - - /** - * @brief Generates initial sampling points according to selected point distribution. - * @param initPoints Output vector where the generated points will be saved. - * @param count Number of points to generate. - * @param pointDistribution Point distribution selector. - * Available: UNIFORM, REGULAR, NORMAL. - * @note Generated coordinates are in range [0..1) - */ - CV_WRAP static void generateInitPoints( - std::vector& initPoints, - const int count, - int pointDistribution); - + * @brief Computes signatures for multiple images in parallel. + * @param images Vector of input images of CV_8U type. + * @param signatures Vector of computed signatures. + */ + CV_WRAP virtual void + computeSignatures(const std::vector &images, + std::vector &signatures) const = 0; + + /** + * @brief Draws signature in the source image and outputs the result. + * Signatures are visualized as a circle + * with radius based on signature weight + * and color based on signature color. + * Contrast and entropy are not visualized. + * @param source Source image. + * @param signature Image signature. + * @param result Output result. + * @param radiusToShorterSideRatio Determines maximal radius of signature in + * the output image. + * @param borderThickness Border thickness of the visualized signature. + */ + CV_WRAP static void drawSignature(InputArray source, InputArray signature, + OutputArray result, + float radiusToShorterSideRatio = 1.0 / 8, + int borderThickness = 1); + + /** + * @brief Generates initial sampling points according to selected point + * distribution. + * @param initPoints Output vector where the generated points will be saved. + * @param count Number of points to generate. + * @param pointDistribution Point distribution selector. + * Available: UNIFORM, REGULAR, NORMAL. + * @note Generated coordinates are in range [0..1) + */ + CV_WRAP static void generateInitPoints(std::vector &initPoints, + const int count, + int pointDistribution); /**** sampler ****/ /** - * @brief Number of initial samples taken from the image. - */ + * @brief Number of initial samples taken from the image. + */ CV_WRAP virtual int getSampleCount() const = 0; /** - * @brief Color resolution of the greyscale bitmap represented in allocated bits - * (i.e., value 4 means that 16 shades of grey are used). - * The greyscale bitmap is used for computing contrast and entropy values. - */ + * @brief Color resolution of the greyscale bitmap represented in allocated + * bits (i.e., value 4 means that 16 shades of grey are used). The greyscale + * bitmap is used for computing contrast and entropy values. + */ CV_WRAP virtual int getGrayscaleBits() const = 0; /** - * @brief Color resolution of the greyscale bitmap represented in allocated bits - * (i.e., value 4 means that 16 shades of grey are used). - * The greyscale bitmap is used for computing contrast and entropy values. - */ + * @brief Color resolution of the greyscale bitmap represented in allocated + * bits (i.e., value 4 means that 16 shades of grey are used). The greyscale + * bitmap is used for computing contrast and entropy values. + */ CV_WRAP virtual void setGrayscaleBits(int grayscaleBits) = 0; /** - * @brief Size of the texture sampling window used to compute contrast and entropy - * (center of the window is always in the pixel selected by x,y coordinates - * of the corresponding feature sample). - */ + * @brief Size of the texture sampling window used to compute contrast and + * entropy (center of the window is always in the pixel selected by x,y + * coordinates of the corresponding feature sample). + */ CV_WRAP virtual int getWindowRadius() const = 0; /** - * @brief Size of the texture sampling window used to compute contrast and entropy - * (center of the window is always in the pixel selected by x,y coordinates - * of the corresponding feature sample). - */ + * @brief Size of the texture sampling window used to compute contrast and + * entropy (center of the window is always in the pixel selected by x,y + * coordinates of the corresponding feature sample). + */ CV_WRAP virtual void setWindowRadius(int radius) = 0; - /** - * @brief Weights (multiplicative constants) that linearly stretch individual axes of the feature space - * (x,y = position; L,a,b = color in CIE Lab space; c = contrast. e = entropy) - */ + * @brief Weights (multiplicative constants) that linearly stretch + * individual axes of the feature space (x,y = position; L,a,b = color in + * CIE Lab space; c = contrast. e = entropy) + */ CV_WRAP virtual float getWeightX() const = 0; /** - * @brief Weights (multiplicative constants) that linearly stretch individual axes of the feature space - * (x,y = position; L,a,b = color in CIE Lab space; c = contrast. e = entropy) - */ + * @brief Weights (multiplicative constants) that linearly stretch + * individual axes of the feature space (x,y = position; L,a,b = color in + * CIE Lab space; c = contrast. e = entropy) + */ CV_WRAP virtual void setWeightX(float weight) = 0; /** - * @brief Weights (multiplicative constants) that linearly stretch individual axes of the feature space - * (x,y = position; L,a,b = color in CIE Lab space; c = contrast. e = entropy) - */ + * @brief Weights (multiplicative constants) that linearly stretch + * individual axes of the feature space (x,y = position; L,a,b = color in + * CIE Lab space; c = contrast. e = entropy) + */ CV_WRAP virtual float getWeightY() const = 0; /** - * @brief Weights (multiplicative constants) that linearly stretch individual axes of the feature space - * (x,y = position; L,a,b = color in CIE Lab space; c = contrast. e = entropy) - */ + * @brief Weights (multiplicative constants) that linearly stretch + * individual axes of the feature space (x,y = position; L,a,b = color in + * CIE Lab space; c = contrast. e = entropy) + */ CV_WRAP virtual void setWeightY(float weight) = 0; /** - * @brief Weights (multiplicative constants) that linearly stretch individual axes of the feature space - * (x,y = position; L,a,b = color in CIE Lab space; c = contrast. e = entropy) - */ + * @brief Weights (multiplicative constants) that linearly stretch + * individual axes of the feature space (x,y = position; L,a,b = color in + * CIE Lab space; c = contrast. e = entropy) + */ CV_WRAP virtual float getWeightL() const = 0; /** - * @brief Weights (multiplicative constants) that linearly stretch individual axes of the feature space - * (x,y = position; L,a,b = color in CIE Lab space; c = contrast. e = entropy) - */ + * @brief Weights (multiplicative constants) that linearly stretch + * individual axes of the feature space (x,y = position; L,a,b = color in + * CIE Lab space; c = contrast. e = entropy) + */ CV_WRAP virtual void setWeightL(float weight) = 0; /** - * @brief Weights (multiplicative constants) that linearly stretch individual axes of the feature space - * (x,y = position; L,a,b = color in CIE Lab space; c = contrast. e = entropy) - */ + * @brief Weights (multiplicative constants) that linearly stretch + * individual axes of the feature space (x,y = position; L,a,b = color in + * CIE Lab space; c = contrast. e = entropy) + */ CV_WRAP virtual float getWeightA() const = 0; /** - * @brief Weights (multiplicative constants) that linearly stretch individual axes of the feature space - * (x,y = position; L,a,b = color in CIE Lab space; c = contrast. e = entropy) - */ + * @brief Weights (multiplicative constants) that linearly stretch + * individual axes of the feature space (x,y = position; L,a,b = color in + * CIE Lab space; c = contrast. e = entropy) + */ CV_WRAP virtual void setWeightA(float weight) = 0; /** - * @brief Weights (multiplicative constants) that linearly stretch individual axes of the feature space - * (x,y = position; L,a,b = color in CIE Lab space; c = contrast. e = entropy) - */ + * @brief Weights (multiplicative constants) that linearly stretch + * individual axes of the feature space (x,y = position; L,a,b = color in + * CIE Lab space; c = contrast. e = entropy) + */ CV_WRAP virtual float getWeightB() const = 0; /** - * @brief Weights (multiplicative constants) that linearly stretch individual axes of the feature space - * (x,y = position; L,a,b = color in CIE Lab space; c = contrast. e = entropy) - */ + * @brief Weights (multiplicative constants) that linearly stretch + * individual axes of the feature space (x,y = position; L,a,b = color in + * CIE Lab space; c = contrast. e = entropy) + */ CV_WRAP virtual void setWeightB(float weight) = 0; /** - * @brief Weights (multiplicative constants) that linearly stretch individual axes of the feature space - * (x,y = position; L,a,b = color in CIE Lab space; c = contrast. e = entropy) - */ + * @brief Weights (multiplicative constants) that linearly stretch + * individual axes of the feature space (x,y = position; L,a,b = color in + * CIE Lab space; c = contrast. e = entropy) + */ CV_WRAP virtual float getWeightContrast() const = 0; /** - * @brief Weights (multiplicative constants) that linearly stretch individual axes of the feature space - * (x,y = position; L,a,b = color in CIE Lab space; c = contrast. e = entropy) - */ + * @brief Weights (multiplicative constants) that linearly stretch + * individual axes of the feature space (x,y = position; L,a,b = color in + * CIE Lab space; c = contrast. e = entropy) + */ CV_WRAP virtual void setWeightContrast(float weight) = 0; /** - * @brief Weights (multiplicative constants) that linearly stretch individual axes of the feature space - * (x,y = position; L,a,b = color in CIE Lab space; c = contrast. e = entropy) - */ + * @brief Weights (multiplicative constants) that linearly stretch + * individual axes of the feature space (x,y = position; L,a,b = color in + * CIE Lab space; c = contrast. e = entropy) + */ CV_WRAP virtual float getWeightEntropy() const = 0; /** - * @brief Weights (multiplicative constants) that linearly stretch individual axes of the feature space - * (x,y = position; L,a,b = color in CIE Lab space; c = contrast. e = entropy) - */ + * @brief Weights (multiplicative constants) that linearly stretch + * individual axes of the feature space (x,y = position; L,a,b = color in + * CIE Lab space; c = contrast. e = entropy) + */ CV_WRAP virtual void setWeightEntropy(float weight) = 0; /** - * @brief Initial samples taken from the image. - * These sampled features become the input for clustering. - */ + * @brief Initial samples taken from the image. + * These sampled features become the input for clustering. + */ CV_WRAP virtual std::vector getSamplingPoints() const = 0; - - /** - * @brief Weights (multiplicative constants) that linearly stretch individual axes of the feature space. - * @param idx ID of the weight - * @param value Value of the weight - * @note - * WEIGHT_IDX = 0; - * X_IDX = 1; - * Y_IDX = 2; - * L_IDX = 3; - * A_IDX = 4; - * B_IDX = 5; - * CONTRAST_IDX = 6; - * ENTROPY_IDX = 7; - */ + * @brief Weights (multiplicative constants) that linearly stretch + * individual axes of the feature space. + * @param idx ID of the weight + * @param value Value of the weight + * @note + * WEIGHT_IDX = 0; + * X_IDX = 1; + * Y_IDX = 2; + * L_IDX = 3; + * A_IDX = 4; + * B_IDX = 5; + * CONTRAST_IDX = 6; + * ENTROPY_IDX = 7; + */ CV_WRAP virtual void setWeight(int idx, float value) = 0; /** - * @brief Weights (multiplicative constants) that linearly stretch individual axes of the feature space. - * @param weights Values of all weights. - * @note - * WEIGHT_IDX = 0; - * X_IDX = 1; - * Y_IDX = 2; - * L_IDX = 3; - * A_IDX = 4; - * B_IDX = 5; - * CONTRAST_IDX = 6; - * ENTROPY_IDX = 7; - */ - CV_WRAP virtual void setWeights(const std::vector& weights) = 0; - - /** - * @brief Translations of the individual axes of the feature space. - * @param idx ID of the translation - * @param value Value of the translation - * @note - * WEIGHT_IDX = 0; - * X_IDX = 1; - * Y_IDX = 2; - * L_IDX = 3; - * A_IDX = 4; - * B_IDX = 5; - * CONTRAST_IDX = 6; - * ENTROPY_IDX = 7; - */ + * @brief Weights (multiplicative constants) that linearly stretch + * individual axes of the feature space. + * @param weights Values of all weights. + * @note + * WEIGHT_IDX = 0; + * X_IDX = 1; + * Y_IDX = 2; + * L_IDX = 3; + * A_IDX = 4; + * B_IDX = 5; + * CONTRAST_IDX = 6; + * ENTROPY_IDX = 7; + */ + CV_WRAP virtual void setWeights(const std::vector &weights) = 0; + + /** + * @brief Translations of the individual axes of the feature space. + * @param idx ID of the translation + * @param value Value of the translation + * @note + * WEIGHT_IDX = 0; + * X_IDX = 1; + * Y_IDX = 2; + * L_IDX = 3; + * A_IDX = 4; + * B_IDX = 5; + * CONTRAST_IDX = 6; + * ENTROPY_IDX = 7; + */ CV_WRAP virtual void setTranslation(int idx, float value) = 0; /** - * @brief Translations of the individual axes of the feature space. - * @param translations Values of all translations. - * @note - * WEIGHT_IDX = 0; - * X_IDX = 1; - * Y_IDX = 2; - * L_IDX = 3; - * A_IDX = 4; - * B_IDX = 5; - * CONTRAST_IDX = 6; - * ENTROPY_IDX = 7; - */ - CV_WRAP virtual void setTranslations(const std::vector& translations) = 0; + * @brief Translations of the individual axes of the feature space. + * @param translations Values of all translations. + * @note + * WEIGHT_IDX = 0; + * X_IDX = 1; + * Y_IDX = 2; + * L_IDX = 3; + * A_IDX = 4; + * B_IDX = 5; + * CONTRAST_IDX = 6; + * ENTROPY_IDX = 7; + */ + CV_WRAP virtual void + setTranslations(const std::vector &translations) = 0; /** - * @brief Sets sampling points used to sample the input image. - * @param samplingPoints Vector of sampling points in range [0..1) - * @note Number of sampling points must be greater or equal to clusterization seed count. - */ - CV_WRAP virtual void setSamplingPoints(std::vector samplingPoints) = 0; - - + * @brief Sets sampling points used to sample the input image. + * @param samplingPoints Vector of sampling points in range [0..1) + * @note Number of sampling points must be greater or equal to + * clusterization seed count. + */ + CV_WRAP virtual void + setSamplingPoints(std::vector samplingPoints) = 0; /**** clusterizer ****/ /** - * @brief Initial seeds (initial number of clusters) for the k-means algorithm. - */ + * @brief Initial seeds (initial number of clusters) for the k-means + * algorithm. + */ CV_WRAP virtual std::vector getInitSeedIndexes() const = 0; /** - * @brief Initial seed indexes for the k-means algorithm. - */ - CV_WRAP virtual void setInitSeedIndexes(std::vector initSeedIndexes) = 0; + * @brief Initial seed indexes for the k-means algorithm. + */ + CV_WRAP virtual void + setInitSeedIndexes(std::vector initSeedIndexes) = 0; /** - * @brief Number of initial seeds (initial number of clusters) for the k-means algorithm. - */ + * @brief Number of initial seeds (initial number of clusters) for the + * k-means algorithm. + */ CV_WRAP virtual int getInitSeedCount() const = 0; /** - * @brief Number of iterations of the k-means clustering. - * We use fixed number of iterations, since the modified clustering is pruning clusters - * (not iteratively refining k clusters). - */ + * @brief Number of iterations of the k-means clustering. + * We use fixed number of iterations, since the modified clustering is + * pruning clusters (not iteratively refining k clusters). + */ CV_WRAP virtual int getIterationCount() const = 0; /** - * @brief Number of iterations of the k-means clustering. - * We use fixed number of iterations, since the modified clustering is pruning clusters - * (not iteratively refining k clusters). - */ + * @brief Number of iterations of the k-means clustering. + * We use fixed number of iterations, since the modified clustering is + * pruning clusters (not iteratively refining k clusters). + */ CV_WRAP virtual void setIterationCount(int iterationCount) = 0; /** - * @brief Maximal number of generated clusters. If the number is exceeded, - * the clusters are sorted by their weights and the smallest clusters are cropped. - */ + * @brief Maximal number of generated clusters. If the number is exceeded, + * the clusters are sorted by their weights and the smallest clusters + * are cropped. + */ CV_WRAP virtual int getMaxClustersCount() const = 0; /** - * @brief Maximal number of generated clusters. If the number is exceeded, - * the clusters are sorted by their weights and the smallest clusters are cropped. - */ + * @brief Maximal number of generated clusters. If the number is exceeded, + * the clusters are sorted by their weights and the smallest clusters + * are cropped. + */ CV_WRAP virtual void setMaxClustersCount(int maxClustersCount) = 0; /** - * @brief This parameter multiplied by the index of iteration gives lower limit for cluster size. - * Clusters containing fewer points than specified by the limit have their centroid dismissed - * and points are reassigned. - */ + * @brief This parameter multiplied by the index of iteration gives lower + * limit for cluster size. Clusters containing fewer points than specified + * by the limit have their centroid dismissed and points are reassigned. + */ CV_WRAP virtual int getClusterMinSize() const = 0; /** - * @brief This parameter multiplied by the index of iteration gives lower limit for cluster size. - * Clusters containing fewer points than specified by the limit have their centroid dismissed - * and points are reassigned. - */ + * @brief This parameter multiplied by the index of iteration gives lower + * limit for cluster size. Clusters containing fewer points than specified + * by the limit have their centroid dismissed and points are reassigned. + */ CV_WRAP virtual void setClusterMinSize(int clusterMinSize) = 0; /** - * @brief Threshold euclidean distance between two centroids. - * If two cluster centers are closer than this distance, - * one of the centroid is dismissed and points are reassigned. - */ + * @brief Threshold euclidean distance between two centroids. + * If two cluster centers are closer than this distance, + * one of the centroid is dismissed and points are reassigned. + */ CV_WRAP virtual float getJoiningDistance() const = 0; /** - * @brief Threshold euclidean distance between two centroids. - * If two cluster centers are closer than this distance, - * one of the centroid is dismissed and points are reassigned. - */ + * @brief Threshold euclidean distance between two centroids. + * If two cluster centers are closer than this distance, + * one of the centroid is dismissed and points are reassigned. + */ CV_WRAP virtual void setJoiningDistance(float joiningDistance) = 0; /** - * @brief Remove centroids in k-means whose weight is lesser or equal to given threshold. - */ + * @brief Remove centroids in k-means whose weight is lesser or equal to + * given threshold. + */ CV_WRAP virtual float getDropThreshold() const = 0; /** - * @brief Remove centroids in k-means whose weight is lesser or equal to given threshold. - */ + * @brief Remove centroids in k-means whose weight is lesser or equal to + * given threshold. + */ CV_WRAP virtual void setDropThreshold(float dropThreshold) = 0; /** - * @brief Distance function selector used for measuring distance between two points in k-means. - */ + * @brief Distance function selector used for measuring distance between two + * points in k-means. + */ CV_WRAP virtual int getDistanceFunction() const = 0; /** - * @brief Distance function selector used for measuring distance between two points in k-means. - * Available: L0_25, L0_5, L1, L2, L2SQUARED, L5, L_INFINITY. - */ + * @brief Distance function selector used for measuring distance between two + * points in k-means. Available: L0_25, L0_5, L1, L2, L2SQUARED, L5, + * L_INFINITY. + */ CV_WRAP virtual void setDistanceFunction(int distanceFunction) = 0; - }; /** -* @brief Class implementing Signature Quadratic Form Distance (SQFD). -* @see Christian Beecks, Merih Seran Uysal, Thomas Seidl. -* Signature quadratic form distance. -* In Proceedings of the ACM International Conference on Image and Video Retrieval, pages 438-445. -* ACM, 2010. -* @cite BeecksUS10 -*/ + * @brief Class implementing Signature Quadratic Form Distance (SQFD). + * @see Christian Beecks, Merih Seran Uysal, Thomas Seidl. + * Signature quadratic form distance. + * In Proceedings of the ACM International Conference on Image and Video + * Retrieval, pages 438-445. ACM, 2010. + * @cite BeecksUS10 + */ class CV_EXPORTS_W PCTSignaturesSQFD : public Algorithm { -public: - - /** - * @brief Creates the algorithm instance using selected distance function, - * similarity function and similarity function parameter. - * @param distanceFunction Distance function selector. Default: L2 - * Available: L0_25, L0_5, L1, L2, L2SQUARED, L5, L_INFINITY - * @param similarityFunction Similarity function selector. Default: HEURISTIC - * Available: MINUS, GAUSSIAN, HEURISTIC - * @param similarityParameter Parameter of the similarity function. - */ - CV_WRAP static Ptr create( - const int distanceFunction = 3, - const int similarityFunction = 2, - const float similarityParameter = 1.0f); - - /** - * @brief Computes Signature Quadratic Form Distance of two signatures. - * @param _signature0 The first signature. - * @param _signature1 The second signature. - */ - CV_WRAP virtual float computeQuadraticFormDistance( - InputArray _signature0, - InputArray _signature1) const = 0; + public: + /** + * @brief Creates the algorithm instance using selected distance function, + * similarity function and similarity function parameter. + * @param distanceFunction Distance function selector. Default: L2 + * Available: L0_25, L0_5, L1, L2, L2SQUARED, L5, L_INFINITY + * @param similarityFunction Similarity function selector. Default: + * HEURISTIC Available: MINUS, GAUSSIAN, HEURISTIC + * @param similarityParameter Parameter of the similarity function. + */ + CV_WRAP static Ptr + create(const int distanceFunction = 3, const int similarityFunction = 2, + const float similarityParameter = 1.0f); /** - * @brief Computes Signature Quadratic Form Distance between the reference signature - * and each of the other image signatures. - * @param sourceSignature The signature to measure distance of other signatures from. - * @param imageSignatures Vector of signatures to measure distance from the source signature. - * @param distances Output vector of measured distances. - */ - CV_WRAP virtual void computeQuadraticFormDistances( - const Mat& sourceSignature, - const std::vector& imageSignatures, - std::vector& distances) const = 0; - + * @brief Computes Signature Quadratic Form Distance of two signatures. + * @param _signature0 The first signature. + * @param _signature1 The second signature. + */ + CV_WRAP virtual float + computeQuadraticFormDistance(InputArray _signature0, + InputArray _signature1) const = 0; + + /** + * @brief Computes Signature Quadratic Form Distance between the reference + * signature and each of the other image signatures. + * @param sourceSignature The signature to measure distance of other + * signatures from. + * @param imageSignatures Vector of signatures to measure distance from the + * source signature. + * @param distances Output vector of measured distances. + */ + CV_WRAP virtual void + computeQuadraticFormDistances(const Mat &sourceSignature, + const std::vector &imageSignatures, + std::vector &distances) const = 0; }; /** -* @brief Elliptic region around an interest point. -*/ + * @brief Elliptic region around an interest point. + */ class CV_EXPORTS Elliptic_KeyPoint : public KeyPoint { -public: + public: Size_ axes; //!< the lengths of the major and minor ellipse axes - float si; //!< the integration scale at which the parameters were estimated - Matx23f transf; //!< the transformation between image space and local patch space + float si; //!< the integration scale at which the parameters were estimated + Matx23f transf; //!< the transformation between image space and local patch + //!< space Elliptic_KeyPoint(); Elliptic_KeyPoint(Point2f pt, float angle, Size axes, float size, float si); virtual ~Elliptic_KeyPoint(); }; /** - * @brief Class implementing the Harris-Laplace feature detector as described in @cite Mikolajczyk2004. + * @brief Class implementing the Harris-Laplace feature detector as described in + * @cite Mikolajczyk2004. */ class CV_EXPORTS_W HarrisLaplaceFeatureDetector : public Feature2D { -public: + public: /** * @brief Creates a new implementation instance. * * @param numOctaves the number of octaves in the scale-space pyramid * @param corn_thresh the threshold for the Harris cornerness measure - * @param DOG_thresh the threshold for the Difference-of-Gaussians scale selection + * @param DOG_thresh the threshold for the Difference-of-Gaussians scale + * selection * @param maxCorners the maximum number of corners to consider * @param num_layers the number of intermediate scales per octave */ - CV_WRAP static Ptr create( - int numOctaves=6, - float corn_thresh=0.01f, - float DOG_thresh=0.01f, - int maxCorners=5000, - int num_layers=4); + CV_WRAP static Ptr + create(int numOctaves = 6, float corn_thresh = 0.01f, + float DOG_thresh = 0.01f, int maxCorners = 5000, int num_layers = 4); }; /** * @brief Class implementing affine adaptation for key points. * - * A @ref FeatureDetector and a @ref DescriptorExtractor are wrapped to augment the - * detected points with their affine invariant elliptic region and to compute - * the feature descriptors on the regions after warping them into circles. + * A @ref FeatureDetector and a @ref DescriptorExtractor are wrapped to augment + * the detected points with their affine invariant elliptic region and to + * compute the feature descriptors on the regions after warping them into + * circles. * * The interface is equivalent to @ref Feature2D, adding operations for - * @ref Elliptic_KeyPoint "Elliptic_KeyPoints" instead of @ref KeyPoint "KeyPoints". + * @ref Elliptic_KeyPoint "Elliptic_KeyPoints" instead of @ref KeyPoint + * "KeyPoints". */ class CV_EXPORTS AffineFeature2D : public Feature2D { -public: + public: /** * @brief Creates an instance wrapping the given keypoint detector and * descriptor extractor. */ - static Ptr create( - Ptr keypoint_detector, - Ptr descriptor_extractor); + static Ptr + create(Ptr keypoint_detector, + Ptr descriptor_extractor); /** * @brief Creates an instance where keypoint detector and descriptor * extractor are identical. */ - static Ptr create( - Ptr keypoint_detector) + static Ptr create(Ptr keypoint_detector) { return create(keypoint_detector, keypoint_detector); } @@ -927,51 +1011,81 @@ class CV_EXPORTS AffineFeature2D : public Feature2D * @brief Detects keypoints in the image using the wrapped detector and * performs affine adaptation to augment them with their elliptic regions. */ - virtual void detect( - InputArray image, - CV_OUT std::vector& keypoints, - InputArray mask=noArray() ) = 0; + virtual void detect(InputArray image, + CV_OUT std::vector &keypoints, + InputArray mask = noArray()) = 0; using Feature2D::detectAndCompute; // overload, don't hide /** * @brief Detects keypoints and computes descriptors for their surrounding * regions, after warping them into circles. */ - virtual void detectAndCompute( - InputArray image, - InputArray mask, - CV_OUT std::vector& keypoints, - OutputArray descriptors, - bool useProvidedKeypoints=false ) = 0; + virtual void + detectAndCompute(InputArray image, InputArray mask, + CV_OUT std::vector &keypoints, + OutputArray descriptors, + bool useProvidedKeypoints = false) = 0; }; +/** +@brief Class implementing the Tree Based Morse Regions (TBMR) as described in +@cite Najman2014 extended with scaled extraction ability. + -/** @brief Estimates cornerness for prespecified KeyPoints using the FAST algorithm +@note This algorithm is based on Component Tree (Min/Max) as well as MSER but +uses a Morse-theory approach to extract features. + +Features are ellipses (similar to MSER, however a MSER feature can never be a +TBMR feature and vice versa). + +*/ +class CV_EXPORTS_W TBMR : public AffineFeature2D +{ + public: + /** @brief Full constructor for %TBMR detector + @param min_area prune areas smaller than minArea + @param max_area_relative prune areas bigger than maxArea = + max_area_relative * input_image_size + */ + CV_WRAP static Ptr create(int min_area = 60, + double max_area_relative = 0.01); + + CV_WRAP virtual void setMinArea(int minArea) = 0; + CV_WRAP virtual int getMinArea() const = 0; + CV_WRAP virtual void setMaxAreaRelative(double maxArea) = 0; + CV_WRAP virtual double getMaxAreaRelative() const = 0; + CV_WRAP virtual String getDefaultName() const CV_OVERRIDE; +}; + +/** @brief Estimates cornerness for prespecified KeyPoints using the FAST +algorithm @param image grayscale image where keypoints (corners) are detected. -@param keypoints keypoints which should be tested to fit the FAST criteria. Keypoints not being -detected as corners are removed. -@param threshold threshold on difference between intensity of the central pixel and pixels of a -circle around this pixel. -@param nonmaxSuppression if true, non-maximum suppression is applied to detected corners -(keypoints). +@param keypoints keypoints which should be tested to fit the FAST criteria. +Keypoints not being detected as corners are removed. +@param threshold threshold on difference between intensity of the central pixel +and pixels of a circle around this pixel. +@param nonmaxSuppression if true, non-maximum suppression is applied to detected +corners (keypoints). @param type one of the three neighborhoods as defined in the paper: FastFeatureDetector::TYPE_9_16, FastFeatureDetector::TYPE_7_12, FastFeatureDetector::TYPE_5_8 Detects corners using the FAST algorithm by @cite Rosten06 . */ -CV_EXPORTS void FASTForPointSet( InputArray image, CV_IN_OUT std::vector& keypoints, - int threshold, bool nonmaxSuppression=true, cv::FastFeatureDetector::DetectorType type=FastFeatureDetector::TYPE_9_16); - +CV_EXPORTS void FASTForPointSet(InputArray image, + CV_IN_OUT std::vector &keypoints, + int threshold, bool nonmaxSuppression = true, + cv::FastFeatureDetector::DetectorType type = + FastFeatureDetector::TYPE_9_16); //! @} - //! @addtogroup xfeatures2d_match //! @{ -/** @brief GMS (Grid-based Motion Statistics) feature matching strategy described in @cite Bian2017gms . +/** @brief GMS (Grid-based Motion Statistics) feature matching strategy + described in @cite Bian2017gms . @param size1 Input size of image1. @param size2 Input size of image2. @param keypoints1 Input keypoints of image1. @@ -982,32 +1096,43 @@ CV_EXPORTS void FASTForPointSet( InputArray image, CV_IN_OUT std::vector& keypoints1, const std::vector& keypoints2, - const std::vector& matches1to2, CV_OUT std::vector& matchesGMS, const bool withRotation = false, - const bool withScale = false, const double thresholdFactor = 6.0); - -/** @brief LOGOS (Local geometric support for high-outlier spatial verification) feature matching strategy described in @cite Lowry2018LOGOSLG . +CV_EXPORTS_W void matchGMS(const Size &size1, const Size &size2, + const std::vector &keypoints1, + const std::vector &keypoints2, + const std::vector &matches1to2, + CV_OUT std::vector &matchesGMS, + const bool withRotation = false, + const bool withScale = false, + const double thresholdFactor = 6.0); + +/** @brief LOGOS (Local geometric support for high-outlier spatial verification) + feature matching strategy described in @cite Lowry2018LOGOSLG . @param keypoints1 Input keypoints of image1. @param keypoints2 Input keypoints of image2. @param nn1 Index to the closest BoW centroid for each descriptors of image1. @param nn2 Index to the closest BoW centroid for each descriptors of image2. @param matches1to2 Matches returned by the LOGOS matching strategy. @note - This matching strategy is suitable for features matching against large scale database. - First step consists in constructing the bag-of-words (BoW) from a representative image database. - Image descriptors are then represented by their closest codevector (nearest BoW centroid). + This matching strategy is suitable for features matching against large + scale database. First step consists in constructing the bag-of-words (BoW) + from a representative image database. Image descriptors are then represented + by their closest codevector (nearest BoW centroid). */ -CV_EXPORTS_W void matchLOGOS(const std::vector& keypoints1, const std::vector& keypoints2, - const std::vector& nn1, const std::vector& nn2, - std::vector& matches1to2); +CV_EXPORTS_W void matchLOGOS(const std::vector &keypoints1, + const std::vector &keypoints2, + const std::vector &nn1, + const std::vector &nn2, + std::vector &matches1to2); //! @} -} -} +} // namespace xfeatures2d +} // namespace cv #endif diff --git a/modules/tbmr/src/tbmr.cpp b/modules/xfeatures2d/src/tbmr.cpp similarity index 89% rename from modules/tbmr/src/tbmr.cpp rename to modules/xfeatures2d/src/tbmr.cpp index 503dc03d0e4..61bb32c08d6 100644 --- a/modules/tbmr/src/tbmr.cpp +++ b/modules/xfeatures2d/src/tbmr.cpp @@ -6,7 +6,7 @@ namespace cv { -namespace tbmr +namespace xfeatures2d { class TBMR_Impl CV_FINAL : public TBMR { @@ -46,6 +46,15 @@ class TBMR_Impl CV_FINAL : public TBMR void detect(InputArray image, CV_OUT std::vector &keypoints, InputArray mask = noArray()) CV_OVERRIDE; + void detect(InputArray image, + CV_OUT std::vector &keypoints, + InputArray mask = noArray()) CV_OVERRIDE; + + void detectAndCompute(InputArray image, InputArray mask, + CV_OUT std::vector &keypoints, + OutputArray descriptors, + bool useProvidedKeypoints = false) CV_OVERRIDE; + CV_INLINE uint zfindroot(uint *parent, uint p) { if (parent[p] == p) @@ -154,7 +163,7 @@ class TBMR_Impl CV_FINAL : public TBMR free(dejaVu); } - void calculateTBMRs(const Mat &image, std::vector &tbmrs, + void calculateTBMRs(const Mat &image, std::vector &tbmrs, const Mat &mask) { uint imSize = image.cols * image.rows; @@ -378,6 +387,7 @@ class TBMR_Impl CV_FINAL : public TBMR 2); double minAxL = std::min(l1, l2); double majAxL = std::max(l1, l2); + if (minAxL >= 1.5 && v != 0 && (mask.empty() || mask.at(cvRound(y), cvRound(x)) != 0)) @@ -391,11 +401,11 @@ class TBMR_Impl CV_FINAL : public TBMR else theta = CV_PI / 2. + 0.5 * std::atan2(2 * b, (a - c)); - // we have all ellipse informations, but - // KeyPoint does not store ellipses. Use - // Elliptic_Keypoint from xFeatures2D? - tbmrs.push_back(KeyPoint(Point2f((float)x, (float)y), - (float)majAxL, (float)theta)); + float size = (float)majAxL; + + tbmrs.push_back(Elliptic_KeyPoint( + Point2f((float)x, (float)y), (float)theta, + cv::Size2f((float)majAxL, (float)minAxL), size, 1.f)); } } } @@ -410,7 +420,7 @@ class TBMR_Impl CV_FINAL : public TBMR Mat tempsrc; - // component tree representation: see + // component tree representation (parent,S): see // https://ieeexplore.ieee.org/document/6850018 Mat parent; Mat S; @@ -422,14 +432,31 @@ class TBMR_Impl CV_FINAL : public TBMR void TBMR_Impl::detect(InputArray _image, std::vector &keypoints, InputArray _mask) +{ + std::vector kp; + detect(_image, kp, _mask); + keypoints.resize(kp.size()); + for (size_t i = 0; i < kp.size(); ++i) + keypoints[i] = kp[i]; +} + +void TBMR_Impl::detect(InputArray _image, + std::vector &keypoints, + InputArray _mask) { Mat mask = _mask.getMat(); Mat src = _image.getMat(); keypoints.clear(); - CV_Assert(!src.empty()); - CV_Assert(src.type() == CV_8UC1); + if (src.empty()) + return; + + if (!mask.empty()) + { + CV_Assert(mask.type() == CV_8UC1); + CV_Assert(mask.size == src.size); + } if (!src.isContinuous()) { @@ -437,6 +464,9 @@ void TBMR_Impl::detect(InputArray _image, std::vector &keypoints, src = tempsrc; } + if (src.channels() != 1) + cv::cvtColor(src, src, cv::COLOR_BGR2GRAY); + // append max tree tbmrs sortIdx(src.reshape(1, 1), S, SortFlags::SORT_ASCENDING | SortFlags::SORT_EVERY_ROW); @@ -447,6 +477,16 @@ void TBMR_Impl::detect(InputArray _image, std::vector &keypoints, calculateTBMRs(src, keypoints, mask); } +void TBMR_Impl::detectAndCompute( + InputArray image, InputArray mask, + CV_OUT std::vector &keypoints, OutputArray descriptors, + bool useProvidedKeypoints) +{ + CV_INSTRUMENT_REGION(); + + CV_Error(Error::StsNotImplemented, ""); +} + CV_WRAP Ptr TBMR::create(int _min_area, double _max_area_relative) { return cv::makePtr( @@ -457,5 +497,5 @@ String TBMR::getDefaultName() const { return (Feature2D::getDefaultName() + ".TBMR"); } -} // namespace tbmr +} // namespace xfeatures2d } // namespace cv diff --git a/modules/xfeatures2d/test/test_features2d.cpp b/modules/xfeatures2d/test/test_features2d.cpp index 6a8d4b5db89..454f9210baf 100644 --- a/modules/xfeatures2d/test/test_features2d.cpp +++ b/modules/xfeatures2d/test/test_features2d.cpp @@ -2,7 +2,8 @@ // // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. // -// By downloading, copying, installing or using the software you agree to this license. +// By downloading, copying, installing or using the software you agree to this +license. // If you do not agree to this license, do not download, install, // copy or use the software. // @@ -13,23 +14,29 @@ // Copyright (C) 2000, Intel Corporation, all rights reserved. // Third party copyrights are property of their respective owners. // -// Redistribution and use in source and binary forms, with or without modification, +// Redistribution and use in source and binary forms, with or without +modification, // are permitted provided that the following conditions are met: // // * Redistribution's of source code must retain the above copyright notice, // this list of conditions and the following disclaimer. // -// * Redistribution's in binary form must reproduce the above copyright notice, +// * Redistribution's in binary form must reproduce the above copyright +notice, // this list of conditions and the following disclaimer in the documentation // and/or other materials provided with the distribution. // -// * The name of Intel Corporation may not be used to endorse or promote products +// * The name of Intel Corporation may not be used to endorse or promote +products // derived from this software without specific prior written permission. // -// This software is provided by the copyright holders and contributors "as is" and +// This software is provided by the copyright holders and contributors "as is" +and // any express or implied warranties, including, but not limited to, the implied -// warranties of merchantability and fitness for a particular purpose are disclaimed. -// In no event shall the Intel Corporation or contributors be liable for any direct, +// warranties of merchantability and fitness for a particular purpose are +disclaimed. +// In no event shall the Intel Corporation or contributors be liable for any +direct, // indirect, incidental, special, exemplary, or consequential damages // (including, but not limited to, procurement of substitute goods or services; // loss of use, data, or profits; or business interruption) however caused @@ -41,47 +48,65 @@ #include "test_precomp.hpp" -namespace opencv_test { namespace { +namespace opencv_test +{ +namespace +{ const string FEATURES2D_DIR = "features2d"; const string DETECTOR_DIR = FEATURES2D_DIR + "/feature_detectors"; const string DESCRIPTOR_DIR = FEATURES2D_DIR + "/descriptor_extractors"; const string IMAGE_FILENAME = "tsukuba.png"; -}} // namespace +} // namespace +} // namespace opencv_test -#include "features2d/test/test_detectors_regression.impl.hpp" #include "features2d/test/test_descriptors_regression.impl.hpp" +#include "features2d/test/test_detectors_regression.impl.hpp" -namespace opencv_test { namespace { +namespace opencv_test +{ +namespace +{ #ifdef OPENCV_ENABLE_NONFREE -TEST( Features2d_Detector_SURF, regression ) +TEST(Features2d_Detector_SURF, regression) { - CV_FeatureDetectorTest test( "detector-surf", SURF::create() ); + CV_FeatureDetectorTest test("detector-surf", SURF::create()); test.safe_run(); } #endif -TEST( Features2d_Detector_STAR, regression ) +TEST(Features2d_Detector_STAR, regression) { - CV_FeatureDetectorTest test( "detector-star", StarDetector::create() ); + CV_FeatureDetectorTest test("detector-star", StarDetector::create()); test.safe_run(); } -TEST( Features2d_Detector_Harris_Laplace, regression ) +TEST(Features2d_Detector_Harris_Laplace, regression) { - CV_FeatureDetectorTest test( "detector-harris-laplace", HarrisLaplaceFeatureDetector::create() ); + CV_FeatureDetectorTest test("detector-harris-laplace", + HarrisLaplaceFeatureDetector::create()); test.safe_run(); } -TEST( Features2d_Detector_Harris_Laplace_Affine_Keypoint_Invariance, regression ) +TEST(Features2d_Detector_Harris_Laplace_Affine_Keypoint_Invariance, regression) { - CV_FeatureDetectorTest test( "detector-harris-laplace", AffineFeature2D::create(HarrisLaplaceFeatureDetector::create())); + CV_FeatureDetectorTest test( + "detector-harris-laplace", + AffineFeature2D::create(HarrisLaplaceFeatureDetector::create())); test.safe_run(); } -TEST( Features2d_Detector_Harris_Laplace_Affine, regression ) +TEST(Features2d_Detector_Harris_Laplace_Affine, regression) { - CV_FeatureDetectorTest test( "detector-harris-laplace-affine", AffineFeature2D::create(HarrisLaplaceFeatureDetector::create())); + CV_FeatureDetectorTest test( + "detector-harris-laplace-affine", + AffineFeature2D::create(HarrisLaplaceFeatureDetector::create())); + test.safe_run(); +} + +TEST(Features2d_Detector_TBMR_Affine, regression) +{ + CV_FeatureDetectorTest test("detector-tbmr-affine", TBMR::create()); test.safe_run(); } @@ -90,15 +115,15 @@ TEST( Features2d_Detector_Harris_Laplace_Affine, regression ) */ #ifdef OPENCV_ENABLE_NONFREE -TEST( Features2d_DescriptorExtractor_SURF, regression ) +TEST(Features2d_DescriptorExtractor_SURF, regression) { #ifdef HAVE_OPENCL bool useOCL = cv::ocl::useOpenCL(); cv::ocl::setUseOpenCL(false); #endif - CV_DescriptorExtractorTest > test( "descriptor-surf", 0.05f, - SURF::create() ); + CV_DescriptorExtractorTest> test("descriptor-surf", 0.05f, + SURF::create()); test.safe_run(); #ifdef HAVE_OPENCL @@ -107,14 +132,14 @@ TEST( Features2d_DescriptorExtractor_SURF, regression ) } #ifdef HAVE_OPENCL -TEST( Features2d_DescriptorExtractor_SURF_OCL, regression ) +TEST(Features2d_DescriptorExtractor_SURF_OCL, regression) { bool useOCL = cv::ocl::useOpenCL(); cv::ocl::setUseOpenCL(true); - if(cv::ocl::useOpenCL()) + if (cv::ocl::useOpenCL()) { - CV_DescriptorExtractorTest > test( "descriptor-surf_ocl", 0.05f, - SURF::create() ); + CV_DescriptorExtractorTest> test("descriptor-surf_ocl", 0.05f, + SURF::create()); test.safe_run(); } cv::ocl::setUseOpenCL(useOCL); @@ -122,34 +147,36 @@ TEST( Features2d_DescriptorExtractor_SURF_OCL, regression ) #endif #endif // NONFREE -TEST( Features2d_DescriptorExtractor_DAISY, regression ) +TEST(Features2d_DescriptorExtractor_DAISY, regression) { - CV_DescriptorExtractorTest > test( "descriptor-daisy", 0.05f, - DAISY::create() ); + CV_DescriptorExtractorTest> test("descriptor-daisy", 0.05f, + DAISY::create()); test.safe_run(); } -TEST( Features2d_DescriptorExtractor_FREAK, regression ) +TEST(Features2d_DescriptorExtractor_FREAK, regression) { - CV_DescriptorExtractorTest test("descriptor-freak", (CV_DescriptorExtractorTest::DistanceType)12.f, - FREAK::create()); + CV_DescriptorExtractorTest test( + "descriptor-freak", + (CV_DescriptorExtractorTest::DistanceType)12.f, + FREAK::create()); test.safe_run(); } -TEST( Features2d_DescriptorExtractor_BRIEF, regression ) +TEST(Features2d_DescriptorExtractor_BRIEF, regression) { - CV_DescriptorExtractorTest test( "descriptor-brief", 1, - BriefDescriptorExtractor::create() ); + CV_DescriptorExtractorTest test( + "descriptor-brief", 1, BriefDescriptorExtractor::create()); test.safe_run(); } -template -struct LUCIDEqualityDistance +template struct LUCIDEqualityDistance { typedef unsigned char ValueType; typedef int ResultType; - ResultType operator()( const unsigned char* a, const unsigned char* b, int size ) const + ResultType operator()(const unsigned char *a, const unsigned char *b, + int size) const { int res = 0; for (int i = 0; i < size; i++) @@ -163,86 +190,89 @@ struct LUCIDEqualityDistance } }; -TEST( Features2d_DescriptorExtractor_LUCID, regression ) +TEST(Features2d_DescriptorExtractor_LUCID, regression) { - CV_DescriptorExtractorTest< LUCIDEqualityDistance<1/*used blur is not bit-exact*/> > test( - "descriptor-lucid", 2, - LUCID::create(1, 2) - ); + CV_DescriptorExtractorTest< + LUCIDEqualityDistance<1 /*used blur is not bit-exact*/>> + test("descriptor-lucid", 2, LUCID::create(1, 2)); test.safe_run(); } -TEST( Features2d_DescriptorExtractor_LATCH, regression ) +TEST(Features2d_DescriptorExtractor_LATCH, regression) { - CV_DescriptorExtractorTest test( "descriptor-latch", 1, - LATCH::create(32, true, 3, 0) ); + CV_DescriptorExtractorTest test("descriptor-latch", 1, + LATCH::create(32, true, 3, 0)); test.safe_run(); } -TEST( Features2d_DescriptorExtractor_VGG, regression ) +TEST(Features2d_DescriptorExtractor_VGG, regression) { - CV_DescriptorExtractorTest > test( "descriptor-vgg", 0.03f, - VGG::create() ); + CV_DescriptorExtractorTest> test("descriptor-vgg", 0.03f, + VGG::create()); test.safe_run(); } -TEST( Features2d_DescriptorExtractor_BGM, regression ) +TEST(Features2d_DescriptorExtractor_BGM, regression) { - CV_DescriptorExtractorTest test( "descriptor-boostdesc-bgm", - (CV_DescriptorExtractorTest::DistanceType)12.f, - BoostDesc::create(BoostDesc::BGM) ); + CV_DescriptorExtractorTest test( + "descriptor-boostdesc-bgm", + (CV_DescriptorExtractorTest::DistanceType)12.f, + BoostDesc::create(BoostDesc::BGM)); test.safe_run(); } -TEST( Features2d_DescriptorExtractor_BGM_HARD, regression ) +TEST(Features2d_DescriptorExtractor_BGM_HARD, regression) { - CV_DescriptorExtractorTest test( "descriptor-boostdesc-bgm_hard", - (CV_DescriptorExtractorTest::DistanceType)12.f, - BoostDesc::create(BoostDesc::BGM_HARD) ); + CV_DescriptorExtractorTest test( + "descriptor-boostdesc-bgm_hard", + (CV_DescriptorExtractorTest::DistanceType)12.f, + BoostDesc::create(BoostDesc::BGM_HARD)); test.safe_run(); } -TEST( Features2d_DescriptorExtractor_BGM_BILINEAR, regression ) +TEST(Features2d_DescriptorExtractor_BGM_BILINEAR, regression) { - CV_DescriptorExtractorTest test( "descriptor-boostdesc-bgm_bilinear", - (CV_DescriptorExtractorTest::DistanceType)15.f, - BoostDesc::create(BoostDesc::BGM_BILINEAR) ); + CV_DescriptorExtractorTest test( + "descriptor-boostdesc-bgm_bilinear", + (CV_DescriptorExtractorTest::DistanceType)15.f, + BoostDesc::create(BoostDesc::BGM_BILINEAR)); test.safe_run(); } -TEST( Features2d_DescriptorExtractor_LBGM, regression ) +TEST(Features2d_DescriptorExtractor_LBGM, regression) { - CV_DescriptorExtractorTest > test( "descriptor-boostdesc-lbgm", - 1.0f, - BoostDesc::create(BoostDesc::LBGM) ); + CV_DescriptorExtractorTest> test( + "descriptor-boostdesc-lbgm", 1.0f, BoostDesc::create(BoostDesc::LBGM)); test.safe_run(); } -TEST( Features2d_DescriptorExtractor_BINBOOST_64, regression ) +TEST(Features2d_DescriptorExtractor_BINBOOST_64, regression) { - CV_DescriptorExtractorTest test( "descriptor-boostdesc-binboost_64", - (CV_DescriptorExtractorTest::DistanceType)12.f, - BoostDesc::create(BoostDesc::BINBOOST_64) ); + CV_DescriptorExtractorTest test( + "descriptor-boostdesc-binboost_64", + (CV_DescriptorExtractorTest::DistanceType)12.f, + BoostDesc::create(BoostDesc::BINBOOST_64)); test.safe_run(); } -TEST( Features2d_DescriptorExtractor_BINBOOST_128, regression ) +TEST(Features2d_DescriptorExtractor_BINBOOST_128, regression) { - CV_DescriptorExtractorTest test( "descriptor-boostdesc-binboost_128", - (CV_DescriptorExtractorTest::DistanceType)12.f, - BoostDesc::create(BoostDesc::BINBOOST_128) ); + CV_DescriptorExtractorTest test( + "descriptor-boostdesc-binboost_128", + (CV_DescriptorExtractorTest::DistanceType)12.f, + BoostDesc::create(BoostDesc::BINBOOST_128)); test.safe_run(); } -TEST( Features2d_DescriptorExtractor_BINBOOST_256, regression ) +TEST(Features2d_DescriptorExtractor_BINBOOST_256, regression) { - CV_DescriptorExtractorTest test( "descriptor-boostdesc-binboost_256", - (CV_DescriptorExtractorTest::DistanceType)12.f, - BoostDesc::create(BoostDesc::BINBOOST_256) ); + CV_DescriptorExtractorTest test( + "descriptor-boostdesc-binboost_256", + (CV_DescriptorExtractorTest::DistanceType)12.f, + BoostDesc::create(BoostDesc::BINBOOST_256)); test.safe_run(); } - #ifdef OPENCV_ENABLE_NONFREE TEST(Features2d_BruteForceDescriptorMatcher_knnMatch, regression) { @@ -253,42 +283,46 @@ TEST(Features2d_BruteForceDescriptorMatcher_knnMatch, regression) ASSERT_TRUE(ext); Ptr det = SURF::create(); - //"%YAML:1.0\nhessianThreshold: 8000.\noctaves: 3\noctaveLayers: 4\nupright: 0\n" + //"%YAML:1.0\nhessianThreshold: 8000.\noctaves: 3\noctaveLayers: 4\nupright: + // 0\n" ASSERT_TRUE(det); Ptr matcher = DescriptorMatcher::create("BruteForce"); ASSERT_TRUE(matcher); Mat imgT(256, 256, CV_8U, Scalar(255)); - line(imgT, Point(20, sz/2), Point(sz-21, sz/2), Scalar(100), 2); - line(imgT, Point(sz/2, 20), Point(sz/2, sz-21), Scalar(100), 2); + line(imgT, Point(20, sz / 2), Point(sz - 21, sz / 2), Scalar(100), 2); + line(imgT, Point(sz / 2, 20), Point(sz / 2, sz - 21), Scalar(100), 2); vector kpT; - kpT.push_back( KeyPoint(50, 50, 16, 0, 20000, 1, -1) ); - kpT.push_back( KeyPoint(42, 42, 16, 160, 10000, 1, -1) ); + kpT.push_back(KeyPoint(50, 50, 16, 0, 20000, 1, -1)); + kpT.push_back(KeyPoint(42, 42, 16, 160, 10000, 1, -1)); Mat descT; ext->compute(imgT, kpT, descT); Mat imgQ(256, 256, CV_8U, Scalar(255)); - line(imgQ, Point(30, sz/2), Point(sz-31, sz/2), Scalar(100), 3); - line(imgQ, Point(sz/2, 30), Point(sz/2, sz-31), Scalar(100), 3); + line(imgQ, Point(30, sz / 2), Point(sz - 31, sz / 2), Scalar(100), 3); + line(imgQ, Point(sz / 2, 30), Point(sz / 2, sz - 31), Scalar(100), 3); vector kpQ; det->detect(imgQ, kpQ); Mat descQ; ext->compute(imgQ, kpQ, descQ); - vector > matches; + vector> matches; matcher->knnMatch(descQ, descT, matches, k); - //cout << "\nBest " << k << " matches to " << descT.rows << " train desc-s." << endl; + // cout << "\nBest " << k << " matches to " << descT.rows << " train + // desc-s." << endl; ASSERT_EQ(descQ.rows, static_cast(matches.size())); - for(size_t i = 0; i(matches[i].size())); - for(size_t j = 0; j " << matches[i][j].trainIdx << endl; + // cout << "\t" << matches[i][j].queryIdx << " -> " << + // matches[i][j].trainIdx << endl; ASSERT_EQ(matches[i][j].queryIdx, static_cast(i)); } } @@ -297,24 +331,29 @@ TEST(Features2d_BruteForceDescriptorMatcher_knnMatch, regression) class CV_DetectPlanarTest : public cvtest::BaseTest { -public: - CV_DetectPlanarTest(const string& _fname, int _min_ninliers, const Ptr& _f2d) - : fname(_fname), min_ninliers(_min_ninliers), f2d(_f2d) {} + public: + CV_DetectPlanarTest(const string &_fname, int _min_ninliers, + const Ptr &_f2d) + : fname(_fname), min_ninliers(_min_ninliers), f2d(_f2d) + { + } -protected: + protected: void run(int) { - if(f2d.empty()) + if (f2d.empty()) return; - string path = string(ts->get_data_path()) + "detectors_descriptors_evaluation/planar/"; + string path = string(ts->get_data_path()) + + "detectors_descriptors_evaluation/planar/"; string imgname1 = path + "box.png"; string imgname2 = path + "box_in_scene.png"; Mat img1 = imread(imgname1, 0); Mat img2 = imread(imgname2, 0); - if( img1.empty() || img2.empty() ) + if (img1.empty() || img2.empty()) { - ts->printf( cvtest::TS::LOG, "missing %s and/or %s\n", imgname1.c_str(), imgname2.c_str()); - ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA ); + ts->printf(cvtest::TS::LOG, "missing %s and/or %s\n", + imgname1.c_str(), imgname2.c_str()); + ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA); return; } vector kpt1, kpt2; @@ -333,16 +372,17 @@ class CV_DetectPlanarTest : public cvtest::BaseTest f2d->detectAndCompute(img1, Mat(), kpt1, d1); f2d->detectAndCompute(img1, Mat(), kpt2, d2); } - for( size_t i = 0; i < kpt1.size(); i++ ) - CV_Assert(kpt1[i].response > 0 ); - for( size_t i = 0; i < kpt2.size(); i++ ) - CV_Assert(kpt2[i].response > 0 ); + for (size_t i = 0; i < kpt1.size(); i++) + CV_Assert(kpt1[i].response > 0); + for (size_t i = 0; i < kpt2.size(); i++) + CV_Assert(kpt2[i].response > 0); vector matches; BFMatcher(f2d->defaultNorm(), true).match(d1, d2, matches); vector pt1, pt2; - for( size_t i = 0; i < matches.size(); i++ ) { + for (size_t i = 0; i < matches.size(); i++) + { pt1.push_back(kpt1[matches[i].queryIdx].pt); pt2.push_back(kpt2[matches[i].trainIdx].pt); } @@ -350,10 +390,12 @@ class CV_DetectPlanarTest : public cvtest::BaseTest Mat inliers, H = findHomography(pt1, pt2, RANSAC, 10, inliers); int ninliers = countNonZero(inliers); - if( ninliers < min_ninliers ) + if (ninliers < min_ninliers) { - ts->printf( cvtest::TS::LOG, "too little inliers (%d) vs expected %d\n", ninliers, min_ninliers); - ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA ); + ts->printf(cvtest::TS::LOG, + "too little inliers (%d) vs expected %d\n", ninliers, + min_ninliers); + ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA); return; } } @@ -363,34 +405,43 @@ class CV_DetectPlanarTest : public cvtest::BaseTest Ptr f2d; }; -TEST(Features2d_SIFTHomographyTest, regression) { CV_DetectPlanarTest test("SIFT", 80, SIFT::create()); test.safe_run(); } +TEST(Features2d_SIFTHomographyTest, regression) +{ + CV_DetectPlanarTest test("SIFT", 80, SIFT::create()); + test.safe_run(); +} #ifdef OPENCV_ENABLE_NONFREE -TEST(Features2d_SURFHomographyTest, regression) { CV_DetectPlanarTest test("SURF", 80, SURF::create()); test.safe_run(); } +TEST(Features2d_SURFHomographyTest, regression) +{ + CV_DetectPlanarTest test("SURF", 80, SURF::create()); + test.safe_run(); +} #endif class FeatureDetectorUsingMaskTest : public cvtest::BaseTest { -public: - FeatureDetectorUsingMaskTest(const Ptr& featureDetector) : - featureDetector_(featureDetector) + public: + FeatureDetectorUsingMaskTest(const Ptr &featureDetector) + : featureDetector_(featureDetector) { CV_Assert(featureDetector_); } -protected: - + protected: void run(int) { const int nStepX = 2; const int nStepY = 2; - const string imageFilename = string(ts->get_data_path()) + "/features2d/tsukuba.png"; + const string imageFilename = + string(ts->get_data_path()) + "/features2d/tsukuba.png"; Mat image = imread(imageFilename); - if(image.empty()) + if (image.empty()) { - ts->printf(cvtest::TS::LOG, "Image %s can not be read.\n", imageFilename.c_str()); + ts->printf(cvtest::TS::LOG, "Image %s can not be read.\n", + imageFilename.c_str()); ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA); return; } @@ -402,8 +453,8 @@ class FeatureDetectorUsingMaskTest : public cvtest::BaseTest vector keyPoints; vector points; - for(int i=0; idetect(image, keyPoints, mask); KeyPoint::convert(keyPoints, points); - for(size_t k=0; kprintf(cvtest::TS::LOG, "The feature point is outside of the mask."); - ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_OUTPUT); + ts->printf(cvtest::TS::LOG, + "The feature point is outside of the mask."); + ts->set_failed_test_info( + cvtest::TS::FAIL_INVALID_OUTPUT); return; } } } - ts->set_failed_test_info( cvtest::TS::OK ); + ts->set_failed_test_info(cvtest::TS::OK); } Ptr featureDetector_; @@ -444,4 +497,5 @@ TEST(DISABLED_Features2d_SURF_using_mask, regression) } #endif // NONFREE -}} // namespace +} // namespace +} // namespace opencv_test diff --git a/modules/xfeatures2d/test/test_keypoints.cpp b/modules/xfeatures2d/test/test_keypoints.cpp index 45d50dfb4b6..0f8b38e98b2 100644 --- a/modules/xfeatures2d/test/test_keypoints.cpp +++ b/modules/xfeatures2d/test/test_keypoints.cpp @@ -2,7 +2,8 @@ // // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. // -// By downloading, copying, installing or using the software you agree to this license. +// By downloading, copying, installing or using the software you agree to this +license. // If you do not agree to this license, do not download, install, // copy or use the software. // @@ -13,23 +14,29 @@ // Copyright (C) 2000, Intel Corporation, all rights reserved. // Third party copyrights are property of their respective owners. // -// Redistribution and use in source and binary forms, with or without modification, +// Redistribution and use in source and binary forms, with or without +modification, // are permitted provided that the following conditions are met: // // * Redistribution's of source code must retain the above copyright notice, // this list of conditions and the following disclaimer. // -// * Redistribution's in binary form must reproduce the above copyright notice, +// * Redistribution's in binary form must reproduce the above copyright +notice, // this list of conditions and the following disclaimer in the documentation // and/or other materials provided with the distribution. // -// * The name of Intel Corporation may not be used to endorse or promote products +// * The name of Intel Corporation may not be used to endorse or promote +products // derived from this software without specific prior written permission. // -// This software is provided by the copyright holders and contributors "as is" and +// This software is provided by the copyright holders and contributors "as is" +and // any express or implied warranties, including, but not limited to, the implied -// warranties of merchantability and fitness for a particular purpose are disclaimed. -// In no event shall the Intel Corporation or contributors be liable for any direct, +// warranties of merchantability and fitness for a particular purpose are +disclaimed. +// In no event shall the Intel Corporation or contributors be liable for any +direct, // indirect, incidental, special, exemplary, or consequential damages // (including, but not limited to, procurement of substitute goods or services; // loss of use, data, or profits; or business interruption) however caused @@ -41,32 +48,39 @@ #include "test_precomp.hpp" -namespace opencv_test { namespace { +namespace opencv_test +{ +namespace +{ const string FEATURES2D_DIR = "features2d"; const string IMAGE_FILENAME = "tsukuba.png"; /****************************************************************************************\ -* Test for KeyPoint * +* Test for KeyPoint * \****************************************************************************************/ class CV_FeatureDetectorKeypointsTest : public cvtest::BaseTest { -public: - explicit CV_FeatureDetectorKeypointsTest(const Ptr& _detector) : - detector(_detector) {} + public: + explicit CV_FeatureDetectorKeypointsTest(const Ptr &_detector) + : detector(_detector) + { + } -protected: + protected: virtual void run(int) { CV_Assert(detector); - string imgFilename = string(ts->get_data_path()) + FEATURES2D_DIR + "/" + IMAGE_FILENAME; + string imgFilename = + string(ts->get_data_path()) + FEATURES2D_DIR + "/" + IMAGE_FILENAME; // Read the test image. Mat image = imread(imgFilename); - if(image.empty()) + if (image.empty()) { - ts->printf(cvtest::TS::LOG, "Image %s can not be read.\n", imgFilename.c_str()); + ts->printf(cvtest::TS::LOG, "Image %s can not be read.\n", + imgFilename.c_str()); ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA); return; } @@ -74,35 +88,42 @@ class CV_FeatureDetectorKeypointsTest : public cvtest::BaseTest vector keypoints; detector->detect(image, keypoints); - if(keypoints.empty()) + if (keypoints.empty()) { - ts->printf(cvtest::TS::LOG, "Detector can't find keypoints in image.\n"); + ts->printf(cvtest::TS::LOG, + "Detector can't find keypoints in image.\n"); ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_OUTPUT); return; } Rect r(0, 0, image.cols, image.rows); - for(size_t i = 0; i < keypoints.size(); i++) + for (size_t i = 0; i < keypoints.size(); i++) { - const KeyPoint& kp = keypoints[i]; + const KeyPoint &kp = keypoints[i]; - if(!r.contains(kp.pt)) + if (!r.contains(kp.pt)) { - ts->printf(cvtest::TS::LOG, "KeyPoint::pt is out of image (x=%f, y=%f).\n", kp.pt.x, kp.pt.y); + ts->printf(cvtest::TS::LOG, + "KeyPoint::pt is out of image (x=%f, y=%f).\n", + kp.pt.x, kp.pt.y); ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_OUTPUT); return; } - if(kp.size <= 0.f) + if (kp.size <= 0.f) { - ts->printf(cvtest::TS::LOG, "KeyPoint::size is not positive (%f).\n", kp.size); + ts->printf(cvtest::TS::LOG, + "KeyPoint::size is not positive (%f).\n", kp.size); ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_OUTPUT); return; } - if((kp.angle < 0.f && kp.angle != -1.f) || kp.angle >= 360.f) + if ((kp.angle < 0.f && kp.angle != -1.f) || kp.angle >= 360.f) { - ts->printf(cvtest::TS::LOG, "KeyPoint::angle is out of range [0, 360). It's %f.\n", kp.angle); + ts->printf( + cvtest::TS::LOG, + "KeyPoint::angle is out of range [0, 360). It's %f.\n", + kp.angle); ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_OUTPUT); return; } @@ -113,7 +134,6 @@ class CV_FeatureDetectorKeypointsTest : public cvtest::BaseTest Ptr detector; }; - // Registration of tests #ifdef OPENCV_ENABLE_NONFREE TEST(Features2d_Detector_Keypoints_SURF, validation) @@ -123,18 +143,23 @@ TEST(Features2d_Detector_Keypoints_SURF, validation) } #endif // NONFREE - TEST(Features2d_Detector_Keypoints_Star, validation) { CV_FeatureDetectorKeypointsTest test(xfeatures2d::StarDetector::create()); test.safe_run(); } - TEST(Features2d_Detector_Keypoints_MSDDetector, validation) { CV_FeatureDetectorKeypointsTest test(xfeatures2d::MSDDetector::create()); test.safe_run(); } -}} // namespace +TEST(Features2d_Detector_Keypoints_TBMRDetector, validation) +{ + CV_FeatureDetectorKeypointsTest test(xfeatures2d::TBMR::create()); + test.safe_run(); +} + +} // namespace +} // namespace opencv_test From 2eaff8679143c08e29b7561719a7c82c50c91dab Mon Sep 17 00:00:00 2001 From: "steffen.ehrle" Date: Thu, 5 Nov 2020 11:49:21 +0100 Subject: [PATCH 13/22] octave/scale and descriptor extraction using sift added --- .../include/opencv2/xfeatures2d.hpp | 23 ++- modules/xfeatures2d/src/tbmr.cpp | 159 +++++++++++++++--- 2 files changed, 146 insertions(+), 36 deletions(-) diff --git a/modules/xfeatures2d/include/opencv2/xfeatures2d.hpp b/modules/xfeatures2d/include/opencv2/xfeatures2d.hpp index 8e486d66ec3..0b4e89c8fab 100644 --- a/modules/xfeatures2d/include/opencv2/xfeatures2d.hpp +++ b/modules/xfeatures2d/include/opencv2/xfeatures2d.hpp @@ -1031,6 +1031,11 @@ class CV_EXPORTS AffineFeature2D : public Feature2D @brief Class implementing the Tree Based Morse Regions (TBMR) as described in @cite Najman2014 extended with scaled extraction ability. +@param min_area prune areas smaller than minArea +@param max_area_relative prune areas bigger than maxArea = max_area_relative * +input_image_size +@param scale_factor scale factor for scaled extraction. +@param n_scales number of applications of the scale factor (octaves). @note This algorithm is based on Component Tree (Min/Max) as well as MSER but uses a Morse-theory approach to extract features. @@ -1042,19 +1047,19 @@ TBMR feature and vice versa). class CV_EXPORTS_W TBMR : public AffineFeature2D { public: - /** @brief Full constructor for %TBMR detector - @param min_area prune areas smaller than minArea - @param max_area_relative prune areas bigger than maxArea = - max_area_relative * input_image_size - */ CV_WRAP static Ptr create(int min_area = 60, - double max_area_relative = 0.01); + float max_area_relative = 0.01, + float scale_factor = 1.25f, + int n_scales = -1); CV_WRAP virtual void setMinArea(int minArea) = 0; CV_WRAP virtual int getMinArea() const = 0; - CV_WRAP virtual void setMaxAreaRelative(double maxArea) = 0; - CV_WRAP virtual double getMaxAreaRelative() const = 0; - CV_WRAP virtual String getDefaultName() const CV_OVERRIDE; + CV_WRAP virtual void setMaxAreaRelative(float maxArea) = 0; + CV_WRAP virtual float getMaxAreaRelative() const = 0; + CV_WRAP virtual void setScaleFactor(float scale_factor) = 0; + CV_WRAP virtual float getScaleFactor() const = 0; + CV_WRAP virtual void setNScales(int n_scales) = 0; + CV_WRAP virtual int getNScales() const = 0; }; /** @brief Estimates cornerness for prespecified KeyPoints using the FAST diff --git a/modules/xfeatures2d/src/tbmr.cpp b/modules/xfeatures2d/src/tbmr.cpp index 61bb32c08d6..118e5b8c571 100644 --- a/modules/xfeatures2d/src/tbmr.cpp +++ b/modules/xfeatures2d/src/tbmr.cpp @@ -8,40 +8,123 @@ namespace cv { namespace xfeatures2d { +/*! +MSD Image Pyramid. (from msd.cpp) +*/ +class TBMRImagePyramid +{ + // Multi-threaded construction of the scale-space pyramid + struct TBMRImagePyramidBuilder : ParallelLoopBody + { + TBMRImagePyramidBuilder(const cv::Mat &_im, + std::vector *_m_imPyr, + float _scaleFactor) + { + im = &_im; + m_imPyr = _m_imPyr; + scaleFactor = _scaleFactor; + } + + void operator()(const Range &range) const CV_OVERRIDE + { + for (int lvl = range.start; lvl < range.end; lvl++) + { + float scale = 1 / std::pow(scaleFactor, (float)lvl); + (*m_imPyr)[lvl] = cv::Mat(cv::Size(cvRound(im->cols * scale), + cvRound(im->rows * scale)), + im->type()); + cv::resize(*im, (*m_imPyr)[lvl], + cv::Size((*m_imPyr)[lvl].cols, (*m_imPyr)[lvl].rows), + 0.0, 0.0, cv::INTER_AREA); + } + } + const cv::Mat *im; + std::vector *m_imPyr; + float scaleFactor; + }; + + public: + TBMRImagePyramid(const cv::Mat &im, const int nLevels, + const float scaleFactor = 1.6f); + ~TBMRImagePyramid(); + + const std::vector getImPyr() const { return m_imPyr; }; + + private: + std::vector m_imPyr; + int m_nLevels; + float m_scaleFactor; +}; + +TBMRImagePyramid::TBMRImagePyramid(const cv::Mat &im, const int nLevels, + const float scaleFactor) +{ + m_nLevels = nLevels; + m_scaleFactor = scaleFactor; + m_imPyr.clear(); + m_imPyr.resize(nLevels); + + m_imPyr[0] = im.clone(); + + if (m_nLevels > 1) + { + parallel_for_(Range(1, nLevels), + TBMRImagePyramidBuilder(im, &m_imPyr, scaleFactor)); + } +} + +TBMRImagePyramid::~TBMRImagePyramid() {} + class TBMR_Impl CV_FINAL : public TBMR { public: struct Params { - Params(int _min_area = 60, double _max_area_relative = 0.01) + Params(int _min_area = 60, float _max_area_relative = 0.01, + float _scale = 1.5, int _n_scale = -1) { CV_Assert(_min_area >= 0); CV_Assert(_max_area_relative >= - std::numeric_limits::epsilon()); + std::numeric_limits::epsilon()); minArea = _min_area; maxAreaRelative = _max_area_relative; + scale = _scale; + n_scale = _n_scale; } uint minArea; - double maxAreaRelative; + float maxAreaRelative; + int n_scale; + float scale; }; explicit TBMR_Impl(const Params &_params) : params(_params) {} virtual ~TBMR_Impl() CV_OVERRIDE {} - void setMinArea(int minArea) CV_OVERRIDE { params.minArea = minArea; } + void setMinArea(int minArea) CV_OVERRIDE + { + params.minArea = std::max(minArea, 0); + } int getMinArea() const CV_OVERRIDE { return params.minArea; } - void setMaxAreaRelative(double maxAreaRelative) CV_OVERRIDE + void setMaxAreaRelative(float maxAreaRelative) CV_OVERRIDE { - params.maxAreaRelative = maxAreaRelative; + params.maxAreaRelative = + std::max(maxAreaRelative, std::numeric_limits::epsilon()); } - double getMaxAreaRelative() const CV_OVERRIDE + float getMaxAreaRelative() const CV_OVERRIDE { return params.maxAreaRelative; } + void setScaleFactor(float scale_factor) CV_OVERRIDE + { + params.scale = std::max(scale_factor, 1.f); + } + float getScaleFactor() const CV_OVERRIDE { return params.scale; } + void setNScales(int n_scales) CV_OVERRIDE { params.minArea = n_scales; } + int getNScales() const CV_OVERRIDE { return params.n_scale; } void detect(InputArray image, CV_OUT std::vector &keypoints, InputArray mask = noArray()) CV_OVERRIDE; @@ -164,7 +247,7 @@ class TBMR_Impl CV_FINAL : public TBMR } void calculateTBMRs(const Mat &image, std::vector &tbmrs, - const Mat &mask) + const Mat &mask, float scale, int octave) { uint imSize = image.cols * image.rows; uint maxArea = static_cast(params.maxAreaRelative * imSize); @@ -403,9 +486,14 @@ class TBMR_Impl CV_FINAL : public TBMR float size = (float)majAxL; - tbmrs.push_back(Elliptic_KeyPoint( - Point2f((float)x, (float)y), (float)theta, - cv::Size2f((float)majAxL, (float)minAxL), size, 1.f)); + // not sure if we should scale or not scale x,y,axes,size + // (as scale is stored in si) + Elliptic_KeyPoint ekp( + Point2f((float)x, (float)y) * scale, (float)theta, + cv::Size2f((float)majAxL, (float)minAxL) * scale, + size * scale, scale); + ekp.octave = octave; + tbmrs.push_back(ekp); } } } @@ -467,14 +555,32 @@ void TBMR_Impl::detect(InputArray _image, if (src.channels() != 1) cv::cvtColor(src, src, cv::COLOR_BGR2GRAY); - // append max tree tbmrs - sortIdx(src.reshape(1, 1), S, - SortFlags::SORT_ASCENDING | SortFlags::SORT_EVERY_ROW); - calculateTBMRs(src, keypoints, mask); + int m_cur_n_scales = + params.n_scale > 0 + ? params.n_scale + : 1 /*todo calculate optimal scale factor from image size*/; + float m_scale_factor = params.scale; - // reverse instead of sort - flip(S, S, -1); - calculateTBMRs(src, keypoints, mask); + std::vector pyr; + TBMRImagePyramid scaleSpacer(src, m_cur_n_scales, m_scale_factor); + pyr = scaleSpacer.getImPyr(); + + int oct = 0; + for (auto &s : pyr) + { + float scale = ((float)s.cols) / pyr.begin()->cols; + + // append max tree tbmrs + sortIdx(s.reshape(1, 1), S, + SortFlags::SORT_ASCENDING | SortFlags::SORT_EVERY_ROW); + calculateTBMRs(s, keypoints, mask, scale, oct); + + // reverse instead of sort + flip(S, S, -1); + calculateTBMRs(s, keypoints, mask, scale, oct); + + oct++; + } } void TBMR_Impl::detectAndCompute( @@ -482,20 +588,19 @@ void TBMR_Impl::detectAndCompute( CV_OUT std::vector &keypoints, OutputArray descriptors, bool useProvidedKeypoints) { - CV_INSTRUMENT_REGION(); - - CV_Error(Error::StsNotImplemented, ""); + // We can use SIFT to compute descriptors for the extracted keypoints... + auto sift = SIFT::create(); + auto dac = AffineFeature2D::create(this, sift); + dac->detectAndCompute(image, mask, keypoints, descriptors, + useProvidedKeypoints); } -CV_WRAP Ptr TBMR::create(int _min_area, double _max_area_relative) +CV_WRAP Ptr TBMR::create(int _min_area, float _max_area_relative, + float _scale, int _n_scale) { return cv::makePtr( - TBMR_Impl::Params(_min_area, _max_area_relative)); + TBMR_Impl::Params(_min_area, _max_area_relative, _scale, _n_scale)); } -String TBMR::getDefaultName() const -{ - return (Feature2D::getDefaultName() + ".TBMR"); -} } // namespace xfeatures2d } // namespace cv From 3223f465e1613403a07e27d1f16715c75edb4c5b Mon Sep 17 00:00:00 2001 From: "steffen.ehrle" Date: Thu, 5 Nov 2020 12:19:41 +0100 Subject: [PATCH 14/22] try fix the errors --- modules/xfeatures2d/src/tbmr.cpp | 37 ++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/modules/xfeatures2d/src/tbmr.cpp b/modules/xfeatures2d/src/tbmr.cpp index 118e5b8c571..9ca9fc29830 100644 --- a/modules/xfeatures2d/src/tbmr.cpp +++ b/modules/xfeatures2d/src/tbmr.cpp @@ -103,40 +103,45 @@ class TBMR_Impl CV_FINAL : public TBMR virtual ~TBMR_Impl() CV_OVERRIDE {} - void setMinArea(int minArea) CV_OVERRIDE + virtual void setMinArea(int minArea) CV_OVERRIDE { params.minArea = std::max(minArea, 0); } int getMinArea() const CV_OVERRIDE { return params.minArea; } - void setMaxAreaRelative(float maxAreaRelative) CV_OVERRIDE + virtual void setMaxAreaRelative(float maxAreaRelative) CV_OVERRIDE { params.maxAreaRelative = std::max(maxAreaRelative, std::numeric_limits::epsilon()); } - float getMaxAreaRelative() const CV_OVERRIDE + virtual float getMaxAreaRelative() const CV_OVERRIDE { return params.maxAreaRelative; } - void setScaleFactor(float scale_factor) CV_OVERRIDE + virtual void setScaleFactor(float scale_factor) CV_OVERRIDE { params.scale = std::max(scale_factor, 1.f); } - float getScaleFactor() const CV_OVERRIDE { return params.scale; } - void setNScales(int n_scales) CV_OVERRIDE { params.minArea = n_scales; } - int getNScales() const CV_OVERRIDE { return params.n_scale; } + virtual float getScaleFactor() const CV_OVERRIDE { return params.scale; } + virtual void setNScales(int n_scales) CV_OVERRIDE + { + params.minArea = n_scales; + } + virtual int getNScales() const CV_OVERRIDE { return params.n_scale; } - void detect(InputArray image, CV_OUT std::vector &keypoints, - InputArray mask = noArray()) CV_OVERRIDE; + virtual void detect(InputArray image, + CV_OUT std::vector &keypoints, + InputArray mask = noArray()) CV_OVERRIDE; - void detect(InputArray image, - CV_OUT std::vector &keypoints, - InputArray mask = noArray()) CV_OVERRIDE; + virtual void detect(InputArray image, + CV_OUT std::vector &keypoints, + InputArray mask = noArray()) CV_OVERRIDE; - void detectAndCompute(InputArray image, InputArray mask, - CV_OUT std::vector &keypoints, - OutputArray descriptors, - bool useProvidedKeypoints = false) CV_OVERRIDE; + virtual void + detectAndCompute(InputArray image, InputArray mask, + CV_OUT std::vector &keypoints, + OutputArray descriptors, + bool useProvidedKeypoints = false) CV_OVERRIDE; CV_INLINE uint zfindroot(uint *parent, uint p) { From 64537c6d0de540ce378c79bae67c313da69080b4 Mon Sep 17 00:00:00 2001 From: "steffen.ehrle" Date: Fri, 6 Nov 2020 12:52:48 +0100 Subject: [PATCH 15/22] fix parameter error --- modules/xfeatures2d/src/tbmr.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/modules/xfeatures2d/src/tbmr.cpp b/modules/xfeatures2d/src/tbmr.cpp index 9ca9fc29830..5716db3fc5f 100644 --- a/modules/xfeatures2d/src/tbmr.cpp +++ b/modules/xfeatures2d/src/tbmr.cpp @@ -125,7 +125,7 @@ class TBMR_Impl CV_FINAL : public TBMR virtual float getScaleFactor() const CV_OVERRIDE { return params.scale; } virtual void setNScales(int n_scales) CV_OVERRIDE { - params.minArea = n_scales; + params.n_scale = n_scales; } virtual int getNScales() const CV_OVERRIDE { return params.n_scale; } @@ -557,6 +557,8 @@ void TBMR_Impl::detect(InputArray _image, src = tempsrc; } + CV_Assert(src.depth() == CV_8U); + if (src.channels() != 1) cv::cvtColor(src, src, cv::COLOR_BGR2GRAY); @@ -600,8 +602,8 @@ void TBMR_Impl::detectAndCompute( useProvidedKeypoints); } -CV_WRAP Ptr TBMR::create(int _min_area, float _max_area_relative, - float _scale, int _n_scale) +Ptr TBMR::create(int _min_area, float _max_area_relative, float _scale, + int _n_scale) { return cv::makePtr( TBMR_Impl::Params(_min_area, _max_area_relative, _scale, _n_scale)); From 187d8cb4667f6b42be8f865a7abdc73392a0314a Mon Sep 17 00:00:00 2001 From: "steffen.ehrle" Date: Mon, 9 Nov 2020 17:55:01 +0100 Subject: [PATCH 16/22] add scale for pyramid extraction and filter scaled points for duplicates. --- modules/xfeatures2d/src/tbmr.cpp | 41 ++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/modules/xfeatures2d/src/tbmr.cpp b/modules/xfeatures2d/src/tbmr.cpp index 5716db3fc5f..2af3fd66362 100644 --- a/modules/xfeatures2d/src/tbmr.cpp +++ b/modules/xfeatures2d/src/tbmr.cpp @@ -255,7 +255,9 @@ class TBMR_Impl CV_FINAL : public TBMR const Mat &mask, float scale, int octave) { uint imSize = image.cols * image.rows; - uint maxArea = static_cast(params.maxAreaRelative * imSize); + uint maxArea = + static_cast(params.maxAreaRelative * imSize * scale); + uint minArea = static_cast(params.minArea * scale); if (parent.empty() || parent.size != image.size) parent = Mat(image.rows, image.cols, CV_32S); @@ -304,7 +306,7 @@ class TBMR_Impl CV_FINAL : public TBMR if (parent_ptr[p] == p || ima_ptr[p] != ima_ptr[parent_ptr[p]]) { vecNodes[numNodes++] = p; - if (imaAttribute[p][0] >= params.minArea) // area + if (imaAttribute[p][0] >= minArea) // area numSons[parent_ptr[p]]++; } } @@ -568,6 +570,11 @@ void TBMR_Impl::detect(InputArray _image, : 1 /*todo calculate optimal scale factor from image size*/; float m_scale_factor = params.scale; + // track and eliminate duplicates introduced with multi scale position -> + // (size) + Mat dupl(src.rows / 4, src.cols / 4, CV_32F, cv::Scalar::all(0)); + float *dupl_ptr = dupl.ptr(); + std::vector pyr; TBMRImagePyramid scaleSpacer(src, m_cur_n_scales, m_scale_factor); pyr = scaleSpacer.getImPyr(); @@ -576,15 +583,41 @@ void TBMR_Impl::detect(InputArray _image, for (auto &s : pyr) { float scale = ((float)s.cols) / pyr.begin()->cols; + std::vector kpts; // append max tree tbmrs sortIdx(s.reshape(1, 1), S, SortFlags::SORT_ASCENDING | SortFlags::SORT_EVERY_ROW); - calculateTBMRs(s, keypoints, mask, scale, oct); + calculateTBMRs(s, kpts, mask, scale, oct); // reverse instead of sort flip(S, S, -1); - calculateTBMRs(s, keypoints, mask, scale, oct); + calculateTBMRs(s, kpts, mask, scale, oct); + + if (oct == 0) + { + for (const auto &k : kpts) + { + dupl_ptr[(int)(k.pt.x / 4) + + (int)(k.pt.y / 4) * (src.cols / 4)] = k.size; + } + keypoints.insert(keypoints.end(), kpts.begin(), kpts.end()); + } + else + { + for (const auto &k : kpts) + { + float &sz = dupl_ptr[(int)(k.pt.x / 4) + + (int)(k.pt.y / 4) * (src.cols / 4)]; + // we hereby add only features that are at least 4 pixels away + // or have a significantly different size + if (std::abs(k.size - sz) / std::max(k.size, sz) >= 0.2f) + { + sz = k.size; + keypoints.push_back(k); + } + } + } oct++; } From cd2909aea8f90e11368c0c3a0006054a89f313fa Mon Sep 17 00:00:00 2001 From: "steffen.ehrle" Date: Mon, 16 Nov 2020 09:43:13 +0100 Subject: [PATCH 17/22] fix exports --- modules/xfeatures2d/include/opencv2/xfeatures2d.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/xfeatures2d/include/opencv2/xfeatures2d.hpp b/modules/xfeatures2d/include/opencv2/xfeatures2d.hpp index 0b4e89c8fab..4672add6587 100644 --- a/modules/xfeatures2d/include/opencv2/xfeatures2d.hpp +++ b/modules/xfeatures2d/include/opencv2/xfeatures2d.hpp @@ -1044,7 +1044,7 @@ Features are ellipses (similar to MSER, however a MSER feature can never be a TBMR feature and vice versa). */ -class CV_EXPORTS_W TBMR : public AffineFeature2D +class CV_EXPORTS TBMR : public AffineFeature2D { public: CV_WRAP static Ptr create(int min_area = 60, From 7b714113cc240f988508e8d4f7760569d237bd93 Mon Sep 17 00:00:00 2001 From: "steffen.ehrle" Date: Mon, 16 Nov 2020 12:19:41 +0100 Subject: [PATCH 18/22] remove unrelated changes due to indentation --- .../include/opencv2/xfeatures2d.hpp | 1196 ++++++++--------- 1 file changed, 550 insertions(+), 646 deletions(-) diff --git a/modules/xfeatures2d/include/opencv2/xfeatures2d.hpp b/modules/xfeatures2d/include/opencv2/xfeatures2d.hpp index 4672add6587..495290bcbc2 100644 --- a/modules/xfeatures2d/include/opencv2/xfeatures2d.hpp +++ b/modules/xfeatures2d/include/opencv2/xfeatures2d.hpp @@ -50,16 +50,14 @@ This section describes experimental algorithms for 2d feature detection. @defgroup xfeatures2d_nonfree Non-free 2D Features Algorithms -This section describes two popular algorithms for 2d feature detection, SIFT and -SURF, that are known to be patented. You need to set the OPENCV_ENABLE_NONFREE -option in cmake to use those. Use them at your own risk. +This section describes two popular algorithms for 2d feature detection, SIFT and SURF, that are +known to be patented. You need to set the OPENCV_ENABLE_NONFREE option in cmake to use those. Use them at your own risk. @defgroup xfeatures2d_match Experimental 2D Features Matching Algorithm This section describes the following matching strategies: - GMS: Grid-based Motion Statistics, @cite Bian2017gms - - LOGOS: Local geometric support for high-outlier spatial verification, -@cite Lowry2018LOGOSLG + - LOGOS: Local geometric support for high-outlier spatial verification, @cite Lowry2018LOGOSLG @} */ @@ -72,16 +70,13 @@ namespace xfeatures2d //! @addtogroup xfeatures2d_experiment //! @{ -/** @brief Class implementing the FREAK (*Fast Retina Keypoint*) keypoint -descriptor, described in @cite AOV12 . +/** @brief Class implementing the FREAK (*Fast Retina Keypoint*) keypoint descriptor, described in @cite AOV12 . -The algorithm propose a novel keypoint descriptor inspired by the human visual -system and more precisely the retina, coined Fast Retina Key- point (FREAK). A -cascade of binary strings is computed by efficiently comparing image intensities -over a retinal sampling pattern. FREAKs are in general faster to compute with -lower memory load and also more robust than SIFT, SURF or BRISK. They are -competitive alternatives to existing keypoints in particular for embedded -applications. +The algorithm propose a novel keypoint descriptor inspired by the human visual system and more +precisely the retina, coined Fast Retina Key- point (FREAK). A cascade of binary strings is +computed by efficiently comparing image intensities over a retinal sampling pattern. FREAKs are in +general faster to compute with lower memory load and also more robust than SIFT, SURF or BRISK. +They are competitive alternatives to existing keypoints in particular for embedded applications. @note - An example on how to use the FREAK descriptor can be found at @@ -89,10 +84,11 @@ applications. */ class CV_EXPORTS_W FREAK : public Feature2D { - public: - static const int NB_SCALES = 64; - static const int NB_PAIRS = 512; - static const int NB_ORIENPAIRS = 45; +public: + + static const int NB_SCALES = 64; + static const int NB_PAIRS = 512; + static const int NB_ORIENPAIRS = 45; /** @param orientationNormalized Enable orientation normalization. @@ -101,24 +97,24 @@ class CV_EXPORTS_W FREAK : public Feature2D @param nOctaves Number of octaves covered by the detected keypoints. @param selectedPairs (Optional) user defined selected pairs indexes, */ - CV_WRAP static Ptr - create(bool orientationNormalized = true, bool scaleNormalized = true, - float patternScale = 22.0f, int nOctaves = 4, - const std::vector &selectedPairs = std::vector()); + CV_WRAP static Ptr create(bool orientationNormalized = true, + bool scaleNormalized = true, + float patternScale = 22.0f, + int nOctaves = 4, + const std::vector& selectedPairs = std::vector()); }; -/** @brief The class implements the keypoint detector introduced by @cite - * Agrawal08, synonym of StarDetector. : + +/** @brief The class implements the keypoint detector introduced by @cite Agrawal08, synonym of StarDetector. : */ class CV_EXPORTS_W StarDetector : public Feature2D { - public: +public: //! the full constructor - CV_WRAP static Ptr create(int maxSize = 45, - int responseThreshold = 30, - int lineThresholdProjected = 10, - int lineThresholdBinarized = 8, - int suppressNonmaxSize = 5); + CV_WRAP static Ptr create(int maxSize=45, int responseThreshold=30, + int lineThresholdProjected=10, + int lineThresholdBinarized=8, + int suppressNonmaxSize=5); }; /* @@ -127,21 +123,17 @@ class CV_EXPORTS_W StarDetector : public Feature2D /** @brief Class for computing BRIEF descriptors described in @cite calon2010 . -@param bytes legth of the descriptor in bytes, valid values are: 16, 32 -(default) or 64 . -@param use_orientation sample patterns using keypoints orientation, disabled by -default. +@param bytes legth of the descriptor in bytes, valid values are: 16, 32 (default) or 64 . +@param use_orientation sample patterns using keypoints orientation, disabled by default. */ class CV_EXPORTS_W BriefDescriptorExtractor : public Feature2D { - public: - CV_WRAP static Ptr - create(int bytes = 32, bool use_orientation = false); +public: + CV_WRAP static Ptr create( int bytes = 32, bool use_orientation = false ); }; -/** @brief Class implementing the locally uniform comparison image descriptor, -described in @cite LUCID +/** @brief Class implementing the locally uniform comparison image descriptor, described in @cite LUCID An image descriptor that can be computed very fast, while being about as robust as, for example, SURF or BRIEF. @@ -150,53 +142,41 @@ about as robust as, for example, SURF or BRIEF. */ class CV_EXPORTS_W LUCID : public Feature2D { - public: +public: /** - * @param lucid_kernel kernel for descriptor construction, where 1=3x3, - * 2=5x5, 3=7x7 and so forth - * @param blur_kernel kernel for blurring image prior to descriptor - * construction, where 1=3x3, 2=5x5, 3=7x7 and so forth + * @param lucid_kernel kernel for descriptor construction, where 1=3x3, 2=5x5, 3=7x7 and so forth + * @param blur_kernel kernel for blurring image prior to descriptor construction, where 1=3x3, 2=5x5, 3=7x7 and so forth */ - CV_WRAP static Ptr create(const int lucid_kernel = 1, - const int blur_kernel = 2); + CV_WRAP static Ptr create(const int lucid_kernel = 1, const int blur_kernel = 2); }; + /* - * LATCH Descriptor - */ +* LATCH Descriptor +*/ /** latch Class for computing the LATCH descriptor. -If you find this code useful, please add a reference to the following paper in -your work: Gil Levi and Tal Hassner, "LATCH: Learned Arrangements of Three Patch -Codes", arXiv preprint arXiv:1501.03719, 15 Jan. 2015 +If you find this code useful, please add a reference to the following paper in your work: +Gil Levi and Tal Hassner, "LATCH: Learned Arrangements of Three Patch Codes", arXiv preprint arXiv:1501.03719, 15 Jan. 2015 -LATCH is a binary descriptor based on learned comparisons of triplets of image -patches. +LATCH is a binary descriptor based on learned comparisons of triplets of image patches. * bytes is the size of the descriptor - can be 64, 32, 16, 8, 4, 2 or 1 -* rotationInvariance - whether or not the descriptor should compansate for -orientation changes. -* half_ssd_size - the size of half of the mini-patches size. For example, if we -would like to compare triplets of patches of size 7x7x then the half_ssd_size -should be (7-1)/2 = 3. -* sigma - sigma value for GaussianBlur smoothing of the source image. Source -image will be used without smoothing in case sigma value is 0. - -Note: the descriptor can be coupled with any keypoint extractor. The only demand -is that if you use set rotationInvariance = True then you will have to use an -extractor which estimates the patch orientation (in degrees). Examples for such -extractors are ORB and SIFT. - -Note: a complete example can be found under -/samples/cpp/tutorial_code/xfeatures2D/latch_match.cpp +* rotationInvariance - whether or not the descriptor should compansate for orientation changes. +* half_ssd_size - the size of half of the mini-patches size. For example, if we would like to compare triplets of patches of size 7x7x + then the half_ssd_size should be (7-1)/2 = 3. +* sigma - sigma value for GaussianBlur smoothing of the source image. Source image will be used without smoothing in case sigma value is 0. + +Note: the descriptor can be coupled with any keypoint extractor. The only demand is that if you use set rotationInvariance = True then + you will have to use an extractor which estimates the patch orientation (in degrees). Examples for such extractors are ORB and SIFT. + +Note: a complete example can be found under /samples/cpp/tutorial_code/xfeatures2D/latch_match.cpp */ class CV_EXPORTS_W LATCH : public Feature2D { - public: - CV_WRAP static Ptr create(int bytes = 32, - bool rotationInvariance = true, - int half_ssd_size = 3, double sigma = 2.0); +public: + CV_WRAP static Ptr create(int bytes = 32, bool rotationInvariance = true, int half_ssd_size = 3, double sigma = 2.0); }; /** @brief Class implementing DAISY descriptor, described in @cite Tola10 @@ -207,59 +187,48 @@ class CV_EXPORTS_W LATCH : public Feature2D @param q_hist amount of gradient orientations range division quantity @param norm choose descriptors normalization type, where DAISY::NRM_NONE will not do any normalization (default), -DAISY::NRM_PARTIAL mean that histograms are normalized independently for L2 norm -equal to 1.0, DAISY::NRM_FULL mean that descriptors are normalized for L2 norm -equal to 1.0, DAISY::NRM_SIFT mean that descriptors are normalized for L2 norm -equal to 1.0 but no individual one is bigger than 0.154 as in SIFT -@param H optional 3x3 homography matrix used to warp the grid of daisy but -sampling keypoints remains unwarped on image -@param interpolation switch to disable interpolation for speed improvement at -minor quality loss -@param use_orientation sample patterns using keypoints orientation, disabled by -default. +DAISY::NRM_PARTIAL mean that histograms are normalized independently for L2 norm equal to 1.0, +DAISY::NRM_FULL mean that descriptors are normalized for L2 norm equal to 1.0, +DAISY::NRM_SIFT mean that descriptors are normalized for L2 norm equal to 1.0 but no individual one is bigger than 0.154 as in SIFT +@param H optional 3x3 homography matrix used to warp the grid of daisy but sampling keypoints remains unwarped on image +@param interpolation switch to disable interpolation for speed improvement at minor quality loss +@param use_orientation sample patterns using keypoints orientation, disabled by default. */ class CV_EXPORTS_W DAISY : public Feature2D { - public: +public: enum NormalizationType { - NRM_NONE = 100, - NRM_PARTIAL = 101, - NRM_FULL = 102, - NRM_SIFT = 103, + NRM_NONE = 100, NRM_PARTIAL = 101, NRM_FULL = 102, NRM_SIFT = 103, }; - CV_WRAP static Ptr - create(float radius = 15, int q_radius = 3, int q_theta = 8, int q_hist = 8, - DAISY::NormalizationType norm = DAISY::NRM_NONE, - InputArray H = noArray(), bool interpolation = true, - bool use_orientation = false); + CV_WRAP static Ptr create( float radius = 15, int q_radius = 3, int q_theta = 8, + int q_hist = 8, DAISY::NormalizationType norm = DAISY::NRM_NONE, InputArray H = noArray(), + bool interpolation = true, bool use_orientation = false ); /** @overload * @param image image to extract descriptors * @param keypoints of interest within image * @param descriptors resulted descriptors array */ - virtual void compute(InputArray image, std::vector &keypoints, - OutputArray descriptors) CV_OVERRIDE = 0; + virtual void compute( InputArray image, std::vector& keypoints, OutputArray descriptors ) CV_OVERRIDE = 0; - virtual void compute(InputArrayOfArrays images, - std::vector> &keypoints, - OutputArrayOfArrays descriptors) CV_OVERRIDE; + virtual void compute( InputArrayOfArrays images, + std::vector >& keypoints, + OutputArrayOfArrays descriptors ) CV_OVERRIDE; /** @overload * @param image image to extract descriptors * @param roi region of interest within image * @param descriptors resulted descriptors array for roi image pixels */ - virtual void compute(InputArray image, Rect roi, - OutputArray descriptors) = 0; + virtual void compute( InputArray image, Rect roi, OutputArray descriptors ) = 0; /**@overload * @param image image to extract descriptors * @param descriptors resulted descriptors array for all image pixels */ - virtual void compute(InputArray image, OutputArray descriptors) = 0; + virtual void compute( InputArray image, OutputArray descriptors ) = 0; /** * @param y position y on image @@ -267,8 +236,7 @@ class CV_EXPORTS_W DAISY : public Feature2D * @param orientation orientation on image (0->360) * @param descriptor supplied array for descriptor storage */ - virtual void GetDescriptor(double y, double x, int orientation, - float *descriptor) const = 0; + virtual void GetDescriptor( double y, double x, int orientation, float* descriptor ) const = 0; /** * @param y position y on image @@ -277,8 +245,7 @@ class CV_EXPORTS_W DAISY : public Feature2D * @param descriptor supplied array for descriptor storage * @param H homography matrix for warped grid */ - virtual bool GetDescriptor(double y, double x, int orientation, - float *descriptor, double *H) const = 0; + virtual bool GetDescriptor( double y, double x, int orientation, float* descriptor, double* H ) const = 0; /** * @param y position y on image @@ -286,8 +253,7 @@ class CV_EXPORTS_W DAISY : public Feature2D * @param orientation orientation on image (0->360) * @param descriptor supplied array for descriptor storage */ - virtual void GetUnnormalizedDescriptor(double y, double x, int orientation, - float *descriptor) const = 0; + virtual void GetUnnormalizedDescriptor( double y, double x, int orientation, float* descriptor ) const = 0; /** * @param y position y on image @@ -296,75 +262,61 @@ class CV_EXPORTS_W DAISY : public Feature2D * @param descriptor supplied array for descriptor storage * @param H homography matrix for warped grid */ - virtual bool GetUnnormalizedDescriptor(double y, double x, int orientation, - float *descriptor, - double *H) const = 0; + virtual bool GetUnnormalizedDescriptor( double y, double x, int orientation, float* descriptor , double *H ) const = 0; + }; -/** @brief Class implementing the MSD (*Maximal Self-Dissimilarity*) keypoint -detector, described in @cite Tombari14. +/** @brief Class implementing the MSD (*Maximal Self-Dissimilarity*) keypoint detector, described in @cite Tombari14. -The algorithm implements a novel interest point detector stemming from the -intuition that image patches which are highly dissimilar over a relatively large -extent of their surroundings hold the property of being repeatable and -distinctive. This concept of "contextual self-dissimilarity" reverses the key -paradigm of recent successful techniques such as the Local Self-Similarity -descriptor and the Non-Local Means filter, which build upon the presence of -similar - rather than dissimilar - patches. Moreover, it extends to contextual -information the local self-dissimilarity notion embedded in established -detectors of corner-like interest points, thereby achieving enhanced -repeatability, distinctiveness and localization accuracy. +The algorithm implements a novel interest point detector stemming from the intuition that image patches +which are highly dissimilar over a relatively large extent of their surroundings hold the property of +being repeatable and distinctive. This concept of "contextual self-dissimilarity" reverses the key +paradigm of recent successful techniques such as the Local Self-Similarity descriptor and the Non-Local +Means filter, which build upon the presence of similar - rather than dissimilar - patches. Moreover, +it extends to contextual information the local self-dissimilarity notion embedded in established +detectors of corner-like interest points, thereby achieving enhanced repeatability, distinctiveness and +localization accuracy. */ -class CV_EXPORTS_W MSDDetector : public Feature2D -{ +class CV_EXPORTS_W MSDDetector : public Feature2D { - public: - static Ptr - create(int m_patch_radius = 3, int m_search_area_radius = 5, - int m_nms_radius = 5, int m_nms_scale_radius = 0, - float m_th_saliency = 250.0f, int m_kNN = 4, - float m_scale_factor = 1.25f, int m_n_scales = -1, - bool m_compute_orientation = false); +public: + + static Ptr create(int m_patch_radius = 3, int m_search_area_radius = 5, + int m_nms_radius = 5, int m_nms_scale_radius = 0, float m_th_saliency = 250.0f, int m_kNN = 4, + float m_scale_factor = 1.25f, int m_n_scales = -1, bool m_compute_orientation = false); }; -/** @brief Class implementing VGG (Oxford Visual Geometry Group) descriptor -trained end to end using "Descriptor Learning Using Convex Optimisation" (DLCO) -aparatus described in @cite Simonyan14. +/** @brief Class implementing VGG (Oxford Visual Geometry Group) descriptor trained end to end +using "Descriptor Learning Using Convex Optimisation" (DLCO) aparatus described in @cite Simonyan14. -@param desc type of descriptor to use, VGG::VGG_120 is default (120 dimensions -float) Available types are VGG::VGG_120, VGG::VGG_80, VGG::VGG_64, VGG::VGG_48 +@param desc type of descriptor to use, VGG::VGG_120 is default (120 dimensions float) +Available types are VGG::VGG_120, VGG::VGG_80, VGG::VGG_64, VGG::VGG_48 @param isigma gaussian kernel value for image blur (default is 1.4f) -@param img_normalize use image sample intensity normalization (enabled by -default) -@param use_orientation sample patterns using keypoints orientation, enabled by -default -@param scale_factor adjust the sampling window of detected keypoints to 64.0f -(VGG sampling window) 6.25f is default and fits for KAZE, SURF detected -keypoints window ratio 6.75f should be the scale for SIFT detected keypoints -window ratio 5.00f should be the scale for AKAZE, MSD, AGAST, FAST, BRISK -keypoints window ratio 0.75f should be the scale for ORB keypoints ratio - -@param dsc_normalize clamp descriptors to 255 and convert to uchar CV_8UC1 -(disabled by default) +@param img_normalize use image sample intensity normalization (enabled by default) +@param use_orientation sample patterns using keypoints orientation, enabled by default +@param scale_factor adjust the sampling window of detected keypoints to 64.0f (VGG sampling window) +6.25f is default and fits for KAZE, SURF detected keypoints window ratio +6.75f should be the scale for SIFT detected keypoints window ratio +5.00f should be the scale for AKAZE, MSD, AGAST, FAST, BRISK keypoints window ratio +0.75f should be the scale for ORB keypoints ratio + +@param dsc_normalize clamp descriptors to 255 and convert to uchar CV_8UC1 (disabled by default) */ class CV_EXPORTS_W VGG : public Feature2D { - public: - CV_WRAP enum { - VGG_120 = 100, - VGG_80 = 101, - VGG_64 = 102, - VGG_48 = 103, +public: + + CV_WRAP enum + { + VGG_120 = 100, VGG_80 = 101, VGG_64 = 102, VGG_48 = 103, }; - CV_WRAP static Ptr create(int desc = VGG::VGG_120, float isigma = 1.4f, - bool img_normalize = true, - bool use_scale_orientation = true, - float scale_factor = 6.25f, - bool dsc_normalize = false); + CV_WRAP static Ptr create( int desc = VGG::VGG_120, float isigma = 1.4f, + bool img_normalize = true, bool use_scale_orientation = true, + float scale_factor = 6.25f, bool dsc_normalize = false ); CV_WRAP virtual void setSigma(const float isigma) = 0; CV_WRAP virtual float getSigma() const = 0; @@ -372,636 +324,600 @@ class CV_EXPORTS_W VGG : public Feature2D CV_WRAP virtual void setUseNormalizeImage(const bool img_normalize) = 0; CV_WRAP virtual bool getUseNormalizeImage() const = 0; - CV_WRAP virtual void - setUseScaleOrientation(const bool use_scale_orientation) = 0; + CV_WRAP virtual void setUseScaleOrientation(const bool use_scale_orientation) = 0; CV_WRAP virtual bool getUseScaleOrientation() const = 0; CV_WRAP virtual void setScaleFactor(const float scale_factor) = 0; CV_WRAP virtual float getScaleFactor() const = 0; - CV_WRAP virtual void - setUseNormalizeDescriptor(const bool dsc_normalize) = 0; + CV_WRAP virtual void setUseNormalizeDescriptor(const bool dsc_normalize) = 0; CV_WRAP virtual bool getUseNormalizeDescriptor() const = 0; }; -/** @brief Class implementing BoostDesc (Learning Image Descriptors with -Boosting), described in +/** @brief Class implementing BoostDesc (Learning Image Descriptors with Boosting), described in @cite Trzcinski13a and @cite Trzcinski13b. -@param desc type of descriptor to use, BoostDesc::BINBOOST_256 is default (256 -bit long dimension) Available types are: BoostDesc::BGM, BoostDesc::BGM_HARD, -BoostDesc::BGM_BILINEAR, BoostDesc::LBGM, BoostDesc::BINBOOST_64, -BoostDesc::BINBOOST_128, BoostDesc::BINBOOST_256 -@param use_orientation sample patterns using keypoints orientation, enabled by -default +@param desc type of descriptor to use, BoostDesc::BINBOOST_256 is default (256 bit long dimension) +Available types are: BoostDesc::BGM, BoostDesc::BGM_HARD, BoostDesc::BGM_BILINEAR, BoostDesc::LBGM, +BoostDesc::BINBOOST_64, BoostDesc::BINBOOST_128, BoostDesc::BINBOOST_256 +@param use_orientation sample patterns using keypoints orientation, enabled by default @param scale_factor adjust the sampling window of detected keypoints 6.25f is default and fits for KAZE, SURF detected keypoints window ratio 6.75f should be the scale for SIFT detected keypoints window ratio -5.00f should be the scale for AKAZE, MSD, AGAST, FAST, BRISK keypoints window -ratio 0.75f should be the scale for ORB keypoints ratio 1.50f was the default in -original implementation - -@note BGM is the base descriptor where each binary dimension is computed as the -output of a single weak learner. BGM_HARD and BGM_BILINEAR refers to same BGM -but use different type of gradient binning. In the BGM_HARD that use ASSIGN_HARD -binning type the gradient is assigned to the nearest orientation bin. In the -BGM_BILINEAR that use ASSIGN_BILINEAR binning type the gradient is assigned to -the two neighbouring bins. In the BGM and all other modes that use ASSIGN_SOFT -binning type the gradient is assigned to 8 nearest bins according to the cosine -value between the gradient angle and the bin center. LBGM (alias FP-Boost) is -the floating point extension where each dimension is computed as a linear -combination of the weak learner responses. BINBOOST and subvariants are the -binary extensions of LBGM where each bit is computed as a thresholded linear -combination of a set of weak learners. BoostDesc header files (boostdesc_*.i) -was exported from original binaries with export-boostdesc.py script from samples -subfolder. +5.00f should be the scale for AKAZE, MSD, AGAST, FAST, BRISK keypoints window ratio +0.75f should be the scale for ORB keypoints ratio +1.50f was the default in original implementation + +@note BGM is the base descriptor where each binary dimension is computed as the output of a single weak learner. +BGM_HARD and BGM_BILINEAR refers to same BGM but use different type of gradient binning. In the BGM_HARD that +use ASSIGN_HARD binning type the gradient is assigned to the nearest orientation bin. In the BGM_BILINEAR that use +ASSIGN_BILINEAR binning type the gradient is assigned to the two neighbouring bins. In the BGM and all other modes that use +ASSIGN_SOFT binning type the gradient is assigned to 8 nearest bins according to the cosine value between the gradient +angle and the bin center. LBGM (alias FP-Boost) is the floating point extension where each dimension is computed +as a linear combination of the weak learner responses. BINBOOST and subvariants are the binary extensions of LBGM +where each bit is computed as a thresholded linear combination of a set of weak learners. +BoostDesc header files (boostdesc_*.i) was exported from original binaries with export-boostdesc.py script from +samples subfolder. */ class CV_EXPORTS_W BoostDesc : public Feature2D { - public: - CV_WRAP enum { - BGM = 100, - BGM_HARD = 101, - BGM_BILINEAR = 102, - LBGM = 200, - BINBOOST_64 = 300, - BINBOOST_128 = 301, - BINBOOST_256 = 302 +public: + + CV_WRAP enum + { + BGM = 100, BGM_HARD = 101, BGM_BILINEAR = 102, LBGM = 200, + BINBOOST_64 = 300, BINBOOST_128 = 301, BINBOOST_256 = 302 }; - CV_WRAP static Ptr create(int desc = BoostDesc::BINBOOST_256, - bool use_scale_orientation = true, - float scale_factor = 6.25f); + CV_WRAP static Ptr create( int desc = BoostDesc::BINBOOST_256, + bool use_scale_orientation = true, float scale_factor = 6.25f ); - CV_WRAP virtual void - setUseScaleOrientation(const bool use_scale_orientation) = 0; + CV_WRAP virtual void setUseScaleOrientation(const bool use_scale_orientation) = 0; CV_WRAP virtual bool getUseScaleOrientation() const = 0; CV_WRAP virtual void setScaleFactor(const float scale_factor) = 0; CV_WRAP virtual float getScaleFactor() const = 0; }; + /* - * Position-Color-Texture signatures - */ +* Position-Color-Texture signatures +*/ /** - * @brief Class implementing PCT (position-color-texture) signature extraction - * as described in @cite KrulisLS16. - * The algorithm is divided to a feature sampler and a clusterizer. - * Feature sampler produces samples at given set of coordinates. - * Clusterizer then produces clusters of these samples using k-means - * algorithm. Resulting set of clusters is the signature of the input image. - * - * A signature is an array of SIGNATURE_DIMENSION-dimensional points. - * Used dimensions are: - * weight, x, y position; lab color, contrast, entropy. - * @cite KrulisLS16 - * @cite BeecksUS10 - */ +* @brief Class implementing PCT (position-color-texture) signature extraction +* as described in @cite KrulisLS16. +* The algorithm is divided to a feature sampler and a clusterizer. +* Feature sampler produces samples at given set of coordinates. +* Clusterizer then produces clusters of these samples using k-means algorithm. +* Resulting set of clusters is the signature of the input image. +* +* A signature is an array of SIGNATURE_DIMENSION-dimensional points. +* Used dimensions are: +* weight, x, y position; lab color, contrast, entropy. +* @cite KrulisLS16 +* @cite BeecksUS10 +*/ class CV_EXPORTS_W PCTSignatures : public Algorithm { - public: +public: /** - * @brief Lp distance function selector. - */ + * @brief Lp distance function selector. + */ enum DistanceFunction { - L0_25, - L0_5, - L1, - L2, - L2SQUARED, - L5, - L_INFINITY + L0_25, L0_5, L1, L2, L2SQUARED, L5, L_INFINITY }; /** - * @brief Point distributions supported by random point generator. - */ + * @brief Point distributions supported by random point generator. + */ enum PointDistribution { - UNIFORM, //!< Generate numbers uniformly. - REGULAR, //!< Generate points in a regular grid. - NORMAL //!< Generate points with normal (gaussian) distribution. + UNIFORM, //!< Generate numbers uniformly. + REGULAR, //!< Generate points in a regular grid. + NORMAL //!< Generate points with normal (gaussian) distribution. }; /** - * @brief Similarity function selector. - * @see - * Christian Beecks, Merih Seran Uysal, Thomas Seidl. - * Signature quadratic form distance. - * In Proceedings of the ACM International Conference on Image and - * Video Retrieval, pages 438-445. ACM, 2010. - * @cite BeecksUS10 - * @note For selected distance function: \f[ d(c_i, c_j) \f] and parameter: - * \f[ \alpha \f] - */ + * @brief Similarity function selector. + * @see + * Christian Beecks, Merih Seran Uysal, Thomas Seidl. + * Signature quadratic form distance. + * In Proceedings of the ACM International Conference on Image and Video Retrieval, pages 438-445. + * ACM, 2010. + * @cite BeecksUS10 + * @note For selected distance function: \f[ d(c_i, c_j) \f] and parameter: \f[ \alpha \f] + */ enum SimilarityFunction { - MINUS, //!< \f[ -d(c_i, c_j) \f] - GAUSSIAN, //!< \f[ e^{ -\alpha * d^2(c_i, c_j)} \f] - HEURISTIC //!< \f[ \frac{1}{\alpha + d(c_i, c_j)} \f] + MINUS, //!< \f[ -d(c_i, c_j) \f] + GAUSSIAN, //!< \f[ e^{ -\alpha * d^2(c_i, c_j)} \f] + HEURISTIC //!< \f[ \frac{1}{\alpha + d(c_i, c_j)} \f] }; - /** - * @brief Creates PCTSignatures algorithm using sample and seed count. - * It generates its own sets of sampling points and clusterization - * seed indexes. - * @param initSampleCount Number of points used for image sampling. - * @param initSeedCount Number of initial clusterization seeds. - * Must be lower or equal to initSampleCount - * @param pointDistribution Distribution of generated points. Default: - * UNIFORM. Available: UNIFORM, REGULAR, NORMAL. - * @return Created algorithm. - */ - CV_WRAP static Ptr create(const int initSampleCount = 2000, - const int initSeedCount = 400, - const int pointDistribution = 0); - - /** - * @brief Creates PCTSignatures algorithm using pre-generated sampling - * points and number of clusterization seeds. It uses the provided sampling - * points and generates its own clusterization seed indexes. - * @param initSamplingPoints Sampling points used in image sampling. - * @param initSeedCount Number of initial clusterization seeds. - * Must be lower or equal to initSamplingPoints.size(). - * @return Created algorithm. - */ - CV_WRAP static Ptr - create(const std::vector &initSamplingPoints, - const int initSeedCount); /** - * @brief Creates PCTSignatures algorithm using pre-generated sampling - * points and clusterization seeds indexes. - * @param initSamplingPoints Sampling points used in image sampling. - * @param initClusterSeedIndexes Indexes of initial clusterization seeds. - * Its size must be lower or equal to initSamplingPoints.size(). - * @return Created algorithm. - */ - CV_WRAP static Ptr - create(const std::vector &initSamplingPoints, - const std::vector &initClusterSeedIndexes); + * @brief Creates PCTSignatures algorithm using sample and seed count. + * It generates its own sets of sampling points and clusterization seed indexes. + * @param initSampleCount Number of points used for image sampling. + * @param initSeedCount Number of initial clusterization seeds. + * Must be lower or equal to initSampleCount + * @param pointDistribution Distribution of generated points. Default: UNIFORM. + * Available: UNIFORM, REGULAR, NORMAL. + * @return Created algorithm. + */ + CV_WRAP static Ptr create( + const int initSampleCount = 2000, + const int initSeedCount = 400, + const int pointDistribution = 0); + + /** + * @brief Creates PCTSignatures algorithm using pre-generated sampling points + * and number of clusterization seeds. It uses the provided + * sampling points and generates its own clusterization seed indexes. + * @param initSamplingPoints Sampling points used in image sampling. + * @param initSeedCount Number of initial clusterization seeds. + * Must be lower or equal to initSamplingPoints.size(). + * @return Created algorithm. + */ + CV_WRAP static Ptr create( + const std::vector& initSamplingPoints, + const int initSeedCount); + + /** + * @brief Creates PCTSignatures algorithm using pre-generated sampling points + * and clusterization seeds indexes. + * @param initSamplingPoints Sampling points used in image sampling. + * @param initClusterSeedIndexes Indexes of initial clusterization seeds. + * Its size must be lower or equal to initSamplingPoints.size(). + * @return Created algorithm. + */ + CV_WRAP static Ptr create( + const std::vector& initSamplingPoints, + const std::vector& initClusterSeedIndexes); + + + + /** + * @brief Computes signature of given image. + * @param image Input image of CV_8U type. + * @param signature Output computed signature. + */ + CV_WRAP virtual void computeSignature( + InputArray image, + OutputArray signature) const = 0; + + /** + * @brief Computes signatures for multiple images in parallel. + * @param images Vector of input images of CV_8U type. + * @param signatures Vector of computed signatures. + */ + CV_WRAP virtual void computeSignatures( + const std::vector& images, + std::vector& signatures) const = 0; + + /** + * @brief Draws signature in the source image and outputs the result. + * Signatures are visualized as a circle + * with radius based on signature weight + * and color based on signature color. + * Contrast and entropy are not visualized. + * @param source Source image. + * @param signature Image signature. + * @param result Output result. + * @param radiusToShorterSideRatio Determines maximal radius of signature in the output image. + * @param borderThickness Border thickness of the visualized signature. + */ + CV_WRAP static void drawSignature( + InputArray source, + InputArray signature, + OutputArray result, + float radiusToShorterSideRatio = 1.0 / 8, + int borderThickness = 1); + + /** + * @brief Generates initial sampling points according to selected point distribution. + * @param initPoints Output vector where the generated points will be saved. + * @param count Number of points to generate. + * @param pointDistribution Point distribution selector. + * Available: UNIFORM, REGULAR, NORMAL. + * @note Generated coordinates are in range [0..1) + */ + CV_WRAP static void generateInitPoints( + std::vector& initPoints, + const int count, + int pointDistribution); - /** - * @brief Computes signature of given image. - * @param image Input image of CV_8U type. - * @param signature Output computed signature. - */ - CV_WRAP virtual void computeSignature(InputArray image, - OutputArray signature) const = 0; - - /** - * @brief Computes signatures for multiple images in parallel. - * @param images Vector of input images of CV_8U type. - * @param signatures Vector of computed signatures. - */ - CV_WRAP virtual void - computeSignatures(const std::vector &images, - std::vector &signatures) const = 0; - - /** - * @brief Draws signature in the source image and outputs the result. - * Signatures are visualized as a circle - * with radius based on signature weight - * and color based on signature color. - * Contrast and entropy are not visualized. - * @param source Source image. - * @param signature Image signature. - * @param result Output result. - * @param radiusToShorterSideRatio Determines maximal radius of signature in - * the output image. - * @param borderThickness Border thickness of the visualized signature. - */ - CV_WRAP static void drawSignature(InputArray source, InputArray signature, - OutputArray result, - float radiusToShorterSideRatio = 1.0 / 8, - int borderThickness = 1); - - /** - * @brief Generates initial sampling points according to selected point - * distribution. - * @param initPoints Output vector where the generated points will be saved. - * @param count Number of points to generate. - * @param pointDistribution Point distribution selector. - * Available: UNIFORM, REGULAR, NORMAL. - * @note Generated coordinates are in range [0..1) - */ - CV_WRAP static void generateInitPoints(std::vector &initPoints, - const int count, - int pointDistribution); /**** sampler ****/ /** - * @brief Number of initial samples taken from the image. - */ + * @brief Number of initial samples taken from the image. + */ CV_WRAP virtual int getSampleCount() const = 0; /** - * @brief Color resolution of the greyscale bitmap represented in allocated - * bits (i.e., value 4 means that 16 shades of grey are used). The greyscale - * bitmap is used for computing contrast and entropy values. - */ + * @brief Color resolution of the greyscale bitmap represented in allocated bits + * (i.e., value 4 means that 16 shades of grey are used). + * The greyscale bitmap is used for computing contrast and entropy values. + */ CV_WRAP virtual int getGrayscaleBits() const = 0; /** - * @brief Color resolution of the greyscale bitmap represented in allocated - * bits (i.e., value 4 means that 16 shades of grey are used). The greyscale - * bitmap is used for computing contrast and entropy values. - */ + * @brief Color resolution of the greyscale bitmap represented in allocated bits + * (i.e., value 4 means that 16 shades of grey are used). + * The greyscale bitmap is used for computing contrast and entropy values. + */ CV_WRAP virtual void setGrayscaleBits(int grayscaleBits) = 0; /** - * @brief Size of the texture sampling window used to compute contrast and - * entropy (center of the window is always in the pixel selected by x,y - * coordinates of the corresponding feature sample). - */ + * @brief Size of the texture sampling window used to compute contrast and entropy + * (center of the window is always in the pixel selected by x,y coordinates + * of the corresponding feature sample). + */ CV_WRAP virtual int getWindowRadius() const = 0; /** - * @brief Size of the texture sampling window used to compute contrast and - * entropy (center of the window is always in the pixel selected by x,y - * coordinates of the corresponding feature sample). - */ + * @brief Size of the texture sampling window used to compute contrast and entropy + * (center of the window is always in the pixel selected by x,y coordinates + * of the corresponding feature sample). + */ CV_WRAP virtual void setWindowRadius(int radius) = 0; + /** - * @brief Weights (multiplicative constants) that linearly stretch - * individual axes of the feature space (x,y = position; L,a,b = color in - * CIE Lab space; c = contrast. e = entropy) - */ + * @brief Weights (multiplicative constants) that linearly stretch individual axes of the feature space + * (x,y = position; L,a,b = color in CIE Lab space; c = contrast. e = entropy) + */ CV_WRAP virtual float getWeightX() const = 0; /** - * @brief Weights (multiplicative constants) that linearly stretch - * individual axes of the feature space (x,y = position; L,a,b = color in - * CIE Lab space; c = contrast. e = entropy) - */ + * @brief Weights (multiplicative constants) that linearly stretch individual axes of the feature space + * (x,y = position; L,a,b = color in CIE Lab space; c = contrast. e = entropy) + */ CV_WRAP virtual void setWeightX(float weight) = 0; /** - * @brief Weights (multiplicative constants) that linearly stretch - * individual axes of the feature space (x,y = position; L,a,b = color in - * CIE Lab space; c = contrast. e = entropy) - */ + * @brief Weights (multiplicative constants) that linearly stretch individual axes of the feature space + * (x,y = position; L,a,b = color in CIE Lab space; c = contrast. e = entropy) + */ CV_WRAP virtual float getWeightY() const = 0; /** - * @brief Weights (multiplicative constants) that linearly stretch - * individual axes of the feature space (x,y = position; L,a,b = color in - * CIE Lab space; c = contrast. e = entropy) - */ + * @brief Weights (multiplicative constants) that linearly stretch individual axes of the feature space + * (x,y = position; L,a,b = color in CIE Lab space; c = contrast. e = entropy) + */ CV_WRAP virtual void setWeightY(float weight) = 0; /** - * @brief Weights (multiplicative constants) that linearly stretch - * individual axes of the feature space (x,y = position; L,a,b = color in - * CIE Lab space; c = contrast. e = entropy) - */ + * @brief Weights (multiplicative constants) that linearly stretch individual axes of the feature space + * (x,y = position; L,a,b = color in CIE Lab space; c = contrast. e = entropy) + */ CV_WRAP virtual float getWeightL() const = 0; /** - * @brief Weights (multiplicative constants) that linearly stretch - * individual axes of the feature space (x,y = position; L,a,b = color in - * CIE Lab space; c = contrast. e = entropy) - */ + * @brief Weights (multiplicative constants) that linearly stretch individual axes of the feature space + * (x,y = position; L,a,b = color in CIE Lab space; c = contrast. e = entropy) + */ CV_WRAP virtual void setWeightL(float weight) = 0; /** - * @brief Weights (multiplicative constants) that linearly stretch - * individual axes of the feature space (x,y = position; L,a,b = color in - * CIE Lab space; c = contrast. e = entropy) - */ + * @brief Weights (multiplicative constants) that linearly stretch individual axes of the feature space + * (x,y = position; L,a,b = color in CIE Lab space; c = contrast. e = entropy) + */ CV_WRAP virtual float getWeightA() const = 0; /** - * @brief Weights (multiplicative constants) that linearly stretch - * individual axes of the feature space (x,y = position; L,a,b = color in - * CIE Lab space; c = contrast. e = entropy) - */ + * @brief Weights (multiplicative constants) that linearly stretch individual axes of the feature space + * (x,y = position; L,a,b = color in CIE Lab space; c = contrast. e = entropy) + */ CV_WRAP virtual void setWeightA(float weight) = 0; /** - * @brief Weights (multiplicative constants) that linearly stretch - * individual axes of the feature space (x,y = position; L,a,b = color in - * CIE Lab space; c = contrast. e = entropy) - */ + * @brief Weights (multiplicative constants) that linearly stretch individual axes of the feature space + * (x,y = position; L,a,b = color in CIE Lab space; c = contrast. e = entropy) + */ CV_WRAP virtual float getWeightB() const = 0; /** - * @brief Weights (multiplicative constants) that linearly stretch - * individual axes of the feature space (x,y = position; L,a,b = color in - * CIE Lab space; c = contrast. e = entropy) - */ + * @brief Weights (multiplicative constants) that linearly stretch individual axes of the feature space + * (x,y = position; L,a,b = color in CIE Lab space; c = contrast. e = entropy) + */ CV_WRAP virtual void setWeightB(float weight) = 0; /** - * @brief Weights (multiplicative constants) that linearly stretch - * individual axes of the feature space (x,y = position; L,a,b = color in - * CIE Lab space; c = contrast. e = entropy) - */ + * @brief Weights (multiplicative constants) that linearly stretch individual axes of the feature space + * (x,y = position; L,a,b = color in CIE Lab space; c = contrast. e = entropy) + */ CV_WRAP virtual float getWeightContrast() const = 0; /** - * @brief Weights (multiplicative constants) that linearly stretch - * individual axes of the feature space (x,y = position; L,a,b = color in - * CIE Lab space; c = contrast. e = entropy) - */ + * @brief Weights (multiplicative constants) that linearly stretch individual axes of the feature space + * (x,y = position; L,a,b = color in CIE Lab space; c = contrast. e = entropy) + */ CV_WRAP virtual void setWeightContrast(float weight) = 0; /** - * @brief Weights (multiplicative constants) that linearly stretch - * individual axes of the feature space (x,y = position; L,a,b = color in - * CIE Lab space; c = contrast. e = entropy) - */ + * @brief Weights (multiplicative constants) that linearly stretch individual axes of the feature space + * (x,y = position; L,a,b = color in CIE Lab space; c = contrast. e = entropy) + */ CV_WRAP virtual float getWeightEntropy() const = 0; /** - * @brief Weights (multiplicative constants) that linearly stretch - * individual axes of the feature space (x,y = position; L,a,b = color in - * CIE Lab space; c = contrast. e = entropy) - */ + * @brief Weights (multiplicative constants) that linearly stretch individual axes of the feature space + * (x,y = position; L,a,b = color in CIE Lab space; c = contrast. e = entropy) + */ CV_WRAP virtual void setWeightEntropy(float weight) = 0; /** - * @brief Initial samples taken from the image. - * These sampled features become the input for clustering. - */ + * @brief Initial samples taken from the image. + * These sampled features become the input for clustering. + */ CV_WRAP virtual std::vector getSamplingPoints() const = 0; + + /** - * @brief Weights (multiplicative constants) that linearly stretch - * individual axes of the feature space. - * @param idx ID of the weight - * @param value Value of the weight - * @note - * WEIGHT_IDX = 0; - * X_IDX = 1; - * Y_IDX = 2; - * L_IDX = 3; - * A_IDX = 4; - * B_IDX = 5; - * CONTRAST_IDX = 6; - * ENTROPY_IDX = 7; - */ + * @brief Weights (multiplicative constants) that linearly stretch individual axes of the feature space. + * @param idx ID of the weight + * @param value Value of the weight + * @note + * WEIGHT_IDX = 0; + * X_IDX = 1; + * Y_IDX = 2; + * L_IDX = 3; + * A_IDX = 4; + * B_IDX = 5; + * CONTRAST_IDX = 6; + * ENTROPY_IDX = 7; + */ CV_WRAP virtual void setWeight(int idx, float value) = 0; /** - * @brief Weights (multiplicative constants) that linearly stretch - * individual axes of the feature space. - * @param weights Values of all weights. - * @note - * WEIGHT_IDX = 0; - * X_IDX = 1; - * Y_IDX = 2; - * L_IDX = 3; - * A_IDX = 4; - * B_IDX = 5; - * CONTRAST_IDX = 6; - * ENTROPY_IDX = 7; - */ - CV_WRAP virtual void setWeights(const std::vector &weights) = 0; - - /** - * @brief Translations of the individual axes of the feature space. - * @param idx ID of the translation - * @param value Value of the translation - * @note - * WEIGHT_IDX = 0; - * X_IDX = 1; - * Y_IDX = 2; - * L_IDX = 3; - * A_IDX = 4; - * B_IDX = 5; - * CONTRAST_IDX = 6; - * ENTROPY_IDX = 7; - */ + * @brief Weights (multiplicative constants) that linearly stretch individual axes of the feature space. + * @param weights Values of all weights. + * @note + * WEIGHT_IDX = 0; + * X_IDX = 1; + * Y_IDX = 2; + * L_IDX = 3; + * A_IDX = 4; + * B_IDX = 5; + * CONTRAST_IDX = 6; + * ENTROPY_IDX = 7; + */ + CV_WRAP virtual void setWeights(const std::vector& weights) = 0; + + /** + * @brief Translations of the individual axes of the feature space. + * @param idx ID of the translation + * @param value Value of the translation + * @note + * WEIGHT_IDX = 0; + * X_IDX = 1; + * Y_IDX = 2; + * L_IDX = 3; + * A_IDX = 4; + * B_IDX = 5; + * CONTRAST_IDX = 6; + * ENTROPY_IDX = 7; + */ CV_WRAP virtual void setTranslation(int idx, float value) = 0; /** - * @brief Translations of the individual axes of the feature space. - * @param translations Values of all translations. - * @note - * WEIGHT_IDX = 0; - * X_IDX = 1; - * Y_IDX = 2; - * L_IDX = 3; - * A_IDX = 4; - * B_IDX = 5; - * CONTRAST_IDX = 6; - * ENTROPY_IDX = 7; - */ - CV_WRAP virtual void - setTranslations(const std::vector &translations) = 0; + * @brief Translations of the individual axes of the feature space. + * @param translations Values of all translations. + * @note + * WEIGHT_IDX = 0; + * X_IDX = 1; + * Y_IDX = 2; + * L_IDX = 3; + * A_IDX = 4; + * B_IDX = 5; + * CONTRAST_IDX = 6; + * ENTROPY_IDX = 7; + */ + CV_WRAP virtual void setTranslations(const std::vector& translations) = 0; /** - * @brief Sets sampling points used to sample the input image. - * @param samplingPoints Vector of sampling points in range [0..1) - * @note Number of sampling points must be greater or equal to - * clusterization seed count. - */ - CV_WRAP virtual void - setSamplingPoints(std::vector samplingPoints) = 0; + * @brief Sets sampling points used to sample the input image. + * @param samplingPoints Vector of sampling points in range [0..1) + * @note Number of sampling points must be greater or equal to clusterization seed count. + */ + CV_WRAP virtual void setSamplingPoints(std::vector samplingPoints) = 0; + + /**** clusterizer ****/ /** - * @brief Initial seeds (initial number of clusters) for the k-means - * algorithm. - */ + * @brief Initial seeds (initial number of clusters) for the k-means algorithm. + */ CV_WRAP virtual std::vector getInitSeedIndexes() const = 0; /** - * @brief Initial seed indexes for the k-means algorithm. - */ - CV_WRAP virtual void - setInitSeedIndexes(std::vector initSeedIndexes) = 0; + * @brief Initial seed indexes for the k-means algorithm. + */ + CV_WRAP virtual void setInitSeedIndexes(std::vector initSeedIndexes) = 0; /** - * @brief Number of initial seeds (initial number of clusters) for the - * k-means algorithm. - */ + * @brief Number of initial seeds (initial number of clusters) for the k-means algorithm. + */ CV_WRAP virtual int getInitSeedCount() const = 0; /** - * @brief Number of iterations of the k-means clustering. - * We use fixed number of iterations, since the modified clustering is - * pruning clusters (not iteratively refining k clusters). - */ + * @brief Number of iterations of the k-means clustering. + * We use fixed number of iterations, since the modified clustering is pruning clusters + * (not iteratively refining k clusters). + */ CV_WRAP virtual int getIterationCount() const = 0; /** - * @brief Number of iterations of the k-means clustering. - * We use fixed number of iterations, since the modified clustering is - * pruning clusters (not iteratively refining k clusters). - */ + * @brief Number of iterations of the k-means clustering. + * We use fixed number of iterations, since the modified clustering is pruning clusters + * (not iteratively refining k clusters). + */ CV_WRAP virtual void setIterationCount(int iterationCount) = 0; /** - * @brief Maximal number of generated clusters. If the number is exceeded, - * the clusters are sorted by their weights and the smallest clusters - * are cropped. - */ + * @brief Maximal number of generated clusters. If the number is exceeded, + * the clusters are sorted by their weights and the smallest clusters are cropped. + */ CV_WRAP virtual int getMaxClustersCount() const = 0; /** - * @brief Maximal number of generated clusters. If the number is exceeded, - * the clusters are sorted by their weights and the smallest clusters - * are cropped. - */ + * @brief Maximal number of generated clusters. If the number is exceeded, + * the clusters are sorted by their weights and the smallest clusters are cropped. + */ CV_WRAP virtual void setMaxClustersCount(int maxClustersCount) = 0; /** - * @brief This parameter multiplied by the index of iteration gives lower - * limit for cluster size. Clusters containing fewer points than specified - * by the limit have their centroid dismissed and points are reassigned. - */ + * @brief This parameter multiplied by the index of iteration gives lower limit for cluster size. + * Clusters containing fewer points than specified by the limit have their centroid dismissed + * and points are reassigned. + */ CV_WRAP virtual int getClusterMinSize() const = 0; /** - * @brief This parameter multiplied by the index of iteration gives lower - * limit for cluster size. Clusters containing fewer points than specified - * by the limit have their centroid dismissed and points are reassigned. - */ + * @brief This parameter multiplied by the index of iteration gives lower limit for cluster size. + * Clusters containing fewer points than specified by the limit have their centroid dismissed + * and points are reassigned. + */ CV_WRAP virtual void setClusterMinSize(int clusterMinSize) = 0; /** - * @brief Threshold euclidean distance between two centroids. - * If two cluster centers are closer than this distance, - * one of the centroid is dismissed and points are reassigned. - */ + * @brief Threshold euclidean distance between two centroids. + * If two cluster centers are closer than this distance, + * one of the centroid is dismissed and points are reassigned. + */ CV_WRAP virtual float getJoiningDistance() const = 0; /** - * @brief Threshold euclidean distance between two centroids. - * If two cluster centers are closer than this distance, - * one of the centroid is dismissed and points are reassigned. - */ + * @brief Threshold euclidean distance between two centroids. + * If two cluster centers are closer than this distance, + * one of the centroid is dismissed and points are reassigned. + */ CV_WRAP virtual void setJoiningDistance(float joiningDistance) = 0; /** - * @brief Remove centroids in k-means whose weight is lesser or equal to - * given threshold. - */ + * @brief Remove centroids in k-means whose weight is lesser or equal to given threshold. + */ CV_WRAP virtual float getDropThreshold() const = 0; /** - * @brief Remove centroids in k-means whose weight is lesser or equal to - * given threshold. - */ + * @brief Remove centroids in k-means whose weight is lesser or equal to given threshold. + */ CV_WRAP virtual void setDropThreshold(float dropThreshold) = 0; /** - * @brief Distance function selector used for measuring distance between two - * points in k-means. - */ + * @brief Distance function selector used for measuring distance between two points in k-means. + */ CV_WRAP virtual int getDistanceFunction() const = 0; /** - * @brief Distance function selector used for measuring distance between two - * points in k-means. Available: L0_25, L0_5, L1, L2, L2SQUARED, L5, - * L_INFINITY. - */ + * @brief Distance function selector used for measuring distance between two points in k-means. + * Available: L0_25, L0_5, L1, L2, L2SQUARED, L5, L_INFINITY. + */ CV_WRAP virtual void setDistanceFunction(int distanceFunction) = 0; + }; /** - * @brief Class implementing Signature Quadratic Form Distance (SQFD). - * @see Christian Beecks, Merih Seran Uysal, Thomas Seidl. - * Signature quadratic form distance. - * In Proceedings of the ACM International Conference on Image and Video - * Retrieval, pages 438-445. ACM, 2010. - * @cite BeecksUS10 - */ +* @brief Class implementing Signature Quadratic Form Distance (SQFD). +* @see Christian Beecks, Merih Seran Uysal, Thomas Seidl. +* Signature quadratic form distance. +* In Proceedings of the ACM International Conference on Image and Video Retrieval, pages 438-445. +* ACM, 2010. +* @cite BeecksUS10 +*/ class CV_EXPORTS_W PCTSignaturesSQFD : public Algorithm { - public: - /** - * @brief Creates the algorithm instance using selected distance function, - * similarity function and similarity function parameter. - * @param distanceFunction Distance function selector. Default: L2 - * Available: L0_25, L0_5, L1, L2, L2SQUARED, L5, L_INFINITY - * @param similarityFunction Similarity function selector. Default: - * HEURISTIC Available: MINUS, GAUSSIAN, HEURISTIC - * @param similarityParameter Parameter of the similarity function. - */ - CV_WRAP static Ptr - create(const int distanceFunction = 3, const int similarityFunction = 2, - const float similarityParameter = 1.0f); +public: + + /** + * @brief Creates the algorithm instance using selected distance function, + * similarity function and similarity function parameter. + * @param distanceFunction Distance function selector. Default: L2 + * Available: L0_25, L0_5, L1, L2, L2SQUARED, L5, L_INFINITY + * @param similarityFunction Similarity function selector. Default: HEURISTIC + * Available: MINUS, GAUSSIAN, HEURISTIC + * @param similarityParameter Parameter of the similarity function. + */ + CV_WRAP static Ptr create( + const int distanceFunction = 3, + const int similarityFunction = 2, + const float similarityParameter = 1.0f); + + /** + * @brief Computes Signature Quadratic Form Distance of two signatures. + * @param _signature0 The first signature. + * @param _signature1 The second signature. + */ + CV_WRAP virtual float computeQuadraticFormDistance( + InputArray _signature0, + InputArray _signature1) const = 0; + + /** + * @brief Computes Signature Quadratic Form Distance between the reference signature + * and each of the other image signatures. + * @param sourceSignature The signature to measure distance of other signatures from. + * @param imageSignatures Vector of signatures to measure distance from the source signature. + * @param distances Output vector of measured distances. + */ + CV_WRAP virtual void computeQuadraticFormDistances( + const Mat& sourceSignature, + const std::vector& imageSignatures, + std::vector& distances) const = 0; - /** - * @brief Computes Signature Quadratic Form Distance of two signatures. - * @param _signature0 The first signature. - * @param _signature1 The second signature. - */ - CV_WRAP virtual float - computeQuadraticFormDistance(InputArray _signature0, - InputArray _signature1) const = 0; - - /** - * @brief Computes Signature Quadratic Form Distance between the reference - * signature and each of the other image signatures. - * @param sourceSignature The signature to measure distance of other - * signatures from. - * @param imageSignatures Vector of signatures to measure distance from the - * source signature. - * @param distances Output vector of measured distances. - */ - CV_WRAP virtual void - computeQuadraticFormDistances(const Mat &sourceSignature, - const std::vector &imageSignatures, - std::vector &distances) const = 0; }; /** - * @brief Elliptic region around an interest point. - */ +* @brief Elliptic region around an interest point. +*/ class CV_EXPORTS Elliptic_KeyPoint : public KeyPoint { - public: +public: Size_ axes; //!< the lengths of the major and minor ellipse axes - float si; //!< the integration scale at which the parameters were estimated - Matx23f transf; //!< the transformation between image space and local patch - //!< space + float si; //!< the integration scale at which the parameters were estimated + Matx23f transf; //!< the transformation between image space and local patch space Elliptic_KeyPoint(); Elliptic_KeyPoint(Point2f pt, float angle, Size axes, float size, float si); virtual ~Elliptic_KeyPoint(); }; /** - * @brief Class implementing the Harris-Laplace feature detector as described in - * @cite Mikolajczyk2004. + * @brief Class implementing the Harris-Laplace feature detector as described in @cite Mikolajczyk2004. */ class CV_EXPORTS_W HarrisLaplaceFeatureDetector : public Feature2D { - public: +public: /** * @brief Creates a new implementation instance. * * @param numOctaves the number of octaves in the scale-space pyramid * @param corn_thresh the threshold for the Harris cornerness measure - * @param DOG_thresh the threshold for the Difference-of-Gaussians scale - * selection + * @param DOG_thresh the threshold for the Difference-of-Gaussians scale selection * @param maxCorners the maximum number of corners to consider * @param num_layers the number of intermediate scales per octave */ - CV_WRAP static Ptr - create(int numOctaves = 6, float corn_thresh = 0.01f, - float DOG_thresh = 0.01f, int maxCorners = 5000, int num_layers = 4); + CV_WRAP static Ptr create( + int numOctaves=6, + float corn_thresh=0.01f, + float DOG_thresh=0.01f, + int maxCorners=5000, + int num_layers=4); }; /** * @brief Class implementing affine adaptation for key points. * - * A @ref FeatureDetector and a @ref DescriptorExtractor are wrapped to augment - * the detected points with their affine invariant elliptic region and to - * compute the feature descriptors on the regions after warping them into - * circles. + * A @ref FeatureDetector and a @ref DescriptorExtractor are wrapped to augment the + * detected points with their affine invariant elliptic region and to compute + * the feature descriptors on the regions after warping them into circles. * * The interface is equivalent to @ref Feature2D, adding operations for - * @ref Elliptic_KeyPoint "Elliptic_KeyPoints" instead of @ref KeyPoint - * "KeyPoints". + * @ref Elliptic_KeyPoint "Elliptic_KeyPoints" instead of @ref KeyPoint "KeyPoints". */ class CV_EXPORTS AffineFeature2D : public Feature2D { - public: +public: /** * @brief Creates an instance wrapping the given keypoint detector and * descriptor extractor. */ - static Ptr - create(Ptr keypoint_detector, - Ptr descriptor_extractor); + static Ptr create( + Ptr keypoint_detector, + Ptr descriptor_extractor); /** * @brief Creates an instance where keypoint detector and descriptor * extractor are identical. */ - static Ptr create(Ptr keypoint_detector) + static Ptr create( + Ptr keypoint_detector) { return create(keypoint_detector, keypoint_detector); } @@ -1011,20 +927,22 @@ class CV_EXPORTS AffineFeature2D : public Feature2D * @brief Detects keypoints in the image using the wrapped detector and * performs affine adaptation to augment them with their elliptic regions. */ - virtual void detect(InputArray image, - CV_OUT std::vector &keypoints, - InputArray mask = noArray()) = 0; + virtual void detect( + InputArray image, + CV_OUT std::vector& keypoints, + InputArray mask=noArray() ) = 0; using Feature2D::detectAndCompute; // overload, don't hide /** * @brief Detects keypoints and computes descriptors for their surrounding * regions, after warping them into circles. */ - virtual void - detectAndCompute(InputArray image, InputArray mask, - CV_OUT std::vector &keypoints, - OutputArray descriptors, - bool useProvidedKeypoints = false) = 0; + virtual void detectAndCompute( + InputArray image, + InputArray mask, + CV_OUT std::vector& keypoints, + OutputArray descriptors, + bool useProvidedKeypoints=false ) = 0; }; /** @@ -1046,11 +964,11 @@ TBMR feature and vice versa). */ class CV_EXPORTS TBMR : public AffineFeature2D { - public: +public: CV_WRAP static Ptr create(int min_area = 60, - float max_area_relative = 0.01, - float scale_factor = 1.25f, - int n_scales = -1); + float max_area_relative = 0.01, + float scale_factor = 1.25f, + int n_scales = -1); CV_WRAP virtual void setMinArea(int minArea) = 0; CV_WRAP virtual int getMinArea() const = 0; @@ -1062,35 +980,32 @@ class CV_EXPORTS TBMR : public AffineFeature2D CV_WRAP virtual int getNScales() const = 0; }; -/** @brief Estimates cornerness for prespecified KeyPoints using the FAST -algorithm +/** @brief Estimates cornerness for prespecified KeyPoints using the FAST algorithm @param image grayscale image where keypoints (corners) are detected. -@param keypoints keypoints which should be tested to fit the FAST criteria. -Keypoints not being detected as corners are removed. -@param threshold threshold on difference between intensity of the central pixel -and pixels of a circle around this pixel. -@param nonmaxSuppression if true, non-maximum suppression is applied to detected -corners (keypoints). +@param keypoints keypoints which should be tested to fit the FAST criteria. Keypoints not being +detected as corners are removed. +@param threshold threshold on difference between intensity of the central pixel and pixels of a +circle around this pixel. +@param nonmaxSuppression if true, non-maximum suppression is applied to detected corners +(keypoints). @param type one of the three neighborhoods as defined in the paper: FastFeatureDetector::TYPE_9_16, FastFeatureDetector::TYPE_7_12, FastFeatureDetector::TYPE_5_8 Detects corners using the FAST algorithm by @cite Rosten06 . */ -CV_EXPORTS void FASTForPointSet(InputArray image, - CV_IN_OUT std::vector &keypoints, - int threshold, bool nonmaxSuppression = true, - cv::FastFeatureDetector::DetectorType type = - FastFeatureDetector::TYPE_9_16); +CV_EXPORTS void FASTForPointSet( InputArray image, CV_IN_OUT std::vector& keypoints, + int threshold, bool nonmaxSuppression=true, cv::FastFeatureDetector::DetectorType type=FastFeatureDetector::TYPE_9_16); + //! @} + //! @addtogroup xfeatures2d_match //! @{ -/** @brief GMS (Grid-based Motion Statistics) feature matching strategy - described in @cite Bian2017gms . +/** @brief GMS (Grid-based Motion Statistics) feature matching strategy described in @cite Bian2017gms . @param size1 Input size of image1. @param size2 Input size of image2. @param keypoints1 Input keypoints of image1. @@ -1101,43 +1016,32 @@ CV_EXPORTS void FASTForPointSet(InputArray image, @param withScale Take scale transformation into account. @param thresholdFactor The higher, the less matches. @note - Since GMS works well when the number of features is large, we recommend - to use the ORB feature and set FastThreshold to 0 to get as many as possible - features quickly. If matching results are not satisfying, please add more - features. (We use 10000 for images with 640 X 480). If your images have big - rotation and scale changes, please set withRotation or withScale to true. + Since GMS works well when the number of features is large, we recommend to use the ORB feature and set FastThreshold to 0 to get as many as possible features quickly. + If matching results are not satisfying, please add more features. (We use 10000 for images with 640 X 480). + If your images have big rotation and scale changes, please set withRotation or withScale to true. */ -CV_EXPORTS_W void matchGMS(const Size &size1, const Size &size2, - const std::vector &keypoints1, - const std::vector &keypoints2, - const std::vector &matches1to2, - CV_OUT std::vector &matchesGMS, - const bool withRotation = false, - const bool withScale = false, - const double thresholdFactor = 6.0); - -/** @brief LOGOS (Local geometric support for high-outlier spatial verification) - feature matching strategy described in @cite Lowry2018LOGOSLG . +CV_EXPORTS_W void matchGMS(const Size& size1, const Size& size2, const std::vector& keypoints1, const std::vector& keypoints2, + const std::vector& matches1to2, CV_OUT std::vector& matchesGMS, const bool withRotation = false, + const bool withScale = false, const double thresholdFactor = 6.0); + +/** @brief LOGOS (Local geometric support for high-outlier spatial verification) feature matching strategy described in @cite Lowry2018LOGOSLG . @param keypoints1 Input keypoints of image1. @param keypoints2 Input keypoints of image2. @param nn1 Index to the closest BoW centroid for each descriptors of image1. @param nn2 Index to the closest BoW centroid for each descriptors of image2. @param matches1to2 Matches returned by the LOGOS matching strategy. @note - This matching strategy is suitable for features matching against large - scale database. First step consists in constructing the bag-of-words (BoW) - from a representative image database. Image descriptors are then represented - by their closest codevector (nearest BoW centroid). + This matching strategy is suitable for features matching against large scale database. + First step consists in constructing the bag-of-words (BoW) from a representative image database. + Image descriptors are then represented by their closest codevector (nearest BoW centroid). */ -CV_EXPORTS_W void matchLOGOS(const std::vector &keypoints1, - const std::vector &keypoints2, - const std::vector &nn1, - const std::vector &nn2, - std::vector &matches1to2); +CV_EXPORTS_W void matchLOGOS(const std::vector& keypoints1, const std::vector& keypoints2, + const std::vector& nn1, const std::vector& nn2, + std::vector& matches1to2); //! @} -} // namespace xfeatures2d -} // namespace cv +} +} #endif From 0c96156552cf80f967583c529b3f46f4be410e02 Mon Sep 17 00:00:00 2001 From: "steffen.ehrle" Date: Mon, 16 Nov 2020 12:24:04 +0100 Subject: [PATCH 19/22] remove unrelated indentation changes in tests --- modules/xfeatures2d/test/test_features2d.cpp | 304 ++++++++----------- modules/xfeatures2d/test/test_keypoints.cpp | 79 ++--- 2 files changed, 158 insertions(+), 225 deletions(-) diff --git a/modules/xfeatures2d/test/test_features2d.cpp b/modules/xfeatures2d/test/test_features2d.cpp index 454f9210baf..e79e50370bb 100644 --- a/modules/xfeatures2d/test/test_features2d.cpp +++ b/modules/xfeatures2d/test/test_features2d.cpp @@ -2,8 +2,7 @@ // // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. // -// By downloading, copying, installing or using the software you agree to this -license. +// By downloading, copying, installing or using the software you agree to this license. // If you do not agree to this license, do not download, install, // copy or use the software. // @@ -14,29 +13,23 @@ license. // Copyright (C) 2000, Intel Corporation, all rights reserved. // Third party copyrights are property of their respective owners. // -// Redistribution and use in source and binary forms, with or without -modification, +// Redistribution and use in source and binary forms, with or without modification, // are permitted provided that the following conditions are met: // // * Redistribution's of source code must retain the above copyright notice, // this list of conditions and the following disclaimer. // -// * Redistribution's in binary form must reproduce the above copyright -notice, +// * Redistribution's in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation // and/or other materials provided with the distribution. // -// * The name of Intel Corporation may not be used to endorse or promote -products +// * The name of Intel Corporation may not be used to endorse or promote products // derived from this software without specific prior written permission. // -// This software is provided by the copyright holders and contributors "as is" -and +// This software is provided by the copyright holders and contributors "as is" and // any express or implied warranties, including, but not limited to, the implied -// warranties of merchantability and fitness for a particular purpose are -disclaimed. -// In no event shall the Intel Corporation or contributors be liable for any -direct, +// warranties of merchantability and fitness for a particular purpose are disclaimed. +// In no event shall the Intel Corporation or contributors be liable for any direct, // indirect, incidental, special, exemplary, or consequential damages // (including, but not limited to, procurement of substitute goods or services; // loss of use, data, or profits; or business interruption) however caused @@ -48,59 +41,47 @@ direct, #include "test_precomp.hpp" -namespace opencv_test -{ -namespace -{ +namespace opencv_test { namespace { const string FEATURES2D_DIR = "features2d"; const string DETECTOR_DIR = FEATURES2D_DIR + "/feature_detectors"; const string DESCRIPTOR_DIR = FEATURES2D_DIR + "/descriptor_extractors"; const string IMAGE_FILENAME = "tsukuba.png"; -} // namespace -} // namespace opencv_test +}} // namespace -#include "features2d/test/test_descriptors_regression.impl.hpp" #include "features2d/test/test_detectors_regression.impl.hpp" +#include "features2d/test/test_descriptors_regression.impl.hpp" -namespace opencv_test -{ -namespace -{ +namespace opencv_test { namespace { #ifdef OPENCV_ENABLE_NONFREE -TEST(Features2d_Detector_SURF, regression) +TEST( Features2d_Detector_SURF, regression ) { - CV_FeatureDetectorTest test("detector-surf", SURF::create()); + CV_FeatureDetectorTest test( "detector-surf", SURF::create() ); test.safe_run(); } #endif -TEST(Features2d_Detector_STAR, regression) +TEST( Features2d_Detector_STAR, regression ) { - CV_FeatureDetectorTest test("detector-star", StarDetector::create()); + CV_FeatureDetectorTest test( "detector-star", StarDetector::create() ); test.safe_run(); } -TEST(Features2d_Detector_Harris_Laplace, regression) +TEST( Features2d_Detector_Harris_Laplace, regression ) { - CV_FeatureDetectorTest test("detector-harris-laplace", - HarrisLaplaceFeatureDetector::create()); + CV_FeatureDetectorTest test( "detector-harris-laplace", HarrisLaplaceFeatureDetector::create() ); test.safe_run(); } -TEST(Features2d_Detector_Harris_Laplace_Affine_Keypoint_Invariance, regression) +TEST( Features2d_Detector_Harris_Laplace_Affine_Keypoint_Invariance, regression ) { - CV_FeatureDetectorTest test( - "detector-harris-laplace", - AffineFeature2D::create(HarrisLaplaceFeatureDetector::create())); + CV_FeatureDetectorTest test( "detector-harris-laplace", AffineFeature2D::create(HarrisLaplaceFeatureDetector::create())); test.safe_run(); } -TEST(Features2d_Detector_Harris_Laplace_Affine, regression) +TEST( Features2d_Detector_Harris_Laplace_Affine, regression ) { - CV_FeatureDetectorTest test( - "detector-harris-laplace-affine", - AffineFeature2D::create(HarrisLaplaceFeatureDetector::create())); + CV_FeatureDetectorTest test( "detector-harris-laplace-affine", AffineFeature2D::create(HarrisLaplaceFeatureDetector::create())); test.safe_run(); } @@ -115,15 +96,15 @@ TEST(Features2d_Detector_TBMR_Affine, regression) */ #ifdef OPENCV_ENABLE_NONFREE -TEST(Features2d_DescriptorExtractor_SURF, regression) +TEST( Features2d_DescriptorExtractor_SURF, regression ) { #ifdef HAVE_OPENCL bool useOCL = cv::ocl::useOpenCL(); cv::ocl::setUseOpenCL(false); #endif - CV_DescriptorExtractorTest> test("descriptor-surf", 0.05f, - SURF::create()); + CV_DescriptorExtractorTest > test( "descriptor-surf", 0.05f, + SURF::create() ); test.safe_run(); #ifdef HAVE_OPENCL @@ -132,14 +113,14 @@ TEST(Features2d_DescriptorExtractor_SURF, regression) } #ifdef HAVE_OPENCL -TEST(Features2d_DescriptorExtractor_SURF_OCL, regression) +TEST( Features2d_DescriptorExtractor_SURF_OCL, regression ) { bool useOCL = cv::ocl::useOpenCL(); cv::ocl::setUseOpenCL(true); - if (cv::ocl::useOpenCL()) + if(cv::ocl::useOpenCL()) { - CV_DescriptorExtractorTest> test("descriptor-surf_ocl", 0.05f, - SURF::create()); + CV_DescriptorExtractorTest > test( "descriptor-surf_ocl", 0.05f, + SURF::create() ); test.safe_run(); } cv::ocl::setUseOpenCL(useOCL); @@ -147,36 +128,34 @@ TEST(Features2d_DescriptorExtractor_SURF_OCL, regression) #endif #endif // NONFREE -TEST(Features2d_DescriptorExtractor_DAISY, regression) +TEST( Features2d_DescriptorExtractor_DAISY, regression ) { - CV_DescriptorExtractorTest> test("descriptor-daisy", 0.05f, - DAISY::create()); + CV_DescriptorExtractorTest > test( "descriptor-daisy", 0.05f, + DAISY::create() ); test.safe_run(); } -TEST(Features2d_DescriptorExtractor_FREAK, regression) +TEST( Features2d_DescriptorExtractor_FREAK, regression ) { - CV_DescriptorExtractorTest test( - "descriptor-freak", - (CV_DescriptorExtractorTest::DistanceType)12.f, - FREAK::create()); + CV_DescriptorExtractorTest test("descriptor-freak", (CV_DescriptorExtractorTest::DistanceType)12.f, + FREAK::create()); test.safe_run(); } -TEST(Features2d_DescriptorExtractor_BRIEF, regression) +TEST( Features2d_DescriptorExtractor_BRIEF, regression ) { - CV_DescriptorExtractorTest test( - "descriptor-brief", 1, BriefDescriptorExtractor::create()); + CV_DescriptorExtractorTest test( "descriptor-brief", 1, + BriefDescriptorExtractor::create() ); test.safe_run(); } -template struct LUCIDEqualityDistance +template +struct LUCIDEqualityDistance { typedef unsigned char ValueType; typedef int ResultType; - ResultType operator()(const unsigned char *a, const unsigned char *b, - int size) const + ResultType operator()( const unsigned char* a, const unsigned char* b, int size ) const { int res = 0; for (int i = 0; i < size; i++) @@ -190,89 +169,86 @@ template struct LUCIDEqualityDistance } }; -TEST(Features2d_DescriptorExtractor_LUCID, regression) +TEST( Features2d_DescriptorExtractor_LUCID, regression ) { - CV_DescriptorExtractorTest< - LUCIDEqualityDistance<1 /*used blur is not bit-exact*/>> - test("descriptor-lucid", 2, LUCID::create(1, 2)); + CV_DescriptorExtractorTest< LUCIDEqualityDistance<1/*used blur is not bit-exact*/> > test( + "descriptor-lucid", 2, + LUCID::create(1, 2) + ); test.safe_run(); } -TEST(Features2d_DescriptorExtractor_LATCH, regression) +TEST( Features2d_DescriptorExtractor_LATCH, regression ) { - CV_DescriptorExtractorTest test("descriptor-latch", 1, - LATCH::create(32, true, 3, 0)); + CV_DescriptorExtractorTest test( "descriptor-latch", 1, + LATCH::create(32, true, 3, 0) ); test.safe_run(); } -TEST(Features2d_DescriptorExtractor_VGG, regression) +TEST( Features2d_DescriptorExtractor_VGG, regression ) { - CV_DescriptorExtractorTest> test("descriptor-vgg", 0.03f, - VGG::create()); + CV_DescriptorExtractorTest > test( "descriptor-vgg", 0.03f, + VGG::create() ); test.safe_run(); } -TEST(Features2d_DescriptorExtractor_BGM, regression) +TEST( Features2d_DescriptorExtractor_BGM, regression ) { - CV_DescriptorExtractorTest test( - "descriptor-boostdesc-bgm", - (CV_DescriptorExtractorTest::DistanceType)12.f, - BoostDesc::create(BoostDesc::BGM)); + CV_DescriptorExtractorTest test( "descriptor-boostdesc-bgm", + (CV_DescriptorExtractorTest::DistanceType)12.f, + BoostDesc::create(BoostDesc::BGM) ); test.safe_run(); } -TEST(Features2d_DescriptorExtractor_BGM_HARD, regression) +TEST( Features2d_DescriptorExtractor_BGM_HARD, regression ) { - CV_DescriptorExtractorTest test( - "descriptor-boostdesc-bgm_hard", - (CV_DescriptorExtractorTest::DistanceType)12.f, - BoostDesc::create(BoostDesc::BGM_HARD)); + CV_DescriptorExtractorTest test( "descriptor-boostdesc-bgm_hard", + (CV_DescriptorExtractorTest::DistanceType)12.f, + BoostDesc::create(BoostDesc::BGM_HARD) ); test.safe_run(); } -TEST(Features2d_DescriptorExtractor_BGM_BILINEAR, regression) +TEST( Features2d_DescriptorExtractor_BGM_BILINEAR, regression ) { - CV_DescriptorExtractorTest test( - "descriptor-boostdesc-bgm_bilinear", - (CV_DescriptorExtractorTest::DistanceType)15.f, - BoostDesc::create(BoostDesc::BGM_BILINEAR)); + CV_DescriptorExtractorTest test( "descriptor-boostdesc-bgm_bilinear", + (CV_DescriptorExtractorTest::DistanceType)15.f, + BoostDesc::create(BoostDesc::BGM_BILINEAR) ); test.safe_run(); } -TEST(Features2d_DescriptorExtractor_LBGM, regression) +TEST( Features2d_DescriptorExtractor_LBGM, regression ) { - CV_DescriptorExtractorTest> test( - "descriptor-boostdesc-lbgm", 1.0f, BoostDesc::create(BoostDesc::LBGM)); + CV_DescriptorExtractorTest > test( "descriptor-boostdesc-lbgm", + 1.0f, + BoostDesc::create(BoostDesc::LBGM) ); test.safe_run(); } -TEST(Features2d_DescriptorExtractor_BINBOOST_64, regression) +TEST( Features2d_DescriptorExtractor_BINBOOST_64, regression ) { - CV_DescriptorExtractorTest test( - "descriptor-boostdesc-binboost_64", - (CV_DescriptorExtractorTest::DistanceType)12.f, - BoostDesc::create(BoostDesc::BINBOOST_64)); + CV_DescriptorExtractorTest test( "descriptor-boostdesc-binboost_64", + (CV_DescriptorExtractorTest::DistanceType)12.f, + BoostDesc::create(BoostDesc::BINBOOST_64) ); test.safe_run(); } -TEST(Features2d_DescriptorExtractor_BINBOOST_128, regression) +TEST( Features2d_DescriptorExtractor_BINBOOST_128, regression ) { - CV_DescriptorExtractorTest test( - "descriptor-boostdesc-binboost_128", - (CV_DescriptorExtractorTest::DistanceType)12.f, - BoostDesc::create(BoostDesc::BINBOOST_128)); + CV_DescriptorExtractorTest test( "descriptor-boostdesc-binboost_128", + (CV_DescriptorExtractorTest::DistanceType)12.f, + BoostDesc::create(BoostDesc::BINBOOST_128) ); test.safe_run(); } -TEST(Features2d_DescriptorExtractor_BINBOOST_256, regression) +TEST( Features2d_DescriptorExtractor_BINBOOST_256, regression ) { - CV_DescriptorExtractorTest test( - "descriptor-boostdesc-binboost_256", - (CV_DescriptorExtractorTest::DistanceType)12.f, - BoostDesc::create(BoostDesc::BINBOOST_256)); + CV_DescriptorExtractorTest test( "descriptor-boostdesc-binboost_256", + (CV_DescriptorExtractorTest::DistanceType)12.f, + BoostDesc::create(BoostDesc::BINBOOST_256) ); test.safe_run(); } + #ifdef OPENCV_ENABLE_NONFREE TEST(Features2d_BruteForceDescriptorMatcher_knnMatch, regression) { @@ -283,46 +259,42 @@ TEST(Features2d_BruteForceDescriptorMatcher_knnMatch, regression) ASSERT_TRUE(ext); Ptr det = SURF::create(); - //"%YAML:1.0\nhessianThreshold: 8000.\noctaves: 3\noctaveLayers: 4\nupright: - // 0\n" + //"%YAML:1.0\nhessianThreshold: 8000.\noctaves: 3\noctaveLayers: 4\nupright: 0\n" ASSERT_TRUE(det); Ptr matcher = DescriptorMatcher::create("BruteForce"); ASSERT_TRUE(matcher); Mat imgT(256, 256, CV_8U, Scalar(255)); - line(imgT, Point(20, sz / 2), Point(sz - 21, sz / 2), Scalar(100), 2); - line(imgT, Point(sz / 2, 20), Point(sz / 2, sz - 21), Scalar(100), 2); + line(imgT, Point(20, sz/2), Point(sz-21, sz/2), Scalar(100), 2); + line(imgT, Point(sz/2, 20), Point(sz/2, sz-21), Scalar(100), 2); vector kpT; - kpT.push_back(KeyPoint(50, 50, 16, 0, 20000, 1, -1)); - kpT.push_back(KeyPoint(42, 42, 16, 160, 10000, 1, -1)); + kpT.push_back( KeyPoint(50, 50, 16, 0, 20000, 1, -1) ); + kpT.push_back( KeyPoint(42, 42, 16, 160, 10000, 1, -1) ); Mat descT; ext->compute(imgT, kpT, descT); Mat imgQ(256, 256, CV_8U, Scalar(255)); - line(imgQ, Point(30, sz / 2), Point(sz - 31, sz / 2), Scalar(100), 3); - line(imgQ, Point(sz / 2, 30), Point(sz / 2, sz - 31), Scalar(100), 3); + line(imgQ, Point(30, sz/2), Point(sz-31, sz/2), Scalar(100), 3); + line(imgQ, Point(sz/2, 30), Point(sz/2, sz-31), Scalar(100), 3); vector kpQ; det->detect(imgQ, kpQ); Mat descQ; ext->compute(imgQ, kpQ, descQ); - vector> matches; + vector > matches; matcher->knnMatch(descQ, descT, matches, k); - // cout << "\nBest " << k << " matches to " << descT.rows << " train - // desc-s." << endl; + //cout << "\nBest " << k << " matches to " << descT.rows << " train desc-s." << endl; ASSERT_EQ(descQ.rows, static_cast(matches.size())); - for (size_t i = 0; i < matches.size(); i++) + for(size_t i = 0; i(matches[i].size())); - for (size_t j = 0; j < matches[i].size(); j++) + for(size_t j = 0; j " << - // matches[i][j].trainIdx << endl; + //cout << "\t" << matches[i][j].queryIdx << " -> " << matches[i][j].trainIdx << endl; ASSERT_EQ(matches[i][j].queryIdx, static_cast(i)); } } @@ -331,29 +303,24 @@ TEST(Features2d_BruteForceDescriptorMatcher_knnMatch, regression) class CV_DetectPlanarTest : public cvtest::BaseTest { - public: - CV_DetectPlanarTest(const string &_fname, int _min_ninliers, - const Ptr &_f2d) - : fname(_fname), min_ninliers(_min_ninliers), f2d(_f2d) - { - } +public: + CV_DetectPlanarTest(const string& _fname, int _min_ninliers, const Ptr& _f2d) + : fname(_fname), min_ninliers(_min_ninliers), f2d(_f2d) {} - protected: +protected: void run(int) { - if (f2d.empty()) + if(f2d.empty()) return; - string path = string(ts->get_data_path()) + - "detectors_descriptors_evaluation/planar/"; + string path = string(ts->get_data_path()) + "detectors_descriptors_evaluation/planar/"; string imgname1 = path + "box.png"; string imgname2 = path + "box_in_scene.png"; Mat img1 = imread(imgname1, 0); Mat img2 = imread(imgname2, 0); - if (img1.empty() || img2.empty()) + if( img1.empty() || img2.empty() ) { - ts->printf(cvtest::TS::LOG, "missing %s and/or %s\n", - imgname1.c_str(), imgname2.c_str()); - ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA); + ts->printf( cvtest::TS::LOG, "missing %s and/or %s\n", imgname1.c_str(), imgname2.c_str()); + ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA ); return; } vector kpt1, kpt2; @@ -372,17 +339,16 @@ class CV_DetectPlanarTest : public cvtest::BaseTest f2d->detectAndCompute(img1, Mat(), kpt1, d1); f2d->detectAndCompute(img1, Mat(), kpt2, d2); } - for (size_t i = 0; i < kpt1.size(); i++) - CV_Assert(kpt1[i].response > 0); - for (size_t i = 0; i < kpt2.size(); i++) - CV_Assert(kpt2[i].response > 0); + for( size_t i = 0; i < kpt1.size(); i++ ) + CV_Assert(kpt1[i].response > 0 ); + for( size_t i = 0; i < kpt2.size(); i++ ) + CV_Assert(kpt2[i].response > 0 ); vector matches; BFMatcher(f2d->defaultNorm(), true).match(d1, d2, matches); vector pt1, pt2; - for (size_t i = 0; i < matches.size(); i++) - { + for( size_t i = 0; i < matches.size(); i++ ) { pt1.push_back(kpt1[matches[i].queryIdx].pt); pt2.push_back(kpt2[matches[i].trainIdx].pt); } @@ -390,12 +356,10 @@ class CV_DetectPlanarTest : public cvtest::BaseTest Mat inliers, H = findHomography(pt1, pt2, RANSAC, 10, inliers); int ninliers = countNonZero(inliers); - if (ninliers < min_ninliers) + if( ninliers < min_ninliers ) { - ts->printf(cvtest::TS::LOG, - "too little inliers (%d) vs expected %d\n", ninliers, - min_ninliers); - ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA); + ts->printf( cvtest::TS::LOG, "too little inliers (%d) vs expected %d\n", ninliers, min_ninliers); + ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA ); return; } } @@ -405,43 +369,34 @@ class CV_DetectPlanarTest : public cvtest::BaseTest Ptr f2d; }; -TEST(Features2d_SIFTHomographyTest, regression) -{ - CV_DetectPlanarTest test("SIFT", 80, SIFT::create()); - test.safe_run(); -} +TEST(Features2d_SIFTHomographyTest, regression) { CV_DetectPlanarTest test("SIFT", 80, SIFT::create()); test.safe_run(); } #ifdef OPENCV_ENABLE_NONFREE -TEST(Features2d_SURFHomographyTest, regression) -{ - CV_DetectPlanarTest test("SURF", 80, SURF::create()); - test.safe_run(); -} +TEST(Features2d_SURFHomographyTest, regression) { CV_DetectPlanarTest test("SURF", 80, SURF::create()); test.safe_run(); } #endif class FeatureDetectorUsingMaskTest : public cvtest::BaseTest { - public: - FeatureDetectorUsingMaskTest(const Ptr &featureDetector) - : featureDetector_(featureDetector) +public: + FeatureDetectorUsingMaskTest(const Ptr& featureDetector) : + featureDetector_(featureDetector) { CV_Assert(featureDetector_); } - protected: +protected: + void run(int) { const int nStepX = 2; const int nStepY = 2; - const string imageFilename = - string(ts->get_data_path()) + "/features2d/tsukuba.png"; + const string imageFilename = string(ts->get_data_path()) + "/features2d/tsukuba.png"; Mat image = imread(imageFilename); - if (image.empty()) + if(image.empty()) { - ts->printf(cvtest::TS::LOG, "Image %s can not be read.\n", - imageFilename.c_str()); + ts->printf(cvtest::TS::LOG, "Image %s can not be read.\n", imageFilename.c_str()); ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA); return; } @@ -453,8 +408,8 @@ class FeatureDetectorUsingMaskTest : public cvtest::BaseTest vector keyPoints; vector points; - for (int i = 0; i < nStepX; ++i) - for (int j = 0; j < nStepY; ++j) + for(int i=0; idetect(image, keyPoints, mask); KeyPoint::convert(keyPoints, points); - for (size_t k = 0; k < points.size(); ++k) + for(size_t k=0; kprintf(cvtest::TS::LOG, - "The feature point is outside of the mask."); - ts->set_failed_test_info( - cvtest::TS::FAIL_INVALID_OUTPUT); + ts->printf(cvtest::TS::LOG, "The feature point is outside of the mask."); + ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_OUTPUT); return; } } } - ts->set_failed_test_info(cvtest::TS::OK); + ts->set_failed_test_info( cvtest::TS::OK ); } Ptr featureDetector_; @@ -497,5 +450,4 @@ TEST(DISABLED_Features2d_SURF_using_mask, regression) } #endif // NONFREE -} // namespace -} // namespace opencv_test +}} // namespace diff --git a/modules/xfeatures2d/test/test_keypoints.cpp b/modules/xfeatures2d/test/test_keypoints.cpp index 0f8b38e98b2..28d125d0836 100644 --- a/modules/xfeatures2d/test/test_keypoints.cpp +++ b/modules/xfeatures2d/test/test_keypoints.cpp @@ -2,8 +2,7 @@ // // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. // -// By downloading, copying, installing or using the software you agree to this -license. +// By downloading, copying, installing or using the software you agree to this license. // If you do not agree to this license, do not download, install, // copy or use the software. // @@ -14,29 +13,23 @@ license. // Copyright (C) 2000, Intel Corporation, all rights reserved. // Third party copyrights are property of their respective owners. // -// Redistribution and use in source and binary forms, with or without -modification, +// Redistribution and use in source and binary forms, with or without modification, // are permitted provided that the following conditions are met: // // * Redistribution's of source code must retain the above copyright notice, // this list of conditions and the following disclaimer. // -// * Redistribution's in binary form must reproduce the above copyright -notice, +// * Redistribution's in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation // and/or other materials provided with the distribution. // -// * The name of Intel Corporation may not be used to endorse or promote -products +// * The name of Intel Corporation may not be used to endorse or promote products // derived from this software without specific prior written permission. // -// This software is provided by the copyright holders and contributors "as is" -and +// This software is provided by the copyright holders and contributors "as is" and // any express or implied warranties, including, but not limited to, the implied -// warranties of merchantability and fitness for a particular purpose are -disclaimed. -// In no event shall the Intel Corporation or contributors be liable for any -direct, +// warranties of merchantability and fitness for a particular purpose are disclaimed. +// In no event shall the Intel Corporation or contributors be liable for any direct, // indirect, incidental, special, exemplary, or consequential damages // (including, but not limited to, procurement of substitute goods or services; // loss of use, data, or profits; or business interruption) however caused @@ -48,39 +41,32 @@ direct, #include "test_precomp.hpp" -namespace opencv_test -{ -namespace -{ +namespace opencv_test { namespace { const string FEATURES2D_DIR = "features2d"; const string IMAGE_FILENAME = "tsukuba.png"; /****************************************************************************************\ -* Test for KeyPoint * +* Test for KeyPoint * \****************************************************************************************/ class CV_FeatureDetectorKeypointsTest : public cvtest::BaseTest { - public: - explicit CV_FeatureDetectorKeypointsTest(const Ptr &_detector) - : detector(_detector) - { - } +public: + explicit CV_FeatureDetectorKeypointsTest(const Ptr& _detector) : + detector(_detector) {} - protected: +protected: virtual void run(int) { CV_Assert(detector); - string imgFilename = - string(ts->get_data_path()) + FEATURES2D_DIR + "/" + IMAGE_FILENAME; + string imgFilename = string(ts->get_data_path()) + FEATURES2D_DIR + "/" + IMAGE_FILENAME; // Read the test image. Mat image = imread(imgFilename); - if (image.empty()) + if(image.empty()) { - ts->printf(cvtest::TS::LOG, "Image %s can not be read.\n", - imgFilename.c_str()); + ts->printf(cvtest::TS::LOG, "Image %s can not be read.\n", imgFilename.c_str()); ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA); return; } @@ -88,42 +74,35 @@ class CV_FeatureDetectorKeypointsTest : public cvtest::BaseTest vector keypoints; detector->detect(image, keypoints); - if (keypoints.empty()) + if(keypoints.empty()) { - ts->printf(cvtest::TS::LOG, - "Detector can't find keypoints in image.\n"); + ts->printf(cvtest::TS::LOG, "Detector can't find keypoints in image.\n"); ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_OUTPUT); return; } Rect r(0, 0, image.cols, image.rows); - for (size_t i = 0; i < keypoints.size(); i++) + for(size_t i = 0; i < keypoints.size(); i++) { - const KeyPoint &kp = keypoints[i]; + const KeyPoint& kp = keypoints[i]; - if (!r.contains(kp.pt)) + if(!r.contains(kp.pt)) { - ts->printf(cvtest::TS::LOG, - "KeyPoint::pt is out of image (x=%f, y=%f).\n", - kp.pt.x, kp.pt.y); + ts->printf(cvtest::TS::LOG, "KeyPoint::pt is out of image (x=%f, y=%f).\n", kp.pt.x, kp.pt.y); ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_OUTPUT); return; } - if (kp.size <= 0.f) + if(kp.size <= 0.f) { - ts->printf(cvtest::TS::LOG, - "KeyPoint::size is not positive (%f).\n", kp.size); + ts->printf(cvtest::TS::LOG, "KeyPoint::size is not positive (%f).\n", kp.size); ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_OUTPUT); return; } - if ((kp.angle < 0.f && kp.angle != -1.f) || kp.angle >= 360.f) + if((kp.angle < 0.f && kp.angle != -1.f) || kp.angle >= 360.f) { - ts->printf( - cvtest::TS::LOG, - "KeyPoint::angle is out of range [0, 360). It's %f.\n", - kp.angle); + ts->printf(cvtest::TS::LOG, "KeyPoint::angle is out of range [0, 360). It's %f.\n", kp.angle); ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_OUTPUT); return; } @@ -134,6 +113,7 @@ class CV_FeatureDetectorKeypointsTest : public cvtest::BaseTest Ptr detector; }; + // Registration of tests #ifdef OPENCV_ENABLE_NONFREE TEST(Features2d_Detector_Keypoints_SURF, validation) @@ -143,12 +123,14 @@ TEST(Features2d_Detector_Keypoints_SURF, validation) } #endif // NONFREE + TEST(Features2d_Detector_Keypoints_Star, validation) { CV_FeatureDetectorKeypointsTest test(xfeatures2d::StarDetector::create()); test.safe_run(); } + TEST(Features2d_Detector_Keypoints_MSDDetector, validation) { CV_FeatureDetectorKeypointsTest test(xfeatures2d::MSDDetector::create()); @@ -161,5 +143,4 @@ TEST(Features2d_Detector_Keypoints_TBMRDetector, validation) test.safe_run(); } -} // namespace -} // namespace opencv_test +}} // namespace From 117abe42115b3edd2c106b9d8c647819f4d23091 Mon Sep 17 00:00:00 2001 From: "steffen.ehrle" Date: Mon, 16 Nov 2020 13:50:33 +0100 Subject: [PATCH 20/22] externalize msd_pyramid add export wrappers --- .../include/opencv2/xfeatures2d.hpp | 4 +- modules/xfeatures2d/src/msd.cpp | 66 +--------------- modules/xfeatures2d/src/msd_pyramid.hpp | 75 +++++++++++++++++++ modules/xfeatures2d/src/tbmr.cpp | 69 +---------------- 4 files changed, 80 insertions(+), 134 deletions(-) create mode 100644 modules/xfeatures2d/src/msd_pyramid.hpp diff --git a/modules/xfeatures2d/include/opencv2/xfeatures2d.hpp b/modules/xfeatures2d/include/opencv2/xfeatures2d.hpp index 495290bcbc2..fb6338b620b 100644 --- a/modules/xfeatures2d/include/opencv2/xfeatures2d.hpp +++ b/modules/xfeatures2d/include/opencv2/xfeatures2d.hpp @@ -901,7 +901,7 @@ class CV_EXPORTS_W HarrisLaplaceFeatureDetector : public Feature2D * The interface is equivalent to @ref Feature2D, adding operations for * @ref Elliptic_KeyPoint "Elliptic_KeyPoints" instead of @ref KeyPoint "KeyPoints". */ -class CV_EXPORTS AffineFeature2D : public Feature2D +class CV_EXPORTS_W AffineFeature2D : public Feature2D { public: /** @@ -962,7 +962,7 @@ Features are ellipses (similar to MSER, however a MSER feature can never be a TBMR feature and vice versa). */ -class CV_EXPORTS TBMR : public AffineFeature2D +class CV_EXPORTS_W TBMR : public AffineFeature2D { public: CV_WRAP static Ptr create(int min_area = 60, diff --git a/modules/xfeatures2d/src/msd.cpp b/modules/xfeatures2d/src/msd.cpp index 637d992cbc9..9fe5ae8cf0a 100644 --- a/modules/xfeatures2d/src/msd.cpp +++ b/modules/xfeatures2d/src/msd.cpp @@ -55,78 +55,14 @@ University of Bologna, Open Perception */ #include "precomp.hpp" +#include "msd_pyramid.hpp" #include namespace cv { namespace xfeatures2d { - /*! - MSD Image Pyramid. - */ - class MSDImagePyramid - { - // Multi-threaded construction of the scale-space pyramid - struct MSDImagePyramidBuilder : ParallelLoopBody - { - - MSDImagePyramidBuilder(const cv::Mat& _im, std::vector* _m_imPyr, float _scaleFactor) - { - im = &_im; - m_imPyr = _m_imPyr; - scaleFactor = _scaleFactor; - - } - - void operator()(const Range& range) const CV_OVERRIDE - { - for (int lvl = range.start; lvl < range.end; lvl++) - { - float scale = 1 / std::pow(scaleFactor, (float) lvl); - (*m_imPyr)[lvl] = cv::Mat(cv::Size(cvRound(im->cols * scale), cvRound(im->rows * scale)), im->type()); - cv::resize(*im, (*m_imPyr)[lvl], cv::Size((*m_imPyr)[lvl].cols, (*m_imPyr)[lvl].rows), 0.0, 0.0, cv::INTER_AREA); - } - } - const cv::Mat* im; - std::vector* m_imPyr; - float scaleFactor; - }; - - public: - MSDImagePyramid(const cv::Mat &im, const int nLevels, const float scaleFactor = 1.6f); - ~MSDImagePyramid(); - - const std::vector getImPyr() const - { - return m_imPyr; - }; - - private: - - std::vector m_imPyr; - int m_nLevels; - float m_scaleFactor; - }; - - MSDImagePyramid::MSDImagePyramid(const cv::Mat & im, const int nLevels, const float scaleFactor) - { - m_nLevels = nLevels; - m_scaleFactor = scaleFactor; - m_imPyr.clear(); - m_imPyr.resize(nLevels); - - m_imPyr[0] = im.clone(); - - if (m_nLevels > 1) - { - parallel_for_(Range(1, nLevels), MSDImagePyramidBuilder(im, &m_imPyr, scaleFactor)); - } - } - - MSDImagePyramid::~MSDImagePyramid() - { - } /*! MSD Implementation. diff --git a/modules/xfeatures2d/src/msd_pyramid.hpp b/modules/xfeatures2d/src/msd_pyramid.hpp new file mode 100644 index 00000000000..b5a20a4b748 --- /dev/null +++ b/modules/xfeatures2d/src/msd_pyramid.hpp @@ -0,0 +1,75 @@ +///////////// see LICENSE.txt in the OpenCV root directory ////////////// + +#ifndef __OPENCV_XFEATURES2D_MSD_PYRAMID_HPP__ +#define __OPENCV_XFEATURES2D_MSD_PYRAMID_HPP__ + +#include "precomp.hpp" + +namespace cv +{ +namespace xfeatures2d +{ +/*! + MSD Image Pyramid. + */ +class MSDImagePyramid +{ + // Multi-threaded construction of the scale-space pyramid + struct MSDImagePyramidBuilder : ParallelLoopBody + { + + MSDImagePyramidBuilder(const cv::Mat& _im, std::vector* _m_imPyr, float _scaleFactor) + { + im = &_im; + m_imPyr = _m_imPyr; + scaleFactor = _scaleFactor; + + } + + void operator()(const Range& range) const CV_OVERRIDE + { + for (int lvl = range.start; lvl < range.end; lvl++) + { + float scale = 1 / std::pow(scaleFactor, (float) lvl); + (*m_imPyr)[lvl] = cv::Mat(cv::Size(cvRound(im->cols * scale), cvRound(im->rows * scale)), im->type()); + cv::resize(*im, (*m_imPyr)[lvl], cv::Size((*m_imPyr)[lvl].cols, (*m_imPyr)[lvl].rows), 0.0, 0.0, cv::INTER_AREA); + } + } + const cv::Mat* im; + std::vector* m_imPyr; + float scaleFactor; + }; + +public: + + MSDImagePyramid(const cv::Mat &im, const int nLevels, const float scaleFactor = 1.6f) + { + m_nLevels = nLevels; + m_scaleFactor = scaleFactor; + m_imPyr.clear(); + m_imPyr.resize(nLevels); + + m_imPyr[0] = im.clone(); + + if (m_nLevels > 1) + { + parallel_for_(Range(1, nLevels), MSDImagePyramidBuilder(im, &m_imPyr, scaleFactor)); + } + } + ~MSDImagePyramid() {}; + + const std::vector getImPyr() const + { + return m_imPyr; + }; + +private: + + std::vector m_imPyr; + int m_nLevels; + float m_scaleFactor; +}; +} +} + +#endif \ No newline at end of file diff --git a/modules/xfeatures2d/src/tbmr.cpp b/modules/xfeatures2d/src/tbmr.cpp index 2af3fd66362..76b696e537c 100644 --- a/modules/xfeatures2d/src/tbmr.cpp +++ b/modules/xfeatures2d/src/tbmr.cpp @@ -3,77 +3,12 @@ // directory of this distribution and at http://opencv.org/license.html. #include "precomp.hpp" +#include "msd_pyramid.hpp" namespace cv { namespace xfeatures2d { -/*! -MSD Image Pyramid. (from msd.cpp) -*/ -class TBMRImagePyramid -{ - // Multi-threaded construction of the scale-space pyramid - struct TBMRImagePyramidBuilder : ParallelLoopBody - { - TBMRImagePyramidBuilder(const cv::Mat &_im, - std::vector *_m_imPyr, - float _scaleFactor) - { - im = &_im; - m_imPyr = _m_imPyr; - scaleFactor = _scaleFactor; - } - - void operator()(const Range &range) const CV_OVERRIDE - { - for (int lvl = range.start; lvl < range.end; lvl++) - { - float scale = 1 / std::pow(scaleFactor, (float)lvl); - (*m_imPyr)[lvl] = cv::Mat(cv::Size(cvRound(im->cols * scale), - cvRound(im->rows * scale)), - im->type()); - cv::resize(*im, (*m_imPyr)[lvl], - cv::Size((*m_imPyr)[lvl].cols, (*m_imPyr)[lvl].rows), - 0.0, 0.0, cv::INTER_AREA); - } - } - const cv::Mat *im; - std::vector *m_imPyr; - float scaleFactor; - }; - - public: - TBMRImagePyramid(const cv::Mat &im, const int nLevels, - const float scaleFactor = 1.6f); - ~TBMRImagePyramid(); - - const std::vector getImPyr() const { return m_imPyr; }; - - private: - std::vector m_imPyr; - int m_nLevels; - float m_scaleFactor; -}; - -TBMRImagePyramid::TBMRImagePyramid(const cv::Mat &im, const int nLevels, - const float scaleFactor) -{ - m_nLevels = nLevels; - m_scaleFactor = scaleFactor; - m_imPyr.clear(); - m_imPyr.resize(nLevels); - - m_imPyr[0] = im.clone(); - - if (m_nLevels > 1) - { - parallel_for_(Range(1, nLevels), - TBMRImagePyramidBuilder(im, &m_imPyr, scaleFactor)); - } -} - -TBMRImagePyramid::~TBMRImagePyramid() {} class TBMR_Impl CV_FINAL : public TBMR { @@ -576,7 +511,7 @@ void TBMR_Impl::detect(InputArray _image, float *dupl_ptr = dupl.ptr(); std::vector pyr; - TBMRImagePyramid scaleSpacer(src, m_cur_n_scales, m_scale_factor); + MSDImagePyramid scaleSpacer(src, m_cur_n_scales, m_scale_factor); pyr = scaleSpacer.getImPyr(); int oct = 0; From 0245d3ad57ab701ec410940e8d4d17b179e13d70 Mon Sep 17 00:00:00 2001 From: "steffen.ehrle" Date: Mon, 16 Nov 2020 14:08:55 +0100 Subject: [PATCH 21/22] exchange malloc/calloc with AutoBuffer --- modules/xfeatures2d/src/tbmr.cpp | 47 +++++++++++++++++--------------- 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/modules/xfeatures2d/src/tbmr.cpp b/modules/xfeatures2d/src/tbmr.cpp index 76b696e537c..36a0a538b7e 100644 --- a/modules/xfeatures2d/src/tbmr.cpp +++ b/modules/xfeatures2d/src/tbmr.cpp @@ -99,12 +99,17 @@ class TBMR_Impl CV_FINAL : public TBMR }; // {-1,0}, {0,-1}, {0,1}, {1,0} yx std::array offsetsv = { Vec2i(0, -1), Vec2i(-1, 0), Vec2i(1, 0), Vec2i(0, 1) }; // xy - - uint *zpar = (uint *)malloc(imSize * sizeof(uint)); - uint *root = (uint *)malloc(imSize * sizeof(uint)); - uint *rank = (uint *)calloc(imSize, sizeof(uint)); + AutoBuffer zparb(imSize); + AutoBuffer rootb(imSize); + AutoBuffer rankb(imSize); + memset(rankb.data(), 0, imSize * sizeof(uint)); + uint* zpar = zparb.data(); + uint *root = rootb.data(); + uint *rank = rankb.data(); parent = Mat(rs, cs, CV_32S); // unsigned - bool *dejaVu = (bool *)calloc(imSize, sizeof(bool)); + AutoBuffer dejaVub(imSize); + memset(dejaVub.data(), 0, imSize * sizeof(bool)); + bool* dejaVu = dejaVub.data(); const uint *S_ptr = S.ptr(); uint *parent_ptr = parent.ptr(); @@ -179,11 +184,6 @@ class TBMR_Impl CV_FINAL : public TBMR } } } - - free(zpar); - free(root); - free(rank); - free(dejaVu); } void calculateTBMRs(const Mat &image, std::vector &tbmrs, @@ -229,9 +229,13 @@ class TBMR_Impl CV_FINAL : public TBMR // as final TBMRs //-------------------------------------------------------------------------- - uint *numSons = (uint *)calloc(imSize, sizeof(uint)); + AutoBuffer numSonsb(imSize); + memset(numSonsb.data(), 0, imSize * sizeof(uint)); + uint* numSons = numSonsb.data(); uint vecNodesSize = imaAttribute[S_ptr[0]][0]; // area - uint *vecNodes = (uint *)calloc(vecNodesSize, sizeof(uint)); // area + AutoBuffer vecNodesb(vecNodesSize); + memset(vecNodesb.data(), 0, vecNodesSize * sizeof(uint)); + uint *vecNodes = vecNodesb.data(); // area uint numNodes = 0; // leaf to root propagation to select the canonized nodes @@ -246,10 +250,14 @@ class TBMR_Impl CV_FINAL : public TBMR } } - bool *isSeen = (bool *)calloc(imSize, sizeof(bool)); + AutoBuffer isSeenb(imSize); + memset(isSeenb.data(), 0, imSize * sizeof(bool)); + bool *isSeen = isSeenb.data(); // parent of critical leaf node - bool *isParentofLeaf = (bool *)calloc(imSize, sizeof(bool)); + AutoBuffer isParentofLeafb(imSize); + memset(isParentofLeafb.data(), 0, imSize * sizeof(bool)); + bool* isParentofLeaf = isParentofLeafb.data(); for (uint i = 0; i < vecNodesSize; i++) { @@ -259,7 +267,8 @@ class TBMR_Impl CV_FINAL : public TBMR } uint numTbmrs = 0; - uint *vecTbmrs = (uint *)malloc(numNodes * sizeof(uint)); + AutoBuffer vecTbmrsb(numNodes); + uint* vecTbmrs = vecTbmrsb.data(); for (uint i = 0; i < vecNodesSize; i++) { uint p = vecNodes[i]; @@ -439,12 +448,6 @@ class TBMR_Impl CV_FINAL : public TBMR } } } - - free(numSons); - free(vecNodes); - free(isSeen); - free(isParentofLeaf); - free(vecTbmrs); //--------------------------------------------- } @@ -578,4 +581,4 @@ Ptr TBMR::create(int _min_area, float _max_area_relative, float _scale, } } // namespace xfeatures2d -} // namespace cv +} // namespace cv \ No newline at end of file From ef488ea33fe115442d8abceca8688c79541c11f6 Mon Sep 17 00:00:00 2001 From: "steffen.ehrle" Date: Mon, 16 Nov 2020 20:15:22 +0100 Subject: [PATCH 22/22] fix warning fix correct license text --- modules/xfeatures2d/include/opencv2/xfeatures2d.hpp | 2 +- modules/xfeatures2d/src/msd_pyramid.hpp | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/modules/xfeatures2d/include/opencv2/xfeatures2d.hpp b/modules/xfeatures2d/include/opencv2/xfeatures2d.hpp index fb6338b620b..103dbe8e78c 100644 --- a/modules/xfeatures2d/include/opencv2/xfeatures2d.hpp +++ b/modules/xfeatures2d/include/opencv2/xfeatures2d.hpp @@ -966,7 +966,7 @@ class CV_EXPORTS_W TBMR : public AffineFeature2D { public: CV_WRAP static Ptr create(int min_area = 60, - float max_area_relative = 0.01, + float max_area_relative = 0.01f, float scale_factor = 1.25f, int n_scales = -1); diff --git a/modules/xfeatures2d/src/msd_pyramid.hpp b/modules/xfeatures2d/src/msd_pyramid.hpp index b5a20a4b748..9fc3243a320 100644 --- a/modules/xfeatures2d/src/msd_pyramid.hpp +++ b/modules/xfeatures2d/src/msd_pyramid.hpp @@ -1,4 +1,6 @@ -///////////// see LICENSE.txt in the OpenCV root directory ////////////// +// This file is part of OpenCV project. +// It is subject to the license terms in the LICENSE file found in the top-level directory +// of this distribution and at http://opencv.org/license.html. #ifndef __OPENCV_XFEATURES2D_MSD_PYRAMID_HPP__ #define __OPENCV_XFEATURES2D_MSD_PYRAMID_HPP__