Commit b549900f authored by Alexey Spizhevoy's avatar Alexey Spizhevoy

Minor fixes and updates in videostab module and sample

parent 673f879c
...@@ -101,7 +101,7 @@ public: ...@@ -101,7 +101,7 @@ public:
virtual void setMotionModel(MotionModel val) { motionModel_ = val; } virtual void setMotionModel(MotionModel val) { motionModel_ = val; }
virtual MotionModel motionModel() const { return motionModel_; } virtual MotionModel motionModel() const { return motionModel_; }
virtual Mat estimate(const Mat &frame0, const Mat &frame1) = 0; virtual Mat estimate(const Mat &frame0, const Mat &frame1, bool *ok = 0) = 0;
protected: protected:
MotionModel motionModel_; MotionModel motionModel_;
...@@ -111,7 +111,7 @@ class CV_EXPORTS FromFileMotionReader : public GlobalMotionEstimatorBase ...@@ -111,7 +111,7 @@ class CV_EXPORTS FromFileMotionReader : public GlobalMotionEstimatorBase
{ {
public: public:
FromFileMotionReader(const std::string &path); FromFileMotionReader(const std::string &path);
virtual Mat estimate(const Mat &frame0, const Mat &frame1); virtual Mat estimate(const Mat &frame0, const Mat &frame1, bool *ok = 0);
private: private:
std::ifstream file_; std::ifstream file_;
...@@ -121,7 +121,7 @@ class CV_EXPORTS ToFileMotionWriter : public GlobalMotionEstimatorBase ...@@ -121,7 +121,7 @@ class CV_EXPORTS ToFileMotionWriter : public GlobalMotionEstimatorBase
{ {
public: public:
ToFileMotionWriter(const std::string &path, Ptr<GlobalMotionEstimatorBase> estimator); ToFileMotionWriter(const std::string &path, Ptr<GlobalMotionEstimatorBase> estimator);
virtual Mat estimate(const Mat &frame0, const Mat &frame1); virtual Mat estimate(const Mat &frame0, const Mat &frame1, bool *ok = 0);
private: private:
std::ofstream file_; std::ofstream file_;
...@@ -148,7 +148,7 @@ public: ...@@ -148,7 +148,7 @@ public:
void setMinInlierRatio(float val) { minInlierRatio_ = val; } void setMinInlierRatio(float val) { minInlierRatio_ = val; }
float minInlierRatio() const { return minInlierRatio_; } float minInlierRatio() const { return minInlierRatio_; }
virtual Mat estimate(const Mat &frame0, const Mat &frame1); virtual Mat estimate(const Mat &frame0, const Mat &frame1, bool *ok = 0);
private: private:
Ptr<FeatureDetector> detector_; Ptr<FeatureDetector> detector_;
......
...@@ -295,12 +295,14 @@ FromFileMotionReader::FromFileMotionReader(const string &path) ...@@ -295,12 +295,14 @@ FromFileMotionReader::FromFileMotionReader(const string &path)
} }
Mat FromFileMotionReader::estimate(const Mat &/*frame0*/, const Mat &/*frame1*/) Mat FromFileMotionReader::estimate(const Mat &/*frame0*/, const Mat &/*frame1*/, bool *ok)
{ {
Mat_<float> M(3, 3); Mat_<float> M(3, 3);
bool ok_;
file_ >> M(0,0) >> M(0,1) >> M(0,2) file_ >> M(0,0) >> M(0,1) >> M(0,2)
>> M(1,0) >> M(1,1) >> M(1,2) >> M(1,0) >> M(1,1) >> M(1,2)
>> M(2,0) >> M(2,1) >> M(2,2); >> M(2,0) >> M(2,1) >> M(2,2) >> ok_;
if (ok) *ok = ok_;
return M; return M;
} }
...@@ -313,12 +315,14 @@ ToFileMotionWriter::ToFileMotionWriter(const string &path, Ptr<GlobalMotionEstim ...@@ -313,12 +315,14 @@ ToFileMotionWriter::ToFileMotionWriter(const string &path, Ptr<GlobalMotionEstim
} }
Mat ToFileMotionWriter::estimate(const Mat &frame0, const Mat &frame1) Mat ToFileMotionWriter::estimate(const Mat &frame0, const Mat &frame1, bool *ok)
{ {
Mat_<float> M = estimator_->estimate(frame0, frame1); bool ok_;
Mat_<float> M = estimator_->estimate(frame0, frame1, &ok_);
file_ << M(0,0) << " " << M(0,1) << " " << M(0,2) << " " file_ << M(0,0) << " " << M(0,1) << " " << M(0,2) << " "
<< M(1,0) << " " << M(1,1) << " " << M(1,2) << " " << M(1,0) << " " << M(1,1) << " " << M(1,2) << " "
<< M(2,0) << " " << M(2,1) << " " << M(2,2) << endl; << M(2,0) << " " << M(2,1) << " " << M(2,2) << " " << ok_ << endl;
if (ok) *ok = ok_;
return M; return M;
} }
...@@ -343,7 +347,7 @@ PyrLkRobustMotionEstimator::PyrLkRobustMotionEstimator(MotionModel model) ...@@ -343,7 +347,7 @@ PyrLkRobustMotionEstimator::PyrLkRobustMotionEstimator(MotionModel model)
} }
Mat PyrLkRobustMotionEstimator::estimate(const Mat &frame0, const Mat &frame1) Mat PyrLkRobustMotionEstimator::estimate(const Mat &frame0, const Mat &frame1, bool *ok)
{ {
detector_->detect(frame0, keypointsPrev_); detector_->detect(frame0, keypointsPrev_);
...@@ -402,8 +406,12 @@ Mat PyrLkRobustMotionEstimator::estimate(const Mat &frame0, const Mat &frame1) ...@@ -402,8 +406,12 @@ Mat PyrLkRobustMotionEstimator::estimate(const Mat &frame0, const Mat &frame1)
rmse = sqrt(rmse / static_cast<float>(ninliers)); rmse = sqrt(rmse / static_cast<float>(ninliers));
} }
if (ok) *ok = true;
if (rmse > maxRmse_ || static_cast<float>(ninliers) / pointsGood_.size() < minInlierRatio_) if (rmse > maxRmse_ || static_cast<float>(ninliers) / pointsGood_.size() < minInlierRatio_)
{
M = Mat::eye(3, 3, CV_32F); M = Mat::eye(3, 3, CV_32F);
if (ok) *ok = false;
}
return M; return M;
} }
......
...@@ -338,6 +338,7 @@ void TwoPassStabilizer::runPrePassIfNecessary() ...@@ -338,6 +338,7 @@ void TwoPassStabilizer::runPrePassIfNecessary()
WobbleSuppressorBase *wobbleSuppressor = static_cast<WobbleSuppressorBase*>(wobbleSuppressor_); WobbleSuppressorBase *wobbleSuppressor = static_cast<WobbleSuppressorBase*>(wobbleSuppressor_);
doWobbleSuppression_ = dynamic_cast<NullWobbleSuppressor*>(wobbleSuppressor) == 0; doWobbleSuppression_ = dynamic_cast<NullWobbleSuppressor*>(wobbleSuppressor) == 0;
bool okEst;
while (!(frame = frameSource_->nextFrame()).empty()) while (!(frame = frameSource_->nextFrame()).empty())
{ {
if (frameCount_ > 0) if (frameCount_ > 0)
...@@ -345,8 +346,11 @@ void TwoPassStabilizer::runPrePassIfNecessary() ...@@ -345,8 +346,11 @@ void TwoPassStabilizer::runPrePassIfNecessary()
motions_.push_back(motionEstimator_->estimate(prevFrame, frame)); motions_.push_back(motionEstimator_->estimate(prevFrame, frame));
if (doWobbleSuppression_) if (doWobbleSuppression_)
{ {
motions2_.push_back( Mat M = wobbleSuppressor_->motionEstimator()->estimate(prevFrame, frame, &okEst);
wobbleSuppressor_->motionEstimator()->estimate(prevFrame, frame)); if (okEst)
motions2_.push_back(M);
else
motions2_.push_back(motions_.back());
} }
} }
else else
......
...@@ -68,10 +68,9 @@ void printHelp() ...@@ -68,10 +68,9 @@ void printHelp()
" -m, --model=(transl|transl_and_scale|linear_sim|affine|homography)\n" " -m, --model=(transl|transl_and_scale|linear_sim|affine|homography)\n"
" Set motion model. The default is affine.\n" " Set motion model. The default is affine.\n"
" --outlier-ratio=<float_number>\n" " --outlier-ratio=<float_number>\n"
" Outliers ratio in motion estimation. The default is 0.5.\n" " Motion estimation outlier ratio hypothesis. The default is 0.5."
" --min-inlier-ratio=<float_number>\n" " --min-inlier-ratio=<float_number>\n"
" Minimum inlier ratio to decide if estimated motion is OK. The default is 0.1,\n" " Minimum inlier ratio to decide if estimated motion is OK. The default is 0.1.\n\n"
" but you may want to increase it.\n\n"
" -sm, --save-motions=(<file_path>|no)\n" " -sm, --save-motions=(<file_path>|no)\n"
" Save estimated motions into file. The default is no.\n" " Save estimated motions into file. The default is no.\n"
" -lm, --load-motions=(<file_path>|no)\n" " -lm, --load-motions=(<file_path>|no)\n"
...@@ -113,6 +112,10 @@ void printHelp() ...@@ -113,6 +112,10 @@ void printHelp()
" --ws-model=(transl|transl_and_scale|linear_sim|affine|homography)\n" " --ws-model=(transl|transl_and_scale|linear_sim|affine|homography)\n"
" Set wobble suppression motion model (must have more DOF than motion \n" " Set wobble suppression motion model (must have more DOF than motion \n"
" estimation model). The default is homography.\n" " estimation model). The default is homography.\n"
" --ws-min-inlier-ratio=<float_number>\n"
" Minimum inlier ratio to decide if estimated motion is OK. The default is 0.1.\n"
" --ws-outlier-ratio=<float_number>\n"
" Motion estimation outlier ratio hypothesis. The default is 0.5.\n"
" -sm2, --save-motions2=(<file_path>|no)\n" " -sm2, --save-motions2=(<file_path>|no)\n"
" Save motions estimated for wobble suppression. The default is no.\n" " Save motions estimated for wobble suppression. The default is no.\n"
" -lm2, --load-motions2=(<file_path>|no)\n" " -lm2, --load-motions2=(<file_path>|no)\n"
...@@ -157,6 +160,8 @@ int main(int argc, const char **argv) ...@@ -157,6 +160,8 @@ int main(int argc, const char **argv)
"{ ws | wobble-suppress | no | }" "{ ws | wobble-suppress | no | }"
"{ | ws-period | 30 | }" "{ | ws-period | 30 | }"
"{ | ws-model | homography | }" "{ | ws-model | homography | }"
"{ | ws-min-inlier-ratio | 0.1 | }"
"{ | ws-outlier-ratio | 0.5 | }"
"{ sm2 | save-motions2 | no | }" "{ sm2 | save-motions2 | no | }"
"{ lm2 | load-motions2 | no | }" "{ lm2 | load-motions2 | no | }"
"{ o | output | stabilized.avi | }" "{ o | output | stabilized.avi | }"
...@@ -192,24 +197,41 @@ int main(int argc, const char **argv) ...@@ -192,24 +197,41 @@ int main(int argc, const char **argv)
MoreAccurateMotionWobbleSuppressor *ws = new MoreAccurateMotionWobbleSuppressor(); MoreAccurateMotionWobbleSuppressor *ws = new MoreAccurateMotionWobbleSuppressor();
twoPassStabilizer->setWobbleSuppressor(ws); twoPassStabilizer->setWobbleSuppressor(ws);
ws->setPeriod(argi("ws-period")); ws->setPeriod(argi("ws-period"));
PyrLkRobustMotionEstimator *est = 0;
if (arg("ws-model") == "transl") if (arg("ws-model") == "transl")
ws->setMotionEstimator(new PyrLkRobustMotionEstimator(TRANSLATION)); est = new PyrLkRobustMotionEstimator(TRANSLATION);
else if (arg("ws-model") == "transl_and_scale") else if (arg("ws-model") == "transl_and_scale")
ws->setMotionEstimator(new PyrLkRobustMotionEstimator(TRANSLATION_AND_SCALE)); est = new PyrLkRobustMotionEstimator(TRANSLATION_AND_SCALE);
else if (arg("ws-model") == "linear_sim") else if (arg("ws-model") == "linear_sim")
ws->setMotionEstimator(new PyrLkRobustMotionEstimator(LINEAR_SIMILARITY)); est = new PyrLkRobustMotionEstimator(LINEAR_SIMILARITY);
else if (arg("ws-model") == "affine") else if (arg("ws-model") == "affine")
ws->setMotionEstimator(new PyrLkRobustMotionEstimator(AFFINE)); est = new PyrLkRobustMotionEstimator(AFFINE);
else if (arg("ws-model") == "homography") else if (arg("ws-model") == "homography")
ws->setMotionEstimator(new PyrLkRobustMotionEstimator(HOMOGRAPHY)); est = new PyrLkRobustMotionEstimator(HOMOGRAPHY);
else else
{
delete est;
throw runtime_error("unknown wobble suppression motion model: " + arg("ws-model")); throw runtime_error("unknown wobble suppression motion model: " + arg("ws-model"));
}
RansacParams ransac = est->ransacParams();
ransac.eps = argf("ws-outlier-ratio");
est->setRansacParams(ransac);
est->setMinInlierRatio(argf("ws-min-inlier-ratio"));
ws->setMotionEstimator(est);
MotionModel model = est->motionModel();
if (arg("load-motions2") != "no") if (arg("load-motions2") != "no")
{
ws->setMotionEstimator(new FromFileMotionReader(arg("load-motions2"))); ws->setMotionEstimator(new FromFileMotionReader(arg("load-motions2")));
ws->motionEstimator()->setMotionModel(model);
}
if (arg("save-motions2") != "no") if (arg("save-motions2") != "no")
{ {
Ptr<GlobalMotionEstimatorBase> est = ws->motionEstimator(); ws->setMotionEstimator(new ToFileMotionWriter(arg("save-motions2"), ws->motionEstimator()));
ws->setMotionEstimator(new ToFileMotionWriter(arg("save-motions2"), est)); ws->motionEstimator()->setMotionModel(model);
} }
} }
} }
...@@ -233,6 +255,7 @@ int main(int argc, const char **argv) ...@@ -233,6 +255,7 @@ int main(int argc, const char **argv)
stabilizer->setFrameSource(source); stabilizer->setFrameSource(source);
PyrLkRobustMotionEstimator *est = 0; PyrLkRobustMotionEstimator *est = 0;
if (arg("model") == "transl") if (arg("model") == "transl")
est = new PyrLkRobustMotionEstimator(TRANSLATION); est = new PyrLkRobustMotionEstimator(TRANSLATION);
else if (arg("model") == "transl_and_scale") else if (arg("model") == "transl_and_scale")
...@@ -248,24 +271,22 @@ int main(int argc, const char **argv) ...@@ -248,24 +271,22 @@ int main(int argc, const char **argv)
delete est; delete est;
throw runtime_error("unknown motion model: " + arg("model")); throw runtime_error("unknown motion model: " + arg("model"));
} }
RansacParams ransac = est->ransacParams(); RansacParams ransac = est->ransacParams();
ransac.eps = argf("outlier-ratio"); ransac.eps = argf("outlier-ratio");
est->setRansacParams(ransac); est->setRansacParams(ransac);
est->setMinInlierRatio(argf("min-inlier-ratio")); est->setMinInlierRatio(argf("min-inlier-ratio"));
stabilizer->setMotionEstimator(est); stabilizer->setMotionEstimator(est);
MotionModel model = stabilizer->motionEstimator()->motionModel();
if (arg("load-motions") != "no") if (arg("load-motions") != "no")
{ {
MotionModel model = stabilizer->motionEstimator()->motionModel();
stabilizer->setMotionEstimator(new FromFileMotionReader(arg("load-motions"))); stabilizer->setMotionEstimator(new FromFileMotionReader(arg("load-motions")));
stabilizer->motionEstimator()->setMotionModel(model); stabilizer->motionEstimator()->setMotionModel(model);
} }
if (arg("save-motions") != "no") if (arg("save-motions") != "no")
{ {
MotionModel model = stabilizer->motionEstimator()->motionModel(); stabilizer->setMotionEstimator(new ToFileMotionWriter(arg("save-motions"), stabilizer->motionEstimator()));
stabilizer->setMotionEstimator(
new ToFileMotionWriter(arg("save-motions"), stabilizer->motionEstimator()));
stabilizer->motionEstimator()->setMotionModel(model); stabilizer->motionEstimator()->setMotionModel(model);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment