Skip to content

Commit 996c6e7

Browse files
committed
Merge pull request #2052 from SBCV:feature_mask
2 parents 64eba17 + ff0167b commit 996c6e7

File tree

6 files changed

+61
-2
lines changed

6 files changed

+61
-2
lines changed

modules/videostab/include/opencv2/videostab/frame_source.hpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,28 @@ class CV_EXPORTS VideoFileSource : public IFrameSource
8686
Ptr<IFrameSource> impl;
8787
};
8888

89+
class MaskFrameSource : public IFrameSource
90+
{
91+
public:
92+
MaskFrameSource(const Ptr<IFrameSource>& source): impl(source) {};
93+
94+
virtual void reset() CV_OVERRIDE { impl->reset(); }
95+
virtual Mat nextFrame() CV_OVERRIDE {
96+
Mat nextFrame = impl->nextFrame();
97+
maskCallback_(nextFrame);
98+
return nextFrame;
99+
}
100+
101+
void setMaskCallback(std::function<void(Mat&)> MaskCallback)
102+
{
103+
maskCallback_ = std::bind(MaskCallback, std::placeholders::_1);
104+
};
105+
106+
private:
107+
Ptr<IFrameSource> impl;
108+
std::function<void(Mat&)> maskCallback_;
109+
};
110+
89111
//! @}
90112

91113
} // namespace videostab

modules/videostab/include/opencv2/videostab/global_motion.hpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,12 @@ class CV_EXPORTS ImageMotionEstimatorBase
180180
virtual void setMotionModel(MotionModel val) { motionModel_ = val; }
181181
virtual MotionModel motionModel() const { return motionModel_; }
182182

183+
virtual void setFrameMask(InputArray mask)
184+
{
185+
if (!mask.empty())
186+
CV_Error(Error::StsNotImplemented, "Mask support is not implemented.");
187+
}
188+
183189
virtual Mat estimate(const Mat &frame0, const Mat &frame1, bool *ok = 0) = 0;
184190

185191
protected:
@@ -208,6 +214,8 @@ class CV_EXPORTS ToFileMotionWriter : public ImageMotionEstimatorBase
208214
virtual void setMotionModel(MotionModel val) CV_OVERRIDE { motionEstimator_->setMotionModel(val); }
209215
virtual MotionModel motionModel() const CV_OVERRIDE { return motionEstimator_->motionModel(); }
210216

217+
virtual void setFrameMask(InputArray mask) CV_OVERRIDE { motionEstimator_->setFrameMask(mask); }
218+
211219
virtual Mat estimate(const Mat &frame0, const Mat &frame1, bool *ok = 0) CV_OVERRIDE;
212220

213221
private:
@@ -235,6 +243,8 @@ class CV_EXPORTS KeypointBasedMotionEstimator : public ImageMotionEstimatorBase
235243
void setOutlierRejector(Ptr<IOutlierRejector> val) { outlierRejector_ = val; }
236244
Ptr<IOutlierRejector> outlierRejector() const { return outlierRejector_; }
237245

246+
virtual void setFrameMask(InputArray mask) CV_OVERRIDE { mask_ = mask.getMat(); }
247+
238248
virtual Mat estimate(const Mat &frame0, const Mat &frame1, bool *ok = 0) CV_OVERRIDE;
239249
Mat estimate(InputArray frame0, InputArray frame1, bool *ok = 0);
240250

@@ -243,6 +253,7 @@ class CV_EXPORTS KeypointBasedMotionEstimator : public ImageMotionEstimatorBase
243253
Ptr<FeatureDetector> detector_;
244254
Ptr<ISparseOptFlowEstimator> optFlowEstimator_;
245255
Ptr<IOutlierRejector> outlierRejector_;
256+
Mat mask_;
246257

247258
std::vector<uchar> status_;
248259
std::vector<KeyPoint> keypointsPrev_;
@@ -263,6 +274,8 @@ class CV_EXPORTS KeypointBasedMotionEstimatorGpu : public ImageMotionEstimatorBa
263274
void setOutlierRejector(Ptr<IOutlierRejector> val) { outlierRejector_ = val; }
264275
Ptr<IOutlierRejector> outlierRejector() const { return outlierRejector_; }
265276

277+
virtual void setFrameMask(InputArray mask) CV_OVERRIDE { mask_ = mask.getMat(); }
278+
266279
virtual Mat estimate(const Mat &frame0, const Mat &frame1, bool *ok = 0) CV_OVERRIDE;
267280
Mat estimate(const cuda::GpuMat &frame0, const cuda::GpuMat &frame1, bool *ok = 0);
268281

@@ -271,6 +284,7 @@ class CV_EXPORTS KeypointBasedMotionEstimatorGpu : public ImageMotionEstimatorBa
271284
Ptr<cuda::CornersDetector> detector_;
272285
SparsePyrLkOptFlowEstimatorGpu optFlowEstimator_;
273286
Ptr<IOutlierRejector> outlierRejector_;
287+
GpuMat mask_;
274288

275289
cuda::GpuMat frame0_, grayFrame0_, frame1_;
276290
cuda::GpuMat pointsPrev_, points_;

modules/videostab/include/opencv2/videostab/stabilizer.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ class CV_EXPORTS StabilizerBase
7777
void setFrameSource(Ptr<IFrameSource> val) { frameSource_ = val; }
7878
Ptr<IFrameSource> frameSource() const { return frameSource_; }
7979

80+
void setMaskSource(const Ptr<IFrameSource>& val) { maskSource_ = val; }
81+
Ptr<IFrameSource> maskSource() const { return maskSource_; }
82+
8083
void setMotionEstimator(Ptr<ImageMotionEstimatorBase> val) { motionEstimator_ = val; }
8184
Ptr<ImageMotionEstimatorBase> motionEstimator() const { return motionEstimator_; }
8285

@@ -110,6 +113,7 @@ class CV_EXPORTS StabilizerBase
110113

111114
Ptr<ILog> log_;
112115
Ptr<IFrameSource> frameSource_;
116+
Ptr<IFrameSource> maskSource_;
113117
Ptr<ImageMotionEstimatorBase> motionEstimator_;
114118
Ptr<DeblurerBase> deblurer_;
115119
Ptr<InpainterBase> inpainter_;

modules/videostab/samples/videostab.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ void printHelp()
8989
" Number of keypoints to find in each frame. The default is 1000.\n"
9090
" --local-outlier-rejection=(yes|no)\n"
9191
" Perform local outlier rejection. The default is no.\n\n"
92+
" --feature-masks=(file_path|no)\n"
93+
" Load masks from file. The default is no.\n\n"
9294
" -sm=, --save-motions=(<file_path>|no)\n"
9395
" Save estimated motions into file. The default is no.\n"
9496
" -lm=, --load-motions=(<file_path>|no)\n"
@@ -297,6 +299,7 @@ int main(int argc, const char **argv)
297299
"{ nkps | 1000 | }"
298300
"{ extra-kps | 0 | }"
299301
"{ local-outlier-rejection | no | }"
302+
"{ feature-masks | no | }"
300303
"{ sm save-motions | no | }"
301304
"{ lm load-motions | no | }"
302305
"{ r radius | 15 | }"
@@ -461,6 +464,19 @@ int main(int argc, const char **argv)
461464
stabilizer->setFrameSource(source);
462465
stabilizer->setMotionEstimator(motionEstBuilder->build());
463466

467+
if (arg("feature-masks") != "no")
468+
{
469+
Ptr<MaskFrameSource> maskSource = makePtr<MaskFrameSource>(
470+
makePtr<VideoFileSource>(arg("feature-masks")));
471+
std::function<void(Mat&)> maskCallback = [](Mat & inputFrame)
472+
{
473+
cv::cvtColor(inputFrame, inputFrame, cv::COLOR_BGR2GRAY);
474+
threshold(inputFrame, inputFrame, 127, 255, THRESH_BINARY);
475+
};
476+
maskSource->setMaskCallback(maskCallback);
477+
stabilizer->setMaskSource(maskSource);
478+
}
479+
464480
// cast stabilizer to simple frame source interface to read stabilized frames
465481
stabilizedFrames.reset(dynamic_cast<IFrameSource*>(stabilizer));
466482

modules/videostab/src/global_motion.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,7 @@ Mat KeypointBasedMotionEstimator::estimate(const Mat &frame0, const Mat &frame1,
725725
Mat KeypointBasedMotionEstimator::estimate(InputArray frame0, InputArray frame1, bool *ok)
726726
{
727727
// find keypoints
728-
detector_->detect(frame0, keypointsPrev_);
728+
detector_->detect(frame0, keypointsPrev_, mask_);
729729
if (keypointsPrev_.empty())
730730
return Mat::eye(3, 3, CV_32F);
731731

@@ -815,7 +815,7 @@ Mat KeypointBasedMotionEstimatorGpu::estimate(const cuda::GpuMat &frame0, const
815815
}
816816

817817
// find keypoints
818-
detector_->detect(grayFrame0, pointsPrev_);
818+
detector_->detect(grayFrame0, pointsPrev_, mask_);
819819

820820
// find correspondences
821821
optFlowEstimator_.run(frame0, frame1, pointsPrev_, points_, status_);

modules/videostab/src/stabilizer.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,9 @@ void TwoPassStabilizer::runPrePassIfNecessary()
391391
{
392392
if (frameCount_ > 0)
393393
{
394+
if (maskSource_)
395+
motionEstimator_->setFrameMask(maskSource_->nextFrame());
396+
394397
motions_.push_back(motionEstimator_->estimate(prevFrame, frame, &ok));
395398

396399
if (doWobbleSuppression_)

0 commit comments

Comments
 (0)