1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Key point used in matching algorithm.
33  *
34  * Authors:
35  * Nicolas Melchior
36  *
37  *****************************************************************************/
38 
39 #ifndef vpBasicKeyPoint_H
40 #define vpBasicKeyPoint_H
41 
42 /*!
43   \file vpBasicKeyPoint.h
44   \brief class that defines what is a Keypoint
45 */
46 
47 #include <visp3/core/vpColor.h>
48 #include <visp3/core/vpImage.h>
49 #include <visp3/core/vpImagePoint.h>
50 #include <visp3/core/vpRect.h>
51 
52 #include <vector>
53 
54 /*!
55   \class vpBasicKeyPoint
56   \ingroup group_vision_keypoints
57 
58   \brief class that defines what is a Keypoint. This class provides
59   all the basic elements to implement classes which aims to match
60   points from an image to another.
61 */
62 class VISP_EXPORT vpBasicKeyPoint
63 {
64 public:
65   vpBasicKeyPoint();
66 
~vpBasicKeyPoint()67   virtual ~vpBasicKeyPoint()
68   {
69     matchedReferencePoints.resize(0);
70     currentImagePointsList.resize(0);
71     referenceImagePointsList.resize(0);
72   }
73 
74   virtual unsigned int buildReference(const vpImage<unsigned char> &I) = 0;
75 
76   virtual unsigned int buildReference(const vpImage<unsigned char> &I, const vpImagePoint &iP,
77                                       unsigned int height, unsigned int width) = 0;
78 
79   virtual unsigned int buildReference(const vpImage<unsigned char> &I, const vpRect &rectangle) = 0;
80 
81   virtual unsigned int matchPoint(const vpImage<unsigned char> &I) = 0;
82 
83   virtual unsigned int matchPoint(const vpImage<unsigned char> &I, const vpImagePoint &iP, unsigned int height,
84                                   unsigned int width) = 0;
85 
86   virtual unsigned int matchPoint(const vpImage<unsigned char> &I, const vpRect &rectangle) = 0;
87 
88   virtual void display(const vpImage<unsigned char> &Iref, const vpImage<unsigned char> &Icurrent,
89                        unsigned int size = 3) = 0;
90 
91   virtual void display(const vpImage<unsigned char> &Icurrent, unsigned int size = 3,
92                        const vpColor &color = vpColor::green) = 0;
93 
94   /*!
95     Indicate wether the reference has been built or not.
96 
97     \return True if the reference of the current instance has been built.
98   */
referenceBuilt()99   bool referenceBuilt() const { return _reference_computed; }
100 
101   /*!
102     Get the pointer to the complete list of reference points. The pointer is
103     const. Thus the points can not be modified
104 
105     \return The pointer to the complete list of reference points.
106   */
getAllPointsInReferenceImage()107   inline const vpImagePoint *getAllPointsInReferenceImage() { return &referenceImagePointsList[0]; }
108 
109   /*!
110     Get the nth reference point. This point is copied in the vpImagePoint
111     instance given in argument.
112 
113     \param index : The index of the desired reference point. The index must be
114     between 0 and the number of reference points - 1.
115     \param referencePoint :
116     The coordinates of the desired reference point are copied there.
117   */
getReferencePoint(unsigned int index,vpImagePoint & referencePoint)118   inline void getReferencePoint(unsigned int index, vpImagePoint &referencePoint)
119   {
120     if (index >= referenceImagePointsList.size()) {
121       vpTRACE("Index of the reference point out of range");
122       throw(vpException(vpException::fatalError, "Index of the reference point out of range"));
123     }
124 
125     referencePoint.set_ij(referenceImagePointsList[index].get_i(), referenceImagePointsList[index].get_j());
126   }
127 
128   /*!
129     Get the nth couple of reference point and current point which have been
130     matched. These points are copied in the vpImagePoint instances given in
131     argument.
132 
133     \param index : The index of the desired couple of reference point and
134     current point. The index must be between 0 and the number of matched
135     points - 1.
136     \param referencePoint : The coordinates of the desired reference point are copied here.
137     \param currentPoint : The coordinates of the desired current point are copied here.
138   */
getMatchedPoints(unsigned int index,vpImagePoint & referencePoint,vpImagePoint & currentPoint)139   inline void getMatchedPoints(unsigned int index, vpImagePoint &referencePoint, vpImagePoint &currentPoint)
140   {
141     if (index >= matchedReferencePoints.size()) {
142       vpTRACE("Index of the matched points out of range");
143       throw(vpException(vpException::fatalError, "Index of the matched points out of range"));
144     }
145     referencePoint.set_ij(referenceImagePointsList[matchedReferencePoints[index]].get_i(),
146                           referenceImagePointsList[matchedReferencePoints[index]].get_j());
147     currentPoint.set_ij(currentImagePointsList[index].get_i(), currentImagePointsList[index].get_j());
148   }
149 
150   /*!
151     Get the nth matched reference point index in the complete list of
152    reference point.
153 
154     In the code below referencePoint1 and referencePoint2 correspond to the
155    same matched reference point.
156 
157    \code
158    vpKeyPointSurf surf;
159 
160    //Here the code to compute the reference points and the current points.
161 
162    vpImagePoint referencePoint1;
163    vpImagePoint currentPoint;
164    surf.getMatchedPoints(1, referencePoint1, currentPoint);  //Get the first matched points
165 
166    vpImagePoint referencePoint2;
167    const vpImagePoint* referencePointsList = surf.getAllPointsInReferenceImage();
168    // Get the first matched reference point index in the complete reference point list
169    int index = surf.getIndexInAllReferencePointList(1);
170    // Get the first matched reference point
171    referencePoint2 = referencePointsList[index];
172    \endcode
173   */
getIndexInAllReferencePointList(unsigned int indexInMatchedPointList)174   inline unsigned int getIndexInAllReferencePointList(unsigned int indexInMatchedPointList)
175   {
176     if (indexInMatchedPointList >= matchedReferencePoints.size()) {
177       vpTRACE("Index of the matched reference point out of range");
178       throw(vpException(vpException::fatalError, "Index of the matched reference point out of range"));
179     }
180     return matchedReferencePoints[indexInMatchedPointList];
181   }
182 
183   /*!
184     Get the number of reference points.
185 
186     \return the number of reference points.
187   */
getReferencePointNumber()188   inline unsigned int getReferencePointNumber() const { return (unsigned int)referenceImagePointsList.size(); };
189 
190   /*!
191     Get the number of matched points.
192 
193     \return the number of matched points.
194   */
getMatchedPointNumber()195   inline unsigned int getMatchedPointNumber() const { return (unsigned int)matchedReferencePoints.size(); };
196 
197   /*!
198     Return the vector of reference image point.
199 
200     \warning Should not be modified.
201 
202     \return Vector of reference image point.
203   */
getReferenceImagePointsList()204   const std::vector<vpImagePoint> &getReferenceImagePointsList() const { return referenceImagePointsList; }
205 
206   /*!
207     Return the vector of current image point.
208 
209     \warning Should not be modified.
210 
211     \return Vector of the current image point.
212   */
getCurrentImagePointsList()213   const std::vector<vpImagePoint> &getCurrentImagePointsList() const { return currentImagePointsList; }
214 
215   /*!
216     Return the index of the matched associated to the current image point i.
217     The ith element of the vector is the index of the reference image point
218     matching with the current image point.
219 
220     \warning Should not be modified.
221 
222     \return The vector of matching index.
223   */
getMatchedReferencePoints()224   const std::vector<unsigned int> &getMatchedReferencePoints() const { return matchedReferencePoints; }
225 
226 private:
227   virtual void init() = 0;
228 
229 protected:
230   /*!
231     List of the points which define the reference.
232   */
233   std::vector<vpImagePoint> referenceImagePointsList;
234 
235   /*!
236     List of the points which belong to the current image and have
237     been matched with points belonging to the reference.
238   */
239   std::vector<vpImagePoint> currentImagePointsList;
240 
241   /*!
242     Array containing the index in the array "referenceImagePointsList" of the
243     reference points which have been matched.
244 
245     The first element of the "currentImagePointsList" array is matched with
246     the nth element of the "referenceImagePointsList" array. The value of n is
247     stored in the first element of the "matchedReferencePoints" array.
248   */
249   std::vector<unsigned int> matchedReferencePoints;
250 
251   //! flag to indicate if the reference has been built.
252   bool _reference_computed;
253 };
254 
255 #endif
256