1 Common Interfaces of Generic Descriptor Matchers
2 ================================================
6 Matchers of keypoint descriptors in OpenCV have wrappers with a common interface that enables you to easily switch
7 between different algorithms solving the same problem. This section is devoted to matching descriptors
8 that cannot be represented as vectors in a multidimensional space. ``GenericDescriptorMatcher`` is a more generic interface for descriptors. It does not make any assumptions about descriptor representation.
9 Every descriptor with the
10 :ocv:class:`DescriptorExtractor` interface has a wrapper with the ``GenericDescriptorMatcher`` interface (see
11 :ocv:class:`VectorDescriptorMatcher` ).
12 There are descriptors such as the One-way descriptor and Ferns that have the ``GenericDescriptorMatcher`` interface implemented but do not support ``DescriptorExtractor``.
16 GenericDescriptorMatcher
17 ------------------------
18 .. ocv:class:: GenericDescriptorMatcher
20 Abstract interface for extracting and matching a keypoint descriptor. There are also :ocv:class:`DescriptorExtractor` and :ocv:class:`DescriptorMatcher` for these purposes but their interfaces are intended for descriptors represented as vectors in a multidimensional space. ``GenericDescriptorMatcher`` is a more generic interface for descriptors. ``DescriptorMatcher`` and ``GenericDescriptorMatcher`` have two groups of match methods: for matching keypoints of an image with another image or with an image set. ::
22 class GenericDescriptorMatcher
25 GenericDescriptorMatcher();
26 virtual ~GenericDescriptorMatcher();
28 virtual void add( const vector<Mat>& images,
29 vector<vector<KeyPoint> >& keypoints );
31 const vector<Mat>& getTrainImages() const;
32 const vector<vector<KeyPoint> >& getTrainKeypoints() const;
35 virtual void train() = 0;
37 virtual bool isMaskSupported() = 0;
39 void classify( const Mat& queryImage,
40 vector<KeyPoint>& queryKeypoints,
41 const Mat& trainImage,
42 vector<KeyPoint>& trainKeypoints ) const;
43 void classify( const Mat& queryImage,
44 vector<KeyPoint>& queryKeypoints );
47 * Group of methods to match keypoints from an image pair.
49 void match( const Mat& queryImage, vector<KeyPoint>& queryKeypoints,
50 const Mat& trainImage, vector<KeyPoint>& trainKeypoints,
51 vector<DMatch>& matches, const Mat& mask=Mat() ) const;
52 void knnMatch( const Mat& queryImage, vector<KeyPoint>& queryKeypoints,
53 const Mat& trainImage, vector<KeyPoint>& trainKeypoints,
54 vector<vector<DMatch> >& matches, int k,
55 const Mat& mask=Mat(), bool compactResult=false ) const;
56 void radiusMatch( const Mat& queryImage, vector<KeyPoint>& queryKeypoints,
57 const Mat& trainImage, vector<KeyPoint>& trainKeypoints,
58 vector<vector<DMatch> >& matches, float maxDistance,
59 const Mat& mask=Mat(), bool compactResult=false ) const;
61 * Group of methods to match keypoints from one image to an image set.
63 void match( const Mat& queryImage, vector<KeyPoint>& queryKeypoints,
64 vector<DMatch>& matches, const vector<Mat>& masks=vector<Mat>() );
65 void knnMatch( const Mat& queryImage, vector<KeyPoint>& queryKeypoints,
66 vector<vector<DMatch> >& matches, int k,
67 const vector<Mat>& masks=vector<Mat>(), bool compactResult=false );
68 void radiusMatch( const Mat& queryImage, vector<KeyPoint>& queryKeypoints,
69 vector<vector<DMatch> >& matches, float maxDistance,
70 const vector<Mat>& masks=vector<Mat>(), bool compactResult=false );
72 virtual void read( const FileNode& );
73 virtual void write( FileStorage& ) const;
75 virtual Ptr<GenericDescriptorMatcher> clone( bool emptyTrainData=false ) const = 0;
84 GenericDescriptorMatcher::add
85 ---------------------------------
86 Adds images and their keypoints to the training collection stored in the class instance.
88 .. ocv:function:: void GenericDescriptorMatcher::add( const vector<Mat>& images, vector<vector<KeyPoint> >& keypoints )
90 :param images: Image collection.
92 :param keypoints: Point collection. It is assumed that ``keypoints[i]`` are keypoints detected in the image ``images[i]`` .
96 GenericDescriptorMatcher::getTrainImages
97 --------------------------------------------
98 Returns a train image collection.
100 .. ocv:function:: const vector<Mat>& GenericDescriptorMatcher::getTrainImages() const
104 GenericDescriptorMatcher::getTrainKeypoints
105 -----------------------------------------------
106 Returns a train keypoints collection.
108 .. ocv:function:: const vector<vector<KeyPoint> >& GenericDescriptorMatcher::getTrainKeypoints() const
112 GenericDescriptorMatcher::clear
113 -----------------------------------
114 Clears a train collection (images and keypoints).
116 .. ocv:function:: void GenericDescriptorMatcher::clear()
120 GenericDescriptorMatcher::train
121 -----------------------------------
122 Trains descriptor matcher
124 .. ocv:function:: void GenericDescriptorMatcher::train()
126 Prepares descriptor matcher, for example, creates a tree-based structure, to extract descriptors or to optimize descriptors matching.
129 GenericDescriptorMatcher::isMaskSupported
130 ---------------------------------------------
131 Returns ``true`` if a generic descriptor matcher supports masking permissible matches.
133 .. ocv:function:: bool GenericDescriptorMatcher::isMaskSupported()
137 GenericDescriptorMatcher::classify
138 --------------------------------------
139 Classifies keypoints from a query set.
141 .. ocv:function:: void GenericDescriptorMatcher::classify( const Mat& queryImage, vector<KeyPoint>& queryKeypoints, const Mat& trainImage, vector<KeyPoint>& trainKeypoints ) const
143 .. ocv:function:: void GenericDescriptorMatcher::classify( const Mat& queryImage, vector<KeyPoint>& queryKeypoints )
145 :param queryImage: Query image.
147 :param queryKeypoints: Keypoints from a query image.
149 :param trainImage: Train image.
151 :param trainKeypoints: Keypoints from a train image.
153 The method classifies each keypoint from a query set. The first variant of the method takes a train image and its keypoints as an input argument. The second variant uses the internally stored training collection that can be built using the ``GenericDescriptorMatcher::add`` method.
155 The methods do the following:
158 Call the ``GenericDescriptorMatcher::match`` method to find correspondence between the query set and the training set.
161 Set the ``class_id`` field of each keypoint from the query set to ``class_id`` of the corresponding keypoint from the training set.
165 GenericDescriptorMatcher::match
166 -----------------------------------
167 Finds the best match in the training set for each keypoint from the query set.
169 .. ocv:function:: void GenericDescriptorMatcher::match( const Mat& queryImage, vector<KeyPoint>& queryKeypoints, const Mat& trainImage, vector<KeyPoint>& trainKeypoints, vector<DMatch>& matches, const Mat& mask=Mat() ) const
171 .. ocv:function:: void GenericDescriptorMatcher::match( const Mat& queryImage, vector<KeyPoint>& queryKeypoints, vector<DMatch>& matches, const vector<Mat>& masks=vector<Mat>() )
173 :param queryImage: Query image.
175 :param queryKeypoints: Keypoints detected in ``queryImage`` .
177 :param trainImage: Train image. It is not added to a train image collection stored in the class object.
179 :param trainKeypoints: Keypoints detected in ``trainImage`` . They are not added to a train points collection stored in the class object.
181 :param matches: Matches. If a query descriptor (keypoint) is masked out in ``mask`` , match is added for this descriptor. So, ``matches`` size may be smaller than the query keypoints count.
183 :param mask: Mask specifying permissible matches between an input query and train keypoints.
185 :param masks: Set of masks. Each ``masks[i]`` specifies permissible matches between input query keypoints and stored train keypoints from the i-th image.
187 The methods find the best match for each query keypoint. In the first variant of the method, a train image and its keypoints are the input arguments. In the second variant, query keypoints are matched to the internally stored training collection that can be built using the ``GenericDescriptorMatcher::add`` method. Optional mask (or masks) can be passed to specify which query and training descriptors can be matched. Namely, ``queryKeypoints[i]`` can be matched with ``trainKeypoints[j]`` only if ``mask.at<uchar>(i,j)`` is non-zero.
191 GenericDescriptorMatcher::knnMatch
192 --------------------------------------
193 Finds the ``k`` best matches for each query keypoint.
195 .. ocv:function:: void GenericDescriptorMatcher::knnMatch( const Mat& queryImage, vector<KeyPoint>& queryKeypoints, const Mat& trainImage, vector<KeyPoint>& trainKeypoints, vector<vector<DMatch> >& matches, int k, const Mat& mask=Mat(), bool compactResult=false ) const
197 .. ocv:function:: void GenericDescriptorMatcher::knnMatch( const Mat& queryImage, vector<KeyPoint>& queryKeypoints, vector<vector<DMatch> >& matches, int k, const vector<Mat>& masks=vector<Mat>(), bool compactResult=false )
199 The methods are extended variants of ``GenericDescriptorMatch::match``. The parameters are similar, and the semantics is similar to ``DescriptorMatcher::knnMatch``. But this class does not require explicitly computed keypoint descriptors.
203 GenericDescriptorMatcher::radiusMatch
204 -----------------------------------------
205 For each query keypoint, finds the training keypoints not farther than the specified distance.
207 .. ocv:function:: void GenericDescriptorMatcher::radiusMatch( const Mat& queryImage, vector<KeyPoint>& queryKeypoints, const Mat& trainImage, vector<KeyPoint>& trainKeypoints, vector<vector<DMatch> >& matches, float maxDistance, const Mat& mask=Mat(), bool compactResult=false ) const
209 .. ocv:function:: void GenericDescriptorMatcher::radiusMatch( const Mat& queryImage, vector<KeyPoint>& queryKeypoints, vector<vector<DMatch> >& matches, float maxDistance, const vector<Mat>& masks=vector<Mat>(), bool compactResult=false )
211 The methods are similar to ``DescriptorMatcher::radius``. But this class does not require explicitly computed keypoint descriptors.
215 GenericDescriptorMatcher::read
216 ----------------------------------
217 Reads a matcher object from a file node.
219 .. ocv:function:: void GenericDescriptorMatcher::read( const FileNode& fn )
223 GenericDescriptorMatcher::write
224 -----------------------------------
225 Writes a match object to a file storage.
227 .. ocv:function:: void GenericDescriptorMatcher::write( FileStorage& fs ) const
230 GenericDescriptorMatcher::clone
231 -----------------------------------
234 .. ocv:function:: Ptr<GenericDescriptorMatcher> GenericDescriptorMatcher::clone( bool emptyTrainData=false ) const
236 :param emptyTrainData: If ``emptyTrainData`` is false, the method creates a deep copy of the object, that is, copies
237 both parameters and train data. If ``emptyTrainData`` is true, the method creates an object copy with the current parameters
238 but with empty train data.
241 VectorDescriptorMatcher
242 -----------------------
243 .. ocv:class:: VectorDescriptorMatcher : public GenericDescriptorMatcher
245 Class used for matching descriptors that can be described as vectors in a finite-dimensional space. ::
247 class CV_EXPORTS VectorDescriptorMatcher : public GenericDescriptorMatcher
250 VectorDescriptorMatcher( const Ptr<DescriptorExtractor>& extractor, const Ptr<DescriptorMatcher>& matcher );
251 virtual ~VectorDescriptorMatcher();
253 virtual void add( const vector<Mat>& imgCollection,
254 vector<vector<KeyPoint> >& pointCollection );
255 virtual void clear();
256 virtual void train();
257 virtual bool isMaskSupported();
259 virtual void read( const FileNode& fn );
260 virtual void write( FileStorage& fs ) const;
262 virtual Ptr<GenericDescriptorMatcher> clone( bool emptyTrainData=false ) const;
271 VectorDescriptorMatcher matcher( new SurfDescriptorExtractor,
272 new BruteForceMatcher<L2<float> > );