Project

General

Profile

BitmapToolkit Scol plugin
ArFeaturedMarker.h
Go to the documentation of this file.
1/*
2-----------------------------------------------------------------------------
3This source file is part of OpenSpace3D
4For the latest info, see http://www.openspace3d.com
5
6Copyright (c) 2012 I-maginer
7
8This program is free software; you can redistribute it and/or modify it under
9the terms of the GNU Lesser General Public License as published by the Free Software
10Foundation; either version 2 of the License, or (at your option) any later
11version.
12
13This program is distributed in the hope that it will be useful, but WITHOUT
14ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
16
17You should have received a copy of the GNU Lesser General Public License along with
18this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19Place - Suite 330, Boston, MA 02111-1307, USA, or go to
20http://www.gnu.org/copyleft/lesser.txt
21
22-----------------------------------------------------------------------------
23*/
24
25/*
26 Toolkit based on OpenCV library
27 First version : dec 2010
28 Author : Bastien BOURINEAU
29*/
30
31#ifndef __BTK_FEATUREDMARKER_H__
32#define __BTK_FEATUREDMARKER_H__
33
34#include <vector>
35#include <iostream>
36#include "opencv2/features2d/features2d.hpp"
37#include "cameraparameters.h"
38#include "ArMarker.h"
39#include "Prerequisites.h"
40#include <boost/thread.hpp>
41#include <iostream>
42
44public:
46 virtual ~RobustMatcher();
47
48 // Set the feature detector
49 void setFeatureDetector(const cv::Ptr<cv::FeatureDetector>& detect) { detector_ = detect; }
50
51 // Set the train feature detector
52 void setTrainFeatureDetector(const cv::Ptr<cv::FeatureDetector>& detect) { traindetector_ = detect; }
53
54 // Set the matcher
55 void setDescriptorMatcher(const cv::Ptr<cv::DescriptorMatcher>& match) { matcher_ = match; }
56
57 // Set ratio parameter for the ratio test
58 void setRatio(float rat) { ratio_ = rat; }
59
60 // Clear matches for which NN ratio is > than threshold
61 // return the number of removed points
62 // (corresponding entries being cleared,
63 // i.e. size will be 0)
64 int ratioTest(std::vector<std::vector<cv::DMatch> > &matches);
65
66 // Insert symmetrical matches in symMatches vector
67 void symmetryTest(const std::vector<std::vector<cv::DMatch> >& matches1, const std::vector<std::vector<cv::DMatch> >& matches2, std::vector<cv::DMatch>& symMatches);
68
69 // Match feature points using ratio and symmetry test
70 void robustMatch(const cv::Mat& frame, std::vector<cv::DMatch>& good_matches, std::vector<cv::KeyPoint>& keypoints_frame, const cv::Mat& descriptors_model, const cv::Mat& mask);
71
72 // Match feature points using ratio test
73 void fastRobustMatch(const cv::Mat& frame, std::vector<cv::DMatch>& good_matches, std::vector<cv::KeyPoint>& keypoints_frame, const cv::Mat& mask);
74
75 bool addTrainData(const cv::Mat& frame, std::vector<cv::KeyPoint> &marker_keypoints);
76
77private:
78 // pointer to the feature point detector for training
79 cv::Ptr<cv::FeatureDetector> traindetector_;
80 // pointer to the feature point detector object
81 cv::Ptr<cv::FeatureDetector> detector_;
82 // pointer to the matcher object
83 cv::Ptr<cv::DescriptorMatcher> matcher_;
84 // max ratio between 1st and 2nd NN
85
86 /*
87 // pointer to the feature point detector object
88 cv::FeatureDetector* detector_;
89 // pointer to the feature descriptor extractor object
90 cv::DescriptorExtractor* extractor_;
91 // pointer to the matcher object
92 cv::DescriptorMatcher* matcher_;
93 */
94
95 // max ratio between 1st and 2nd NN
96 float ratio_;
97};
98
104{
105protected:
106 cv::Mat m_image;
107 cv::Mat m_dmask;
109 std::vector<cv::Point2f> m_prevFramePoints;
110 std::vector<cv::Mat> m_prevPyr;
111 std::vector<cv::Mat> m_nextPyr;
112
113public:
114 std::vector<cv::Point2f> m_lastCorner;
115
118 ArFeaturedMarker(std::string filePath, unsigned int nid, float size, unsigned int maxFeatures = 600);
119
122 ArFeaturedMarker(cv::Mat tpl, unsigned int nid, float size, unsigned int maxFeatures = 600);
123
126 ArFeaturedMarker(unsigned int nid, float size);
127
130 virtual ~ArFeaturedMarker();
131
134 void SetTrackedImage(cv::Mat tpl);
135
136 void RegisterNextFrame(cv::Point point, cv::Size size);
137
138 bool GetWarpedMarker(cv::Mat &image);
139
142 void calculateFeaturedExtrinsics(float markerSize, aruco::CameraParameters& camParams, bool setYPerpendicular = true);
143
146 bool detectFeatured(cv::Mat &frame, cv::Mat &color, cv::Size camsize);
147
148 void Update(cv::Mat frame, cv::Mat color, aruco::CameraParameters& camparam, bool reverse);
149private:
150 boost::mutex m_mutex;
151 int64 m_lastTick;
152 unsigned int m_maxFeatures;
153 std::string m_file;
154 cv::Mat m_warped;
155 unsigned int m_minMatches;
156 double m_HomographyThreshold;
157
158 std::vector<cv::Point2f> m_prevTrainPoints;
159 std::vector<cv::Point2f> m_pcorners;
160 cv::Size m_lastFrameSize;
161
162 cv::Ptr<cv::FeatureDetector> m_detector;
163 cv::Ptr<cv::DescriptorMatcher> m_matcher;
164
165 RobustMatcher* m_robustMatcher;
166 std::vector<cv::KeyPoint> m_trainKeypoints;
167 bool m_registerNextFrame;
168 bool m_KalmanInit;
169 cv::Point m_cropPos;
170 cv::Size m_cropSize;
171 FilterDoubleExponential m_CornerSmoothers;
172
175 bool trainMatcher();
176
179 bool initTrackingPicture();
180
183 void CommonConstructor();
184
185
188 bool GetPointsFromMatches(const std::vector<cv::KeyPoint> &querykeypoints, const std::vector<cv::KeyPoint> &trainKeypoints, std::vector<cv::DMatch> matches, std::vector<cv::Point2f> &trainPoints, std::vector<cv::Point2f> &framePoints);
189
192 bool GetHomography(std::vector<cv::Point2f> &trainPoints, std::vector<cv::Point2f> &framePoints, cv::Mat &HMatrix);
193
196 bool detectMotionFlow(cv::Mat &frame, cv::Mat &color, const std::vector<cv::Point2f> &trainPoints, const std::vector<cv::Point2f> &framePoints, std::vector<cv::Point2f> &gtrainPoints, std::vector<cv::Point2f> &gframePoints);
197
200 bool GetCorners(cv::Mat &HMatrix);
201};
202
203#endif
std::vector< cv::Point2f > GetCorners()
Definition ArMarker.cpp:325
void setRatio(float rat)
void setDescriptorMatcher(const cv::Ptr< cv::DescriptorMatcher > &match)
void setFeatureDetector(const cv::Ptr< cv::FeatureDetector > &detect)
bool addTrainData(const cv::Mat &frame, std::vector< cv::KeyPoint > &marker_keypoints)
void fastRobustMatch(const cv::Mat &frame, std::vector< cv::DMatch > &good_matches, std::vector< cv::KeyPoint > &keypoints_frame, const cv::Mat &mask)
int ratioTest(std::vector< std::vector< cv::DMatch > > &matches)
void setTrainFeatureDetector(const cv::Ptr< cv::FeatureDetector > &detect)
void robustMatch(const cv::Mat &frame, std::vector< cv::DMatch > &good_matches, std::vector< cv::KeyPoint > &keypoints_frame, const cv::Mat &descriptors_model, const cv::Mat &mask)
void symmetryTest(const std::vector< std::vector< cv::DMatch > > &matches1, const std::vector< std::vector< cv::DMatch > > &matches2, std::vector< cv::DMatch > &symMatches)
virtual ~RobustMatcher()