Commit d9d47553 authored by Alexey Spizhevoy's avatar Alexey Spizhevoy

Updated wobble suppression code in videostab module

parent fa09f3d1
...@@ -131,7 +131,7 @@ private: ...@@ -131,7 +131,7 @@ private:
class CV_EXPORTS PyrLkRobustMotionEstimator : public GlobalMotionEstimatorBase class CV_EXPORTS PyrLkRobustMotionEstimator : public GlobalMotionEstimatorBase
{ {
public: public:
PyrLkRobustMotionEstimator(); PyrLkRobustMotionEstimator(MotionModel model = AFFINE);
void setDetector(Ptr<FeatureDetector> val) { detector_ = val; } void setDetector(Ptr<FeatureDetector> val) { detector_ = val; }
Ptr<FeatureDetector> detector() const { return detector_; } Ptr<FeatureDetector> detector() const { return detector_; }
......
...@@ -97,7 +97,16 @@ public: ...@@ -97,7 +97,16 @@ public:
class CV_EXPORTS MoreAccurateMotionWobbleSuppressor : public WobbleSuppressorBase class CV_EXPORTS MoreAccurateMotionWobbleSuppressor : public WobbleSuppressorBase
{ {
public: public:
MoreAccurateMotionWobbleSuppressor();
void setPeriod(int val) { period_ = val; }
int period() const { return period_; }
virtual void suppress(int idx, const Mat &frame, Mat &result); virtual void suppress(int idx, const Mat &frame, Mat &result);
private:
int period_;
Mat_<float> mapx_, mapy_;
}; };
} // namespace videostab } // namespace videostab
......
...@@ -323,12 +323,21 @@ Mat ToFileMotionWriter::estimate(const Mat &frame0, const Mat &frame1) ...@@ -323,12 +323,21 @@ Mat ToFileMotionWriter::estimate(const Mat &frame0, const Mat &frame1)
} }
PyrLkRobustMotionEstimator::PyrLkRobustMotionEstimator() PyrLkRobustMotionEstimator::PyrLkRobustMotionEstimator(MotionModel model)
: ransacParams_(RansacParams::affine2dMotionStd())
{ {
setDetector(new GoodFeaturesToTrackDetector()); setDetector(new GoodFeaturesToTrackDetector());
setOptFlowEstimator(new SparsePyrLkOptFlowEstimator()); setOptFlowEstimator(new SparsePyrLkOptFlowEstimator());
setMotionModel(AFFINE); setMotionModel(model);
if (model == TRANSLATION)
setRansacParams(RansacParams::translation2dMotionStd());
else if (model == TRANSLATION_AND_SCALE)
setRansacParams(RansacParams::translationAndScale2dMotionStd());
else if (model == LINEAR_SIMILARITY)
setRansacParams(RansacParams::linearSimilarity2dMotionStd());
else if (model == AFFINE)
setRansacParams(RansacParams::affine2dMotionStd());
else if (model == HOMOGRAPHY)
setRansacParams(RansacParams::homography2dMotionStd());
setMaxRmse(0.5f); setMaxRmse(0.5f);
setMinInlierRatio(0.1f); setMinInlierRatio(0.1f);
} }
......
...@@ -51,8 +51,7 @@ namespace cv ...@@ -51,8 +51,7 @@ namespace cv
namespace videostab namespace videostab
{ {
WobbleSuppressorBase::WobbleSuppressorBase() WobbleSuppressorBase::WobbleSuppressorBase() : motions_(0), stabilizationMotions_(0)
: motions_(0), stabilizationMotions_(0)
{ {
PyrLkRobustMotionEstimator *est = new PyrLkRobustMotionEstimator(); PyrLkRobustMotionEstimator *est = new PyrLkRobustMotionEstimator();
est->setMotionModel(HOMOGRAPHY); est->setMotionModel(HOMOGRAPHY);
...@@ -67,14 +66,61 @@ void NullWobbleSuppressor::suppress(int /*idx*/, const Mat &frame, Mat &result) ...@@ -67,14 +66,61 @@ void NullWobbleSuppressor::suppress(int /*idx*/, const Mat &frame, Mat &result)
} }
MoreAccurateMotionWobbleSuppressor::MoreAccurateMotionWobbleSuppressor()
{
setPeriod(30);
}
void MoreAccurateMotionWobbleSuppressor::suppress(int idx, const Mat &frame, Mat &result) void MoreAccurateMotionWobbleSuppressor::suppress(int idx, const Mat &frame, Mat &result)
{ {
CV_Assert(motions_ && stabilizationMotions_); CV_Assert(motions_ && stabilizationMotions_);
// TODO implement if (idx % period_ == 0)
CV_Error(CV_StsNotImplemented, "MoreAccurateMotionWobbleSuppressor"); {
result = frame;
return;
}
result = frame; int k1 = idx / period_ * period_;
int k2 = std::min(k1 + period_, frameCount_ - 1);
Mat S1 = (*stabilizationMotions_)[idx];
Mat_<float> ML = S1 * getMotion(k1, idx, *motions2_) * getMotion(k1, idx, *motions_).inv() * S1.inv();
Mat_<float> MR = S1 * getMotion(idx, k2, *motions2_).inv() * getMotion(idx, k2, *motions_) * S1.inv();
mapx_.create(frame.size());
mapy_.create(frame.size());
float xl, yl, zl, wl;
float xr, yr, zr, wr;
for (int y = 0; y < frame.rows; ++y)
{
for (int x = 0; x < frame.cols; ++x)
{
xl = ML(0,0)*x + ML(0,1)*y + ML(0,2);
yl = ML(1,0)*x + ML(1,1)*y + ML(1,2);
zl = ML(2,0)*x + ML(2,1)*y + ML(2,2);
xl /= zl; yl /= zl;
wl = 1.f / (sqrt(sqr(x - xl) + sqr(y - yl)) + 1e-5f);
xr = MR(0,0)*x + MR(0,1)*y + MR(0,2);
yr = MR(1,0)*x + MR(1,1)*y + MR(1,2);
zr = MR(2,0)*x + MR(2,1)*y + MR(2,2);
xr /= zr; yr /= zr;
wr = 1.f / (sqrt(sqr(x - xr) + sqr(y - yr)) + 1e-5f);
mapx_(y,x) = (wl * xl + wr * xr) / (wl + wr);
mapy_(y,x) = (wl * yl + wr * yr) / (wl + wr);
}
}
if (result.data == frame.data)
result = Mat(frame.size(), frame.type());
remap(frame, result, mapx_, mapy_, INTER_LINEAR);
} }
} // namespace videostab } // namespace videostab
......
...@@ -72,52 +72,60 @@ void printHelp() ...@@ -72,52 +72,60 @@ void printHelp()
" --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"
" but you may want to increase it.\n\n" " but you may want to increase it.\n\n"
" --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"
" --load-motions=(<file_path>|no)\n" " -lm, --load-motions=(<file_path>|no)\n"
" Load motions from file. The default is no.\n\n" " Load motions from file. The default is no.\n\n"
" -r, --radius=<int_number>\n" " -r, --radius=<int_number>\n"
" Set sliding window radius. The default is 15.\n" " Set sliding window radius. The default is 15.\n"
" --stdev=(<float_number>|auto)\n" " --stdev=(<float_number>|auto)\n"
" Set smoothing weights standard deviation. The default is sqrt(radius),\n" " Set smoothing weights standard deviation. The default is auto\n"
" i.e. auto.\n\n" " (i.e. sqrt(radius)).\n"
" --deblur=(yes|no)\n" " --deblur=(yes|no)\n"
" Do deblurring.\n" " Do deblurring.\n"
" --deblur-sens=<float_number>\n" " --deblur-sens=<float_number>\n"
" Set deblurring sensitivity (from 0 to +inf). The default is 0.1.\n\n" " Set deblurring sensitivity (from 0 to +inf). The default is 0.1.\n\n"
" -t, --trim-ratio=<float_number>\n" " -t, --trim-ratio=<float_number>\n"
" Set trimming ratio (from 0 to 0.5). The default is 0.1.\n" " Set trimming ratio (from 0 to 0.5). The default is 0.1.\n"
" --est-trim=(yes|no)\n" " -et, --est-trim=(yes|no)\n"
" Estimate trim ratio automatically. The default is yes (that leads to two passes,\n" " Estimate trim ratio automatically. The default is yes.\n"
" you can turn it off if you want to use one pass only).\n" " -ic, --incl-constr=(yes|no)\n"
" --incl-constr=(yes|no)\n"
" Ensure the inclusion constraint is always satisfied. The default is no.\n\n" " Ensure the inclusion constraint is always satisfied. The default is no.\n\n"
" --border-mode=(replicate|reflect|const)\n" " -bm, --border-mode=(replicate|reflect|const)\n"
" Set border extrapolation mode. The default is replicate.\n\n" " Set border extrapolation mode. The default is replicate.\n\n"
" --mosaic=(yes|no)\n" " --mosaic=(yes|no)\n"
" Do consistent mosaicing. The default is no.\n" " Do consistent mosaicing. The default is no.\n"
" --mosaic-stdev=<float_number>\n" " --mosaic-stdev=<float_number>\n"
" Consistent mosaicing stdev threshold. The default is 10.0.\n\n" " Consistent mosaicing stdev threshold. The default is 10.0.\n\n"
" --motion-inpaint=(yes|no)\n" " -mi, --motion-inpaint=(yes|no)\n"
" Do motion inpainting (requires GPU support). The default is no.\n" " Do motion inpainting (requires GPU support). The default is no.\n"
" --dist-thresh=<float_number>\n" " --mi-dist-thresh=<float_number>\n"
" Estimated flow distance threshold for motion inpainting. The default is 5.0.\n\n" " Estimated flow distance threshold for motion inpainting. The default is 5.0.\n\n"
" --color-inpaint=(no|average|ns|telea)\n" " -ci, --color-inpaint=(no|average|ns|telea)\n"
" Do color inpainting. The defailt is no.\n" " Do color inpainting. The defailt is no.\n"
" --color-inpaint-radius=<float_number>\n" " --ci-radius=<float_number>\n"
" Set color inpainting radius (for ns and telea options only).\n" " Set color inpainting radius (for ns and telea options only).\n"
" The default is 2.0\n\n" " The default is 2.0\n\n"
" --wobble-suppress=(yes|no)\n" " -ws, --wobble-suppress=(yes|no)\n"
" Perform wobble suppression. The default is no.\n\n" " Perform wobble suppression. The default is no.\n"
" --ws-period=<int_number>\n"
" Set wobble suppression period. The default is 30.\n"
" --ws-model=(transl|transl_and_scale|linear_sim|affine|homography)\n"
" Set wobble suppression motion model (must have more DOF than motion \n"
" estimation model). The default is homography.\n"
" -sm2, --save-motions2=(<file_path>|no)\n"
" Save motions estimated for wobble suppression. The default is no.\n"
" -lm2, --load-motions2=(<file_path>|no)\n"
" Load motions for wobble suppression from file. The default is no.\n\n"
" -o, --output=(no|<file_path>)\n" " -o, --output=(no|<file_path>)\n"
" Set output file path explicitely. The default is stabilized.avi.\n" " Set output file path explicitely. The default is stabilized.avi.\n"
" --fps=(<int_number>|auto)\n" " --fps=(<float_number>|auto)\n"
" Set output video FPS explicitely. By default the source FPS is used.\n" " Set output video FPS explicitely. By default the source FPS is used (auto).\n"
" -q, --quiet\n" " -q, --quiet\n"
" Don't show output video frames.\n\n" " Don't show output video frames.\n\n"
" -h, --help\n" " -h, --help\n"
" Print help.\n" " Print help.\n\n"
"\n"; "Note: some argument configurations lead to two passes, some to single pass.\n\n";
} }
...@@ -130,23 +138,27 @@ int main(int argc, const char **argv) ...@@ -130,23 +138,27 @@ int main(int argc, const char **argv)
"{ m | model | affine| }" "{ m | model | affine| }"
"{ | min-inlier-ratio | 0.1 | }" "{ | min-inlier-ratio | 0.1 | }"
"{ | outlier-ratio | 0.5 | }" "{ | outlier-ratio | 0.5 | }"
"{ | save-motions | no | }" "{ sm | save-motions | no | }"
"{ | load-motions | no | }" "{ lm | load-motions | no | }"
"{ r | radius | 15 | }" "{ r | radius | 15 | }"
"{ | stdev | auto | }" "{ | stdev | auto | }"
"{ | deblur | no | }" "{ | deblur | no | }"
"{ | deblur-sens | 0.1 | }" "{ | deblur-sens | 0.1 | }"
"{ | est-trim | yes | }" "{ et | est-trim | yes | }"
"{ t | trim-ratio | 0.1 | }" "{ t | trim-ratio | 0.1 | }"
"{ | incl-constr | no | }" "{ ic | incl-constr | no | }"
"{ | border-mode | replicate | }" "{ bm | border-mode | replicate | }"
"{ | mosaic | no | }" "{ | mosaic | no | }"
"{ | mosaic-stdev | 10.0 | }" "{ ms | mosaic-stdev | 10.0 | }"
"{ | motion-inpaint | no | }" "{ mi | motion-inpaint | no | }"
"{ | dist-thresh | 5.0 | }" "{ | mi-dist-thresh | 5.0 | }"
"{ | color-inpaint | no | }" "{ ci | color-inpaint | no | }"
"{ | color-inpaint-radius | 2 | }" "{ | ci-radius | 2 | }"
"{ | wobble-suppress | no | }" "{ ws | wobble-suppress | no | }"
"{ | ws-period | 30 | }"
"{ | ws-model | homography | }"
"{ sm2 | save-motions2 | no | }"
"{ lm2 | load-motions2 | no | }"
"{ o | output | stabilized.avi | }" "{ o | output | stabilized.avi | }"
"{ | fps | auto | }" "{ | fps | auto | }"
"{ q | quiet | false | }" "{ q | quiet | false | }"
...@@ -177,15 +189,27 @@ int main(int argc, const char **argv) ...@@ -177,15 +189,27 @@ int main(int argc, const char **argv)
twoPassStabilizer->setMotionStabilizer(new GaussianMotionFilter(argi("radius"), argf("stdev"))); twoPassStabilizer->setMotionStabilizer(new GaussianMotionFilter(argi("radius"), argf("stdev")));
if (arg("wobble-suppress") == "yes") if (arg("wobble-suppress") == "yes")
{ {
twoPassStabilizer->setWobbleSuppressor(new MoreAccurateMotionWobbleSuppressor()); MoreAccurateMotionWobbleSuppressor *ws = new MoreAccurateMotionWobbleSuppressor();
if (arg("load-motions") != "no") twoPassStabilizer->setWobbleSuppressor(ws);
twoPassStabilizer->wobbleSuppressor()->setMotionEstimator( ws->setPeriod(argi("ws-period"));
new FromFileMotionReader("motions2." + arg("load-motions"))); if (arg("ws-model") == "transl")
if (arg("save-motions") != "no") ws->setMotionEstimator(new PyrLkRobustMotionEstimator(TRANSLATION));
else if (arg("ws-model") == "transl_and_scale")
ws->setMotionEstimator(new PyrLkRobustMotionEstimator(TRANSLATION_AND_SCALE));
else if (arg("ws-model") == "linear_sim")
ws->setMotionEstimator(new PyrLkRobustMotionEstimator(LINEAR_SIMILARITY));
else if (arg("ws-model") == "affine")
ws->setMotionEstimator(new PyrLkRobustMotionEstimator(AFFINE));
else if (arg("ws-model") == "homography")
ws->setMotionEstimator(new PyrLkRobustMotionEstimator(HOMOGRAPHY));
else
throw runtime_error("unknown wobble suppression motion model: " + arg("ws-model"));
if (arg("load-motions2") != "no")
ws->setMotionEstimator(new FromFileMotionReader(arg("load-motions2")));
if (arg("save-motions2") != "no")
{ {
Ptr<GlobalMotionEstimatorBase> est = twoPassStabilizer->wobbleSuppressor()->motionEstimator(); Ptr<GlobalMotionEstimatorBase> est = ws->motionEstimator();
twoPassStabilizer->wobbleSuppressor()->setMotionEstimator( ws->setMotionEstimator(new ToFileMotionWriter(arg("save-motions2"), est));
new ToFileMotionWriter("motions2." + arg("save-motions"), est));
} }
} }
} }
...@@ -209,43 +233,33 @@ int main(int argc, const char **argv) ...@@ -209,43 +233,33 @@ int main(int argc, const char **argv)
stabilizer->setFrameSource(source); stabilizer->setFrameSource(source);
if (arg("load-motions") == "no") if (arg("load-motions") == "no")
{ {
RansacParams ransac; PyrLkRobustMotionEstimator *est = 0;
PyrLkRobustMotionEstimator *est = new PyrLkRobustMotionEstimator();
Ptr<GlobalMotionEstimatorBase> est_(est);
if (arg("model") == "transl") if (arg("model") == "transl")
{ est = new PyrLkRobustMotionEstimator(TRANSLATION);
est->setMotionModel(TRANSLATION);
ransac = RansacParams::translation2dMotionStd();
}
else if (arg("model") == "transl_and_scale") else if (arg("model") == "transl_and_scale")
{ est = new PyrLkRobustMotionEstimator(TRANSLATION_AND_SCALE);
est->setMotionModel(TRANSLATION_AND_SCALE);
ransac = RansacParams::translationAndScale2dMotionStd();
}
else if (arg("model") == "linear_sim") else if (arg("model") == "linear_sim")
{ est = new PyrLkRobustMotionEstimator(LINEAR_SIMILARITY);
est->setMotionModel(LINEAR_SIMILARITY);
ransac = RansacParams::linearSimilarity2dMotionStd();
}
else if (arg("model") == "affine") else if (arg("model") == "affine")
{ est = new PyrLkRobustMotionEstimator(AFFINE);
est->setMotionModel(AFFINE);
ransac = RansacParams::affine2dMotionStd();
}
else else
{
delete est;
throw runtime_error("unknown motion model: " + arg("model")); throw runtime_error("unknown motion model: " + arg("model"));
}
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);
} }
else else
stabilizer->setMotionEstimator(new FromFileMotionReader("motions." + arg("load-motions"))); stabilizer->setMotionEstimator(new FromFileMotionReader(arg("load-motions")));
if (arg("save-motions") != "no") if (arg("save-motions") != "no")
stabilizer->setMotionEstimator( stabilizer->setMotionEstimator(
new ToFileMotionWriter("motions." + arg("save-motions"), stabilizer->motionEstimator())); new ToFileMotionWriter(arg("save-motions"), stabilizer->motionEstimator()));
stabilizer->setRadius(argi("radius")); stabilizer->setRadius(argi("radius"));
if (arg("deblur") == "yes") if (arg("deblur") == "yes")
...@@ -280,15 +294,15 @@ int main(int argc, const char **argv) ...@@ -280,15 +294,15 @@ int main(int argc, const char **argv)
if (arg("motion-inpaint") == "yes") if (arg("motion-inpaint") == "yes")
{ {
MotionInpainter *inp = new MotionInpainter(); MotionInpainter *inp = new MotionInpainter();
inp->setDistThreshold(argf("dist-thresh")); inp->setDistThreshold(argf("mi-dist-thresh"));
inpainters->pushBack(inp); inpainters->pushBack(inp);
} }
if (arg("color-inpaint") == "average") if (arg("color-inpaint") == "average")
inpainters->pushBack(new ColorAverageInpainter()); inpainters->pushBack(new ColorAverageInpainter());
else if (arg("color-inpaint") == "ns") else if (arg("color-inpaint") == "ns")
inpainters->pushBack(new ColorInpainter(INPAINT_NS, argd("color-inpaint-radius"))); inpainters->pushBack(new ColorInpainter(INPAINT_NS, argd("ci-radius")));
else if (arg("color-inpaint") == "telea") else if (arg("color-inpaint") == "telea")
inpainters->pushBack(new ColorInpainter(INPAINT_TELEA, argd("color-inpaint-radius"))); inpainters->pushBack(new ColorInpainter(INPAINT_TELEA, argd("ci-radius")));
else if (arg("color-inpaint") != "no") else if (arg("color-inpaint") != "no")
throw runtime_error("unknown color inpainting method: " + arg("color-inpaint")); throw runtime_error("unknown color inpainting method: " + arg("color-inpaint"));
if (!inpainters->empty()) if (!inpainters->empty())
......
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