Visual Servoing Platform  version 3.5.0 under development (2022-02-15)
vpPlanarObjectDetector.h
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  * Planar surface detection tool.
33  *
34  * Authors:
35  * Romain Tallonneau
36  *
37  *****************************************************************************/
38 
39 #ifndef VPPLANAROBJECTDETECTOR_H_
40 #define VPPLANAROBJECTDETECTOR_H_
41 
42 #include <visp3/core/vpConfig.h>
43 
44 #if (VISP_HAVE_OPENCV_VERSION >= 0x020000) && \
45  (VISP_HAVE_OPENCV_VERSION < 0x030000) // Require opencv >= 2.0.0 and < 3.0.0
46 
47 #if (VISP_HAVE_OPENCV_VERSION >= 0x020101) // Require opencv >= 2.1.1
48 #include <opencv2/calib3d/calib3d.hpp>
49 #include <opencv2/features2d/features2d.hpp>
50 #include <opencv2/imgproc/imgproc.hpp>
51 #elif (VISP_HAVE_OPENCV_VERSION >= 0x020000) // Require opencv >= 2.0.0
52 #include <cv.h>
53 #include <cvaux.hpp>
54 #endif
55 
56 #include <visp3/core/vpCameraParameters.h>
57 #include <visp3/core/vpHomogeneousMatrix.h>
58 #include <visp3/core/vpImage.h>
59 #include <visp3/core/vpImagePoint.h>
60 #include <visp3/core/vpPoint.h>
61 #include <visp3/core/vpRect.h>
62 #include <visp3/vision/vpFernClassifier.h>
63 #include <visp3/vision/vpHomography.h>
64 
140 class VISP_EXPORT vpPlanarObjectDetector
141 {
142 protected:
145  vpFernClassifier fern;
146 
148  vpHomography homography;
150  cv::Mat H;
151 
154  std::vector<cv::Point2f> dst_corners;
155 
157  bool isCorrect;
158 
160  std::vector<cv::Point2f> ref_corners;
161 
163  cv::Rect modelROI;
164 
167  std::vector<vpImagePoint> currentImagePoints;
170  std::vector<vpImagePoint> refImagePoints;
171 
174  unsigned int minNbMatching;
175 
176 public:
177  // constructors and destructors
178  vpPlanarObjectDetector();
179  vpPlanarObjectDetector(const std::string &dataFile, const std::string &objectName);
180  virtual ~vpPlanarObjectDetector();
181 
182  // main functions
183  // creation of reference
184  unsigned int buildReference(const vpImage<unsigned char> &I);
185  unsigned int buildReference(const vpImage<unsigned char> &I, const vpImagePoint &iP, unsigned int height,
186  unsigned int width);
187  unsigned int buildReference(const vpImage<unsigned char> &I, const vpRect &rectangle);
188 
189  // matching
190  bool matchPoint(const vpImage<unsigned char> &I);
191  bool matchPoint(const vpImage<unsigned char> &I, const vpImagePoint &iP, unsigned int height,
192  unsigned int width);
193  bool matchPoint(const vpImage<unsigned char> &I, const vpRect &rectangle);
194  // database management
195  void recordDetector(const std::string &objectName, const std::string &dataFile);
196  void load(const std::string &dataFilename, const std::string &objName);
197 
198  // display
199  void display(vpImage<unsigned char> &I, bool displayKpts = false);
200  void display(vpImage<unsigned char> &Iref, vpImage<unsigned char> &Icurrent, bool displayKpts = false);
201 
208  std::vector<vpImagePoint> getDetectedCorners() const;
209 
215  vpFernClassifier &getFernClassifier() { return this->fern; }
216 
223  inline void getHomography(vpHomography &_H) const { _H = this->homography; }
224 
230  inline unsigned int getNbRefPoints() { return (unsigned int)currentImagePoints.size(); }
231 
240  void getReferencePoint(const unsigned int _i, vpImagePoint &_imPoint);
241 
253  void getMatchedPoints(const unsigned int _index, vpImagePoint &_referencePoint, vpImagePoint &_currentPoint);
254 
261  void setMinNbPointValidation(const unsigned int _min) { this->minNbMatching = _min; }
262 
269  unsigned int getMinNbPointValidation() const { return this->minNbMatching; }
270 
271 protected:
272  virtual void init();
273  void computeRoi(vpImagePoint *ip, unsigned int nbpt);
274  void initialiseRefCorners(const cv::Rect &_modelROI);
275 };
276 
277 #endif
278 
279 #endif /* VPPLANAROBJECTDETECTOR_H_ */
unsigned int size() const
Return the number of elements of the 2D array.
Definition: vpArray2D.h:291
Implementation of an homography and operations on homographies.
Definition: vpHomography.h:174
Defines a rectangle in the plane.
Definition: vpRect.h:79
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:87