Visual Servoing Platform  version 3.6.1 under development (2023-10-19)
vpPlanarObjectDetector.h
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
4  *
5  * This software is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See https://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Planar surface detection tool.
32  */
33 
34 #ifndef _vpPlanarObjectDetector_h_
35 #define _vpPlanarObjectDetector_h_
36 
37 #include <visp3/core/vpConfig.h>
38 
39 #if (VISP_HAVE_OPENCV_VERSION >= 0x020408) && \
40  (VISP_HAVE_OPENCV_VERSION < 0x030000) // Require opencv >= 2.4.8 and < 3.0.0
41 
42 #include <opencv2/calib3d/calib3d.hpp>
43 #include <opencv2/features2d/features2d.hpp>
44 #include <opencv2/imgproc/imgproc.hpp>
45 
46 #include <visp3/core/vpCameraParameters.h>
47 #include <visp3/core/vpHomogeneousMatrix.h>
48 #include <visp3/core/vpImage.h>
49 #include <visp3/core/vpImagePoint.h>
50 #include <visp3/core/vpPoint.h>
51 #include <visp3/core/vpRect.h>
52 #include <visp3/vision/vpFernClassifier.h>
53 #include <visp3/vision/vpHomography.h>
54 
130 class VISP_EXPORT vpPlanarObjectDetector
131 {
132 protected:
135  vpFernClassifier fern;
136 
138  vpHomography homography;
140  cv::Mat H;
141 
144  std::vector<cv::Point2f> dst_corners;
145 
147  bool isCorrect;
148 
150  std::vector<cv::Point2f> ref_corners;
151 
153  cv::Rect modelROI;
154 
157  std::vector<vpImagePoint> currentImagePoints;
160  std::vector<vpImagePoint> refImagePoints;
161 
164  unsigned int minNbMatching;
165 
166 public:
167  // constructors and destructors
168  vpPlanarObjectDetector();
169  vpPlanarObjectDetector(const std::string &dataFile, const std::string &objectName);
170  virtual ~vpPlanarObjectDetector();
171 
172  // main functions
173  // creation of reference
174  unsigned int buildReference(const vpImage<unsigned char> &I);
175  unsigned int buildReference(const vpImage<unsigned char> &I, const vpImagePoint &iP, unsigned int height,
176  unsigned int width);
177  unsigned int buildReference(const vpImage<unsigned char> &I, const vpRect &rectangle);
178 
179  // matching
180  bool matchPoint(const vpImage<unsigned char> &I);
181  bool matchPoint(const vpImage<unsigned char> &I, const vpImagePoint &iP, unsigned int height, unsigned int width);
182  bool matchPoint(const vpImage<unsigned char> &I, const vpRect &rectangle);
183  // database management
184  void recordDetector(const std::string &objectName, const std::string &dataFile);
185  void load(const std::string &dataFilename, const std::string &objName);
186 
187  // display
188  void display(vpImage<unsigned char> &I, bool displayKpts = false);
189  void display(vpImage<unsigned char> &Iref, vpImage<unsigned char> &Icurrent, bool displayKpts = false);
190 
197  std::vector<vpImagePoint> getDetectedCorners() const;
198 
204  vpFernClassifier &getFernClassifier() { return this->fern; }
205 
212  inline void getHomography(vpHomography &_H) const { _H = this->homography; }
213 
219  inline unsigned int getNbRefPoints() { return (unsigned int)currentImagePoints.size(); }
220 
229  void getReferencePoint(const unsigned int _i, vpImagePoint &_imPoint);
230 
242  void getMatchedPoints(const unsigned int _index, vpImagePoint &_referencePoint, vpImagePoint &_currentPoint);
243 
250  void setMinNbPointValidation(const unsigned int _min) { this->minNbMatching = _min; }
251 
258  unsigned int getMinNbPointValidation() const { return this->minNbMatching; }
259 
260 protected:
261  virtual void init();
262  void computeRoi(vpImagePoint *ip, unsigned int nbpt);
263  void initialiseRefCorners(const cv::Rect &_modelROI);
264 };
265 
266 #endif
267 
268 #endif /* VPPLANAROBJECTDETECTOR_H_ */
unsigned int size() const
Return the number of elements of the 2D array.
Definition: vpArray2D.h:292
Implementation of an homography and operations on homographies.
Definition: vpHomography.h:168
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:82
Defines a rectangle in the plane.
Definition: vpRect.h:76