OpenCV 4.5.3(日本語機械翻訳)
global_motion.hpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
8 //
9 //
10 // License Agreement
11 // For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2009-2011, Willow Garage Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
16 //
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
19 //
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
22 //
23 // * Redistribution's in binary form must reproduce the above copyright notice,
24 // this list of conditions and the following disclaimer in the documentation
25 // and/or other materials provided with the distribution.
26 //
27 // * The name of the copyright holders may not be used to endorse or promote products
28 // derived from this software without specific prior written permission.
29 //
30 // This software is provided by the copyright holders and contributors "as is" and
31 // any express or implied warranties, including, but not limited to, the implied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
40 //
41 //M*/
42
43 #ifndef OPENCV_VIDEOSTAB_GLOBAL_MOTION_HPP
44 #define OPENCV_VIDEOSTAB_GLOBAL_MOTION_HPP
45
46 #include <vector>
47 #include <fstream>
48 #include "opencv2/core.hpp"
49 #include "opencv2/features2d.hpp"
50 #include "opencv2/opencv_modules.hpp"
51 #include "opencv2/videostab/optical_flow.hpp"
52 #include "opencv2/videostab/motion_core.hpp"
53 #include "opencv2/videostab/outlier_rejection.hpp"
54
55 #ifdef HAVE_OPENCV_CUDAIMGPROC
56 # include "opencv2/cudaimgproc.hpp"
57 #endif
58
59 namespace cv
60{
61 namespace videostab
62{
63
66
78 InputOutputArray points0, InputOutputArray points1, int model = MM_AFFINE,
79 float *rmse = 0);
80
91 InputArray points0, InputArray points1, int model = MM_AFFINE,
92 const RansacParams &params = RansacParams::default2dMotion(MM_AFFINE),
93 float *rmse = 0, int *ninliers = 0);
94
97 class CV_EXPORTS MotionEstimatorBase
98{
99 public:
100 virtual ~MotionEstimatorBase() {}
101
106 virtual void setMotionModel(MotionModel val) { motionModel_ = val; }
107
111 virtual MotionModel motionModel() const { return motionModel_; }
112
120 virtual Mat estimate(InputArray points0, InputArray points1, bool *ok = 0) = 0;
121
122 protected:
123 MotionEstimatorBase(MotionModel model) { setMotionModel(model); }
124
125 private:
126 MotionModel motionModel_;
127};
128
132{
133 public:
134 MotionEstimatorRansacL2(MotionModel model = MM_AFFINE);
135
136 void setRansacParams(const RansacParams &val) { ransacParams_ = val; }
137 RansacParams ransacParams() const { return ransacParams_; }
138
139 void setMinInlierRatio(float val) { minInlierRatio_ = val; }
140 float minInlierRatio() const { return minInlierRatio_; }
141
142 virtual Mat estimate(InputArray points0, InputArray points1, bool *ok = 0) CV_OVERRIDE;
143
144private:
145 RansacParams ransacParams_;
146 float minInlierRatio_;
147};
148
153 class CV_EXPORTS MotionEstimatorL1 : public MotionEstimatorBase
154{
155 public:
156 MotionEstimatorL1(MotionModel model = MM_AFFINE);
157
158 virtual Mat estimate(InputArray points0, InputArray points1, bool *ok = 0) CV_OVERRIDE;
159
160private:
161 std::vector<double> obj_, collb_, colub_;
162 std::vector<double> elems_, rowlb_, rowub_;
163 std::vector<int> rows_, cols_;
164
165 void set(int row, int col, double coef)
166 {
167 rows_.push_back(row);
168 cols_.push_back(col);
169 elems_.push_back(coef);
170 }
171};
172
175 class CV_EXPORTS ImageMotionEstimatorBase
176{
177 public:
178 virtual ~ImageMotionEstimatorBase() {}
179
180 virtual void setMotionModel(MotionModel val) { motionModel_ = val; }
181 virtual MotionModel motionModel() const { return motionModel_; }
182
183 virtual void setFrameMask(InputArray mask)
184 {
185 if (!mask.empty())
186 CV_Error(Error::StsNotImplemented, "Mask support is not implemented.");
187 }
188
189 virtual Mat estimate(const Mat &frame0, const Mat &frame1, bool *ok = 0) = 0;
190
191 protected:
192 ImageMotionEstimatorBase(MotionModel model) { setMotionModel(model); }
193
194 private:
195 MotionModel motionModel_;
196};
197
199{
200 public:
201 FromFileMotionReader(const String &path);
202
203 virtual Mat estimate(const Mat &frame0, const Mat &frame1, bool *ok = 0) CV_OVERRIDE;
204
205 private:
206 std::ifstream file_;
207};
208
210{
211 public:
212 ToFileMotionWriter(const String &path, Ptr<ImageMotionEstimatorBase> estimator);
213
214 virtual void setMotionModel(MotionModel val) CV_OVERRIDE { motionEstimator_->setMotionModel(val); }
215 virtual MotionModel motionModel() const CV_OVERRIDE { return motionEstimator_->motionModel(); }
216
217 virtual void setFrameMask(InputArray mask) CV_OVERRIDE { motionEstimator_->setFrameMask(mask); }
218
219 virtual Mat estimate(const Mat &frame0, const Mat &frame1, bool *ok = 0) CV_OVERRIDE;
220
221 private:
222 std::ofstream file_;
223 Ptr<ImageMotionEstimatorBase> motionEstimator_;
224};
225
230{
231 public:
233
234 virtual void setMotionModel(MotionModel val) CV_OVERRIDE { motionEstimator_->setMotionModel(val); }
235 virtual MotionModel motionModel() const CV_OVERRIDE { return motionEstimator_->motionModel(); }
236
237 void setDetector(Ptr<FeatureDetector> val) { detector_ = val; }
238 Ptr<FeatureDetector> detector() const { return detector_; }
239
240 void setOpticalFlowEstimator(Ptr<ISparseOptFlowEstimator> val) { optFlowEstimator_ = val; }
241 Ptr<ISparseOptFlowEstimator> opticalFlowEstimator() const { return optFlowEstimator_; }
242
243 void setOutlierRejector(Ptr<IOutlierRejector> val) { outlierRejector_ = val; }
244 Ptr<IOutlierRejector> outlierRejector() const { return outlierRejector_; }
245
246 virtual void setFrameMask(InputArray mask) CV_OVERRIDE { mask_ = mask.getMat(); }
247
248 virtual Mat estimate(const Mat &frame0, const Mat &frame1, bool *ok = 0) CV_OVERRIDE;
249 Mat estimate(InputArray frame0, InputArray frame1, bool *ok = 0);
250
251 private:
252 Ptr<MotionEstimatorBase> motionEstimator_;
253 Ptr<FeatureDetector> detector_;
254 Ptr<ISparseOptFlowEstimator> optFlowEstimator_;
255 Ptr<IOutlierRejector> outlierRejector_;
256 Mat mask_;
257
258 std::vector<uchar> status_;
259 std::vector<KeyPoint> keypointsPrev_;
260 std::vector<Point2f> pointsPrev_, points_;
261 std::vector<Point2f> pointsPrevGood_, pointsGood_;
262};
263
264 #if defined(HAVE_OPENCV_CUDAIMGPROC) && defined(HAVE_OPENCV_CUDAOPTFLOW)
265
266 class CV_EXPORTS KeypointBasedMotionEstimatorGpu : public ImageMotionEstimatorBase
267{
268 public:
269 KeypointBasedMotionEstimatorGpu(Ptr<MotionEstimatorBase> estimator);
270
271 virtual void setMotionModel(MotionModel val) CV_OVERRIDE { motionEstimator_->setMotionModel(val); }
272 virtual MotionModel motionModel() const CV_OVERRIDE { return motionEstimator_->motionModel(); }
273
274 void setOutlierRejector(Ptr<IOutlierRejector> val) { outlierRejector_ = val; }
275 Ptr<IOutlierRejector> outlierRejector() const { return outlierRejector_; }
276
277 virtual Mat estimate(const Mat &frame0, const Mat &frame1, bool *ok = 0) CV_OVERRIDE;
278 Mat estimate(const cuda::GpuMat &frame0, const cuda::GpuMat &frame1, bool *ok = 0);
279
280private:
281 Ptr<MotionEstimatorBase> motionEstimator_;
282 Ptr<cuda::CornersDetector> detector_;
283 SparsePyrLkOptFlowEstimatorGpu optFlowEstimator_;
284 Ptr<IOutlierRejector> outlierRejector_;
285
286 cuda::GpuMat frame0_, grayFrame0_, frame1_;
287 cuda::GpuMat pointsPrev_, points_;
288 cuda::GpuMat status_;
289
290 Mat hostPointsPrev_, hostPoints_;
291 std::vector<Point2f> hostPointsPrevTmp_, hostPointsTmp_;
292 std::vector<uchar> rejectionStatus_;
293};
294
295 #endif // defined(HAVE_OPENCV_CUDAIMGPROC) && defined(HAVE_OPENCV_CUDAOPTFLOW)
296
304 CV_EXPORTS Mat getMotion(int from, int to, const std::vector<Mat> &motions);
305
307
308} // namespace videostab
309} // namespace cv
310
311 #endif
Definition: mat.hpp:386
n-dimensional dense array class
Definition: mat.hpp:802
void push_back(const _Tp &elem)
Adds elements to the bottom of the matrix.
Definition: global_motion.hpp:199
Base class for global 2D motion estimation methods which take frames as input.
Definition: global_motion.hpp:176
Describes a global 2D motion estimation method which uses keypoints detection and optical flow for ma...
Definition: global_motion.hpp:230
Base class for all global motion estimation methods.
Definition: global_motion.hpp:98
virtual MotionModel motionModel() const
Definition: global_motion.hpp:111
virtual void setMotionModel(MotionModel val)
Sets motion model.
Definition: global_motion.hpp:106
virtual Mat estimate(InputArray points0, InputArray points1, bool *ok=0)=0
Estimates global motion between two 2D point clouds.
Describes a global 2D motion estimation method which minimizes L1 error.
Definition: global_motion.hpp:154
virtual Mat estimate(InputArray points0, InputArray points1, bool *ok=0) CV_OVERRIDE
Estimates global motion between two 2D point clouds.
Describes a robust RANSAC-based global 2D motion estimation method which minimizes L2 error.
Definition: global_motion.hpp:132
virtual Mat estimate(InputArray points0, InputArray points1, bool *ok=0) CV_OVERRIDE
Estimates global motion between two 2D point clouds.
Definition: global_motion.hpp:210
#define CV_Error(code, msg)
Call the error handler.
Definition: base.hpp:320
CV_EXPORTS Mat getMotion(int from, int to, const std::vector< Mat > &motions)
Computes motion between two frames assuming that all the intermediate motions are known.
CV_EXPORTS Mat estimateGlobalMotionRansac(InputArray points0, InputArray points1, int model=MM_AFFINE, const RansacParams &params=RansacParams::default2dMotion(MM_AFFINE), float *rmse=0, int *ninliers=0)
Estimates best global motion between two 2D point clouds robustly (using RANSAC method).
CV_EXPORTS Mat estimateGlobalMotionLeastSquares(InputOutputArray points0, InputOutputArray points1, int model=MM_AFFINE, float *rmse=0)
Estimates best global motion between two 2D point clouds in the least-squares sense.
MotionModel
Describes motion model between two point clouds.
Definition: motion_core.hpp:60
cv
"black box" representation of the file storage associated with a file on disk.
Definition: aruco.hpp:75
Definition: cvstd_wrapper.hpp:74
Describes RANSAC method parameters.
Definition: motion_core.hpp:74
static RansacParams default2dMotion(MotionModel model)
Definition: motion_core.hpp:102