Commit b4edf34f authored by Vadim Pisarevsky's avatar Vadim Pisarevsky

Merge pull request #38 from ubisum/line_descriptor_module

Line descriptor module
parents 71c98af0 c28417c8
set(the_description "Line descriptor")
ocv_define_module(line_descriptor opencv_features2d opencv_imgproc opencv_optim)
.. _LSDDetector:
Line Segments Detector
======================
Lines extraction methodology
----------------------------
The lines extraction methodology described in the following is mainly based on [EDLN]_.
The extraction starts with a Gaussian pyramid generated from an original image, downsampled N-1 times, blurred N times, to obtain N layers (one for each octave), with layer 0 corresponding to input image. Then, from each layer (octave) in the pyramid, lines are extracted using LSD algorithm.
Differently from EDLine lines extractor used in original article, LSD furnishes information only about lines extremes; thus, additional information regarding slope and equation of line are computed via analytic methods. The number of pixels is obtained using *LineIterator*. Extracted lines are returned in the form of KeyLine objects, but since extraction is based on a method different from the one used in *BinaryDescriptor* class, data associated to a line's extremes in original image and in octave it was extracted from, coincide. KeyLine's field *class_id* is used as an index to indicate the order of extraction of a line inside a single octave.
LSDDetector::createLSDDetector
------------------------------
Creates ad LSDDetector object, using smart pointers.
.. ocv:function:: Ptr<LSDDetector> LSDDetector::createLSDDetector()
LSDDetector::detect
-------------------
Detect lines inside an image.
.. ocv:function:: void LSDDetector::detect( const Mat& image, std::vector<KeyLine>& keylines, int scale, int numOctaves, const Mat& mask=Mat())
.. ocv:function:: void LSDDetector::detect( const std::vector<Mat>& images, std::vector<std::vector<KeyLine> >& keylines, int scale, int numOctaves, const std::vector<Mat>& masks=std::vector<Mat>() ) const
:param image: input image
:param images: input images
:param keylines: vector or set of vectors that will store extracted lines for one or more images
:param mask: mask matrix to detect only KeyLines of interest
:param masks: vector of mask matrices to detect only KeyLines of interest from each input image
:param scale: scale factor used in pyramids generation
:param numOctaves: number of octaves inside pyramid
References
----------
.. [EDLN] Von Gioi, R. Grompone, et al. *LSD: A fast line segment detector with a false detection control*, IEEE Transactions on Pattern Analysis and Machine Intelligence 32.4 (2010): 722-732.
.. _binary_descriptor:
BinaryDescriptor Class
======================
.. highlight:: cpp
BinaryDescriptor Class implements both functionalities for detection of lines and computation of their binary descriptor. Class' interface is mainly based on the ones of classical detectors and extractors, such as Feature2d's `FeatureDetector <http://docs.opencv.org/modules/features2d/doc/common_interfaces_of_feature_detectors.html?highlight=featuredetector#featuredetector>`_ and `DescriptorExtractor <http://docs.opencv.org/modules/features2d/doc/common_interfaces_of_descriptor_extractors.html?highlight=extractor#DescriptorExtractor : public Algorithm>`_.
Retrieved information about lines is stored in *KeyLine* objects.
BinaryDescriptor::Params
-----------------------------------------------------------------------
.. ocv:struct:: BinaryDescriptor::Params
List of BinaryDescriptor parameters::
struct CV_EXPORTS_W_SIMPLE Params{
CV_WRAP Params();
/* the number of image octaves (default = 1) */
CV_PROP_RW int numOfOctave_;
/* the width of band; (default = 7) */
CV_PROP_RW int widthOfBand_;
/* image's reduction ratio in construction of Gaussian pyramids (default = 2) */
CV_PROP_RW int reductionRatio;
/* read parameters from a FileNode object and store them (struct function) */
void read( const FileNode& fn );
/* store parameters to a FileStorage object (struct function) */
void write( FileStorage& fs ) const;
};
BinaryDescriptor::BinaryDescriptor
----------------------------------
Constructor
.. ocv:function:: bool BinaryDescriptor::BinaryDescriptor( const BinaryDescriptor::Params &parameters = BinaryDescriptor::Params() )
:param parameters: configuration parameters :ocv:struct:`BinaryDescriptor::Params`
If no argument is provided, constructor sets default values (see comments in the code snippet in previous section). Default values are strongly reccomended.
BinaryDescriptor::getNumOfOctaves
---------------------------------
Get current number of octaves
.. ocv:function:: int BinaryDescriptor::getNumOfOctaves()
BinaryDescriptor::setNumOfOctaves
---------------------------------
Set number of octaves
.. ocv:function:: void BinaryDescriptor::setNumOfOctaves( int octaves )
:param octaves: number of octaves
BinaryDescriptor::getWidthOfBand
--------------------------------
Get current width of bands
.. ocv:function:: int BinaryDescriptor::getWidthOfBand()
BinaryDescriptor::setWidthOfBand
--------------------------------
Set width of bands
.. ocv:function:: void BinaryDescriptor::setWidthOfBand( int width )
:param width: width of bands
BinaryDescriptor::getReductionRatio
-----------------------------------
Get current reduction ratio (used in Gaussian pyramids)
.. ocv:function:: int BinaryDescriptor::getReductionRatio()
BinaryDescriptor::setReductionRatio
-----------------------------------
Set reduction ratio (used in Gaussian pyramids)
.. ocv:function:: void BinaryDescriptor::setReductionRatio( int rRatio )
:param rRatio: reduction ratio
BinaryDescriptor::createBinaryDescriptor
----------------------------------------
Create a BinaryDescriptor object with default parameters (or with the ones provided) and return a smart pointer to it
.. ocv:function:: Ptr<BinaryDescriptor> BinaryDescriptor::createBinaryDescriptor()
.. ocv:function:: Ptr<BinaryDescriptor> BinaryDescriptor::createBinaryDescriptor( Params parameters )
BinaryDescriptor::operator()
----------------------------
Define operator '()' to perform detection of KeyLines and computation of descriptors in a row.
.. ocv:function:: void BinaryDescriptor::operator()( InputArray image, InputArray mask, vector<KeyLine>& keylines, OutputArray descriptors, bool useProvidedKeyLines=false, bool returnFloatDescr ) const
:param image: input image
:param mask: mask matrix to select which lines in KeyLines must be accepted among the ones extracted (used when *keylines* is not empty)
:param keylines: vector that contains input lines (when filled, the detection part will be skipped and input lines will be passed as input to the algorithm computing descriptors)
:param descriptors: matrix that will store final descriptors
:param useProvidedKeyLines: flag (when set to true, detection phase will be skipped and only computation of descriptors will be executed, using lines provided in *keylines*)
:param returnFloatDescr: flag (when set to true, original non-binary descriptors are returned)
BinaryDescriptor::read
----------------------
Read parameters from a FileNode object and store them
.. ocv:function:: void BinaryDescriptor::read( const FileNode& fn )
:param fn: source FileNode file
BinaryDescriptor::write
-----------------------
Store parameters to a FileStorage object
.. ocv:function:: void BinaryDescriptor::write( FileStorage& fs ) const
:param fs: output FileStorage file
BinaryDescriptor::defaultNorm
-----------------------------
Return norm mode
.. ocv:function:: int BinaryDescriptor::defaultNorm() const
BinaryDescriptor::descriptorType
--------------------------------
Return data type
.. ocv:function:: int BinaryDescriptor::descriptorType() const
BinaryDescriptor::descriptorSize
--------------------------------
Return descriptor size
.. ocv:function:: int BinaryDescriptor::descriptorSize() const
BinaryDescriptor::detect
------------------------
Requires line detection (for one or more images)
.. ocv:function:: void detect( const Mat& image, vector<KeyLine>& keylines, Mat& mask=Mat() )
.. ocv:function:: void detect( const vector<Mat>& images, vector<vector<KeyLine> >& keylines, vector<Mat>& masks=vector<Mat>() ) const
:param image: input image
:param images: input images
:param keylines: vector or set of vectors that will store extracted lines for one or more images
:param mask: mask matrix to detect only KeyLines of interest
:param masks: vector of mask matrices to detect only KeyLines of interest from each input image
BinaryDescriptor::compute
-------------------------
Requires descriptors computation (for one or more images)
.. ocv:function:: void compute( const Mat& image, vector<KeyLine>& keylines, Mat& descriptors, bool returnFloatDescr ) const
.. ocv:function:: void compute( const vector<Mat>& images, vector<vector<KeyLine> >& keylines, vector<Mat>& descriptors, bool returnFloatDescr ) const
:param image: input image
:param images: input images
:param keylines: vector or set of vectors containing lines for which descriptors must be computed
:param mask: mask to select for which lines, among the ones provided in input, descriptors must be computed
:param masks: set of masks to select for which lines, among the ones provided in input, descriptors must be computed
:param returnFloatDescr: flag (when set to true, original non-binary descriptors are returned)
Related pages
-------------
* :ref:`line_descriptor`
* :ref:`matching`
* :ref:`drawing`
.. _drawing:
Drawing Functions for Keylines and Matches
==========================================
.. highlight:: cpp
drawLineMatches
---------------
Draws the found matches of keylines from two images.
.. ocv:function:: void drawLineMatches( const Mat& img1, const std::vector<KeyLine>& keylines1, const Mat& img2, const std::vector<KeyLine>& keylines2, const std::vector<DMatch>& matches1to2, Mat& outImg, const Scalar& matchColor=Scalar::all(-1), const Scalar& singleLineColor=Scalar::all(-1), const std::vector<char>& matchesMask=std::vector<char>(), int flags=DrawLinesMatchesFlags::DEFAULT )
:param img1: first image
:param keylines1: keylines extracted from first image
:param img2: second image
:param keylines2: keylines extracted from second image
:param matches1to2: vector of matches
:param outImg: output matrix to draw on
:param matchColor: drawing color for matches (chosen randomly in case of default value)
:param singleLineColor: drawing color for keylines (chosen randomly in case of default value)
:param matchesMask: mask to indicate which matches must be drawn
:param flags: drawing flags
.. note:: If both *matchColor* and *singleLineColor* are set to their default values, function draws matched lines and line connecting them with same color
The structure of drawing flags is shown in the following:
.. code-block:: cpp
/* struct for drawing options */
struct CV_EXPORTS DrawLinesMatchesFlags
{
enum
{
DEFAULT = 0, // Output image matrix will be created (Mat::create),
// i.e. existing memory of output image may be reused.
// Two source images, matches, and single keylines
// will be drawn.
DRAW_OVER_OUTIMG = 1, // Output image matrix will not be
// created (using Mat::create). Matches will be drawn
// on existing content of output image.
NOT_DRAW_SINGLE_LINES = 2 // Single keylines will not be drawn.
};
};
..
drawKeylines
------------
Draws keylines.
.. ocv:function:: void drawKeylines( const Mat& image, const std::vector<KeyLine>& keylines, Mat& outImage, const Scalar& color=Scalar::all(-1), int flags=DrawLinesMatchesFlags::DEFAULT )
:param image: input image
:param keylines: keylines to be drawn
:param outImage: output image to draw on
:param color: color of lines to be drawn (if set to defaul value, color is chosen randomly)
:param flags: drawing flags
Related pages
-------------
* :ref:`line_descriptor`
* :ref:`binary_descriptor`
* :ref:`matching`
.. _line_descriptor:
Binary descriptors for lines extracted from an image
====================================================
.. highlight:: cpp
Introduction
------------
One of the most challenging activities in computer vision is the extraction of useful information from a given image. Such information, usually comes in the form of points that preserve some kind of property (for instance, they are scale-invariant) and are actually representative of input image.
The goal of this module is seeking a new kind of representative information inside an image and providing the functionalities for its extraction and representation. In particular, differently from previous methods for detection of relevant elements inside an image, lines are extracted in place of points; a new class is defined ad hoc to summarize a line's properties, for reuse and plotting purposes.
A class to represent a line: KeyLine
------------------------------------
As aformentioned, it is been necessary to design a class that fully stores the information needed to characterize completely a line and plot it on image it was extracted from, when required.
*KeyLine* class has been created for such goal; it is mainly inspired to Feature2d's KeyPoint class, since KeyLine shares some of *KeyPoint*'s fields, even if a part of them assumes a different meaning, when speaking about lines.
In particular:
* the *class_id* field is used to gather lines extracted from different octaves which refer to same line inside original image (such lines and the one they represent in original image share the same *class_id* value)
* the *angle* field represents line's slope with respect to (positive) X axis
* the *pt* field represents line's midpoint
* the *response* field is computed as the ratio between the line's length and maximum between image's width and height
* the *size* field is the area of the smallest rectangle containing line
Apart from fields inspired to KeyPoint class, KeyLines stores information about extremes of line in original image and in octave it was extracted from, about line's length and number of pixels it covers. Code relative to KeyLine class is reported in the following snippet:
.. ocv:class:: KeyLine
::
class CV_EXPORTS_W KeyLine
{
public:
/* orientation of the line */
float angle;
/* object ID, that can be used to cluster keylines by the line they represent */
int class_id;
/* octave (pyramid layer), from which the keyline has been extracted */
int octave;
/* coordinates of the middlepoint */
Point pt;
/* the response, by which the strongest keylines have been selected.
It's represented by the ratio between line's length and maximum between
image's width and height */
float response;
/* minimum area containing line */
float size;
/* lines's extremes in original image */
float startPointX;
float startPointY;
float endPointX;
float endPointY;
/* line's extremes in image it was extracted from */
float sPointInOctaveX;
float sPointInOctaveY;
float ePointInOctaveX;
float ePointInOctaveY;
/* the length of line */
float lineLength;
/* number of pixels covered by the line */
unsigned int numOfPixels;
/* constructor */
KeyLine(){}
};
Computation of binary descriptors
---------------------------------
To obtatin a binary descriptor representing a certain line detected from a certain octave of an image, we first compute a non-binary descriptor as described in [LBD]_. Such algorithm works on lines extracted using EDLine detector, as explained in [EDL]_. Given a line, we consider a rectangular region centered at it and called *line support region (LSR)*. Such region is divided into a set of bands :math:`\{B_1, B_2, ..., B_m\}`, whose length equals the one of line.
If we indicate with :math:`\bf{d}_L` the direction of line, the orthogonal and clockwise direction to line :math:`\bf{d}_{\perp}` can be determined; these two directions, are used to construct a reference frame centered in the middle point of line. The gradients of pixels :math:`\bf{g'}` inside LSR can be projected to the newly determined frame, obtaining their local equivalent :math:`\bf{g'} = (\bf{g}^T \cdot \bf{d}_{\perp}, \bf{g}^T \cdot \bf{d}_L)^T \triangleq (\bf{g'}_{d_{\perp}}, \bf{g'}_{d_L})^T`.
Later on, a Gaussian function is applied to all LSR's pixels along :math:`\bf{d}_\perp` direction; first, we assign a global weighting coefficient :math:`f_g(i) = (1/\sqrt{2\pi}\sigma_g)e^{-d^2_i/2\sigma^2_g}` to *i*-th row in LSR, where :math:`d_i` is the distance of *i*-th row from the center row in LSR, :math:`\sigma_g = 0.5(m \cdot w - 1)` and :math:`w` is the width of bands (the same for every band). Secondly, considering a band :math:`B_j` and its neighbor bands :math:`B_{j-1}, B_{j+1}`, we assign a local weighting :math:`F_l(k) = (1/\sqrt{2\pi}\sigma_l)e^{-d'^2_k/2\sigma_l^2}`, where :math:`d'_k` is the distance of *k*-th row from the center row in :math:`B_j` and :math:`\sigma_l = w`. Using the global and local weights, we obtain, at the same time, the reduction of role played by gradients far from line and of boundary effect, respectively.
Each band :math:`B_j` in LSR has an associated *band descriptor(BD)* which is computed considering previous and next band (top and bottom bands are ignored when computing descriptor for first and last band). Once each band has been assignen its BD, the LBD descriptor of line is simply given by
.. math::
LBD = (BD_1^T, BD_2^T, ... , BD^T_m)^T.
To compute a band descriptor :math:`B_j`, each *k*-th row in it is considered and the gradients in such row are accumulated:
.. math::
\begin{matrix} \bf{V1}^k_j = \lambda \sum\limits_{\bf{g}'_{d_\perp}>0}\bf{g}'_{d_\perp}, & \bf{V2}^k_j = \lambda \sum\limits_{\bf{g}'_{d_\perp}<0} -\bf{g}'_{d_\perp}, \\ \bf{V3}^k_j = \lambda \sum\limits_{\bf{g}'_{d_L}>0}\bf{g}'_{d_L}, & \bf{V4}^k_j = \lambda \sum\limits_{\bf{g}'_{d_L}<0} -\bf{g}'_{d_L}\end{matrix}.
with :math:`\lambda = f_g(k)f_l(k)`.
By stacking previous results, we obtain the *band description matrix (BDM)*
.. math::
BDM_j = \left(\begin{matrix} \bf{V1}_j^1 & \bf{V1}_j^2 & \ldots & \bf{V1}_j^n \\ \bf{V2}_j^1 & \bf{V2}_j^2 & \ldots & \bf{V2}_j^n \\ \bf{V3}_j^1 & \bf{V3}_j^2 & \ldots & \bf{V3}_j^n \\ \bf{V4}_j^1 & \bf{V4}_j^2 & \ldots & \bf{V4}_j^n \end{matrix} \right) \in \mathbb{R}^{4\times n},
with :math:`n` the number of rows in band :math:`B_j`:
.. math::
n = \begin{cases} 2w, & j = 1||m; \\ 3w, & \mbox{else}. \end{cases}
Each :math:`BD_j` can be obtained using the standard deviation vector :math:`S_j` and mean vector :math:`M_j` of :math:`BDM_J`. Thus, finally:
.. math::
LBD = (M_1^T, S_1^T, M_2^T, S_2^T, \ldots, M_m^T, S_m^T)^T \in \mathbb{R}^{8m}
Once the LBD has been obtained, it must be converted into a binary form. For such purpose, we consider 32 possible pairs of BD inside it; each couple of BD is compared bit by bit and comparison generates an 8 bit string. Concatenating 32 comparison strings, we get the 256-bit final binary representation of a single LBD.
References
----------
.. [LBD] Zhang, Lilian, and Reinhard Koch. *An efficient and robust line segment matching approach based on LBD descriptor and pairwise geometric consistency*, Journal of Visual Communication and Image Representation 24.7 (2013): 794-805.
.. [EDL] Von Gioi, R. Grompone, et al. *LSD: A fast line segment detector with a false detection control*, IEEE Transactions on Pattern Analysis and Machine Intelligence 32.4 (2010): 722-732.
Summary
-------
.. toctree::
:maxdepth: 2
binary_descriptor
LSDDetector
matching
drawing_functions
tutorial
.. _matching:
Matching with binary descriptors
================================
.. highlight:: cpp
Once descriptors have been extracted from an image (both they represent lines and points), it becomes interesting to be able to match a descriptor with another one extracted from a different image and representing the same line or point, seen from a differente perspective or on a different scale.
In reaching such goal, the main headache is designing an efficient search algorithm to associate a query descriptor to one extracted from a dataset.
In the following, a matching modality based on *Multi-Index Hashing (MiHashing)* will be described.
Multi-Index Hashing
-------------------
The theory described in this section is based on [MIH]_.
Given a dataset populated with binary codes, each code is indexed *m* times into *m* different hash tables, according to *m* substrings it has been divided into. Thus, given a query code, all the entries close to it at least in one substring are returned by search as *neighbor candidates*. Returned entries are then checked for validity by verifying that their full codes are not distant (in Hamming space) more than *r* bits from query code.
In details, each binary code **h** composed of *b* bits is divided into *m* disjoint substrings :math:`\mathbf{h}^{(1)}, ..., \mathbf{h}^{(m)}`, each with length :math:`\lfloor b/m \rfloor` or :math:`\lceil b/m \rceil` bits. Formally, when two codes **h** and **g** differ by at the most *r* bits, in at the least one of their *m* substrings they differ by at the most :math:`\lfloor r/m \rfloor` bits. In particular, when :math:`||\mathbf{h}-\mathbf{g}||_H \le r` (where :math:`||.||_H` is the Hamming norm), there must exist a substring *k* (with :math:`1 \le k \le m`) such that
.. math::
||\mathbf{h}^{(k)} - \mathbf{g}^{(k)}||_H \le \left\lfloor \frac{r}{m} \right\rfloor .
That means that if Hamming distance between each of the *m* substring is strictly greater than :math:`\lfloor r/m \rfloor`, then :math:`||\mathbf{h}-\mathbf{g}||_H` must be larger that *r* and that is a contradiction.
If the codes in dataset are divided into *m* substrings, then *m* tables will be built. Given a query **q** with substrings :math:`\{\mathbf{q}^{(i)}\}^m_{i=1}`, *i*-th hash table is searched for entries distant at the most :math:`\lfloor r/m \rfloor` from :math:`\mathbf{q}^{(i)}` and a set of candidates :math:`\mathcal{N}_i(\mathbf{q})` is obtained.
The union of sets :math:`\mathcal{N}(\mathbf{q}) = \bigcup_i \mathcal{N}_i(\mathbf{q})` is a superset of the *r*-neighbors of **q**. Then, last step of algorithm is computing the Hamming distance between **q** and each element in :math:`\mathcal{N}(\mathbf{q})`, deleting the codes that are distant more that *r* from **q**.
BinaryDescriptorMatcher Class
=============================
BinaryDescriptorMatcher Class furnishes all functionalities for querying a dataset provided by user or internal to class (that user must, anyway, populate) on the model of Feature2d's `DescriptorMatcher <http://docs.opencv.org/modules/features2d/doc/common_interfaces_of_descriptor_matchers.html?highlight=bfmatcher#descriptormatcher>`_.
BinaryDescriptorMatcher::BinaryDescriptorMatcher
--------------------------------------------------
Constructor.
.. ocv:function:: BinaryDescriptorMatcher::BinaryDescriptorMatcher()
The BinaryDescriptorMatcher constructed is able to store and manage 256-bits long entries.
BinaryDescriptorMatcher::createBinaryDescriptorMatcher
------------------------------------------------------
Create a BinaryDescriptorMatcher object and return a smart pointer to it.
.. ocv:function:: Ptr<BinaryDescriptorMatcher> BinaryDescriptorMatcher::createBinaryDescriptorMatcher()
BinaryDescriptorMatcher::add
----------------------------
Store locally new descriptors to be inserted in dataset, without updating dataset.
.. ocv:function:: void BinaryDescriptorMatcher::add( const std::vector<Mat>& descriptors )
:param descriptors: matrices containing descriptors to be inserted into dataset
.. note:: Each matrix *i* in **descriptors** should contain descriptors relative to lines extracted from *i*-th image.
BinaryDescriptorMatcher::train
------------------------------
Update dataset by inserting into it all descriptors that were stored locally by *add* function.
.. ocv:function:: void BinaryDescriptorMatcher::train()
.. note:: Every time this function is invoked, current dataset is deleted and locally stored descriptors are inserted into dataset. The locally stored copy of just inserted descriptors is then removed.
BinaryDescriptorMatcher::clear
------------------------------
Clear dataset and internal data
.. ocv:function:: void BinaryDescriptorMatcher::clear()
BinaryDescriptorMatcher::match
------------------------------
For every input query descriptor, retrieve the best matching one from a dataset provided from user or from the one internal to class
.. ocv:function:: void BinaryDescriptorMatcher::match( const Mat& queryDescriptors, const Mat& trainDescriptors, std::vector<DMatch>& matches, const Mat& mask=Mat() ) const
.. ocv:function:: void BinaryDescriptorMatcher::match( const Mat& queryDescriptors, std::vector<DMatch>& matches, const std::vector<Mat>& masks=std::vector<Mat>() )
:param queryDescriptors: query descriptors
:param trainDescriptors: dataset of descriptors furnished by user
:param matches: vector to host retrieved matches
:param mask: mask to select which input descriptors must be matched to one in dataset
:param masks: vector of masks to select which input descriptors must be matched to one in dataset (the *i*-th mask in vector indicates whether each input query can be matched with descriptors in dataset relative to *i*-th image)
BinaryDescriptorMatcher::knnMatch
---------------------------------
For every input query descriptor, retrieve the best *k* matching ones from a dataset provided from user or from the one internal to class
.. ocv:function:: void BinaryDescriptorMatcher::knnMatch( const Mat& queryDescriptors, const Mat& trainDescriptors, std::vector<std::vector<DMatch> >& matches, int k, const Mat& mask=Mat(), bool compactResult=false ) const
.. ocv:function:: void BinaryDescriptorMatcher::knnMatch( const Mat& queryDescriptors, std::vector<std::vector<DMatch> >& matches, int k, const std::vector<Mat>& masks=std::vector<Mat>(), bool compactResult=false )
:param queryDescriptors: query descriptors
:param trainDescriptors: dataset of descriptors furnished by user
:param matches: vector to host retrieved matches
:param k: number of the closest descriptors to be returned for every input query
:param mask: mask to select which input descriptors must be matched to ones in dataset
:param masks: vector of masks to select which input descriptors must be matched to ones in dataset (the *i*-th mask in vector indicates whether each input query can be matched with descriptors in dataset relative to *i*-th image)
:param compactResult: flag to obtain a compact result (if true, a vector that doesn't contain any matches for a given query is not inserted in final result)
BinaryDescriptorMatcher::radiusMatch
------------------------------------
For every input query descriptor, retrieve, from a dataset provided from user or from the one internal to class, all the descriptors that are not further than *maxDist* from input query
.. ocv:function:: void BinaryDescriptorMatcher::radiusMatch( const Mat& queryDescriptors, const Mat& trainDescriptors, std::vector<std::vector<DMatch> >& matches, float maxDistance, const Mat& mask=Mat(), bool compactResult=false ) const
.. ocv:function:: void BinaryDescriptorMatcher::radiusMatch( const Mat& queryDescriptors, std::vector<std::vector<DMatch> >& matches, float maxDistance, const std::vector<Mat>& masks=std::vector<Mat>(), bool compactResult=false )
:param queryDescriptors: query descriptors
:param trainDescriptors: dataset of descriptors furnished by user
:param matches: vector to host retrieved matches
:param maxDist: search radius
:param mask: mask to select which input descriptors must be matched to ones in dataset
:param masks: vector of masks to select which input descriptors must be matched to ones in dataset (the *i*-th mask in vector indicates whether each input query can be matched with descriptors in dataset relative to *i*-th image)
:param compactResult: flag to obtain a compact result (if true, a vector that doesn't contain any matches for a given query is not inserted in final result)
Related pages
-------------
* :ref:`line_descriptor`
* :ref:`binary_descriptor`
* :ref:`drawing`
References
----------
.. [MIH] Norouzi, Mohammad, Ali Punjani, and David J. Fleet. *Fast search in hamming space with multi-index hashing*, Computer Vision and Pattern Recognition (CVPR), 2012 IEEE Conference on. IEEE, 2012.
Line Features Tutorial
======================
In this tutorial it will be shown how to:
* use the *BinaryDescriptor* interface to extract lines and store them in *KeyLine* objects
* use the same interface to compute descriptors for every extracted line
* use the *BynaryDescriptorMatcher* to determine matches among descriptors obtained from different images
Lines extraction and descriptors computation
--------------------------------------------
In the following snippet of code, it is shown how to detect lines from an image. The LSD extractor is initialized with *LSD_REFINE_ADV* option; remaining parameters are left to their default values. A mask of ones is used in order to accept all extracted lines, which, at the end, are displayed using random colors for octave 0.
.. code-block:: cpp
#include <opencv2/line_descriptor.hpp>
#include "opencv2/core/utility.hpp"
#include "opencv2/core/private.hpp"
#include <opencv2/imgproc.hpp>
#include <opencv2/features2d.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
static const char* keys =
{ "{@image_path | | Image path }" };
static void help()
{
cout << "\nThis example shows the functionalities of lines extraction " << "furnished by BinaryDescriptor class\n"
<< "Please, run this sample using a command in the form\n" << "./example_line_descriptor_lines_extraction <path_to_input_image>" << endl;
}
int main( int argc, char** argv )
{
/* get parameters from comand line */
CommandLineParser parser( argc, argv, keys );
String image_path = parser.get<String>( 0 );
if( image_path.empty() )
{
help();
return -1;
}
/* load image */
cv::Mat imageMat = imread( image_path, 1 );
if( imageMat.data == NULL )
{
std::cout << "Error, image could not be loaded. Please, check its path" << std::endl;
}
/* create a ramdom binary mask */
cv::Mat mask = Mat::ones( imageMat.size(), CV_8UC1 );
/* create a pointer to a BinaryDescriptor object with deafult parameters */
Ptr<BinaryDescriptor> bd = BinaryDescriptor::createBinaryDescriptor();
/* create a structure to store extracted lines */
vector<KeyLine> lines;
/* extract lines */
bd->detect( imageMat, lines, mask );
/* draw lines extracted from octave 0 */
cv::Mat output = imageMat.clone();
if( output.channels() == 1 )
cvtColor( output, output, COLOR_GRAY2BGR );
for ( size_t i = 0; i < lines.size(); i++ )
{
KeyLine kl = lines[i];
if( kl.octave == 0)
{
/* get a random color */
int R = ( rand() % (int) ( 255 + 1 ) );
int G = ( rand() % (int) ( 255 + 1 ) );
int B = ( rand() % (int) ( 255 + 1 ) );
/* get extremes of line */
Point pt1 = Point( kl.startPointX, kl.startPointY );
Point pt2 = Point( kl.endPointX, kl.endPointY );
/* draw line */
line( output, pt1, pt2, Scalar( B, G, R ), 5 );
}
}
/* show lines on image */
imshow( "Lines", output );
waitKey();
}
..
This is the result obtained for famous cameraman image:
.. image:: pics/lines_cameraman_edl.png
:width: 512px
:align: center
:height: 512px
:alt: alternate text
Another way to extract lines is using *LSDDetector* class; such class uses the LSD extractor to compute lines. To obtain this result, it is sufficient to use the snippet code seen above, just modifying it by the rows
.. code-block:: cpp
/* create a pointer to an LSDDetector object */
Ptr<LSDDetector> lsd = LSDDetector::createLSDDetector();
/* compute lines */
std::vector<KeyLine> keylines;
lsd->detect( imageMat, keylines, mask );
..
Here's the result returned by LSD detector again on cameraman picture:
.. image:: pics/cameraman_lines2.png
:width: 512px
:align: center
:height: 512px
:alt: alternate text
Once keylines have been detected, it is possible to compute their descriptors as shown in the following:
.. code-block:: cpp
#include <opencv2/line_descriptor.hpp>
#include "opencv2/core/utility.hpp"
#include "opencv2/core/private.hpp"
#include <opencv2/imgproc.hpp>
#include <opencv2/features2d.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
using namespace cv;
static const char* keys =
{ "{@image_path | | Image path }" };
static void help()
{
std::cout << "\nThis example shows the functionalities of lines extraction " << "and descriptors computation furnished by BinaryDescriptor class\n"
<< "Please, run this sample using a command in the form\n" << "./example_line_descriptor_compute_descriptors <path_to_input_image>"
<< std::endl;
}
int main( int argc, char** argv )
{
/* get parameters from command line */
CommandLineParser parser( argc, argv, keys );
String image_path = parser.get<String>( 0 );
if( image_path.empty() )
{
help();
return -1;
}
/* load image */
cv::Mat imageMat = imread( image_path, 1 );
if( imageMat.data == NULL )
{
std::cout << "Error, image could not be loaded. Please, check its path" << std::endl;
}
/* create a binary mask */
cv::Mat mask = Mat::ones( imageMat.size(), CV_8UC1 );
/* create a pointer to a BinaryDescriptor object with default parameters */
Ptr<BinaryDescriptor> bd = BinaryDescriptor::createBinaryDescriptor();
/* compute lines */
std::vector<KeyLine> keylines;
bd->detect( imageMat, keylines, mask );
/* compute descriptors */
cv::Mat descriptors;
bd->compute( imageMat, keylines, descriptors );
}
..
Matching among descriptors
--------------------------
If we have extracted descriptors from two different images, it is possible to search for matches among them. One way of doing it is matching exactly a descriptor to each input query descriptor, choosing the one at closest distance:
.. code-block:: cpp
#include <opencv2/line_descriptor.hpp>
#include "opencv2/core/utility.hpp"
#include "opencv2/core/private.hpp"
#include <opencv2/imgproc.hpp>
#include <opencv2/features2d.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
using namespace cv;
static const char* keys =
{ "{@image_path1 | | Image path 1 }"
"{@image_path2 | | Image path 2 }" };
static void help()
{
std::cout << "\nThis example shows the functionalities of lines extraction " << "and descriptors computation furnished by BinaryDescriptor class\n"
<< "Please, run this sample using a command in the form\n" << "./example_line_descriptor_compute_descriptors <path_to_input_image 1>"
<< "<path_to_input_image 2>" << std::endl;
}
int main( int argc, char** argv )
{
/* get parameters from comand line */
CommandLineParser parser( argc, argv, keys );
String image_path1 = parser.get<String>( 0 );
String image_path2 = parser.get<String>( 1 );
if( image_path1.empty() || image_path2.empty() )
{
help();
return -1;
}
/* load image */
cv::Mat imageMat1 = imread( image_path1, 1 );
cv::Mat imageMat2 = imread( image_path2, 1 );
waitKey();
if( imageMat1.data == NULL || imageMat2.data == NULL )
{
std::cout << "Error, images could not be loaded. Please, check their path" << std::endl;
}
/* create binary masks */
cv::Mat mask1 = Mat::ones( imageMat1.size(), CV_8UC1 );
cv::Mat mask2 = Mat::ones( imageMat2.size(), CV_8UC1 );
/* create a pointer to a BinaryDescriptor object with default parameters */
Ptr<BinaryDescriptor> bd = BinaryDescriptor::createBinaryDescriptor();
/* compute lines */
std::vector<KeyLine> keylines1, keylines2;
bd->detect( imageMat1, keylines1, mask1 );
bd->detect( imageMat2, keylines2, mask2 );
/* compute descriptors */
cv::Mat descr1, descr2;
bd->compute( imageMat1, keylines1, descr1 );
bd->compute( imageMat2, keylines2, descr2 );
/* create a BinaryDescriptorMatcher object */
Ptr<BinaryDescriptorMatcher> bdm = BinaryDescriptorMatcher::createBinaryDescriptorMatcher();
/* require match */
std::vector<DMatch> matches;
bdm->match( descr1, descr2, matches );
/* plot matches */
cv::Mat outImg;
std::vector<char> mask( matches.size(), 1 );
drawLineMatches( imageMat1, keylines1, imageMat2, keylines2, matches, outImg, Scalar::all( -1 ), Scalar::all( -1 ), mask,
DrawLinesMatchesFlags::DEFAULT );
imshow( "Matches", outImg );
waitKey();
}
..
Sometimes, we could be interested in searching for the closest *k* descriptors, given an input one. This requires to modify slightly previous code:
.. code-block:: cpp
/* prepare a structure to host matches */
std::vector<std::vector<DMatch> > matches;
/* require knn match */
bdm->knnMatch( descr1, descr2, matches, 6 );
..
In the above example, the closest 6 descriptors are returned for every query. In some cases, we could have a search radius and look for all descriptors distant at the most *r* from input query. Previous code must me modified:
.. code-block:: cpp
/* prepare a structure to host matches */
std::vector<std::vector<DMatch> > matches;
/* compute matches */
bdm->radiusMatch( queries, matches, 30 );
..
Here's an example om matching among descriptors extratced from original cameraman image and its downsampled (and blurred) version:
.. image:: pics/matching2.png
:width: 765px
:align: center
:height: 540px
:alt: alternate text
Querying internal database
--------------------------
The *BynaryDescriptorMatcher* class, owns an internal database that can be populated with descriptors extracted from different images and queried using one of the modalities described in previous section.
Population of internal dataset can be done using the *add* function; such function doesn't directly add new data to database, but it just stores it them locally. The real update happens when function *train* is invoked or when any querying function is executed, since each of them invokes *train* before querying.
When queried, internal database not only returns required descriptors, but, for every returned match, it is able to tell which image matched descriptor was extracted from.
An example of internal dataset usage is described in the following code; after adding locally new descriptors, a radius search is invoked. This provokes local data to be transferred to dataset, which, in turn, is then queried.
.. code-block:: cpp
#include <opencv2/line_descriptor.hpp>
#include "opencv2/core/utility.hpp"
#include "opencv2/core/private.hpp"
#include <opencv2/imgproc.hpp>
#include <opencv2/features2d.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <vector>
using namespace cv;
static const std::string images[] =
{ "cameraman.jpg", "church.jpg", "church2.png", "einstein.jpg", "stuff.jpg" };
static const char* keys =
{ "{@image_path | | Image path }" };
static void help()
{
std::cout << "\nThis example shows the functionalities of radius matching " << "Please, run this sample using a command in the form\n"
<< "./example_line_descriptor_radius_matching <path_to_input_images>/" << std::endl;
}
int main( int argc, char** argv )
{
/* get parameters from comand line */
CommandLineParser parser( argc, argv, keys );
String pathToImages = parser.get<String>( 0 );
/* create structures for hosting KeyLines and descriptors */
int num_elements = sizeof ( images ) / sizeof ( images[0] );
std::vector<Mat> descriptorsMat;
std::vector<std::vector<KeyLine> > linesMat;
/*create a pointer to a BinaryDescriptor object */
Ptr<BinaryDescriptor> bd = BinaryDescriptor::createBinaryDescriptor();
/* compute lines and descriptors */
for ( int i = 0; i < num_elements; i++ )
{
/* get path to image */
std::stringstream image_path;
image_path << pathToImages << images[i];
/* load image */
Mat loadedImage = imread( image_path.str().c_str(), 1 );
if( loadedImage.data == NULL )
{
std::cout << "Could not load images." << std::endl;
help();
exit( -1 );
}
/* compute lines and descriptors */
std::vector<KeyLine> lines;
Mat computedDescr;
bd->detect( loadedImage, lines );
bd->compute( loadedImage, lines, computedDescr );
descriptorsMat.push_back( computedDescr );
linesMat.push_back( lines );
}
/* compose a queries matrix */
Mat queries;
for ( size_t j = 0; j < descriptorsMat.size(); j++ )
{
if( descriptorsMat[j].rows >= 5 )
queries.push_back( descriptorsMat[j].rowRange( 0, 5 ) );
else if( descriptorsMat[j].rows > 0 && descriptorsMat[j].rows < 5 )
queries.push_back( descriptorsMat[j] );
}
std::cout << "It has been generated a matrix of " << queries.rows << " descriptors" << std::endl;
/* create a BinaryDescriptorMatcher object */
Ptr<BinaryDescriptorMatcher> bdm = BinaryDescriptorMatcher::createBinaryDescriptorMatcher();
/* populate matcher */
bdm->add( descriptorsMat );
/* compute matches */
std::vector<std::vector<DMatch> > matches;
bdm->radiusMatch( queries, matches, 30 );
/* print matches */
for ( size_t q = 0; q < matches.size(); q++ )
{
for ( size_t m = 0; m < matches[q].size(); m++ )
{
DMatch dm = matches[q][m];
std::cout << "Descriptor: " << q << " Image: " << dm.imgIdx << " Distance: " << dm.distance << std::endl;
}
}
}
..
/*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) 2013, OpenCV Foundation, 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_LINE_DESCRIPTOR_HPP__
#define __OPENCV_LINE_DESCRIPTOR_HPP__
#include "opencv2/line_descriptor/descriptor.hpp"
namespace cv
{
CV_EXPORTS bool initModule_line_descriptor( void );
}
#endif
/*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) 2014, Biagio Montesano, 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_DESCRIPTOR_HPP__
#define __OPENCV_DESCRIPTOR_HPP__
#include <map>
#include <vector>
#include <list>
#include <inttypes.h>
#include <stdio.h>
#include <iostream>
#include "opencv2/core/utility.hpp"
#include "opencv2/core/private.hpp"
#include <opencv2/imgproc.hpp>
#include <opencv2/features2d.hpp>
#include <opencv2/highgui.hpp>
#include "opencv2/core.hpp"
/* define data types */
typedef uint64_t UINT64;
typedef uint32_t UINT32;
typedef uint16_t UINT16;
typedef uint8_t UINT8;
/* define constants */
#define UINT64_1 ((UINT64)0x01)
#define UINT32_1 ((UINT32)0x01)
namespace cv
{
namespace line_descriptor
{
CV_EXPORTS bool initModule_line_descriptor();
struct CV_EXPORTS KeyLine
{
public:
/* orientation of the line */
/*CV_PROP_RW*/
float angle;
/* object ID, that can be used to cluster keylines by the line they represent */
/*CV_PROP_RW*/
int class_id;
/* octave (pyramid layer), from which the keyline has been extracted */
/*CV_PROP_RW*/
int octave;
/* coordinates of the middlepoint */
/*CV_PROP_RW*/
Point2f pt;
/* the response, by which the strongest keylines have been selected.
It's represented by the ratio between line's length and maximum between
image's width and height */
/*CV_PROP_RW*/
float response;
/* minimum area containing line */
/*CV_PROP_RW*/
float size;
/* lines's extremes in original image */
/*CV_PROP_RW*/
float startPointX;/*CV_PROP_RW*/
float startPointY;/*CV_PROP_RW*/
float endPointX;/*CV_PROP_RW*/
float endPointY;
/* line's extremes in image it was extracted from */
/*CV_PROP_RW*/
float sPointInOctaveX;/*CV_PROP_RW*/
float sPointInOctaveY;/*CV_PROP_RW*/
float ePointInOctaveX;/*CV_PROP_RW*/
float ePointInOctaveY;
/* the length of line */
/*CV_PROP_RW*/
float lineLength;
/* number of pixels covered by the line */
/*CV_PROP_RW*/
int numOfPixels;
/* constructor */
/*CV_WRAP*/
KeyLine()
{
}
};
class CV_EXPORTS BinaryDescriptor : public Algorithm
{
public:
struct CV_EXPORTS Params
{
/*CV_WRAP*/
Params();
/* the number of image octaves (default = 1) */
/*CV_PROP_RW*/
int numOfOctave_;
/* the width of band; (default: 7) */
/*CV_PROP_RW*/
int widthOfBand_;
/* image's reduction ratio in construction of Gaussian pyramids */
/*CV_PROP_RW*/
int reductionRatio;
/*CV_PROP_RW*/
int ksize_;
/* read parameters from a FileNode object and store them (struct function) */
/*CV_WRAP*/
void read( const FileNode& fn );
/* store parameters to a FileStorage object (struct function) */
/*CV_WRAP*/
void write( FileStorage& fs ) const;
};
/* constructor */
/*CV_WRAP*/
BinaryDescriptor( const BinaryDescriptor::Params &parameters = BinaryDescriptor::Params() );
/* constructors with smart pointers */
/*CV_WRAP*/
static Ptr<BinaryDescriptor> createBinaryDescriptor();/*CV_WRAP*/
static Ptr<BinaryDescriptor> createBinaryDescriptor( Params parameters );
/* destructor */
~BinaryDescriptor();
/* setters and getters */
/*CV_WRAP*/
int getNumOfOctaves();/*CV_WRAP*/
void setNumOfOctaves( int octaves );/*CV_WRAP*/
int getWidthOfBand();/*CV_WRAP*/
void setWidthOfBand( int width );/*CV_WRAP*/
int getReductionRatio();/*CV_WRAP*/
void setReductionRatio( int rRatio );
/* reads parameters from a FileNode object and store them (class function ) */
/*CV_WRAP*/
virtual void read( const cv::FileNode& fn );
/* stores parameters to a FileStorage object (class function) */
/*CV_WRAP*/
virtual void write( cv::FileStorage& fs ) const;
/* requires line detection (only one image) */
/*CV_WRAP*/
void detect( const Mat& image, CV_OUT std::vector<KeyLine>& keypoints, const Mat& mask = Mat() );
/* requires line detection (more than one image) */
/*CV_WRAP*/
void detect( const std::vector<Mat>& images, std::vector<std::vector<KeyLine> >& keylines, const std::vector<Mat>& masks =
std::vector<Mat>() ) const;
/* requires descriptors computation (only one image) */
/*CV_WRAP*/
void compute( const Mat& image, CV_OUT CV_IN_OUT std::vector<KeyLine>& keylines, CV_OUT Mat& descriptors, bool returnFloatDescr = false ) const;
/* requires descriptors computation (more than one image) */
/*CV_WRAP*/
void compute( const std::vector<Mat>& images, std::vector<std::vector<KeyLine> >& keylines, std::vector<Mat>& descriptors, bool returnFloatDescr =
false ) const;
/* returns descriptor size */
/*CV_WRAP*/
int descriptorSize() const;
/* returns data type */
/*CV_WRAP*/
int descriptorType() const;
/* returns norm mode */
/*CV_WRAP*/
int defaultNorm() const;
/* definition of operator () */
//CV_WRAP_AS(detectAndCompute)
virtual void operator()( InputArray image, InputArray mask, CV_OUT std::vector<KeyLine>& keylines, OutputArray descriptors,
bool useProvidedKeyLines = false, bool returnFloatDescr = false ) const;
protected:
/* implementation of line detection */
virtual void detectImpl( const Mat& imageSrc, std::vector<KeyLine>& keylines, const Mat& mask = Mat() ) const;
/* implementation of descriptors' computation */
virtual void computeImpl( const Mat& imageSrc, std::vector<KeyLine>& keylines, Mat& descriptors, bool returnFloatDescr,
bool useDetectionData ) const;
/* function inherited from Algorithm */
AlgorithmInfo* info() const;
private:
/* struct to represent lines extracted from an octave */
struct OctaveLine
{
unsigned int octaveCount; //the octave which this line is detected
unsigned int lineIDInOctave; //the line ID in that octave image
unsigned int lineIDInScaleLineVec; //the line ID in Scale line vector
float lineLength; //the length of line in original image scale
};
// A 2D line (normal equation parameters).
struct SingleLine
{
//note: rho and theta are based on coordinate origin, i.e. the top-left corner of image
double rho; //unit: pixel length
double theta; //unit: rad
double linePointX; // = rho * cos(theta);
double linePointY; // = rho * sin(theta);
//for EndPoints, the coordinate origin is the top-left corner of image.
double startPointX;
double startPointY;
double endPointX;
double endPointY;
//direction of a line, the angle between positive line direction (dark side is in the left) and positive X axis.
double direction;
//mean gradient magnitude
double gradientMagnitude;
//mean gray value of pixels in dark side of line
double darkSideGrayValue;
//mean gray value of pixels in light side of line
double lightSideGrayValue;
//the length of line
double lineLength;
//the width of line;
double width;
//number of pixels
int numOfPixels;
//the decriptor of line
std::vector<double> descriptor;
};
// Specifies a vector of lines.
typedef std::vector<SingleLine> Lines_list;
struct OctaveSingleLine
{
/*endPoints, the coordinate origin is the top-left corner of the original image.
*startPointX = sPointInOctaveX * (factor)^octaveCount; */
float startPointX;
float startPointY;
float endPointX;
float endPointY;
//endPoints, the coordinate origin is the top-left corner of the octave image.
float sPointInOctaveX;
float sPointInOctaveY;
float ePointInOctaveX;
float ePointInOctaveY;
//direction of a line, the angle between positive line direction (dark side is in the left) and positive X axis.
float direction;
//the summation of gradient magnitudes of pixels on lines
float salience;
//the length of line
float lineLength;
//number of pixels
unsigned int numOfPixels;
//the octave which this line is detected
unsigned int octaveCount;
//the decriptor of line
std::vector<float> descriptor;
};
struct Pixel
{
unsigned int x; //X coordinate
unsigned int y; //Y coordinate
};
struct EdgeChains
{
std::vector<unsigned int> xCors; //all the x coordinates of edge points
std::vector<unsigned int> yCors; //all the y coordinates of edge points
std::vector<unsigned int> sId; //the start index of each edge in the coordinate arrays
unsigned int numOfEdges; //the number of edges whose length are larger than minLineLen; numOfEdges < sId.size;
};
struct LineChains
{
std::vector<unsigned int> xCors; //all the x coordinates of line points
std::vector<unsigned int> yCors; //all the y coordinates of line points
std::vector<unsigned int> sId; //the start index of each line in the coordinate arrays
unsigned int numOfLines; //the number of lines whose length are larger than minLineLen; numOfLines < sId.size;
};
typedef std::list<Pixel> PixelChain; //each edge is a pixel chain
struct EDLineParam
{
int ksize;
float sigma;
float gradientThreshold;
float anchorThreshold;
int scanIntervals;
int minLineLen;
double lineFitErrThreshold;
};
#define RELATIVE_ERROR_FACTOR 100.0
#define MLN10 2.30258509299404568402
#define log_gamma(x) ((x)>15.0?log_gamma_windschitl(x):log_gamma_lanczos(x))
/* This class is used to detect lines from input image.
* First, edges are extracted from input image following the method presented in Cihan Topal and
* Cuneyt Akinlar's paper:"Edge Drawing: A Heuristic Approach to Robust Real-Time Edge Detection", 2010.
* Then, lines are extracted from the edge image following the method presented in Cuneyt Akinlar and
* Cihan Topal's paper:"EDLines: A real-time line segment detector with a false detection control", 2011
* PS: The linking step of edge detection has a little bit difference with the Edge drawing algorithm
* described in the paper. The edge chain doesn't stop when the pixel direction is changed.
*/
class EDLineDetector
{
public:
EDLineDetector();
EDLineDetector( EDLineParam param );
~EDLineDetector();
/*extract edges from image
*image: In, gray image;
*edges: Out, store the edges, each edge is a pixel chain
*return -1: error happen
*/
int EdgeDrawing( cv::Mat &image, EdgeChains &edgeChains );
/*extract lines from image
*image: In, gray image;
*lines: Out, store the extracted lines,
*return -1: error happen
*/
int EDline( cv::Mat &image, LineChains &lines );
/* extract line from image, and store them */
int EDline( cv::Mat &image );
cv::Mat dxImg_; //store the dxImg;
cv::Mat dyImg_; //store the dyImg;
cv::Mat gImgWO_; //store the gradient image without threshold;
LineChains lines_; //store the detected line chains;
//store the line Equation coefficients, vec3=[w1,w2,w3] for line w1*x + w2*y + w3=0;
std::vector<std::vector<double> > lineEquations_;
//store the line endpoints, [x1,y1,x2,y3]
std::vector<std::vector<float> > lineEndpoints_;
//store the line direction
std::vector<float> lineDirection_;
//store the line salience, which is the summation of gradients of pixels on line
std::vector<float> lineSalience_;
// image sizes
unsigned int imageWidth;
unsigned int imageHeight;
/*The threshold of line fit error;
*If lineFitErr is large than this threshold, then
*the pixel chain is not accepted as a single line segment.*/
double lineFitErrThreshold_;
/*the threshold of pixel gradient magnitude.
*Only those pixel whose gradient magnitude are larger than this threshold will be
*taken as possible edge points. Default value is 36*/
short gradienThreshold_;
/*If the pixel's gradient value is bigger than both of its neighbors by a
*certain threshold (ANCHOR_THRESHOLD), the pixel is marked to be an anchor.
*Default value is 8*/
unsigned char anchorThreshold_;
/*anchor testing can be performed at different scan intervals, i.e.,
*every row/column, every second row/column etc.
*Default value is 2*/
unsigned int scanIntervals_;
int minLineLen_; //minimal acceptable line length
private:
void InitEDLine_();
/*For an input edge chain, find the best fit line, the default chain length is minLineLen_
*xCors: In, pointer to the X coordinates of pixel chain;
*yCors: In, pointer to the Y coordinates of pixel chain;
*offsetS:In, start index of this chain in vector;
*lineEquation: Out, [a,b] which are the coefficient of lines y=ax+b(horizontal) or x=ay+b(vertical);
*return: line fit error; -1:error happens;
*/
double LeastSquaresLineFit_( unsigned int *xCors, unsigned int *yCors, unsigned int offsetS, std::vector<double> &lineEquation );
/*For an input pixel chain, find the best fit line. Only do the update based on new points.
*For A*x=v, Least square estimation of x = Inv(A^T * A) * (A^T * v);
*If some new observations are added, i.e, [A; A'] * x = [v; v'],
*then x' = Inv(A^T * A + (A')^T * A') * (A^T * v + (A')^T * v');
*xCors: In, pointer to the X coordinates of pixel chain;
*yCors: In, pointer to the Y coordinates of pixel chain;
*offsetS:In, start index of this chain in vector;
*newOffsetS: In, start index of extended part;
*offsetE:In, end index of this chain in vector;
*lineEquation: Out, [a,b] which are the coefficient of lines y=ax+b(horizontal) or x=ay+b(vertical);
*return: line fit error; -1:error happens;
*/
double LeastSquaresLineFit_( unsigned int *xCors, unsigned int *yCors, unsigned int offsetS, unsigned int newOffsetS, unsigned int offsetE,
std::vector<double> &lineEquation );
/* Validate line based on the Helmholtz principle, which basically states that
* for a structure to be perceptually meaningful, the expectation of this structure
* by chance must be very low.
*/
bool LineValidation_( unsigned int *xCors, unsigned int *yCors, unsigned int offsetS, unsigned int offsetE, std::vector<double> &lineEquation,
float &direction );
bool bValidate_; //flag to decide whether line will be validated
int ksize_; //the size of Gaussian kernel: ksize X ksize, default value is 5.
float sigma_; //the sigma of Gaussian kernal, default value is 1.0.
/*For example, there two edges in the image:
*edge1 = [(7,4), (8,5), (9,6),| (10,7)|, (11, 8), (12,9)] and
*edge2 = [(14,9), (15,10), (16,11), (17,12),| (18, 13)|, (19,14)] ; then we store them as following:
*pFirstPartEdgeX_ = [10, 11, 12, 18, 19];//store the first part of each edge[from middle to end]
*pFirstPartEdgeY_ = [7, 8, 9, 13, 14];
*pFirstPartEdgeS_ = [0,3,5];// the index of start point of first part of each edge
*pSecondPartEdgeX_ = [10, 9, 8, 7, 18, 17, 16, 15, 14];//store the second part of each edge[from middle to front]
*pSecondPartEdgeY_ = [7, 6, 5, 4, 13, 12, 11, 10, 9];//anchor points(10, 7) and (18, 13) are stored again
*pSecondPartEdgeS_ = [0, 4, 9];// the index of start point of second part of each edge
*This type of storage order is because of the order of edge detection process.
*For each edge, start from one anchor point, first go right, then go left or first go down, then go up*/
//store the X coordinates of the first part of the pixels for chains
unsigned int *pFirstPartEdgeX_;
//store the Y coordinates of the first part of the pixels for chains
unsigned int *pFirstPartEdgeY_;
//store the start index of every edge chain in the first part arrays
unsigned int *pFirstPartEdgeS_;
//store the X coordinates of the second part of the pixels for chains
unsigned int *pSecondPartEdgeX_;
//store the Y coordinates of the second part of the pixels for chains
unsigned int *pSecondPartEdgeY_;
//store the start index of every edge chain in the second part arrays
unsigned int *pSecondPartEdgeS_;
//store the X coordinates of anchors
unsigned int *pAnchorX_;
//store the Y coordinates of anchors
unsigned int *pAnchorY_;
//edges
cv::Mat edgeImage_;
cv::Mat gImg_; //store the gradient image;
cv::Mat dirImg_; //store the direction image
double logNT_;
cv::Mat_<float> ATA; //the previous matrix of A^T * A;
cv::Mat_<float> ATV; //the previous vector of A^T * V;
cv::Mat_<float> fitMatT; //the matrix used in line fit function;
cv::Mat_<float> fitVec; //the vector used in line fit function;
cv::Mat_<float> tempMatLineFit; //the matrix used in line fit function;
cv::Mat_<float> tempVecLineFit; //the vector used in line fit function;
/** Compare doubles by relative error.
The resulting rounding error after floating point computations
depend on the specific operations done. The same number computed by
different algorithms could present different rounding errors. For a
useful comparison, an estimation of the relative rounding error
should be considered and compared to a factor times EPS. The factor
should be related to the accumulated rounding error in the chain of
computation. Here, as a simplification, a fixed factor is used.
*/
static int double_equal( double a, double b )
{
double abs_diff, aa, bb, abs_max;
/* trivial case */
if( a == b )
return true;
abs_diff = fabs( a - b );
aa = fabs( a );
bb = fabs( b );
abs_max = aa > bb ? aa : bb;
/* DBL_MIN is the smallest normalized number, thus, the smallest
number whose relative error is bounded by DBL_EPSILON. For
smaller numbers, the same quantization steps as for DBL_MIN
are used. Then, for smaller numbers, a meaningful "relative"
error should be computed by dividing the difference by DBL_MIN. */
if( abs_max < DBL_MIN )
abs_max = DBL_MIN;
/* equal if relative error <= factor x eps */
return ( abs_diff / abs_max ) <= ( RELATIVE_ERROR_FACTOR * DBL_EPSILON );
}
/** Computes the natural logarithm of the absolute value of
the gamma function of x using the Lanczos approximation.
See http://www.rskey.org/gamma.htm
The formula used is
@f[
\Gamma(x) = \frac{ \sum_{n=0}^{N} q_n x^n }{ \Pi_{n=0}^{N} (x+n) }
(x+5.5)^{x+0.5} e^{-(x+5.5)}
@f]
so
@f[
\log\Gamma(x) = \log\left( \sum_{n=0}^{N} q_n x^n \right)
+ (x+0.5) \log(x+5.5) - (x+5.5) - \sum_{n=0}^{N} \log(x+n)
@f]
and
q0 = 75122.6331530,
q1 = 80916.6278952,
q2 = 36308.2951477,
q3 = 8687.24529705,
q4 = 1168.92649479,
q5 = 83.8676043424,
q6 = 2.50662827511.
*/
static double log_gamma_lanczos( double x )
{
static double q[7] =
{ 75122.6331530, 80916.6278952, 36308.2951477, 8687.24529705, 1168.92649479, 83.8676043424, 2.50662827511 };
double a = ( x + 0.5 ) * log( x + 5.5 ) - ( x + 5.5 );
double b = 0.0;
int n;
for ( n = 0; n < 7; n++ )
{
a -= log( x + (double) n );
b += q[n] * pow( x, (double) n );
}
return a + log( b );
}
/** Computes the natural logarithm of the absolute value of
the gamma function of x using Windschitl method.
See http://www.rskey.org/gamma.htm
The formula used is
@f[
\Gamma(x) = \sqrt{\frac{2\pi}{x}} \left( \frac{x}{e}
\sqrt{ x\sinh(1/x) + \frac{1}{810x^6} } \right)^x
@f]
so
@f[
\log\Gamma(x) = 0.5\log(2\pi) + (x-0.5)\log(x) - x
+ 0.5x\log\left( x\sinh(1/x) + \frac{1}{810x^6} \right).
@f]
This formula is a good approximation when x > 15.
*/
static double log_gamma_windschitl( double x )
{
return 0.918938533204673 + ( x - 0.5 ) * log( x ) - x + 0.5 * x * log( x * sinh( 1 / x ) + 1 / ( 810.0 * pow( x, 6.0 ) ) );
}
/** Computes -log10(NFA).
NFA stands for Number of False Alarms:
@f[
\mathrm{NFA} = NT \cdot B(n,k,p)
@f]
- NT - number of tests
- B(n,k,p) - tail of binomial distribution with parameters n,k and p:
@f[
B(n,k,p) = \sum_{j=k}^n
\left(\begin{array}{c}n\\j\end{array}\right)
p^{j} (1-p)^{n-j}
@f]
The value -log10(NFA) is equivalent but more intuitive than NFA:
- -1 corresponds to 10 mean false alarms
- 0 corresponds to 1 mean false alarm
- 1 corresponds to 0.1 mean false alarms
- 2 corresponds to 0.01 mean false alarms
- ...
Used this way, the bigger the value, better the detection,
and a logarithmic scale is used.
@param n,k,p binomial parameters.
@param logNT logarithm of Number of Tests
The computation is based in the gamma function by the following
relation:
@f[
\left(\begin{array}{c}n\\k\end{array}\right)
= \frac{ \Gamma(n+1) }{ \Gamma(k+1) \cdot \Gamma(n-k+1) }.
@f]
We use efficient algorithms to compute the logarithm of
the gamma function.
To make the computation faster, not all the sum is computed, part
of the terms are neglected based on a bound to the error obtained
(an error of 10% in the result is accepted).
*/
static double nfa( int n, int k, double p, double logNT )
{
double tolerance = 0.1; /* an error of 10% in the result is accepted */
double log1term, term, bin_term, mult_term, bin_tail, err, p_term;
int i;
/* check parameters */
if( n < 0 || k < 0 || k > n || p <= 0.0 || p >= 1.0 )
{
std::cout << "nfa: wrong n, k or p values." << std::endl;
exit( 0 );
}
/* trivial cases */
if( n == 0 || k == 0 )
return -logNT;
if( n == k )
return -logNT - (double) n * log10( p );
/* probability term */
p_term = p / ( 1.0 - p );
/* compute the first term of the series */
/*
binomial_tail(n,k,p) = sum_{i=k}^n bincoef(n,i) * p^i * (1-p)^{n-i}
where bincoef(n,i) are the binomial coefficients.
But
bincoef(n,k) = gamma(n+1) / ( gamma(k+1) * gamma(n-k+1) ).
We use this to compute the first term. Actually the log of it.
*/
log1term = log_gamma( (double) n + 1.0 )- log_gamma( (double ) k + 1.0 )- log_gamma( (double ) ( n - k ) + 1.0 )
+ (double) k * log( p )
+ (double) ( n - k ) * log( 1.0 - p );
term = exp( log1term );
/* in some cases no more computations are needed */
if( double_equal( term, 0.0 ) )
{ /* the first term is almost zero */
if( (double) k > (double) n * p ) /* at begin or end of the tail? */
return -log1term / MLN10 - logNT; /* end: use just the first term */
else
return -logNT; /* begin: the tail is roughly 1 */
}
/* compute more terms if needed */
bin_tail = term;
for ( i = k + 1; i <= n; i++ )
{
/* As
term_i = bincoef(n,i) * p^i * (1-p)^(n-i)
and
bincoef(n,i)/bincoef(n,i-1) = n-i+1 / i,
then,
term_i / term_i-1 = (n-i+1)/i * p/(1-p)
and
term_i = term_i-1 * (n-i+1)/i * p/(1-p).
p/(1-p) is computed only once and stored in 'p_term'.
*/
bin_term = (double) ( n - i + 1 ) / (double) i;
mult_term = bin_term * p_term;
term *= mult_term;
bin_tail += term;
if( bin_term < 1.0 )
{
/* When bin_term<1 then mult_term_j<mult_term_i for j>i.
Then, the error on the binomial tail when truncated at
the i term can be bounded by a geometric series of form
term_i * sum mult_term_i^j. */
err = term * ( ( 1.0 - pow( mult_term, (double) ( n - i + 1 ) ) ) / ( 1.0 - mult_term ) - 1.0 );
/* One wants an error at most of tolerance*final_result, or:
tolerance * abs(-log10(bin_tail)-logNT).
Now, the error that can be accepted on bin_tail is
given by tolerance*final_result divided by the derivative
of -log10(x) when x=bin_tail. that is:
tolerance * abs(-log10(bin_tail)-logNT) / (1/bin_tail)
Finally, we truncate the tail if the error is less than:
tolerance * abs(-log10(bin_tail)-logNT) * bin_tail */
if( err < tolerance * fabs( -log10( bin_tail ) - logNT ) * bin_tail )
break;
}
}
return -log10( bin_tail ) - logNT;
}
};
// Specifies a vector of lines.
typedef std::vector<OctaveSingleLine> LinesVec;
// each element in ScaleLines is a vector of lines
// which corresponds the same line detected in different octave images.
typedef std::vector<LinesVec> ScaleLines;
/* compute Gaussian pyramids */
void computeGaussianPyramid( const Mat& image, const int numOctaves );
/* compute Sobel's derivatives */
void computeSobel( const Mat& image, const int numOctaves );
/* conversion of an LBD descriptor to its binary representation */
unsigned char binaryConversion( float* f1, float* f2 );
/* compute LBD descriptors using EDLine extractor */
int computeLBD( ScaleLines &keyLines, bool useDetectionData = false );
/* gathers lines in groups using EDLine extractor.
Each group contains the same line, detected in different octaves */
int OctaveKeyLines( cv::Mat& image, ScaleLines &keyLines );
/* the local gaussian coefficient applied to the orthogonal line direction within each band */
std::vector<double> gaussCoefL_;
/* the global gaussian coefficient applied to each row within line support region */
std::vector<double> gaussCoefG_;
/* descriptor parameters */
Params params;
/* vector of sizes of downsampled and blurred images */
std::vector<cv::Size> images_sizes;
/*For each octave of image, we define an EDLineDetector, because we can get gradient images (dxImg, dyImg, gImg)
*from the EDLineDetector class without extra computation cost. Another reason is that, if we use
*a single EDLineDetector to detect lines in different octave of images, then we need to allocate and release
*memory for gradient images (dxImg, dyImg, gImg) repeatedly for their varying size*/
std::vector<Ptr<EDLineDetector> > edLineVec_;
/* Sobel's derivatives */
std::vector<cv::Mat> dxImg_vector, dyImg_vector;
/* Gaussian pyramid */
std::vector<cv::Mat> octaveImages;
};
class CV_EXPORTS LSDDetector : public Algorithm
{
public:
/* constructor */
/*CV_WRAP*/
LSDDetector()
{
}
;
/* constructor with smart pointer */
/*CV_WRAP*/
static Ptr<LSDDetector> createLSDDetector();
/* requires line detection (only one image) */
/*CV_WRAP*/
void detect( const Mat& image, CV_OUT std::vector<KeyLine>& keypoints, int scale, int numOctaves, const Mat& mask = Mat() );
/* requires line detection (more than one image) */
/*CV_WRAP*/
void detect( const std::vector<Mat>& images, std::vector<std::vector<KeyLine> >& keylines, int scale, int numOctaves,
const std::vector<Mat>& masks = std::vector<Mat>() ) const;
private:
/* compute Gaussian pyramid of input image */
void computeGaussianPyramid( const Mat& image, int numOctaves, int scale );
/* implementation of line detection */
void detectImpl( const Mat& imageSrc, std::vector<KeyLine>& keylines, int numOctaves, int scale, const Mat& mask ) const;
/* matrices for Gaussian pyramids */
std::vector<cv::Mat> gaussianPyrs;
protected:
/* function inherited from Algorithm */
AlgorithmInfo* info() const;
};
class CV_EXPORTS BinaryDescriptorMatcher : public Algorithm
{
public:
/* for every input descriptor,
find the best matching one (for a pair of images) */
/*CV_WRAP*/
void match( const Mat& queryDescriptors, const Mat& trainDescriptors, std::vector<DMatch>& matches, const Mat& mask = Mat() ) const;
/* for every input descriptor,
find the best matching one (from one image to a set) */
/*CV_WRAP*/
void match( const Mat& queryDescriptors, std::vector<DMatch>& matches, const std::vector<Mat>& masks = std::vector<Mat>() );
/* for every input descriptor,
find the best k matching descriptors (for a pair of images) */
/*CV_WRAP*/
void knnMatch( const Mat& queryDescriptors, const Mat& trainDescriptors, std::vector<std::vector<DMatch> >& matches, int k, const Mat& mask = Mat(),
bool compactResult = false ) const;
/* for every input descriptor,
find the best k matching descriptors (from one image to a set) */
/*CV_WRAP*/
void knnMatch( const Mat& queryDescriptors, std::vector<std::vector<DMatch> >& matches, int k, const std::vector<Mat>& masks = std::vector<Mat>(),
bool compactResult = false );
/* for every input descriptor, find all the ones falling in a
certain matching radius (for a pair of images) */
/*CV_WRAP*/
void radiusMatch( const Mat& queryDescriptors, const Mat& trainDescriptors, std::vector<std::vector<DMatch> >& matches, float maxDistance,
const Mat& mask = Mat(), bool compactResult = false ) const;
/* for every input descriptor, find all the ones falling in a
certain matching radius (from one image to a set) */
/*CV_WRAP*/
void radiusMatch( const Mat& queryDescriptors, std::vector<std::vector<DMatch> >& matches, float maxDistance, const std::vector<Mat>& masks =
std::vector<Mat>(),
bool compactResult = false );
/* store new descriptors to be inserted in dataset */
/*CV_WRAP*/
void add( const std::vector<Mat>& descriptors );
/* store new descriptors into dataset */
/*CV_WRAP*/
void train();
/* constructor with smart pointer */
/*CV_WRAP*/
static Ptr<BinaryDescriptorMatcher> createBinaryDescriptorMatcher();
/* clear dataset and internal data */
/*CV_WRAP*/
void clear();
/* constructor */
/*CV_WRAP*/
BinaryDescriptorMatcher();
/* destructor */
~BinaryDescriptorMatcher()
{
}
protected:
/* function inherited from Algorithm */
AlgorithmInfo* info() const;
private:
class BucketGroup
{
public:
/* constructor */
BucketGroup();
/* destructor */
~BucketGroup();
/* insert data into the bucket */
void insert( int subindex, UINT32 data );
/* perform a query to the bucket */
UINT32* query( int subindex, int *size );
/* utility functions */
void insert_value( std::vector<uint32_t>& vec, int index, UINT32 data );
void push_value( std::vector<uint32_t>& vec, UINT32 Data );
/* data fields */
UINT32 empty;
std::vector<uint32_t> group;
};
class SparseHashtable
{
private:
/* Maximum bits per key before folding the table */
static const int MAX_B;
/* Bins (each bin is an Array object for duplicates of the same key) */
BucketGroup *table;
public:
/* constructor */
SparseHashtable();
/* destructor */
~SparseHashtable();
/* initializer */
int init( int _b );
/* insert data */
void insert( UINT64 index, UINT32 data );
/* query data */
UINT32* query( UINT64 index, int* size );
/* Bits per index */
int b;
/* Number of bins */
UINT64 size;
};
/* class defining a sequence of bits */
class bitarray
{
public:
/* pointer to bits sequence and sequence's length */
UINT32 *arr;
UINT32 length;
/* constructor setting default values */
bitarray()
{
arr = NULL;
length = 0;
}
/* constructor setting sequence's length */
bitarray( UINT64 _bits )
{
init( _bits );
}
/* initializer of private fields */
void init( UINT64 _bits )
{
length = (UINT32) ceil( _bits / 32.00 );
arr = new UINT32[length];
erase();
}
/* destructor */
~bitarray()
{
if( arr )
delete[] arr;
}
inline void flip( UINT64 index )
{
arr[index >> 5] ^= ( (UINT32) 0x01 ) << ( index % 32 );
}
inline void set( UINT64 index )
{
arr[index >> 5] |= ( (UINT32) 0x01 ) << ( index % 32 );
}
inline UINT8 get( UINT64 index )
{
return ( arr[index >> 5] & ( ( (UINT32) 0x01 ) << ( index % 32 ) ) ) != 0;
}
/* reserve menory for an UINT32 */
inline void erase()
{
memset( arr, 0, sizeof(UINT32) * length );
}
};
class Mihasher
{
public:
/* Bits per code */
int B;
/* B/8 */
int B_over_8;
/* Bits per chunk (must be less than 64) */
int b;
/* Number of chunks */
int m;
/* Number of chunks with b bits (have 1 bit more than others) */
int mplus;
/* Maximum hamming search radius (we use B/2 by default) */
int D;
/* Maximum hamming search radius per substring */
int d;
/* Maximum results to return */
int K;
/* Number of codes */
UINT64 N;
/* Table of original full-length codes */
cv::Mat codes;
/* Counter for eliminating duplicate results (it is not thread safe) */
bitarray *counter;
/* Array of m hashtables */
SparseHashtable *H;
/* Volume of a b-bit Hamming ball with radius s (for s = 0 to d) */
UINT32 *xornum;
/* Used within generation of binary codes at a certain Hamming distance */
int power[100];
/* constructor */
Mihasher();
/* desctructor */
~Mihasher();
/* constructor 2 */
Mihasher( int B, int m );
/* K setter */
void setK( int K );
/* populate tables */
void populate( cv::Mat & codes, UINT32 N, int dim1codes );
/* execute a batch query */
void batchquery( UINT32 * results, UINT32 *numres/*, qstat *stats*/, const cv::Mat & q, UINT32 numq, int dim1queries );
private:
/* execute a single query */
void query( UINT32 * results, UINT32* numres/*, qstat *stats*/, UINT8 *q, UINT64 * chunks, UINT32 * res );
};
/* retrieve Hamming distances */
void checkKDistances( UINT32 * numres, int k, std::vector<int>& k_distances, int row, int string_length ) const;
/* matrix to store new descriptors */
Mat descriptorsMat;
/* map storing where each bunch of descriptors benins in DS */
std::map<int, int> indexesMap;
/* internal MiHaser representing dataset */
Mihasher* dataset;
/* index from which next added descriptors' bunch must begin */
int nextAddedIndex;
/* number of images whose descriptors are stored in DS */
int numImages;
/* number of descriptors in dataset */
int descrInDS;
};
/* --------------------------------------------------------------------------------------------
UTILITY FUNCTIONS
-------------------------------------------------------------------------------------------- */
/* struct for drawing options */
struct CV_EXPORTS DrawLinesMatchesFlags
{
enum
{
DEFAULT = 0, // Output image matrix will be created (Mat::create),
// i.e. existing memory of output image may be reused.
// Two source images, matches, and single keylines
// will be drawn.
DRAW_OVER_OUTIMG = 1,// Output image matrix will not be
// created (using Mat::create). Matches will be drawn
// on existing content of output image.
NOT_DRAW_SINGLE_LINES = 2// Single keylines will not be drawn.
};
};
/* draw matches between two images */
CV_EXPORTS void drawLineMatches( const Mat& img1, const std::vector<KeyLine>& keylines1, const Mat& img2, const std::vector<KeyLine>& keylines2,
const std::vector<DMatch>& matches1to2, Mat& outImg, const Scalar& matchColor = Scalar::all( -1 ),
const Scalar& singleLineColor = Scalar::all( -1 ), const std::vector<char>& matchesMask = std::vector<char>(),
int flags = DrawLinesMatchesFlags::DEFAULT );
/* draw extracted lines on original image */
CV_EXPORTS void drawKeylines( const Mat& image, const std::vector<KeyLine>& keylines, Mat& outImage, const Scalar& color = Scalar::all( -1 ),
int flags = DrawLinesMatchesFlags::DEFAULT );
}
}
#endif
/*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) 2014, Biagio Montesano, 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 "perf_precomp.hpp"
using namespace cv;
using namespace cv::line_descriptor;
using namespace std;
using namespace perf;
using std::tr1::make_tuple;
using std::tr1::get;
typedef perf::TestBaseWithParam<std::string> file_str;
#define IMAGES \
"line_descriptor_data/cameraman.jpg", "line_descriptor_data/lena.bmp"
PERF_TEST_P(file_str, descriptors, testing::Values(IMAGES))
{
std::string filename = getDataPath( GetParam() );
Mat frame = imread( filename, 1 );
if( frame.empty() )
FAIL()<< "Unable to load source image " << filename;
Mat descriptors;
std::vector<KeyLine> keylines;
Ptr<BinaryDescriptor> bd = BinaryDescriptor::createBinaryDescriptor();
TEST_CYCLE()
{
bd->detect( frame, keylines );
bd->compute( frame, keylines, descriptors );
}
SANITY_CHECK( descriptors );
}
/*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) 2014, Biagio Montesano, 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 "perf_precomp.hpp"
using namespace cv;
using namespace cv::line_descriptor;
using namespace std;
using namespace perf;
using std::tr1::make_tuple;
using std::tr1::get;
typedef perf::TestBaseWithParam<std::string> file_str;
#define IMAGES \
"line_descriptor_data/cameraman.jpg", "line_descriptor_data/lena.bmp"
void createMatFromVec( const std::vector<KeyLine>& linesVec, Mat& output );
void createMatFromVec( const std::vector<KeyLine>& linesVec, Mat& output )
{
output = Mat( (int) linesVec.size(), 17, CV_32FC1 );
for ( int i = 0; i < (int) linesVec.size(); i++ )
{
std::vector<float> klData;
KeyLine kl = linesVec[i];
klData.push_back( kl.angle );
klData.push_back( (float) kl.class_id );
klData.push_back( kl.ePointInOctaveX );
klData.push_back( kl.ePointInOctaveY );
klData.push_back( kl.endPointX );
klData.push_back( kl.endPointY );
klData.push_back( kl.lineLength );
klData.push_back( (float) kl.numOfPixels );
klData.push_back( (float) kl.octave );
klData.push_back( kl.pt.x );
klData.push_back( kl.pt.y );
klData.push_back( kl.response );
klData.push_back( kl.sPointInOctaveX );
klData.push_back( kl.sPointInOctaveY );
klData.push_back( kl.size );
klData.push_back( kl.startPointX );
klData.push_back( kl.startPointY );
float* pointerToRow = output.ptr<float>( i );
for ( int j = 0; j < 17; j++ )
{
*pointerToRow = klData[j];
pointerToRow++;
}
}
}
PERF_TEST_P(file_str, detect, testing::Values(IMAGES))
{
std::string filename = getDataPath( GetParam() );
Mat frame = imread( filename, 1 );
if( frame.empty() )
FAIL()<< "Unable to load source image " << filename;
Mat lines;
std::vector<KeyLine> keylines;
Ptr<BinaryDescriptor> bd = BinaryDescriptor::createBinaryDescriptor();
TEST_CYCLE()
{
bd->detect( frame, keylines );
createMatFromVec( keylines, lines );
}
SANITY_CHECK( lines );
}
PERF_TEST_P(file_str, detect_lsd, testing::Values(IMAGES))
{
std::string filename = getDataPath( GetParam() );
std::cout << filename.c_str() << std::endl;
Mat frame = imread( filename, 1 );
if( frame.empty() )
FAIL()<< "Unable to load source image " << filename;
Mat lines;
std::vector<KeyLine> keylines;
Ptr<LSDDetector> lsd = LSDDetector::createLSDDetector();
TEST_CYCLE()
{
lsd->detect( frame, keylines, 2, 1 );
createMatFromVec( keylines, lines );
}
SANITY_CHECK( lines );
}
/*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) 2014, Biagio Montesano, 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 "perf_precomp.hpp"
CV_PERF_TEST_MAIN( line_descriptor )
/*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) 2014, Biagio Montesano, 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 "perf_precomp.hpp"
using namespace cv;
using namespace cv::line_descriptor;
using namespace std;
using namespace perf;
using std::tr1::make_tuple;
using std::tr1::get;
#define QUERY_DES_COUNT 300
#define DIM 32
#define COUNT_FACTOR 4
#define RADIUS 3
void generateData( Mat& query, Mat& train );
uchar invertSingleBits( uchar dividend_char, int numBits );
/* invert numBits bits in input char */
uchar invertSingleBits( uchar dividend_char, int numBits )
{
std::vector<int> bin_vector;
long dividend;
long bin_num;
/* convert input char to a long */
dividend = (long) dividend_char;
/*if a 0 has been obtained, just generate a 8-bit long vector of zeros */
if( dividend == 0 )
bin_vector = std::vector<int>( 8, 0 );
/* else, apply classic decimal to binary conversion */
else
{
while ( dividend >= 1 )
{
bin_num = dividend % 2;
dividend /= 2;
bin_vector.push_back( bin_num );
}
}
/* ensure that binary vector always has length 8 */
if( bin_vector.size() < 8 )
{
std::vector<int> zeros( 8 - bin_vector.size(), 0 );
bin_vector.insert( bin_vector.end(), zeros.begin(), zeros.end() );
}
/* invert numBits bits */
for ( int index = 0; index < numBits; index++ )
{
if( bin_vector[index] == 0 )
bin_vector[index] = 1;
else
bin_vector[index] = 0;
}
/* reconvert to decimal */
uchar result = 0;
for ( int i = (int) bin_vector.size() - 1; i >= 0; i-- )
result += (uchar) ( bin_vector[i] * pow( 2, i ) );
return result;
}
void generateData( Mat& query, Mat& train )
{
RNG& rng = theRNG();
Mat buf( QUERY_DES_COUNT, DIM, CV_8UC1 );
rng.fill( buf, RNG::UNIFORM, Scalar( 0 ), Scalar( 255 ) );
buf.convertTo( query, CV_8UC1 );
for ( int i = 0; i < query.rows; i++ )
{
for ( int j = 0; j < COUNT_FACTOR; j++ )
{
train.push_back( query.row( i ) );
int randCol = rand() % 32;
uchar u = query.at<uchar>( i, randCol );
uchar modified_u = invertSingleBits( u, j + 1 );
train.at<uchar>( i * COUNT_FACTOR + j, randCol ) = modified_u;
}
}
}
PERF_TEST(matching, single_match)
{
Mat query, train;
std::vector<DMatch> dm;
Ptr<BinaryDescriptorMatcher> bd = BinaryDescriptorMatcher::createBinaryDescriptorMatcher();
generateData( query, train );
TEST_CYCLE()
bd->match( query, train, dm );
SANITY_CHECK_MATCHES( dm );
}
PERF_TEST(knn_matching, knn_match_distances_test)
{
Mat query, train, distances;
std::vector<std::vector<DMatch> > dm;
Ptr<BinaryDescriptorMatcher> bd = BinaryDescriptorMatcher::createBinaryDescriptorMatcher();
generateData( query, train );
TEST_CYCLE()
{
bd->knnMatch( query, train, dm, QUERY_DES_COUNT );
for ( int i = 0; i < (int) dm.size(); i++ )
{
for ( int j = 0; j < (int) dm[i].size(); j++ )
distances.push_back( dm[i][j].distance );
}
}
SANITY_CHECK( distances );
}
PERF_TEST(radius_match, radius_match_distances_test)
{
Mat query, train, distances;
std::vector<std::vector<DMatch> > dm;
Ptr<BinaryDescriptorMatcher> bd = BinaryDescriptorMatcher::createBinaryDescriptorMatcher();
generateData( query, train );
TEST_CYCLE()
{
bd->radiusMatch( query, train, dm, RADIUS );
for ( int i = 0; i < (int) dm.size(); i++ )
{
for ( int j = 0; j < (int) dm[i].size(); j++ )
distances.push_back( dm[i][j].distance );
}
}
SANITY_CHECK( distances );
}
/*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) 2014, Biagio Montesano, 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*/
#ifdef __GNUC__
# pragma GCC diagnostic ignored "-Wmissing-declarations"
# if defined __clang__ || defined __APPLE__
# pragma GCC diagnostic ignored "-Wmissing-prototypes"
# pragma GCC diagnostic ignored "-Wextra"
# endif
#endif
#ifndef __OPENCV_PERF_PRECOMP_HPP__
#define __OPENCV_PERF_PRECOMP_HPP__
#include "opencv2/ts.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/features2d.hpp"
#include "opencv2/line_descriptor.hpp"
#ifdef GTEST_CREATE_SHARED_LIBRARY
#error no modules except ts should have GTEST_CREATE_SHARED_LIBRARY defined
#endif
#endif
/*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) 2014, Biagio Montesano, 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 <opencv2/line_descriptor.hpp>
#include "opencv2/core/utility.hpp"
#include "opencv2/core/private.hpp"
#include <opencv2/imgproc.hpp>
#include <opencv2/features2d.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace cv::line_descriptor;
static const char* keys =
{ "{@image_path | | Image path }" };
static void help()
{
std::cout << "\nThis example shows the functionalities of lines extraction " << "and descriptors computation furnished by BinaryDescriptor class\n"
<< "Please, run this sample using a command in the form\n" << "./example_line_descriptor_compute_descriptors <path_to_input_image>"
<< std::endl;
}
int main( int argc, char** argv )
{
/* get parameters from command line */
CommandLineParser parser( argc, argv, keys );
String image_path = parser.get<String>( 0 );
if( image_path.empty() )
{
help();
return -1;
}
/* load image */
cv::Mat imageMat = imread( image_path, 1 );
if( imageMat.data == NULL )
{
std::cout << "Error, image could not be loaded. Please, check its path" << std::endl;
}
/* create a binary mask */
cv::Mat mask = Mat::ones( imageMat.size(), CV_8UC1 );
/* create a pointer to a BinaryDescriptor object with default parameters */
Ptr<BinaryDescriptor> bd = BinaryDescriptor::createBinaryDescriptor();
/* compute lines */
std::vector<KeyLine> keylines;
bd->detect( imageMat, keylines, mask );
/* compute descriptors */
cv::Mat descriptors;
bd->compute( imageMat, keylines, descriptors);
}
/*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) 2014, Biagio Montesano, 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 <opencv2/line_descriptor.hpp>
#include "opencv2/core/utility.hpp"
#include "opencv2/core/private.hpp"
#include <opencv2/imgproc.hpp>
#include <opencv2/features2d.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <vector>
#include <time.h>
using namespace cv;
using namespace cv::line_descriptor;
static const char* keys =
{ "{@image_path1 | | Image path 1 }"
"{@image_path2 | | Image path 2 }" };
static void help()
{
std::cout << "\nThis example shows the functionalities of descriptors matching\n" << "Please, run this sample using a command in the form\n"
<< "./example_line_descriptor_matching <path_to_input_image 1>" << "<path_to_input_image 2>" << std::endl;
}
uchar invertSingleBits( uchar dividend_char, int numBits );
/* invert numBits bits in input char */
uchar invertSingleBits( uchar dividend_char, int numBits )
{
std::vector<int> bin_vector;
long dividend;
long bin_num;
/* convert input char to a long */
dividend = (long) dividend_char;
/*if a 0 has been obtained, just generate a 8-bit long vector of zeros */
if( dividend == 0 )
bin_vector = std::vector<int>( 8, 0 );
/* else, apply classic decimal to binary conversion */
else
{
while ( dividend >= 1 )
{
bin_num = dividend % 2;
dividend /= 2;
bin_vector.push_back( bin_num );
}
}
/* ensure that binary vector always has length 8 */
if( bin_vector.size() < 8 )
{
std::vector<int> zeros( 8 - bin_vector.size(), 0 );
bin_vector.insert( bin_vector.end(), zeros.begin(), zeros.end() );
}
/* invert numBits bits */
for ( int index = 0; index < numBits; index++ )
{
if( bin_vector[index] == 0 )
bin_vector[index] = 1;
else
bin_vector[index] = 0;
}
/* reconvert to decimal */
uchar result = 0;
for ( int i = (int) bin_vector.size() - 1; i >= 0; i-- )
result += (uchar) ( bin_vector[i] * pow( 2, i ) );
return result;
}
int main( int argc, char** argv )
{
/* get parameters from comand line */
CommandLineParser parser( argc, argv, keys );
String image_path1 = parser.get<String>( 0 );
String image_path2 = parser.get<String>( 1 );
if( image_path1.empty() || image_path2.empty() )
{
help();
return -1;
}
/* load image */
cv::Mat imageMat1 = imread( image_path1, 1 );
cv::Mat imageMat2 = imread( image_path2, 1 );
if( imageMat1.data == NULL || imageMat2.data == NULL )
{
std::cout << "Error, images could not be loaded. Please, check their paths" << std::endl;
}
/* create binary masks */
cv::Mat mask1 = Mat::ones( imageMat1.size(), CV_8UC1 );
cv::Mat mask2 = Mat::ones( imageMat2.size(), CV_8UC1 );
/* create a pointer to a BinaryDescriptor object with default parameters */
Ptr<BinaryDescriptor> bd = BinaryDescriptor::createBinaryDescriptor();
/* compute lines */
std::vector<KeyLine> keylines1, keylines2;
bd->detect( imageMat1, keylines1, mask1 );
bd->detect( imageMat2, keylines2, mask2 );
/* compute descriptors */
cv::Mat descr1, descr2;
bd->compute( imageMat1, keylines1, descr1 );
bd->compute( imageMat2, keylines2, descr2 );
/* create a BinaryDescriptorMatcher object */
Ptr<BinaryDescriptorMatcher> bdm = BinaryDescriptorMatcher::createBinaryDescriptorMatcher();
/* make a copy of descr2 mat */
Mat descr2Copy = descr1.clone();
/* randomly change some bits in original descriptors */
srand( (unsigned int) time( NULL ) );
for ( int j = 0; j < descr1.rows; j++ )
{
/* select a random column */
int randCol = rand() % 32;
/* get correspondent data */
uchar u = descr1.at<uchar>( j, randCol );
/* change bits */
for ( int k = 1; k <= 5; k++ )
{
/* copy current row to train matrix */
descr2Copy.push_back( descr1.row( j ) );
/* invert k bits */
uchar uc = invertSingleBits( u, k );
/* update current row in train matrix */
descr2Copy.at<uchar>( descr2Copy.rows - 1, randCol ) = uc;
}
}
/* prepare a structure to host matches */
std::vector<std::vector<DMatch> > matches;
/* require knn match */
bdm->knnMatch( descr1, descr2, matches, 6 );
}
/*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) 2014, Biagio Montesano, 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 <opencv2/line_descriptor.hpp>
#include "opencv2/core/utility.hpp"
#include "opencv2/core/private.hpp"
#include <opencv2/imgproc.hpp>
#include <opencv2/features2d.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace cv::line_descriptor;
using namespace std;
static const char* keys =
{ "{@image_path | | Image path }" };
static void help()
{
cout << "\nThis example shows the functionalities of lines extraction " << "furnished by BinaryDescriptor class\n"
<< "Please, run this sample using a command in the form\n" << "./example_line_descriptor_lines_extraction <path_to_input_image>" << endl;
}
int main( int argc, char** argv )
{
/* get parameters from comand line */
CommandLineParser parser( argc, argv, keys );
String image_path = parser.get<String>( 0 );
if( image_path.empty() )
{
help();
return -1;
}
/* load image */
cv::Mat imageMat = imread( image_path, 1 );
if( imageMat.data == NULL )
{
std::cout << "Error, image could not be loaded. Please, check its path" << std::endl;
return -1;
}
/* create a random binary mask */
cv::Mat mask = Mat::ones( imageMat.size(), CV_8UC1 );
/* create a pointer to a BinaryDescriptor object with deafult parameters */
Ptr<BinaryDescriptor> bd = BinaryDescriptor::createBinaryDescriptor();
/* create a structure to store extracted lines */
vector<KeyLine> lines;
/* extract lines */
cv::Mat output = imageMat.clone();
bd->detect( imageMat, lines, mask );
/* draw lines extracted from octave 0 */
if( output.channels() == 1 )
cvtColor( output, output, COLOR_GRAY2BGR );
for ( size_t i = 0; i < lines.size(); i++ )
{
KeyLine kl = lines[i];
if( kl.octave == 0)
{
/* get a random color */
int R = ( rand() % (int) ( 255 + 1 ) );
int G = ( rand() % (int) ( 255 + 1 ) );
int B = ( rand() % (int) ( 255 + 1 ) );
/* get extremes of line */
Point pt1 = Point2f( kl.startPointX, kl.startPointY );
Point pt2 = Point2f( kl.endPointX, kl.endPointY );
/* draw line */
line( output, pt1, pt2, Scalar( B, G, R ), 3 );
}
}
/* show lines on image */
imshow( "Lines", output );
waitKey();
}
/*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) 2014, Biagio Montesano, 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 <opencv2/line_descriptor.hpp>
#include "opencv2/core/utility.hpp"
#include "opencv2/core/private.hpp"
#include <opencv2/imgproc.hpp>
#include <opencv2/features2d.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace cv::line_descriptor;
using namespace std;
static const char* keys =
{ "{@image_path | | Image path }" };
static void help()
{
cout << "\nThis example shows the functionalities of lines extraction " << "furnished by BinaryDescriptor class\n"
<< "Please, run this sample using a command in the form\n" << "./example_line_descriptor_lines_extraction <path_to_input_image>" << endl;
}
int main( int argc, char** argv )
{
/* get parameters from comand line */
CommandLineParser parser( argc, argv, keys );
String image_path = parser.get<String>( 0 );
if( image_path.empty() )
{
help();
return -1;
}
/* load image */
cv::Mat imageMat = imread( image_path, 1 );
if( imageMat.data == NULL )
{
std::cout << "Error, image could not be loaded. Please, check its path" << std::endl;
return -1;
}
/* create a random binary mask */
cv::Mat mask = Mat::ones( imageMat.size(), CV_8UC1 );
/* create a pointer to a BinaryDescriptor object with deafult parameters */
Ptr<LSDDetector> bd = LSDDetector::createLSDDetector();
/* create a structure to store extracted lines */
vector<KeyLine> lines;
/* extract lines */
cv::Mat output = imageMat.clone();
bd->detect( imageMat, lines, 2, 1, mask );
/* draw lines extracted from octave 0 */
if( output.channels() == 1 )
cvtColor( output, output, COLOR_GRAY2BGR );
for ( size_t i = 0; i < lines.size(); i++ )
{
KeyLine kl = lines[i];
if( kl.octave == 0)
{
/* get a random color */
int R = ( rand() % (int) ( 255 + 1 ) );
int G = ( rand() % (int) ( 255 + 1 ) );
int B = ( rand() % (int) ( 255 + 1 ) );
/* get extremes of line */
Point pt1 = Point2f( kl.startPointX, kl.startPointY );
Point pt2 = Point2f( kl.endPointX, kl.endPointY );
/* draw line */
line( output, pt1, pt2, Scalar( B, G, R ), 3 );
}
}
/* show lines on image */
imshow( "LSD lines", output );
waitKey();
}
/*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) 2014, Biagio Montesano, 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 <opencv2/line_descriptor.hpp>
#include "opencv2/core/utility.hpp"
#include "opencv2/core/private.hpp"
#include <opencv2/imgproc.hpp>
#include <opencv2/features2d.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#define MATCHES_DIST_THRESHOLD 25
using namespace cv;
using namespace cv::line_descriptor;
static const char* keys =
{ "{@image_path1 | | Image path 1 }"
"{@image_path2 | | Image path 2 }" };
static void help()
{
std::cout << "\nThis example shows the functionalities of lines extraction " << "and descriptors computation furnished by BinaryDescriptor class\n"
<< "Please, run this sample using a command in the form\n" << "./example_line_descriptor_compute_descriptors <path_to_input_image 1>"
<< "<path_to_input_image 2>" << std::endl;
}
int main( int argc, char** argv )
{
/* get parameters from command line */
CommandLineParser parser( argc, argv, keys );
String image_path1 = parser.get<String>( 0 );
String image_path2 = parser.get<String>( 1 );
if( image_path1.empty() || image_path2.empty() )
{
help();
return -1;
}
/* load image */
cv::Mat imageMat1 = imread( image_path1, 1 );
cv::Mat imageMat2 = imread( image_path2, 1 );
if( imageMat1.data == NULL || imageMat2.data == NULL )
{
std::cout << "Error, images could not be loaded. Please, check their path" << std::endl;
}
/* create binary masks */
cv::Mat mask1 = Mat::ones( imageMat1.size(), CV_8UC1 );
cv::Mat mask2 = Mat::ones( imageMat2.size(), CV_8UC1 );
/* create a pointer to a BinaryDescriptor object with default parameters */
Ptr<BinaryDescriptor> bd = BinaryDescriptor::createBinaryDescriptor( );
/* compute lines and descriptors */
std::vector<KeyLine> keylines1, keylines2;
cv::Mat descr1, descr2;
( *bd )( imageMat1, mask1, keylines1, descr1, false, false );
( *bd )( imageMat2, mask2, keylines2, descr2, false, false );
/* select keylines from first octave and their descriptors */
std::vector<KeyLine> lbd_octave1, lbd_octave2;
Mat left_lbd, right_lbd;
for ( int i = 0; i < (int) keylines1.size(); i++ )
{
if( keylines1[i].octave == 0 )
{
lbd_octave1.push_back( keylines1[i] );
left_lbd.push_back( descr1.row( i ) );
}
}
for ( int j = 0; j < (int) keylines2.size(); j++ )
{
if( keylines2[j].octave == 0 )
{
lbd_octave2.push_back( keylines2[j] );
right_lbd.push_back( descr2.row( j ) );
}
}
/* create a BinaryDescriptorMatcher object */
Ptr<BinaryDescriptorMatcher> bdm = BinaryDescriptorMatcher::createBinaryDescriptorMatcher();
/* require match */
std::vector<DMatch> matches;
bdm->match( left_lbd, right_lbd, matches );
/* select best matches */
std::vector<DMatch> good_matches;
for ( int i = 0; i < (int) matches.size(); i++ )
{
if( matches[i].distance < MATCHES_DIST_THRESHOLD )
good_matches.push_back( matches[i] );
}
/* plot matches */
cv::Mat outImg;
cv::Mat scaled1, scaled2;
std::vector<char> mask( matches.size(), 1 );
drawLineMatches( imageMat1, lbd_octave1, imageMat2, lbd_octave2, good_matches, outImg, Scalar::all( -1 ), Scalar::all( -1 ), mask,
DrawLinesMatchesFlags::DEFAULT );
imshow( "Matches", outImg );
waitKey();
imwrite("/home/ubisum/Desktop/images/env_match/matches.jpg", outImg);
/* create an LSD detector */
Ptr<LSDDetector> lsd = LSDDetector::createLSDDetector();
/* detect lines */
std::vector<KeyLine> klsd1, klsd2;
Mat lsd_descr1, lsd_descr2;
lsd->detect( imageMat1, klsd1, 2, 2, mask1 );
lsd->detect( imageMat2, klsd2, 2, 2, mask2 );
/* compute descriptors for lines from first octave */
bd->compute( imageMat1, klsd1, lsd_descr1 );
bd->compute( imageMat2, klsd2, lsd_descr2 );
/* select lines and descriptors from first octave */
std::vector<KeyLine> octave0_1, octave0_2;
Mat leftDEscr, rightDescr;
for ( int i = 0; i < (int) klsd1.size(); i++ )
{
if( klsd1[i].octave == 1 )
{
octave0_1.push_back( klsd1[i] );
leftDEscr.push_back( lsd_descr1.row( i ) );
}
}
for ( int j = 0; j < (int) klsd2.size(); j++ )
{
if( klsd2[j].octave == 1 )
{
octave0_2.push_back( klsd2[j] );
rightDescr.push_back( lsd_descr2.row( j ) );
}
}
/* compute matches */
std::vector<DMatch> lsd_matches;
bdm->match( leftDEscr, rightDescr, lsd_matches );
/* select best matches */
good_matches.clear();
for ( int i = 0; i < (int) lsd_matches.size(); i++ )
{
if( lsd_matches[i].distance < MATCHES_DIST_THRESHOLD )
good_matches.push_back( lsd_matches[i] );
}
/* plot matches */
cv::Mat lsd_outImg;
resize( imageMat1, imageMat1, Size( imageMat1.cols / 2, imageMat1.rows / 2 ) );
resize( imageMat2, imageMat2, Size( imageMat2.cols / 2, imageMat2.rows / 2 ) );
std::vector<char> lsd_mask( matches.size(), 1 );
drawLineMatches( imageMat1, octave0_1, imageMat2, octave0_2, good_matches, lsd_outImg, Scalar::all( -1 ), Scalar::all( -1 ), lsd_mask,
DrawLinesMatchesFlags::DEFAULT );
imshow( "LSD matches", lsd_outImg );
waitKey();
}
/*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) 2014, Biagio Montesano, 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 <opencv2/line_descriptor.hpp>
#include "opencv2/core/utility.hpp"
#include "opencv2/core/private.hpp"
#include <opencv2/imgproc.hpp>
#include <opencv2/features2d.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <vector>
using namespace cv;
using namespace cv::line_descriptor;
static const std::string images[] =
{ "cameraman.jpg", "church.jpg", "church2.png", "einstein.jpg", "stuff.jpg" };
static const char* keys =
{ "{@image_path | | Image path }" };
static void help()
{
std::cout << "\nThis example shows the functionalities of radius matching " << "Please, run this sample using a command in the form\n"
<< "./example_line_descriptor_radius_matching <path_to_input_images>/" << std::endl;
}
int main( int argc, char** argv )
{
/* get parameters from comand line */
CommandLineParser parser( argc, argv, keys );
String pathToImages = parser.get < String > ( 0 );
/* create structures for hosting KeyLines and descriptors */
int num_elements = sizeof ( images ) / sizeof ( images[0] );
std::vector < Mat > descriptorsMat;
std::vector < std::vector<KeyLine> > linesMat;
/*create a pointer to a BinaryDescriptor object */
Ptr < BinaryDescriptor > bd = BinaryDescriptor::createBinaryDescriptor();
/* compute lines and descriptors */
for ( int i = 0; i < num_elements; i++ )
{
/* get path to image */
std::stringstream image_path;
image_path << pathToImages << images[i];
std::cout << image_path.str().c_str() << std::endl;
/* load image */
Mat loadedImage = imread( image_path.str().c_str(), 1 );
if( loadedImage.data == NULL )
{
std::cout << "Could not load images." << std::endl;
help();
exit( -1 );
}
/* compute lines and descriptors */
std::vector < KeyLine > lines;
Mat computedDescr;
bd->detect( loadedImage, lines );
bd->compute( loadedImage, lines, computedDescr );
descriptorsMat.push_back( computedDescr );
linesMat.push_back( lines );
}
/* compose a queries matrix */
Mat queries;
for ( size_t j = 0; j < descriptorsMat.size(); j++ )
{
if( descriptorsMat[j].rows >= 5 )
queries.push_back( descriptorsMat[j].rowRange( 0, 5 ) );
else if( descriptorsMat[j].rows > 0 && descriptorsMat[j].rows < 5 )
queries.push_back( descriptorsMat[j] );
}
std::cout << "It has been generated a matrix of " << queries.rows << " descriptors" << std::endl;
/* create a BinaryDescriptorMatcher object */
Ptr < BinaryDescriptorMatcher > bdm = BinaryDescriptorMatcher::createBinaryDescriptorMatcher();
/* populate matcher */
bdm->add( descriptorsMat );
/* compute matches */
std::vector < std::vector<DMatch> > matches;
bdm->radiusMatch( queries, matches, 30 );
std::cout << "size matches sample " << matches.size() << std::endl;
for ( int i = 0; i < (int) matches.size(); i++ )
{
for ( int j = 0; j < (int) matches[i].size(); j++ )
{
std::cout << "match: " << matches[i][j].queryIdx << " " << matches[i][j].trainIdx << " " << matches[i][j].distance << std::endl;
}
}
}
/*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) 2014, Biagio Montesano, 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 "precomp.hpp"
//using namespace cv;
namespace cv
{
namespace line_descriptor
{
Ptr<LSDDetector> LSDDetector::createLSDDetector()
{
return Ptr<LSDDetector>( new LSDDetector() );
}
/* compute Gaussian pyramid of input image */
void LSDDetector::computeGaussianPyramid( const Mat& image, int numOctaves, int scale )
{
/* clear class fields */
gaussianPyrs.clear();
/* insert input image into pyramid */
cv::Mat currentMat = image.clone();
//cv::GaussianBlur( currentMat, currentMat, cv::Size( 5, 5 ), 1 );
gaussianPyrs.push_back( currentMat );
/* fill Gaussian pyramid */
for ( int pyrCounter = 1; pyrCounter < numOctaves; pyrCounter++ )
{
/* compute and store next image in pyramid and its size */
pyrDown( currentMat, currentMat, Size( currentMat.cols / scale, currentMat.rows / scale ) );
gaussianPyrs.push_back( currentMat );
}
}
/* check lines' extremes */
inline void checkLineExtremes( cv::Vec4i& extremes, cv::Size imageSize )
{
if( extremes[0] < 0 )
extremes[0] = 0;
if( extremes[0] >= imageSize.width )
extremes[0] = imageSize.width - 1;
if( extremes[2] < 0 )
extremes[2] = 0;
if( extremes[2] >= imageSize.width )
extremes[2] = imageSize.width - 1;
if( extremes[1] < 0 )
extremes[1] = 0;
if( extremes[1] >= imageSize.height )
extremes[1] = imageSize.height - 1;
if( extremes[3] < 0 )
extremes[3] = 0;
if( extremes[3] >= imageSize.height )
extremes[3] = imageSize.height - 1;
}
/* requires line detection (only one image) */
void LSDDetector::detect( const Mat& image, CV_OUT std::vector<KeyLine>& keylines, int scale, int numOctaves, const Mat& mask )
{
if( mask.data != NULL && ( mask.size() != image.size() || mask.type() != CV_8UC1 ) )
throw std::runtime_error( "Mask error while detecting lines: please check its dimensions and that data type is CV_8UC1" );
else
detectImpl( image, keylines, numOctaves, scale, mask );
}
/* requires line detection (more than one image) */
void LSDDetector::detect( const std::vector<Mat>& images, std::vector<std::vector<KeyLine> >& keylines, int scale, int numOctaves,
const std::vector<Mat>& masks ) const
{
/* detect lines from each image */
for ( size_t counter = 0; counter < images.size(); counter++ )
{
if( masks[counter].data != NULL && ( masks[counter].size() != images[counter].size() || masks[counter].type() != CV_8UC1 ) )
throw std::runtime_error( "Masks error while detecting lines: please check their dimensions and that data types are CV_8UC1" );
else
detectImpl( images[counter], keylines[counter], numOctaves, scale, masks[counter] );
}
}
/* implementation of line detection */
void LSDDetector::detectImpl( const Mat& imageSrc, std::vector<KeyLine>& keylines, int numOctaves, int scale, const Mat& mask ) const
{
cv::Mat image;
if( imageSrc.channels() != 1 )
cvtColor( imageSrc, image, COLOR_BGR2GRAY );
else
image = imageSrc.clone();
/*check whether image depth is different from 0 */
if( image.depth() != 0 )
throw std::runtime_error( "Error, depth image!= 0" );
/* create a pointer to self */
LSDDetector *lsd = const_cast<LSDDetector*>( this );
/* compute Gaussian pyramids */
lsd->computeGaussianPyramid( image, numOctaves, scale );
/* create an LSD extractor */
cv::Ptr<cv::LineSegmentDetector> ls = cv::createLineSegmentDetector( cv::LSD_REFINE_ADV );
/* prepare a vector to host extracted segments */
std::vector<std::vector<cv::Vec4i> > lines_lsd;
/* extract lines */
for ( int i = 0; i < numOctaves; i++ )
{
std::vector<Vec4i> octave_lines;
ls->detect( gaussianPyrs[i], octave_lines );
lines_lsd.push_back( octave_lines );
}
/* create keylines */
int class_counter = -1;
for ( int j = 0; j < (int) lines_lsd.size(); j++ )
{
for ( int k = 0; k < (int) lines_lsd[j].size(); k++ )
{
KeyLine kl;
cv::Vec4i extremes = lines_lsd[j][k];
/* check data validity */
checkLineExtremes( extremes, gaussianPyrs[j].size() );
/* fill KeyLine's fields */
kl.startPointX = (float) extremes[0];
kl.startPointY = (float) extremes[1];
kl.endPointX = (float) extremes[2];
kl.endPointY = (float) extremes[3];
kl.sPointInOctaveX = (float) extremes[0];
kl.sPointInOctaveY = (float) extremes[1];
kl.ePointInOctaveX = (float) extremes[2];
kl.ePointInOctaveY = (float) extremes[3];
kl.lineLength = (float) sqrt( pow( extremes[0] - extremes[2], 2 ) + pow( extremes[1] - extremes[3], 2 ) );
/* compute number of pixels covered by line */
LineIterator li( gaussianPyrs[j], Point( extremes[0], extremes[1] ), Point( extremes[2], extremes[3] ) );
kl.numOfPixels = li.count;
kl.angle = atan2( ( kl.endPointY - kl.startPointY ), ( kl.endPointX - kl.startPointX ) );
kl.class_id = ++class_counter;
kl.octave = j;
kl.size = ( kl.endPointX - kl.startPointX ) * ( kl.endPointY - kl.startPointY );
kl.response = kl.lineLength / max( gaussianPyrs[j].cols, gaussianPyrs[j].rows );
kl.pt = Point2f( ( kl.endPointX + kl.startPointX ) / 2, ( kl.endPointY + kl.startPointY ) / 2 );
keylines.push_back( kl );
}
}
/* delete undesired KeyLines, according to input mask */
if( !mask.empty() )
{
for ( size_t keyCounter = 0; keyCounter < keylines.size(); keyCounter++ )
{
KeyLine kl = keylines[keyCounter];
if( mask.at<uchar>( (int) kl.startPointY, (int) kl.startPointX ) == 0 && mask.at<uchar>( (int) kl.endPointY, (int) kl.endPointX ) == 0 )
keylines.erase( keylines.begin() + keyCounter );
}
}
}
}
}
This source diff could not be displayed because it is too large. You can view the blob instead.
/*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) 2014, Biagio Montesano, 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 "precomp.hpp"
#define MAX_B 37
double ARRAY_RESIZE_FACTOR = 1.1; // minimum is 1.0
double ARRAY_RESIZE_ADD_FACTOR = 4; // minimum is 1
//using namespace cv;
namespace cv
{
namespace line_descriptor
{
/* constructor */
BinaryDescriptorMatcher::BinaryDescriptorMatcher()
{
dataset = new Mihasher( 256, 32 );
nextAddedIndex = 0;
numImages = 0;
descrInDS = 0;
}
/* constructor with smart pointer */
Ptr<BinaryDescriptorMatcher> BinaryDescriptorMatcher::createBinaryDescriptorMatcher()
{
return Ptr < BinaryDescriptorMatcher > ( new BinaryDescriptorMatcher() );
}
/* store new descriptors to be inserted in dataset */
void BinaryDescriptorMatcher::add( const std::vector<Mat>& descriptors )
{
for ( size_t i = 0; i < descriptors.size(); i++ )
{
descriptorsMat.push_back( descriptors[i] );
indexesMap.insert( std::pair<int, int>( nextAddedIndex, numImages ) );
nextAddedIndex += descriptors[i].rows;
numImages++;
}
}
/* store new descriptors into dataset */
void BinaryDescriptorMatcher::train()
{
if( !dataset )
dataset = new Mihasher( 256, 32 );
if( descriptorsMat.rows > 0 )
dataset->populate( descriptorsMat, descriptorsMat.rows, descriptorsMat.cols );
descrInDS = descriptorsMat.rows;
descriptorsMat.release();
}
/* clear dataset and internal data */
void BinaryDescriptorMatcher::clear()
{
descriptorsMat.release();
indexesMap.clear();
dataset = 0;
nextAddedIndex = 0;
numImages = 0;
descrInDS = 0;
}
/* retrieve Hamming distances */
void BinaryDescriptorMatcher::checkKDistances( UINT32 * numres, int k, std::vector<int> & k_distances, int row, int string_length ) const
{
int k_to_found = k;
UINT32 * numres_tmp = numres + ( ( string_length + 1 ) * row );
for ( int j = 0; j < ( string_length + 1 ) && k_to_found > 0; j++ )
{
if( ( * ( numres_tmp + j ) ) > 0 )
{
for ( int i = 0; i < (int) ( * ( numres_tmp + j ) ) && k_to_found > 0; i++ )
{
k_distances.push_back( j );
k_to_found--;
}
}
}
}
/* for every input descriptor,
find the best matching one (from one image to a set) */
void BinaryDescriptorMatcher::match( const Mat& queryDescriptors, std::vector<DMatch>& matches, const std::vector<Mat>& masks )
{
/* check data validity */
if( queryDescriptors.rows == 0 )
{
std::cout << "Error: query descriptors'matrix is empty" << std::endl;
return;
}
if( masks.size() != 0 && (int) masks.size() != numImages )
{
std::cout << "Error: the number of images in dataset is " << numImages << " but match function received " << masks.size()
<< " masks. Program will be terminated" << std::endl;
return;
}
/* add new descriptors to dataset, if needed */
train();
/* set number of requested matches to return for each query */
dataset->setK( 1 );
/* prepare structures for query */
UINT32 *results = new UINT32[queryDescriptors.rows];
UINT32 * numres = new UINT32[ ( 256 + 1 ) * ( queryDescriptors.rows )];
/* execute query */
dataset->batchquery( results, numres, queryDescriptors, queryDescriptors.rows, queryDescriptors.cols );
/* compose matches */
for ( int counter = 0; counter < queryDescriptors.rows; counter++ )
{
/* create a map iterator */
std::map<int, int>::iterator itup;
/* get info about original image of each returned descriptor */
itup = indexesMap.upper_bound( results[counter] - 1 );
itup--;
/* data validity check */
if( !masks.empty() && ( masks[itup->second].rows != queryDescriptors.rows || masks[itup->second].cols != 1 ) )
{
std::stringstream ss;
ss << "Error: mask " << itup->second << " in knnMatch function " << "should have " << queryDescriptors.rows << " and "
<< "1 column. Program will be terminated";
//throw std::runtime_error( ss.str() );
}
/* create a DMatch object if required by mask or if there is
no mask at all */
else if( masks.empty() || masks[itup->second].at < uchar > ( counter ) != 0 )
{
std::vector<int> k_distances;
checkKDistances( numres, 1, k_distances, counter, 256 );
DMatch dm;
dm.queryIdx = counter;
dm.trainIdx = results[counter] - 1;
dm.imgIdx = itup->second;
dm.distance = (float) k_distances[0];
matches.push_back( dm );
}
}
/* delete data */
delete results;
delete numres;
}
/* for every input descriptor, find the best matching one (for a pair of images) */
void BinaryDescriptorMatcher::match( const Mat& queryDescriptors, const Mat& trainDescriptors, std::vector<DMatch>& matches, const Mat& mask ) const
{
/* check data validity */
if( queryDescriptors.rows == 0 || trainDescriptors.rows == 0 )
{
std::cout << "Error: descriptors matrices cannot be void" << std::endl;
return;
}
if( !mask.empty() && ( mask.rows != queryDescriptors.rows && mask.cols != 1 ) )
{
std::cout << "Error: input mask should have " << queryDescriptors.rows << " rows and 1 column. " << "Program will be terminated" << std::endl;
return;
}
/* create a new mihasher object */
Mihasher *mh = new Mihasher( 256, 32 );
/* populate mihasher */
cv::Mat copy = trainDescriptors.clone();
mh->populate( copy, copy.rows, copy.cols );
mh->setK( 1 );
/* prepare structures for query */
UINT32 *results = new UINT32[queryDescriptors.rows];
UINT32 * numres = new UINT32[ ( 256 + 1 ) * ( queryDescriptors.rows )];
/* execute query */
mh->batchquery( results, numres, queryDescriptors, queryDescriptors.rows, queryDescriptors.cols );
/* compose matches */
for ( int counter = 0; counter < queryDescriptors.rows; counter++ )
{
/* create a DMatch object if required by mask or if there is
no mask at all */
if( mask.empty() || ( !mask.empty() && mask.at < uchar > ( counter ) != 0 ) )
{
std::vector<int> k_distances;
checkKDistances( numres, 1, k_distances, counter, 256 );
DMatch dm;
dm.queryIdx = counter;
dm.trainIdx = results[counter] - 1;
dm.imgIdx = 0;
dm.distance = (float) k_distances[0];
matches.push_back( dm );
}
}
/* delete data */
delete mh;
delete results;
delete numres;
}
/* for every input descriptor,
find the best k matching descriptors (for a pair of images) */
void BinaryDescriptorMatcher::knnMatch( const Mat& queryDescriptors, const Mat& trainDescriptors, std::vector<std::vector<DMatch> >& matches, int k,
const Mat& mask, bool compactResult ) const
{
/* check data validity */
if( queryDescriptors.rows == 0 || trainDescriptors.rows == 0 )
{
std::cout << "Error: descriptors matrices cannot be void" << std::endl;
return;
}
if( !mask.empty() && ( mask.rows != queryDescriptors.rows || mask.cols != 1 ) )
{
std::cout << "Error: input mask should have " << queryDescriptors.rows << " rows and 1 column. " << "Program will be terminated" << std::endl;
return;
}
/* create a new mihasher object */
Mihasher *mh = new Mihasher( 256, 32 );
/* populate mihasher */
cv::Mat copy = trainDescriptors.clone();
mh->populate( copy, copy.rows, copy.cols );
/* set K */
mh->setK( k );
/* prepare structures for query */
UINT32 *results = new UINT32[k * queryDescriptors.rows];
UINT32 * numres = new UINT32[ ( 256 + 1 ) * ( queryDescriptors.rows )];
/* execute query */
mh->batchquery( results, numres, queryDescriptors, queryDescriptors.rows, queryDescriptors.cols );
/* compose matches */
int index = 0;
for ( int counter = 0; counter < queryDescriptors.rows; counter++ )
{
/* initialize a vector of matches */
std::vector < DMatch > tempVec;
/* chech whether query should be ignored */
if( !mask.empty() && mask.at < uchar > ( counter ) == 0 )
{
/* if compact result is not requested, add an empty vector */
if( !compactResult )
matches.push_back( tempVec );
}
/* query matches must be considered */
else
{
std::vector<int> k_distances;
checkKDistances( numres, k, k_distances, counter, 256 );
for ( int j = index; j < index + k; j++ )
{
DMatch dm;
dm.queryIdx = counter;
dm.trainIdx = results[j] - 1;
dm.imgIdx = 0;
dm.distance = (float) k_distances[j - index];
tempVec.push_back( dm );
}
matches.push_back( tempVec );
}
/* increment pointer */
index += k;
}
/* delete data */
delete mh;
delete results;
delete numres;
}
/* for every input descriptor,
find the best k matching descriptors (from one image to a set) */
void BinaryDescriptorMatcher::knnMatch( const Mat& queryDescriptors, std::vector<std::vector<DMatch> >& matches, int k, const std::vector<Mat>& masks,
bool compactResult )
{
/* check data validity */
if( queryDescriptors.rows == 0 )
{
std::cout << "Error: descriptors matrix cannot be void" << std::endl;
return;
}
if( masks.size() != 0 && (int) masks.size() != numImages )
{
std::cout << "Error: the number of images in dataset is " << numImages << " but knnMatch function received " << masks.size()
<< " masks. Program will be terminated" << std::endl;
return;
}
/* add new descriptors to dataset, if needed */
train();
/* set number of requested matches to return for each query */
dataset->setK( k );
/* prepare structures for query */
UINT32 *results = new UINT32[k * queryDescriptors.rows];
UINT32 * numres = new UINT32[ ( 256 + 1 ) * ( queryDescriptors.rows )];
/* execute query */
dataset->batchquery( results, numres, queryDescriptors, queryDescriptors.rows, queryDescriptors.cols );
/* compose matches */
int index = 0;
for ( int counter = 0; counter < queryDescriptors.rows; counter++ )
{
/* create a void vector of matches */
std::vector < DMatch > tempVector;
/* loop over k results returned for every query */
for ( int j = index; j < index + k; j++ )
{
/* retrieve which image returned index refers to */
int currentIndex = results[j] - 1;
std::map<int, int>::iterator itup;
itup = indexesMap.upper_bound( currentIndex );
itup--;
/* data validity check */
if( !masks.empty() && ( masks[itup->second].rows != queryDescriptors.rows || masks[itup->second].cols != 1 ) )
{
std::cout << "Error: mask " << itup->second << " in knnMatch function " << "should have " << queryDescriptors.rows << " and "
<< "1 column. Program will be terminated" << std::endl;
return;
}
/* decide if, according to relative mask, returned match should be
considered */
else if( masks.size() == 0 || masks[itup->second].at < uchar > ( counter ) != 0 )
{
std::vector<int> k_distances;
checkKDistances( numres, k, k_distances, counter, 256 );
DMatch dm;
dm.queryIdx = counter;
dm.trainIdx = results[j] - 1;
dm.imgIdx = itup->second;
dm.distance = (float) k_distances[j - index];
tempVector.push_back( dm );
}
}
/* decide whether temporary vector should be saved */
if( ( tempVector.size() == 0 && !compactResult ) || tempVector.size() > 0 )
matches.push_back( tempVector );
/* increment pointer */
index += k;
}
/* delete data */
delete results;
delete numres;
}
/* for every input desciptor, find all the ones falling in a
certaing matching radius (for a pair of images) */
void BinaryDescriptorMatcher::radiusMatch( const Mat& queryDescriptors, const Mat& trainDescriptors, std::vector<std::vector<DMatch> >& matches,
float maxDistance, const Mat& mask, bool compactResult ) const
{
/* check data validity */
if( queryDescriptors.rows == 0 || trainDescriptors.rows == 0 )
{
std::cout << "Error: descriptors matrices cannot be void" << std::endl;
return;
}
if( !mask.empty() && ( mask.rows != queryDescriptors.rows && mask.cols != 1 ) )
{
std::cout << "Error: input mask should have " << queryDescriptors.rows << " rows and 1 column. " << "Program will be terminated" << std::endl;
return;
}
/* create a new Mihasher */
Mihasher* mh = new Mihasher( 256, 32 );
/* populate Mihasher */
//Mat copy = queryDescriptors.clone();
Mat copy = trainDescriptors.clone();
mh->populate( copy, copy.rows, copy.cols );
/* set K */
mh->setK( trainDescriptors.rows );
/* prepare structures for query */
UINT32 *results = new UINT32[trainDescriptors.rows * queryDescriptors.rows];
UINT32 * numres = new UINT32[ ( 256 + 1 ) * ( queryDescriptors.rows )];
/* execute query */
mh->batchquery( results, numres, queryDescriptors, queryDescriptors.rows, queryDescriptors.cols );
/* compose matches */
int index = 0;
for ( int i = 0; i < queryDescriptors.rows; i++ )
{
std::vector<int> k_distances;
checkKDistances( numres, trainDescriptors.rows, k_distances, i, 256 );
std::vector < DMatch > tempVector;
for ( int j = index; j < index + trainDescriptors.rows; j++ )
{
// if( numres[j] <= maxDistance )
if( k_distances[j - index] <= maxDistance )
{
if( mask.empty() || mask.at < uchar > ( i ) != 0 )
{
DMatch dm;
dm.queryIdx = i;
dm.trainIdx = (int) ( results[j] - 1 );
dm.imgIdx = 0;
dm.distance = (float) k_distances[j - index];
tempVector.push_back( dm );
}
}
}
/* decide whether temporary vector should be saved */
if( ( tempVector.size() == 0 && !compactResult ) || tempVector.size() > 0 )
matches.push_back( tempVector );
/* increment pointer */
index += trainDescriptors.rows;
}
/* delete data */
delete mh;
delete results;
delete numres;
}
/* for every input descriptor, find all the ones falling in a
certain matching radius (from one image to a set) */
void BinaryDescriptorMatcher::radiusMatch( const Mat& queryDescriptors, std::vector<std::vector<DMatch> >& matches, float maxDistance,
const std::vector<Mat>& masks, bool compactResult )
{
/* check data validity */
if( queryDescriptors.rows == 0 )
{
std::cout << "Error: descriptors matrices cannot be void" << std::endl;
return;
}
if( masks.size() != 0 && (int) masks.size() != numImages )
{
std::cout << "Error: the number of images in dataset is " << numImages << " but radiusMatch function received " << masks.size()
<< " masks. Program will be terminated" << std::endl;
return;
}
/* populate dataset */
train();
/* set K */
dataset->setK( descrInDS );
/* prepare structures for query */
UINT32 *results = new UINT32[descrInDS * queryDescriptors.rows];
UINT32 * numres = new UINT32[ ( 256 + 1 ) * ( queryDescriptors.rows )];
/* execute query */
dataset->batchquery( results, numres, queryDescriptors, queryDescriptors.rows, queryDescriptors.cols );
/* compose matches */
int index = 0;
for ( int counter = 0; counter < queryDescriptors.rows; counter++ )
{
std::vector < DMatch > tempVector;
for ( int j = index; j < index + descrInDS; j++ )
{
std::vector<int> k_distances;
checkKDistances( numres, descrInDS, k_distances, counter, 256 );
if( k_distances[j - index] <= maxDistance )
{
int currentIndex = results[j] - 1;
std::map<int, int>::iterator itup;
itup = indexesMap.upper_bound( currentIndex );
itup--;
/* data validity check */
if( !masks.empty() && ( masks[itup->second].rows != queryDescriptors.rows || masks[itup->second].cols != 1 ) )
{
std::cout << "Error: mask " << itup->second << " in radiusMatch function " << "should have " << queryDescriptors.rows << " and "
<< "1 column. Program will be terminated" << std::endl;
return;
}
/* add match if necessary */
else if( masks.empty() || masks[itup->second].at < uchar > ( counter ) != 0 )
{
DMatch dm;
dm.queryIdx = counter;
dm.trainIdx = results[j] - 1;
dm.imgIdx = itup->second;
dm.distance = (float) k_distances[j - index];
tempVector.push_back( dm );
}
}
}
/* decide whether temporary vector should be saved */
if( ( tempVector.size() == 0 && !compactResult ) || tempVector.size() > 0 )
matches.push_back( tempVector );
/* increment pointer */
index += descrInDS;
}
/* delete data */
delete results;
delete numres;
}
/* execute a batch query */
void BinaryDescriptorMatcher::Mihasher::batchquery( UINT32 * results, UINT32 *numres, const cv::Mat & queries, UINT32 numq, int dim1queries )
{
/* create and initialize a bitarray */
counter = new bitarray;
counter->init( N );
UINT32 *res = new UINT32[K * ( D + 1 )];
UINT64 *chunks = new UINT64[m];
UINT32 * presults = results;
UINT32 *pnumres = numres;
/* make a copy of input queries */
cv::Mat queries_clone = queries.clone();
/* set a pointer to first query (row) */
UINT8 *pq = queries_clone.ptr();
/* loop over number of descriptors */
for ( size_t i = 0; i < numq; i++ )
{
/* for every descriptor, query database */
query( presults, pnumres, pq, chunks, res );
/* move pointer to write next K indeces */
presults += K;
pnumres += B + 1;
/* move forward pointer to current row in descriptors matrix */
pq += dim1queries;
}
delete[] res;
delete[] chunks;
delete counter;
}
/* execute a single query */
void BinaryDescriptorMatcher::Mihasher::query( UINT32* results, UINT32* numres, UINT8 * Query, UINT64 *chunks, UINT32 *res )
{
/* if K == 0 that means we want everything to be processed.
So maxres = N in that case. Otherwise K limits the results processed */
UINT32 maxres = K ? K : (UINT32) N;
/* number of results so far obtained (up to a distance of s per chunk) */
UINT32 n = 0;
/* number of candidates tested with full codes (not counting duplicates) */
UINT32 nc = 0;
/* counting everything retrieved (duplicates are counted multiple times)
number of lookups (and xors) */
UINT32 nl = 0;
UINT32 nd = 0;
UINT32 *arr;
int size = 0;
UINT32 index;
int hammd;
counter->erase();
memset( numres, 0, ( B + 1 ) * sizeof ( *numres ) );
split( chunks, Query, m, mplus, b );
/* the growing search radius per substring */
int s;
/* current b: for the first mplus substrings it is b, for the rest it is (b-1) */
int curb = b;
for ( s = 0; s <= d && n < maxres; s++ )
{
for ( int k = 0; k < m; k++ )
{
if( k < mplus )
curb = b;
else
curb = b - 1;
UINT64 chunksk = chunks[k];
/* number of bit-strings with s number of 1s */
nl += xornum[s + 1] - xornum[s];
/* the bit-string with s number of 1s */
UINT64 bitstr = 0;
for ( int i = 0; i < s; i++ )
/* power[i] stores the location of the i'th 1 */
power[i] = i;
/* used for stopping criterion (location of (s+1)th 1) */
power[s] = curb + 1;
/* bit determines the 1 that should be moving to the left */
int bit = s - 1;
/* start from the left-most 1, and move it to the left until
it touches another one */
/* the loop for changing bitstr */
bool infiniteWhile = true;
while ( infiniteWhile )
{
if( bit != -1 )
{
bitstr ^= ( power[bit] == bit ) ? (UINT64) 1 << power[bit] : (UINT64) 3 << ( power[bit] - 1 );
power[bit]++;
bit--;
}
else
{ /* bit == -1 */
/* the binary code bitstr is available for processing */
arr = H[k].query( chunksk ^ bitstr, &size ); // lookup
if( size )
{ /* the corresponding bucket is not empty */
nd += size;
for ( int c = 0; c < size; c++ )
{
index = arr[c];
if( !counter->get( index ) )
{ /* if it is not a duplicate */
counter->set( index );
hammd = cv::line_descriptor::match( codes.ptr() + (UINT64) index * ( B_over_8 ), Query, B_over_8 );
nc++;
if( hammd <= D && numres[hammd] < maxres )
res[hammd * K + numres[hammd]] = index + 1;
numres[hammd]++;
}
}
}
/* end of processing */
while ( ++bit < s && power[bit] == power[bit + 1] - 1 )
{
bitstr ^= (UINT64) 1 << ( power[bit] - 1 );
power[bit] = bit;
}
if( bit == s )
break;
}
}
n = n + numres[s * m + k];
if( n >= maxres )
break;
}
}
n = 0;
for ( s = 0; s <= D && (int) n < K; s++ )
{
for ( int c = 0; c < (int) numres[s] && (int) n < K; c++ )
results[n++] = res[s * K + c];
}
}
/* constructor 2 */
BinaryDescriptorMatcher::Mihasher::Mihasher( int _B, int _m )
{
B = _B;
B_over_8 = B / 8;
m = _m;
b = (int) ceil( (double) B / m );
/* assuming that B/2 is large enough radius to include
all of the k nearest neighbors */
D = (int) ceil( B / 2.0 );
d = (int) ceil( (double) D / m );
/* mplus is the number of chunks with b bits
(m-mplus) is the number of chunks with (b-1) bits */
mplus = B - m * ( b - 1 );
xornum = new UINT32[d + 2];
xornum[0] = 0;
for ( int i = 0; i <= d; i++ )
xornum[i + 1] = xornum[i] + (UINT32) choose( b, i );
H = new SparseHashtable[m];
/* H[i].init might fail */
for ( int i = 0; i < mplus; i++ )
H[i].init( b );
for ( int i = mplus; i < m; i++ )
H[i].init( b - 1 );
}
/* K setter */
void BinaryDescriptorMatcher::Mihasher::setK( int _K )
{
K = _K;
}
/* desctructor */
BinaryDescriptorMatcher::Mihasher::~Mihasher()
{
delete[] xornum;
delete[] H;
}
/* populate tables */
void BinaryDescriptorMatcher::Mihasher::populate( cv::Mat & _codes, UINT32 _N, int dim1codes )
{
N = _N;
codes = _codes;
UINT64 * chunks = new UINT64[m];
UINT8 * pcodes = codes.ptr();
for ( UINT64 i = 0; i < N; i++, pcodes += dim1codes )
{
split( chunks, pcodes, m, mplus, b );
for ( int k = 0; k < m; k++ )
H[k].insert( chunks[k], (UINT32) i );
if( i % (int) ceil( N / 1000.0 ) == 0 )
fflush (stdout);
}
delete[] chunks;
}
/* constructor */
BinaryDescriptorMatcher::SparseHashtable::SparseHashtable()
{
table = NULL;
size = 0;
b = 0;
}
/* initializer */
int BinaryDescriptorMatcher::SparseHashtable::init( int _b )
{
b = _b;
if( b < 5 || b > MAX_B || b > (int) ( sizeof(UINT64) * 8 ) )
return 1;
size = UINT64_1 << ( b - 5 ); // size = 2 ^ b
table = (BucketGroup*) calloc( size, sizeof(BucketGroup) );
return 0;
}
/* destructor */
BinaryDescriptorMatcher::SparseHashtable::~SparseHashtable()
{
free (table);
}
/* insert data */
void BinaryDescriptorMatcher::SparseHashtable::insert( UINT64 index, UINT32 data )
{
table[index >> 5].insert( (int) ( index % 32 ), data );
}
/* query data */
UINT32* BinaryDescriptorMatcher::SparseHashtable::query( UINT64 index, int *Size )
{
return table[index >> 5].query( (int) ( index % 32 ), Size );
}
/* constructor */
BinaryDescriptorMatcher::BucketGroup::BucketGroup()
{
empty = 0;
group = std::vector < uint32_t > ( 2, 0 );
}
/* destructor */
BinaryDescriptorMatcher::BucketGroup::~BucketGroup()
{
}
void BinaryDescriptorMatcher::BucketGroup::insert_value( std::vector<uint32_t>& vec, int index, UINT32 data )
{
if( vec.size() > 1 )
{
if( vec[0] == vec[1] )
{
vec[1] = (UINT32) ceil( vec[0] * 1.1 );
for ( int i = 0; i < (int) ( 2 + vec[1] - vec.size() ); i++ )
vec.push_back( 0 );
}
vec.insert( vec.begin() + 2 + index, data );
vec[2 + index] = data;
vec[0]++;
}
else
{
vec = std::vector < uint32_t > ( 3, 0 );
vec[0] = 1;
vec[1] = 1;
vec[2] = data;
}
}
void BinaryDescriptorMatcher::BucketGroup::push_value( std::vector<uint32_t>& vec, UINT32 Data )
{
if( vec.size() > 0 )
{
if( vec[0] == vec[1] )
{
vec[1] = (UINT32) std::max( ceil( vec[1] * ARRAY_RESIZE_FACTOR ), vec[1] + ARRAY_RESIZE_ADD_FACTOR );
for ( int i = 0; i < (int) ( 2 + vec[1] - vec.size() ); i++ )
vec.push_back( 0 );
}
vec[2 + vec[0]] = Data;
vec[0]++;
}
else
{
vec = std::vector < uint32_t > ( 2 + ARRAY_RESIZE_ADD_FACTOR, 0 );
vec[0] = 1;
vec[1] = 1;
vec[2] = Data;
}
}
/* insert data into the bucket */
void BinaryDescriptorMatcher::BucketGroup::insert( int subindex, UINT32 data )
{
if( group.size() == 0 )
{
push_value( group, 0 );
}
UINT32 lowerbits = ( (UINT32) 1 << subindex ) - 1;
int end = popcnt( empty & lowerbits );
if( ! ( empty & ( (UINT32) 1 << subindex ) ) )
{
insert_value( group, end, group[end + 2] );
empty |= (UINT32) 1 << subindex;
}
int totones = popcnt( empty );
insert_value( group, totones + 1 + group[2 + end + 1], data );
for ( int i = end + 1; i < totones + 1; i++ )
group[2 + i]++;
}
/* perform a query to the bucket */
UINT32* BinaryDescriptorMatcher::BucketGroup::query( int subindex, int *size )
{
if( empty & ( (UINT32) 1 << subindex ) )
{
UINT32 lowerbits = ( (UINT32) 1 << subindex ) - 1;
int end = popcnt( empty & lowerbits );
int totones = popcnt( empty );
*size = group[2 + end + 1] - group[2 + end];
return & ( * ( group.begin() + 2 + totones + 1 + (int) group[2 + end] ) );
}
else
{
*size = 0;
return NULL;
}
}
}
}
/*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) 2014, Mohammad Norouzi, Ali Punjani, David J. Fleet,
// 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_BITARRAY_HPP
#define __OPENCV_BITARRAY_HPP
#ifdef _WIN32
#pragma warning( disable : 4267 )
#endif
#include "types.hpp"
#include <stdio.h>
#include <math.h>
#include <string.h>
/* class defining a sequence of bits */
class bitarray
{
public:
/* pointer to bits sequence and sequence's length */
UINT32 *arr;
UINT32 length;
/* constructor setting default values */
bitarray()
{
arr = NULL;
length = 0;
}
/* constructor setting sequence's length */
bitarray( UINT64 _bits )
{
init( _bits );
}
/* initializer of private fields */
void init( UINT64 _bits )
{
length = (UINT32) ceil( _bits / 32.00 );
arr = new UINT32[length];
erase();
}
/* destructor */
~bitarray()
{
if( arr )
delete[] arr;
}
inline void flip( UINT64 index )
{
arr[index >> 5] ^= ( (UINT32) 0x01 ) << ( index % 32 );
}
inline void set( UINT64 index )
{
arr[index >> 5] |= ( (UINT32) 0x01 ) << ( index % 32 );
}
inline UINT8 get( UINT64 index )
{
return ( arr[index >> 5] & ( ( (UINT32) 0x01 ) << ( index % 32 ) ) ) != 0;
}
/* reserve menory for an UINT32 */
inline void erase()
{
memset( arr, 0, sizeof(UINT32) * length );
}
};
#endif
/*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) 2014, Mohammad Norouzi, Ali Punjani, David J. Fleet,
// 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_BITOPTS_HPP
#define __OPENCV_BITOPTS_HPP
#include "precomp.hpp"
#ifdef _WIN32
# include <intrin.h>
# define popcnt __popcnt
# define popcntll __popcnt64
# pragma warning( disable : 4267 )
#else
# define popcntll __builtin_popcountll
# define popcnt __builtin_popcount
#endif
/* LUT */
const int lookup[] =
{ 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2,
3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3,
3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3,
4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 2, 3, 3, 4,
3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 4, 5, 5, 6, 5,
6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8 };
namespace cv
{
namespace line_descriptor
{
/*matching function */
inline int match( UINT8*P, UINT8*Q, int codelb )
{
switch ( codelb )
{
case 4: // 32 bit
return popcnt( *(UINT32*) P ^ *(UINT32*) Q );
break;
case 8: // 64 bit
return (int) popcntll( ( (UINT64*) P )[0] ^ ( (UINT64*) Q )[0] );
break;
case 16: // 128 bit
return (int) ( popcntll( ( (UINT64*) P )[0] ^ ( (UINT64*) Q )[0] ) + popcntll( ( (UINT64*) P )[1] ^ ( (UINT64*) Q )[1] ) );
break;
case 32: // 256 bit
return (int) ( popcntll( ( (UINT64*) P )[0] ^ ( (UINT64*) Q )[0] ) + popcntll( ( (UINT64*) P )[1] ^ ( (UINT64*) Q )[1] )
+ popcntll( ( (UINT64*) P )[2] ^ ( (UINT64*) Q )[2] ) + popcntll( ( (UINT64*) P )[3] ^ ( (UINT64*) Q )[3] ) );
break;
case 64: // 512 bit
return (int) ( popcntll( ( (UINT64*) P )[0] ^ ( (UINT64*) Q )[0] ) + popcntll( ( (UINT64*) P )[1] ^ ( (UINT64*) Q )[1] )
+ popcntll( ( (UINT64*) P )[2] ^ ( (UINT64*) Q )[2] ) + popcntll( ( (UINT64*) P )[3] ^ ( (UINT64*) Q )[3] )
+ popcntll( ( (UINT64*) P )[4] ^ ( (UINT64*) Q )[4] ) + popcntll( ( (UINT64*) P )[5] ^ ( (UINT64*) Q )[5] )
+ popcntll( ( (UINT64*) P )[6] ^ ( (UINT64*) Q )[6] ) + popcntll( ( (UINT64*) P )[7] ^ ( (UINT64*) Q )[7] ) );
break;
default:
int output = 0;
for ( int i = 0; i < codelb; i++ )
output += lookup[P[i] ^ Q[i]];
return output;
break;
}
}
/* splitting function (b <= 64) */
inline void split( UINT64 *chunks, UINT8 *code, int m, int mplus, int b )
{
UINT64 temp = 0x0;
int nbits = 0;
int nbyte = 0;
UINT64 mask = b == 64 ? 0xFFFFFFFFFFFFFFFFLLU : ( ( UINT64_1 << b ) - UINT64_1 );
for ( int i = 0; i < m; i++ )
{
while ( nbits < b )
{
temp |= ( (UINT64) code[nbyte++] << nbits );
nbits += 8;
}
chunks[i] = temp & mask;
temp = b == 64 ? 0x0 : temp >> b;
nbits -= b;
if( i == mplus - 1 )
{
b--; /* b <= 63 */
mask = ( ( UINT64_1 << b ) - UINT64_1 );
}
}
}
/* generates the next binary code (in alphabetical order) with the
same number of ones as the input x. Taken from
http://www.geeksforgeeks.org/archives/10375 */
inline UINT64 next_set_of_n_elements( UINT64 x )
{
UINT64 smallest, ripple, new_smallest;
smallest = x & -(signed) x;
ripple = x + smallest;
new_smallest = x ^ ripple;
new_smallest = new_smallest / smallest;
new_smallest >>= 2;
return ripple | new_smallest;
}
/* print code */
inline void print_code( UINT64 tmp, int b )
{
for ( long long int j = ( b - 1 ); j >= 0; j-- )
{
printf( "%llu", (long long int) tmp / (UINT64) ( 1 << j ) );
tmp = tmp - ( tmp / (UINT64) ( 1 << j ) ) * (UINT64) ( 1 << j );
}
printf( "\n" );
}
inline UINT64 choose( int n, int r )
{
UINT64 nchooser = 1;
for ( int k = 0; k < r; k++ )
{
nchooser *= n - k;
nchooser /= k + 1;
}
return nchooser;
}
}
}
#endif
/*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) 2014, Biagio Montesano, 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 "precomp.hpp"
namespace cv
{
namespace line_descriptor
{
/* draw matches between two images */
void drawLineMatches( const Mat& img1, const std::vector<KeyLine>& keylines1, const Mat& img2, const std::vector<KeyLine>& keylines2,
const std::vector<DMatch>& matches1to2, Mat& outImg, const Scalar& matchColor, const Scalar& singleLineColor,
const std::vector<char>& matchesMask, int flags )
{
if(img1.type() != img2.type())
{
std::cout << "Input images have different types" << std::endl;
CV_Assert(img1.type() == img2.type());
}
/* initialize output matrix (if necessary) */
if( flags == DrawLinesMatchesFlags::DEFAULT )
{
/* check how many rows are necessary for output matrix */
int totalRows = img1.rows >= img2.rows ? img1.rows : img2.rows;
/* initialize output matrix */
outImg = Mat::zeros( totalRows, img1.cols + img2.cols, img1.type() );
}
/* initialize random seed: */
srand( (unsigned int) time( NULL ) );
Scalar singleLineColorRGB;
if( singleLineColor == Scalar::all( -1 ) )
{
int R = ( rand() % (int) ( 255 + 1 ) );
int G = ( rand() % (int) ( 255 + 1 ) );
int B = ( rand() % (int) ( 255 + 1 ) );
singleLineColorRGB = Scalar( R, G, B );
}
else
singleLineColorRGB = singleLineColor;
/* copy input images to output images */
Mat roi_left( outImg, Rect( 0, 0, img1.cols, img1.rows ) );
Mat roi_right( outImg, Rect( img1.cols, 0, img2.cols, img2.rows ) );
img1.copyTo( roi_left );
img2.copyTo( roi_right );
/* get columns offset */
int offset = img1.cols;
/* if requested, draw lines from both images */
if( flags != DrawLinesMatchesFlags::NOT_DRAW_SINGLE_LINES )
{
for ( size_t i = 0; i < keylines1.size(); i++ )
{
KeyLine k1 = keylines1[i];
//line( outImg, Point2f( k1.startPointX, k1.startPointY ), Point2f( k1.endPointX, k1.endPointY ), singleLineColorRGB, 2 );
line( outImg, Point2f( k1.sPointInOctaveX, k1.sPointInOctaveY ), Point2f( k1.ePointInOctaveX, k1.ePointInOctaveY ), singleLineColorRGB, 2 );
}
for ( size_t j = 0; j < keylines2.size(); j++ )
{
KeyLine k2 = keylines2[j];
line( outImg, Point2f( k2.sPointInOctaveX + offset, k2.sPointInOctaveY ), Point2f( k2.ePointInOctaveX + offset, k2.ePointInOctaveY ), singleLineColorRGB, 2 );
}
}
/* draw matches */
for ( size_t counter = 0; counter < matches1to2.size(); counter++ )
{
if( matchesMask[counter] != 0 )
{
DMatch dm = matches1to2[counter];
KeyLine left = keylines1[dm.queryIdx];
KeyLine right = keylines2[dm.trainIdx];
Scalar matchColorRGB;
if( matchColor == Scalar::all( -1 ) )
{
int R = ( rand() % (int) ( 255 + 1 ) );
int G = ( rand() % (int) ( 255 + 1 ) );
int B = ( rand() % (int) ( 255 + 1 ) );
matchColorRGB = Scalar( R, G, B );
if( singleLineColor == Scalar::all( -1 ) )
singleLineColorRGB = matchColorRGB;
}
else
matchColorRGB = matchColor;
/* draw lines if necessary */
// line( outImg, Point2f( left.startPointX, left.startPointY ), Point2f( left.endPointX, left.endPointY ), singleLineColorRGB, 2 );
//
// line( outImg, Point2f( right.startPointX + offset, right.startPointY ), Point2f( right.endPointX + offset, right.endPointY ), singleLineColorRGB,
// 2 );
//
// /* link correspondent lines */
// line( outImg, Point2f( left.startPointX, left.startPointY ), Point2f( right.startPointX + offset, right.startPointY ), matchColorRGB, 1 );
line( outImg, Point2f( left.sPointInOctaveX, left.sPointInOctaveY ), Point2f( left.ePointInOctaveX, left.ePointInOctaveY ), singleLineColorRGB, 2 );
line( outImg, Point2f( right.sPointInOctaveX + offset, right.sPointInOctaveY ), Point2f( right.ePointInOctaveX + offset, right.ePointInOctaveY ), singleLineColorRGB,
2 );
/* link correspondent lines */
line( outImg, Point2f( left.sPointInOctaveX, left.sPointInOctaveY ), Point2f( right.sPointInOctaveX + offset, right.sPointInOctaveY ), matchColorRGB, 1 );
}
}
}
/* draw extracted lines on original image */
void drawKeylines( const Mat& image, const std::vector<KeyLine>& keylines, Mat& outImage, const Scalar& color, int flags )
{
if( flags == DrawLinesMatchesFlags::DEFAULT )
outImage = image.clone();
for ( size_t i = 0; i < keylines.size(); i++ )
{
/* decide lines' color */
Scalar lineColor;
if( color != Scalar::all( -1 ) )
{
int R = ( rand() % (int) ( 255 + 1 ) );
int G = ( rand() % (int) ( 255 + 1 ) );
int B = ( rand() % (int) ( 255 + 1 ) );
lineColor = Scalar( R, G, B );
}
else
lineColor = color;
/* get line */
KeyLine k = keylines[i];
/* draw line */
line( outImage, Point2f( k.startPointX, k.startPointY ), Point2f( k.endPointX, k.endPointY ), lineColor, 1 );
}
}
}
}
/*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) 2014, Biagio Montesano, 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 "precomp.hpp"
namespace cv
{
namespace line_descriptor
{
CV_INIT_ALGORITHM( BinaryDescriptor, "BINARY.DESCRIPTOR", );
CV_INIT_ALGORITHM( BinaryDescriptorMatcher, "BINARY.DESCRIPTOR.MATCHER", );
CV_INIT_ALGORITHM( LSDDetector, "LSDDETECTOR", );
bool initModule_line_descriptor( void )
{
bool all = true;
all &= !BinaryDescriptor_info_auto.name().empty();
all &= !BinaryDescriptorMatcher_info_auto.name().empty();
all &= !LSDDetector_info_auto.name().empty();
return all;
}
}
}
/*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) 2014, Biagio Montesano, 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__
#ifdef _WIN32
#pragma warning( disable : 4267 )
#endif
#define _USE_MATH_DEFINES
#include <algorithm>
#include "opencv2/core/utility.hpp"
#include "opencv2/core/private.hpp"
#include <opencv2/imgproc.hpp>
#include <opencv2/features2d.hpp>
#include <opencv2/highgui.hpp>
#include "opencv2/core.hpp"
#include <iostream>
#include <map>
#include <stdio.h>
#include <string.h>
#include <cmath>
#include <algorithm>
#include <bitset>
#include <time.h>
#include <stdexcept>
#include <sstream>
#include <vector>
#include "bitarray.hpp"
#include "bitops.hpp"
#include "types.hpp"
#include "opencv2/line_descriptor.hpp"
#endif
/*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) 2014, Mohammad Norouzi, Ali Punjani, David J. Fleet,
// 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 <inttypes.h>
#ifndef __OPENCV_TYPES_HPP
#define __OPENCV_TYPES_HPP
#ifdef _WIN32
#pragma warning( disable : 4267 )
#endif
/* define data types */
typedef uint64_t UINT64;
typedef uint32_t UINT32;
typedef uint16_t UINT16;
typedef uint8_t UINT8;
/* define constants */
#define UINT64_1 ((UINT64)0x01)
#define UINT32_1 ((UINT32)0x01)
#endif
/*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) 2014, Biagio Montesano, 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"
using namespace cv;
using namespace cv::line_descriptor;
/****************************************************************************************\
* Regression tests for line detector comparing keylines. *
\****************************************************************************************/
const std::string LINE_DESCRIPTOR_DIR = "line_descriptor";
const std::string IMAGE_FILENAME = "cameraman.jpg";
const std::string DESCRIPTORS_DIR = LINE_DESCRIPTOR_DIR + "/descriptors";
template<class Distance>
class CV_BD_DescriptorsTest : public cvtest::BaseTest
{
public:
typedef typename Distance::ValueType ValueType;
typedef typename Distance::ResultType DistanceType;
CV_BD_DescriptorsTest( std::string fs, DistanceType _maxDist ): maxDist(_maxDist)
{
bd = BinaryDescriptor::createBinaryDescriptor();
fs_name = fs;
}
protected:
// void compareDescriptors( const Mat& validDescriptors, const Mat& calcDescriptors );
// void createVecFromMat( Mat& inputMat, std::vector<KeyLine>& output );
// virtual bool writeDescriptors( Mat& descs );
// virtual Mat readDescriptors();
// void emptyDataTest();
// void regressionTest();
// virtual void run( int );
Ptr<BinaryDescriptor> bd;
std::string fs_name;
const DistanceType maxDist;
Distance distance;
//};
void compareDescriptors( const Mat& validDescriptors, const Mat& calcDescriptors )
{
if( validDescriptors.size != calcDescriptors.size || validDescriptors.type() != calcDescriptors.type() )
{
ts->printf( cvtest::TS::LOG, "Valid and computed descriptors matrices must have the same size and type.\n" );
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
return;
}
CV_Assert( validDescriptors.type() == CV_8U );
int dimension = validDescriptors.cols;
DistanceType curMaxDist = std::numeric_limits<DistanceType>::min();
for ( int y = 0; y < validDescriptors.rows; y++ )
{
DistanceType dist = distance( validDescriptors.ptr<ValueType>( y ), calcDescriptors.ptr<ValueType>( y ), dimension );
if( dist > curMaxDist )
curMaxDist = dist;
}
std::stringstream ss;
ss << "Max distance between valid and computed descriptors " << curMaxDist;
if( curMaxDist < maxDist )
ss << "." << std::endl;
else
{
ss << ">" << maxDist << " - bad accuracy!" << "\n";
ts->set_failed_test_info( cvtest::TS::FAIL_BAD_ACCURACY );
}
ts->printf( cvtest::TS::LOG, ss.str().c_str() );
}
Mat readDescriptors()
{
Mat descriptors;
FileStorage fs( std::string( ts->get_data_path() ) + LINE_DESCRIPTOR_DIR + "/descriptors/" + fs_name, FileStorage::READ );
fs["descriptors"] >> descriptors;
return descriptors;
}
bool writeDescriptors( Mat& descs )
{
FileStorage fs( std::string( ts->get_data_path() ) + LINE_DESCRIPTOR_DIR + "/descriptors/" + fs_name, FileStorage::WRITE );
fs << "descriptors" << descs;
return true;
}
void createMatFromVec( const std::vector<KeyLine>& linesVec, Mat& output )
{
output = Mat( (int) linesVec.size(), 17, CV_32FC1 );
for ( int i = 0; i < (int) linesVec.size(); i++ )
{
std::vector<float> klData;
KeyLine kl = linesVec[i];
klData.push_back( kl.angle );
klData.push_back( (float) kl.class_id );
klData.push_back( kl.ePointInOctaveX );
klData.push_back( kl.ePointInOctaveY );
klData.push_back( kl.endPointX );
klData.push_back( kl.endPointY );
klData.push_back( kl.lineLength );
klData.push_back( (float) kl.numOfPixels );
klData.push_back( (float) kl.octave );
klData.push_back( kl.pt.x );
klData.push_back( kl.pt.y );
klData.push_back( kl.response );
klData.push_back( kl.sPointInOctaveX );
klData.push_back( kl.sPointInOctaveY );
klData.push_back( kl.size );
klData.push_back( kl.startPointX );
klData.push_back( kl.startPointY );
float* pointerToRow = output.ptr<float>( i );
for ( int j = 0; j < 17; j++ )
{
*pointerToRow = klData[j];
pointerToRow++;
}
}
}
void createVecFromMat( Mat& inputMat, std::vector<KeyLine>& output )
{
for ( int i = 0; i < inputMat.rows; i++ )
{
std::vector<float> tempFloat;
KeyLine kl;
float* pointerToRow = inputMat.ptr<float>( i );
for ( int j = 0; j < 17; j++ )
{
tempFloat.push_back( *pointerToRow );
pointerToRow++;
}
kl.angle = tempFloat[0];
kl.class_id = (int) tempFloat[1];
kl.ePointInOctaveX = tempFloat[2];
kl.ePointInOctaveY = tempFloat[3];
kl.endPointX = tempFloat[4];
kl.endPointY = tempFloat[5];
kl.lineLength = tempFloat[6];
kl.numOfPixels = (int) tempFloat[7];
kl.octave = (int) tempFloat[8];
kl.pt.x = tempFloat[9];
kl.pt.y = tempFloat[10];
kl.response = tempFloat[11];
kl.sPointInOctaveX = tempFloat[12];
kl.sPointInOctaveY = tempFloat[13];
kl.size = tempFloat[14];
kl.startPointX = tempFloat[15];
kl.startPointY = tempFloat[16];
output.push_back( kl );
}
}
void emptyDataTest()
{
assert( bd );
// One image.
Mat image;
std::vector<KeyLine> keypoints;
Mat descriptors;
try
{
bd->compute( image, keypoints, descriptors );
}
catch ( ... )
{
ts->printf( cvtest::TS::LOG, "compute() on empty image and empty keypoints must not generate exception (1).\n" );
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
}
image.create( 50, 50, CV_8UC3 );
try
{
bd->compute( image, keypoints, descriptors );
}
catch ( ... )
{
ts->printf( cvtest::TS::LOG, "compute() on nonempty image and empty keylines must not generate exception (1).\n" );
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
}
// Several images.
std::vector<Mat> images;
std::vector<std::vector<KeyLine> > keylinesCollection;
std::vector<Mat> descriptorsCollection;
try
{
bd->compute( images, keylinesCollection, descriptorsCollection );
}
catch ( ... )
{
ts->printf( cvtest::TS::LOG, "compute() on empty images and empty keylines collection must not generate exception (2).\n" );
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
}
}
void regressionTest()
{
assert( bd );
// Read the test image.
std::string imgFilename = std::string( ts->get_data_path() ) + LINE_DESCRIPTOR_DIR + "/" + IMAGE_FILENAME;
Mat img = imread( imgFilename );
if( img.empty() )
{
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;
}
std::vector<KeyLine> keylines;
FileStorage fs( std::string( ts->get_data_path() ) + LINE_DESCRIPTOR_DIR + "/detectors/edl_detector_keylines_cameraman.yaml", FileStorage::READ );
if( fs.isOpened() )
{
//read( fs.getFirstTopLevelNode(), keypoints );
/* load keylines */
Mat loadedKeylines;
fs["keylines"] >> loadedKeylines;
createVecFromMat( loadedKeylines, keylines );
/* compute descriptors */
Mat calcDescriptors;
double t = (double) getTickCount();
bd->compute( img, keylines, calcDescriptors );
t = getTickCount() - t;
ts->printf( cvtest::TS::LOG, "\nAverage time of computing one descriptor = %g ms.\n",
t / ( (double) getTickFrequency() * 1000. ) / calcDescriptors.rows );
if( calcDescriptors.rows != (int) keylines.size() )
{
ts->printf( cvtest::TS::LOG, "Count of computed descriptors and keylines count must be equal.\n" );
ts->printf( cvtest::TS::LOG, "Count of keylines is %d.\n", (int) keylines.size() );
ts->printf( cvtest::TS::LOG, "Count of computed descriptors is %d.\n", calcDescriptors.rows );
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
return;
}
if( calcDescriptors.cols != bd->descriptorSize() / 8 || calcDescriptors.type() != bd->descriptorType() )
{
ts->printf( cvtest::TS::LOG, "Incorrect descriptor size or descriptor type.\n" );
ts->printf( cvtest::TS::LOG, "Expected size is %d.\n", bd->descriptorSize() );
ts->printf( cvtest::TS::LOG, "Calculated size is %d.\n", calcDescriptors.cols );
ts->printf( cvtest::TS::LOG, "Expected type is %d.\n", bd->descriptorType() );
ts->printf( cvtest::TS::LOG, "Calculated type is %d.\n", calcDescriptors.type() );
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
return;
}
// TODO read and write descriptor extractor parameters and check them
Mat validDescriptors = readDescriptors();
if( !validDescriptors.empty() )
compareDescriptors( validDescriptors, calcDescriptors );
else
{
if( !writeDescriptors( calcDescriptors ) )
{
ts->printf( cvtest::TS::LOG, "Descriptors can not be written.\n" );
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
return;
}
}
}
else
{
ts->printf( cvtest::TS::LOG, "Compute and write keylines.\n" );
fs.open( std::string( ts->get_data_path() ) + LINE_DESCRIPTOR_DIR + "/detectors/edl_detector_keylines_cameraman.yaml", FileStorage::WRITE );
if( fs.isOpened() )
{
bd->detect( img, keylines );
Mat keyLinesToYaml;
createMatFromVec( keylines, keyLinesToYaml );
fs << "keylines" << keyLinesToYaml;
}
else
{
ts->printf( cvtest::TS::LOG, "File for writting keylines can not be opened.\n" );
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
return;
}
}
}
void run( int )
{
if( !bd )
{
ts->printf( cvtest::TS::LOG, "Feature detector is empty.\n" );
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
return;
}
emptyDataTest();
regressionTest();
ts->set_failed_test_info( cvtest::TS::OK );
}
private:
CV_BD_DescriptorsTest& operator=( const CV_BD_DescriptorsTest& )
{
return *this;
}
};
/****************************************************************************************\
* Tests registrations *
\****************************************************************************************/
TEST( BinaryDescriptor_Descriptors, regression )
{
CV_BD_DescriptorsTest<Hamming> test( std::string( "lbd_descriptors_cameraman" ), 1 );
test.safe_run();
}
/*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) 2014, Biagio Montesano, 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"
using namespace cv;
using namespace cv::line_descriptor;
/****************************************************************************************\
* Regression tests for line detector comparing keylines. *
\****************************************************************************************/
const std::string LINE_DESCRIPTOR_DIR = "line_descriptor";
const std::string IMAGE_FILENAME = "cameraman.jpg";
const std::string DETECTOR_DIR = LINE_DESCRIPTOR_DIR + "/detectors";
class CV_BinaryDescriptorDetectorTest : public cvtest::BaseTest
{
public:
CV_BinaryDescriptorDetectorTest( std::string fs )
{
bd = BinaryDescriptor::createBinaryDescriptor();
fs_name = fs;
}
protected:
bool isSimilarKeylines( const KeyLine& k1, const KeyLine& k2 );
void compareKeylineSets( const std::vector<KeyLine>& validKeylines, const std::vector<KeyLine>& calcKeylines );
void createMatFromVec( const std::vector<KeyLine>& linesVec, Mat& output );
void createVecFromMat( Mat& inputMat, std::vector<KeyLine>& output );
void emptyDataTest();
void regressionTest();
virtual void run( int );
Ptr<BinaryDescriptor> bd;
std::string fs_name;
};
void CV_BinaryDescriptorDetectorTest::emptyDataTest()
{
/* one image */
Mat image;
std::vector<KeyLine> keylines;
try
{
bd->detect( image, keylines );
}
catch ( ... )
{
ts->printf( cvtest::TS::LOG, "detect() on empty image must return empty keylines vector (1).\n" );
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
}
if( !keylines.empty() )
{
ts->printf( cvtest::TS::LOG, "detect() on empty image must return empty keylines vector (1).\n" );
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
return;
}
/* more than one image */
std::vector<Mat> images;
std::vector<std::vector<KeyLine> > keylineCollection;
try
{
bd->detect( images, keylineCollection );
}
catch ( ... )
{
ts->printf( cvtest::TS::LOG, "detect() on empty image vector must not generate exception (2).\n" );
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
}
}
void CV_BinaryDescriptorDetectorTest::createMatFromVec( const std::vector<KeyLine>& linesVec, Mat& output )
{
output = Mat( (int) linesVec.size(), 17, CV_32FC1 );
for ( int i = 0; i < (int) linesVec.size(); i++ )
{
std::vector<float> klData;
KeyLine kl = linesVec[i];
klData.push_back( kl.angle );
klData.push_back( (float) kl.class_id );
klData.push_back( kl.ePointInOctaveX );
klData.push_back( kl.ePointInOctaveY );
klData.push_back( kl.endPointX );
klData.push_back( kl.endPointY );
klData.push_back( kl.lineLength );
klData.push_back( (float) kl.numOfPixels );
klData.push_back( (float) kl.octave );
klData.push_back( kl.pt.x );
klData.push_back( kl.pt.y );
klData.push_back( kl.response );
klData.push_back( kl.sPointInOctaveX );
klData.push_back( kl.sPointInOctaveY );
klData.push_back( kl.size );
klData.push_back( kl.startPointX );
klData.push_back( kl.startPointY );
float* pointerToRow = output.ptr<float>( i );
for ( int j = 0; j < 17; j++ )
{
*pointerToRow = klData[j];
pointerToRow++;
}
}
}
void CV_BinaryDescriptorDetectorTest::createVecFromMat( Mat& inputMat, std::vector<KeyLine>& output )
{
for ( int i = 0; i < inputMat.rows; i++ )
{
std::vector<float> tempFloat;
KeyLine kl;
float* pointerToRow = inputMat.ptr<float>( i );
for ( int j = 0; j < 17; j++ )
{
tempFloat.push_back( *pointerToRow );
pointerToRow++;
}
kl.angle = tempFloat[0];
kl.class_id = (int) tempFloat[1];
kl.ePointInOctaveX = tempFloat[2];
kl.ePointInOctaveY = tempFloat[3];
kl.endPointX = tempFloat[4];
kl.endPointY = tempFloat[5];
kl.lineLength = tempFloat[6];
kl.numOfPixels = (int) tempFloat[7];
kl.octave = (int) tempFloat[8];
kl.pt.x = tempFloat[9];
kl.pt.y = tempFloat[10];
kl.response = tempFloat[11];
kl.sPointInOctaveX = tempFloat[12];
kl.sPointInOctaveY = tempFloat[13];
kl.size = tempFloat[14];
kl.startPointX = tempFloat[15];
kl.startPointY = tempFloat[16];
output.push_back( kl );
}
}
bool CV_BinaryDescriptorDetectorTest::isSimilarKeylines( const KeyLine& k1, const KeyLine& k2 )
{
const float maxPtDif = 1.f;
const float maxSizeDif = 1.f;
const float maxAngleDif = 2.f;
const float maxResponseDif = 0.1f;
float dist = (float) norm( k1.pt - k2.pt );
return ( dist < maxPtDif && fabs( k1.size - k2.size ) < maxSizeDif && abs( k1.angle - k2.angle ) < maxAngleDif
&& abs( k1.response - k2.response ) < maxResponseDif && k1.octave == k2.octave && k1.class_id == k2.class_id );
}
void CV_BinaryDescriptorDetectorTest::compareKeylineSets( const std::vector<KeyLine>& validKeylines, const std::vector<KeyLine>& calcKeylines )
{
const float maxCountRatioDif = 0.01f;
// Compare counts of validation and calculated keylines.
float countRatio = (float) validKeylines.size() / (float) calcKeylines.size();
if( countRatio < 1 - maxCountRatioDif || countRatio > 1.f + maxCountRatioDif )
{
ts->printf( cvtest::TS::LOG, "Bad keylines count ratio (validCount = %d, calcCount = %d).\n", validKeylines.size(), calcKeylines.size() );
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
return;
}
int progress = 0;
int progressCount = (int) ( validKeylines.size() * calcKeylines.size() );
int badLineCount = 0;
int commonLineCount = max( (int) validKeylines.size(), (int) calcKeylines.size() );
for ( size_t v = 0; v < validKeylines.size(); v++ )
{
int nearestIdx = -1;
float minDist = std::numeric_limits<float>::max();
for ( size_t c = 0; c < calcKeylines.size(); c++ )
{
progress = update_progress( progress, (int) ( v * calcKeylines.size() + c ), progressCount, 0 );
float curDist = (float) norm( calcKeylines[c].pt - validKeylines[v].pt );
if( curDist < minDist )
{
minDist = curDist;
nearestIdx = (int) c;
}
}
assert( minDist >= 0 );
if( !isSimilarKeylines( validKeylines[v], calcKeylines[nearestIdx] ) )
badLineCount++;
}
ts->printf( cvtest::TS::LOG, "badLineCount = %d; validLineCount = %d; calcLineCount = %d\n", badLineCount, validKeylines.size(),
calcKeylines.size() );
if( badLineCount > 0.9 * commonLineCount )
{
ts->printf( cvtest::TS::LOG, " - Bad accuracy!\n" );
ts->set_failed_test_info( cvtest::TS::FAIL_BAD_ACCURACY );
return;
}
ts->printf( cvtest::TS::LOG, " - OK\n" );
}
void CV_BinaryDescriptorDetectorTest::regressionTest()
{
assert( bd );
std::string imgFilename = std::string( ts->get_data_path() ) + LINE_DESCRIPTOR_DIR + "/" + IMAGE_FILENAME;
std::string resFilename = std::string( ts->get_data_path() ) + DETECTOR_DIR + "/" + fs_name + ".yaml";
// Read the test image.
Mat image = imread( imgFilename );
if( image.empty() )
{
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;
}
// open a storage for reading
FileStorage fs( resFilename, FileStorage::READ );
// Compute keylines.
std::vector<KeyLine> calcKeylines;
bd->detect( image, calcKeylines );
if( fs.isOpened() ) // Compare computed and valid keylines.
{
// Read validation keylines set.
std::vector<KeyLine> validKeylines;
Mat storedKeylines;
fs["keylines"] >> storedKeylines;
createVecFromMat( storedKeylines, validKeylines );
if( validKeylines.empty() )
{
ts->printf( cvtest::TS::LOG, "keylines can not be read.\n" );
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
return;
}
compareKeylineSets( validKeylines, calcKeylines );
}
else // Write detector parameters and computed keylines as validation data.
{
fs.open( resFilename, FileStorage::WRITE );
if( !fs.isOpened() )
{
ts->printf( cvtest::TS::LOG, "File %s can not be opened to write.\n", resFilename.c_str() );
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
return;
}
else
{
fs << "detector_params" << "{";
bd->write( fs );
fs << "}";
Mat lines;
createMatFromVec( calcKeylines, lines );
fs << "keylines" << lines;
}
}
}
void CV_BinaryDescriptorDetectorTest::run( int )
{
if( !bd )
{
ts->printf( cvtest::TS::LOG, "Feature detector is empty.\n" );
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
return;
}
emptyDataTest();
regressionTest();
ts->set_failed_test_info( cvtest::TS::OK );
}
/****************************************************************************************\
* Tests registrations *
\****************************************************************************************/
TEST( BinaryDescriptor_Detector, regression )
{
CV_BinaryDescriptorDetectorTest test( std::string( "edl_detector_keylines_cameraman" ) );
test.safe_run();
}
#include "test_precomp.hpp"
CV_TEST_MAIN("cv")
/*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) 2014, Biagio Montesano, 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"
using namespace cv;
using namespace cv::line_descriptor;
class CV_BinaryDescriptorMatcherTest : public cvtest::BaseTest
{
public:
CV_BinaryDescriptorMatcherTest( float _badPart ) :
badPart( _badPart )
{
dmatcher = BinaryDescriptorMatcher::createBinaryDescriptorMatcher();
}
protected:
static const int dim = 32;
static const int queryDescCount = 300; // must be even number because we split train data in some cases in two
static const int countFactor = 4; // do not change it
const float badPart;
virtual void run( int );
void generateData( Mat& query, Mat& train );
uchar invertSingleBits( uchar dividend_char, int numBits );
void emptyDataTest();
void matchTest( const Mat& query, const Mat& train );
void knnMatchTest( const Mat& query, const Mat& train );
void radiusMatchTest( const Mat& query, const Mat& train );
std::string name;
Ptr<BinaryDescriptorMatcher> dmatcher;
private:
CV_BinaryDescriptorMatcherTest& operator=( const CV_BinaryDescriptorMatcherTest& )
{
return *this;
}
};
/* invert numBits bits in input char */
uchar CV_BinaryDescriptorMatcherTest::invertSingleBits( uchar dividend_char, int numBits )
{
std::vector<int> bin_vector;
long dividend;
long bin_num;
/* convert input char to a long */
dividend = (long) dividend_char;
/*if a 0 has been obtained, just generate a 8-bit long vector of zeros */
if( dividend == 0 )
bin_vector = std::vector<int>( 8, 0 );
/* else, apply classic decimal to binary conversion */
else
{
while ( dividend >= 1 )
{
bin_num = dividend % 2;
dividend /= 2;
bin_vector.push_back( bin_num );
}
}
/* ensure that binary vector always has length 8 */
if( bin_vector.size() < 8 )
{
std::vector<int> zeros( 8 - bin_vector.size(), 0 );
bin_vector.insert( bin_vector.end(), zeros.begin(), zeros.end() );
}
/* invert numBits bits */
for ( int index = 0; index < numBits; index++ )
{
if( bin_vector[index] == 0 )
bin_vector[index] = 1;
else
bin_vector[index] = 0;
}
/* reconvert to decimal */
uchar result = 0;
for ( int i = (int) bin_vector.size() - 1; i >= 0; i-- )
result += (uchar) ( bin_vector[i] * pow( 2, i ) );
return result;
}
void CV_BinaryDescriptorMatcherTest::emptyDataTest()
{
Mat queryDescriptors, trainDescriptors, mask;
std::vector<Mat> trainDescriptorCollection, masks;
std::vector<DMatch> matches;
std::vector<std::vector<DMatch> > vmatches;
try
{
dmatcher->match( queryDescriptors, trainDescriptors, matches, mask );
}
catch ( ... )
{
ts->printf( cvtest::TS::LOG, "match() on empty descriptors must not generate exception (1).\n" );
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
}
try
{
dmatcher->knnMatch( queryDescriptors, trainDescriptors, vmatches, 2, mask );
}
catch ( ... )
{
ts->printf( cvtest::TS::LOG, "knnMatch() on empty descriptors must not generate exception (1).\n" );
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
}
try
{
dmatcher->radiusMatch( queryDescriptors, trainDescriptors, vmatches, 10.f, mask );
}
catch ( ... )
{
ts->printf( cvtest::TS::LOG, "radiusMatch() on empty descriptors must not generate exception (1).\n" );
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
}
try
{
dmatcher->add( trainDescriptorCollection );
}
catch ( ... )
{
ts->printf( cvtest::TS::LOG, "add() on empty descriptors must not generate exception.\n" );
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
}
try
{
dmatcher->match( queryDescriptors, matches, masks );
}
catch ( ... )
{
ts->printf( cvtest::TS::LOG, "match() on empty descriptors must not generate exception (2).\n" );
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
}
try
{
dmatcher->knnMatch( queryDescriptors, vmatches, 2, masks );
}
catch ( ... )
{
ts->printf( cvtest::TS::LOG, "knnMatch() on empty descriptors must not generate exception (2).\n" );
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
}
try
{
dmatcher->radiusMatch( queryDescriptors, vmatches, 10.f, masks );
}
catch ( ... )
{
ts->printf( cvtest::TS::LOG, "radiusMatch() on empty descriptors must not generate exception (2).\n" );
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
}
}
void CV_BinaryDescriptorMatcherTest::generateData( Mat& query, Mat& train )
{
RNG& rng = theRNG();
/* Generate query descriptors randomly.
Descriptor vector elements are binary values. */
Mat buf( queryDescCount, dim, CV_8UC1 );
rng.fill( buf, RNG::UNIFORM, Scalar( 0 ), Scalar( 255 ) );
buf.convertTo( query, CV_8UC1 );
for ( int i = 0; i < query.rows; i++ )
{
for ( int j = 0; j < countFactor; j++ )
{
train.push_back( query.row( i ) );
int randCol = rand() % 32;
uchar u = query.at<uchar>( i, randCol );
uchar modified_u = invertSingleBits( u, j + 1 );
train.at<uchar>( i * countFactor + j, randCol ) = modified_u;
}
}
}
void CV_BinaryDescriptorMatcherTest::matchTest( const Mat& query, const Mat& train )
{
dmatcher->clear();
// test const version of match()
{
std::vector<DMatch> matches;
dmatcher->match( query, train, matches );
if( (int) matches.size() != queryDescCount )
{
ts->printf( cvtest::TS::LOG, "Incorrect matches count while test match() function (1).\n" );
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
}
else
{
int badCount = 0;
for ( size_t i = 0; i < matches.size(); i++ )
{
DMatch& match = matches[i];
if( ( match.queryIdx != (int) i ) || ( match.trainIdx != (int) i * countFactor ) || ( match.imgIdx != 0 ) )
badCount++;
}
if( (float) badCount > (float) queryDescCount * badPart )
{
ts->printf( cvtest::TS::LOG, "%f - too large bad matches part while test match() function (1).\n",
(float) badCount / (float) queryDescCount );
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
}
}
}
// test const version of match() for the same query and test descriptors
{
std::vector<DMatch> matches;
dmatcher->match( query, query, matches );
if( (int) matches.size() != query.rows )
{
ts->printf( cvtest::TS::LOG, "Incorrect matches count while test match() function for the same query and test descriptors (1).\n" );
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
}
else
{
for ( size_t i = 0; i < matches.size(); i++ )
{
DMatch& match = matches[i];
if( match.queryIdx != (int) i || match.trainIdx != (int) i || std::abs( match.distance ) > FLT_EPSILON )
{
ts->printf(
cvtest::TS::LOG,
"Bad match (i=%d, queryIdx=%d, trainIdx=%d, distance=%f) while test match() function for the same query and test descriptors (1).\n", i,
match.queryIdx, match.trainIdx, match.distance );
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
}
}
}
}
// test version of match() with add()
{
dmatcher->clear();
std::vector<DMatch> matches;
// make add() twice to test such case
dmatcher->add( std::vector<Mat>( 1, train.rowRange( 0, train.rows / 2 ) ) );
dmatcher->add( std::vector<Mat>( 1, train.rowRange( train.rows / 2, train.rows ) ) );
// prepare masks (make first nearest match illegal)
std::vector<Mat> masks( 2 );
for ( int mi = 0; mi < 2; mi++ )
masks[mi] = Mat::ones( query.rows, 1/*train.rows / 2*/, CV_8UC1 );
dmatcher->match( query, matches, masks );
if( (int) matches.size() != queryDescCount )
{
ts->printf( cvtest::TS::LOG, "Incorrect matches count while test match() function (2).\n" );
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
}
else
{
int badCount = 0;
for ( size_t i = 0; i < matches.size(); i++ )
{
DMatch& match = matches[i];
if( ( match.queryIdx != (int) i ) || ( match.trainIdx != (int) i * countFactor /*+ shift*/) || ( match.imgIdx > 1 ) )
badCount++;
}
if( (float) badCount > (float) queryDescCount * badPart )
{
ts->printf( cvtest::TS::LOG, "%f - too large bad matches part while test match() function (2).\n",
(float) badCount / (float) queryDescCount );
ts->set_failed_test_info( cvtest::TS::FAIL_BAD_ACCURACY );
}
}
}
}
void CV_BinaryDescriptorMatcherTest::knnMatchTest( const Mat& query, const Mat& train )
{
dmatcher->clear();
// test const version of knnMatch()
{
const int knn = 3;
std::vector<std::vector<DMatch> > matches;
dmatcher->knnMatch( query, train, matches, knn );
if( (int) matches.size() != queryDescCount )
{
ts->printf( cvtest::TS::LOG, "Incorrect matches count while test knnMatch() function (1).\n" );
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
}
else
{
int badCount = 0;
for ( size_t i = 0; i < matches.size(); i++ )
{
if( (int) matches[i].size() != knn )
badCount++;
else
{
int localBadCount = 0;
for ( int k = 0; k < knn; k++ )
{
DMatch& match = matches[i][k];
if( ( match.queryIdx != (int) i ) || ( match.trainIdx != (int) i * countFactor + k ) || ( match.imgIdx != 0 ) )
localBadCount++;
}
badCount += localBadCount > 0 ? 1 : 0;
}
}
if( (float) badCount > (float) queryDescCount * badPart )
{
ts->printf( cvtest::TS::LOG, "%f - too large bad matches part while test knnMatch() function (1).\n",
(float) badCount / (float) queryDescCount );
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
}
}
}
// // test version of knnMatch() with add()
{
const int knn = 2;
std::vector<std::vector<DMatch> > matches;
// make add() twice to test such case
dmatcher->add( std::vector<Mat>( 1, train.rowRange( 0, train.rows / 2 ) ) );
dmatcher->add( std::vector<Mat>( 1, train.rowRange( train.rows / 2, train.rows ) ) );
// prepare masks (make first nearest match illegal)
std::vector<Mat> masks( 2 );
for ( int mi = 0; mi < 2; mi++ )
{
masks[mi] = Mat::ones( query.rows, 1, CV_8UC1 );
}
dmatcher->knnMatch( query, matches, knn, masks );
if( (int) matches.size() != queryDescCount )
{
ts->printf( cvtest::TS::LOG, "Incorrect matches count while test knnMatch() function (2).\n" );
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
}
else
{
int badCount = 0;
for ( size_t i = 0; i < matches.size(); i++ )
{
if( (int) matches[i].size() != knn )
badCount++;
else
{
int localBadCount = 0;
for ( int k = 0; k < knn; k++ )
{
DMatch& match = matches[i][k];
{
if( i < queryDescCount / 2 )
{
if( ( match.queryIdx != (int) i ) || ( match.trainIdx != (int) i * countFactor + k ) || ( match.imgIdx != 0 ) )
localBadCount++;
}
else
{
if( ( match.queryIdx != (int) i ) || ( match.trainIdx != (int) i * countFactor + k ) || ( match.imgIdx != 1 ) )
localBadCount++;
}
}
}
badCount += localBadCount > 0 ? 1 : 0;
}
}
if( (float) badCount > (float) queryDescCount * badPart )
{
ts->printf( cvtest::TS::LOG, "%f - too large bad matches part while test knnMatch() function (2).\n",
(float) badCount / (float) queryDescCount );
ts->set_failed_test_info( cvtest::TS::FAIL_BAD_ACCURACY );
}
}
}
}
void CV_BinaryDescriptorMatcherTest::radiusMatchTest( const Mat& query, const Mat& train )
{
dmatcher->clear();
// test const version of match()
{
const float radius = 1;
std::vector<std::vector<DMatch> > matches;
dmatcher->radiusMatch( query, train, matches, radius );
if( (int) matches.size() != queryDescCount )
{
ts->printf( cvtest::TS::LOG, "Incorrect matches count while test radiusMatch() function (1).\n" );
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
}
else
{
int badCount = 0;
for ( size_t i = 0; i < matches.size(); i++ )
{
if( (int) matches[i].size() != 1 )
{
badCount++;
}
else
{
DMatch& match = matches[i][0];
if( ( match.queryIdx != (int) i ) || ( match.trainIdx != (int) i * countFactor ) || ( match.imgIdx != 0 ) )
badCount++;
}
}
if( (float) badCount > (float) queryDescCount * badPart )
{
ts->printf( cvtest::TS::LOG, "%f - too large bad matches part while test radiusMatch() function (1).\n",
(float) badCount / (float) queryDescCount );
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
}
}
}
{
const float radius = 3;
std::vector<std::vector<DMatch> > matches;
// make add() twice to test such case
dmatcher->add( std::vector<Mat>( 1, train.rowRange( 0, train.rows / 2 ) ) );
dmatcher->add( std::vector<Mat>( 1, train.rowRange( train.rows / 2, train.rows ) ) );
// prepare masks
std::vector<Mat> masks( 2 );
for ( int mi = 0; mi < 2; mi++ )
masks[mi] = Mat::ones( query.rows, 1, CV_8UC1 );
dmatcher->radiusMatch( query, matches, radius, masks );
//int curRes = cvtest::TS::OK;
if( (int) matches.size() != queryDescCount )
{
ts->printf( cvtest::TS::LOG, "Incorrect matches count while test radiusMatch() function (1).\n" );
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
}
int badCount = 0;
for ( size_t i = 0; i < matches.size(); i++ )
{
if( (int) matches[i].size() != radius )
badCount++;
else
{
int localBadCount = 0;
for ( int k = 0; k < radius; k++ )
{
DMatch& match = matches[i][k];
{
if( i < queryDescCount / 2 )
{
if( ( match.queryIdx != (int) i ) || ( match.trainIdx != (int) i * countFactor + k ) || ( match.imgIdx != 0 ) )
localBadCount++;
}
else
{
if( ( match.queryIdx != (int) i ) || ( match.trainIdx != (int) i * countFactor + k ) || ( match.imgIdx != 1 ) )
localBadCount++;
}
}
}
badCount += localBadCount > 0 ? 1 : 0;
}
}
if( (float) badCount > (float) queryDescCount * badPart )
{
//curRes = cvtest::TS::FAIL_INVALID_OUTPUT;
ts->printf( cvtest::TS::LOG, "%f - too large bad matches part while test radiusMatch() function (2).\n",
(float) badCount / (float) queryDescCount );
ts->set_failed_test_info( cvtest::TS::FAIL_BAD_ACCURACY );
}
}
}
void CV_BinaryDescriptorMatcherTest::run( int )
{
Mat query, train;
emptyDataTest();
generateData( query, train );
matchTest( query, train );
knnMatchTest( query, train );
radiusMatchTest( query, train );
}
/****************************************************************************************\
* Tests registrations *
\****************************************************************************************/
TEST( BinaryDescriptor_Matcher, regression)
{
CV_BinaryDescriptorMatcherTest test( 0.01f );
test.safe_run();
}
#ifdef __GNUC__
# pragma GCC diagnostic ignored "-Wmissing-declarations"
# if defined __clang__ || defined __APPLE__
# pragma GCC diagnostic ignored "-Wmissing-prototypes"
# pragma GCC diagnostic ignored "-Wextra"
# endif
#endif
#ifndef __OPENCV_TEST_PRECOMP_HPP__
#define __OPENCV_TEST_PRECOMP_HPP__
#include "opencv2/ts.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/features2d.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/line_descriptor.hpp"
#include <opencv2/core.hpp>
#include <iostream>
#include <string.h>
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment