2012-03-19 14:39:23 +01:00
|
|
|
#include <string>
|
|
|
|
#include <iostream>
|
2012-03-22 10:30:28 +01:00
|
|
|
#include <fstream>
|
2012-03-19 14:39:23 +01:00
|
|
|
#include <sstream>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include "opencv2/core/core.hpp"
|
|
|
|
#include "opencv2/video/video.hpp"
|
|
|
|
#include "opencv2/imgproc/imgproc.hpp"
|
|
|
|
#include "opencv2/highgui/highgui.hpp"
|
|
|
|
#include "opencv2/videostab/videostab.hpp"
|
|
|
|
|
2012-04-02 11:25:27 +02:00
|
|
|
#define arg(name) cmd.get<string>(name)
|
|
|
|
#define argb(name) cmd.get<bool>(name)
|
|
|
|
#define argi(name) cmd.get<int>(name)
|
|
|
|
#define argf(name) cmd.get<float>(name)
|
|
|
|
#define argd(name) cmd.get<double>(name)
|
|
|
|
|
2012-03-19 14:39:23 +01:00
|
|
|
using namespace std;
|
|
|
|
using namespace cv;
|
|
|
|
using namespace cv::videostab;
|
|
|
|
|
2012-03-22 08:52:17 +01:00
|
|
|
Ptr<IFrameSource> stabilizedFrames;
|
2012-03-22 10:30:28 +01:00
|
|
|
string saveMotionsPath;
|
2012-03-19 14:39:23 +01:00
|
|
|
double outputFps;
|
|
|
|
string outputPath;
|
2012-03-20 09:12:58 +01:00
|
|
|
bool quietMode;
|
2012-03-19 14:39:23 +01:00
|
|
|
|
|
|
|
void run();
|
2012-03-22 10:30:28 +01:00
|
|
|
void saveMotionsIfNecessary();
|
2012-03-19 14:39:23 +01:00
|
|
|
void printHelp();
|
|
|
|
|
2012-04-02 11:25:27 +02:00
|
|
|
|
2012-03-19 14:39:23 +01:00
|
|
|
void run()
|
|
|
|
{
|
|
|
|
VideoWriter writer;
|
|
|
|
Mat stabilizedFrame;
|
2012-04-05 11:15:13 +02:00
|
|
|
int nframes = 0;
|
2012-03-19 14:39:23 +01:00
|
|
|
|
2012-03-22 08:52:17 +01:00
|
|
|
while (!(stabilizedFrame = stabilizedFrames->nextFrame()).empty())
|
2012-03-19 14:39:23 +01:00
|
|
|
{
|
2012-04-05 11:15:13 +02:00
|
|
|
nframes++;
|
2012-03-19 14:39:23 +01:00
|
|
|
if (!outputPath.empty())
|
|
|
|
{
|
|
|
|
if (!writer.isOpened())
|
|
|
|
writer.open(outputPath, CV_FOURCC('X','V','I','D'), outputFps, stabilizedFrame.size());
|
|
|
|
writer << stabilizedFrame;
|
|
|
|
}
|
2012-03-20 09:12:58 +01:00
|
|
|
if (!quietMode)
|
|
|
|
{
|
|
|
|
imshow("stabilizedFrame", stabilizedFrame);
|
|
|
|
char key = static_cast<char>(waitKey(3));
|
|
|
|
if (key == 27)
|
|
|
|
break;
|
|
|
|
}
|
2012-03-19 14:39:23 +01:00
|
|
|
}
|
|
|
|
|
2012-04-05 11:15:13 +02:00
|
|
|
cout << endl
|
|
|
|
<< "processed frames: " << nframes << endl
|
|
|
|
<< "finished\n";
|
2012-03-19 14:39:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void printHelp()
|
|
|
|
{
|
|
|
|
cout << "OpenCV video stabilizer.\n"
|
|
|
|
"Usage: videostab <file_path> [arguments]\n\n"
|
|
|
|
"Arguments:\n"
|
2012-04-06 12:51:46 +02:00
|
|
|
" -m, --model=(transl|transl_and_scale|linear_sim|affine|homography)\n"
|
2012-03-19 14:39:23 +01:00
|
|
|
" Set motion model. The default is affine.\n"
|
2012-04-11 16:02:10 +02:00
|
|
|
" --subset=(<int_number>|auto)\n"
|
|
|
|
" Number of random samples per one motion hypothesis. The default is auto.\n"
|
2012-03-19 14:39:23 +01:00
|
|
|
" --outlier-ratio=<float_number>\n"
|
2012-04-11 12:17:35 +02:00
|
|
|
" Motion estimation outlier ratio hypothesis. The default is 0.5.\n"
|
2012-03-19 14:39:23 +01:00
|
|
|
" --min-inlier-ratio=<float_number>\n"
|
2012-04-11 12:17:35 +02:00
|
|
|
" Minimum inlier ratio to decide if estimated motion is OK. The default is 0.1.\n"
|
|
|
|
" --nkps=<int_number>\n"
|
|
|
|
" Number of keypoints to find in each frame. The default is 1000.\n"
|
|
|
|
" --extra-kps=<int_number>\n"
|
|
|
|
" Extra keypoint grid size for motion estimation. The default is 0.\n\n"
|
2012-04-06 10:52:31 +02:00
|
|
|
" -sm, --save-motions=(<file_path>|no)\n"
|
2012-04-02 11:25:27 +02:00
|
|
|
" Save estimated motions into file. The default is no.\n"
|
2012-04-06 10:52:31 +02:00
|
|
|
" -lm, --load-motions=(<file_path>|no)\n"
|
2012-04-02 11:25:27 +02:00
|
|
|
" Load motions from file. The default is no.\n\n"
|
2012-03-19 14:39:23 +01:00
|
|
|
" -r, --radius=<int_number>\n"
|
2012-04-02 11:25:27 +02:00
|
|
|
" Set sliding window radius. The default is 15.\n"
|
|
|
|
" --stdev=(<float_number>|auto)\n"
|
2012-04-06 10:52:31 +02:00
|
|
|
" Set smoothing weights standard deviation. The default is auto\n"
|
|
|
|
" (i.e. sqrt(radius)).\n"
|
2012-04-16 08:41:06 +02:00
|
|
|
" -lp, --lp-stab=(yes|no)\n"
|
|
|
|
" Turn on/off linear programming based stabilization method.\n"
|
|
|
|
" --lp-trim-ratio=(<float_number>|auto)\n"
|
|
|
|
" Trimming ratio used in linear programming based method.\n"
|
|
|
|
" --lp-w1=(<float_number>|1)\n"
|
|
|
|
" 1st derivative weight. The default is 1.\n"
|
|
|
|
" --lp-w2=(<float_number>|10)\n"
|
|
|
|
" 2nd derivative weight. The default is 10.\n"
|
|
|
|
" --lp-w3=(<float_number>|100)\n"
|
|
|
|
" 3rd derivative weight. The default is 100.\n"
|
|
|
|
" --lp-w4=(<float_number>|100)\n"
|
|
|
|
" Non-translation motion components weight. The default is 100.\n\n"
|
2012-03-19 14:39:23 +01:00
|
|
|
" --deblur=(yes|no)\n"
|
|
|
|
" Do deblurring.\n"
|
|
|
|
" --deblur-sens=<float_number>\n"
|
2012-03-21 09:34:00 +01:00
|
|
|
" Set deblurring sensitivity (from 0 to +inf). The default is 0.1.\n\n"
|
2012-03-19 14:39:23 +01:00
|
|
|
" -t, --trim-ratio=<float_number>\n"
|
2012-04-02 14:26:05 +02:00
|
|
|
" Set trimming ratio (from 0 to 0.5). The default is 0.1.\n"
|
2012-04-06 10:52:31 +02:00
|
|
|
" -et, --est-trim=(yes|no)\n"
|
|
|
|
" Estimate trim ratio automatically. The default is yes.\n"
|
|
|
|
" -ic, --incl-constr=(yes|no)\n"
|
2012-03-21 09:34:00 +01:00
|
|
|
" Ensure the inclusion constraint is always satisfied. The default is no.\n\n"
|
2012-04-06 10:52:31 +02:00
|
|
|
" -bm, --border-mode=(replicate|reflect|const)\n"
|
2012-03-21 09:34:00 +01:00
|
|
|
" Set border extrapolation mode. The default is replicate.\n\n"
|
2012-03-19 14:39:23 +01:00
|
|
|
" --mosaic=(yes|no)\n"
|
|
|
|
" Do consistent mosaicing. The default is no.\n"
|
|
|
|
" --mosaic-stdev=<float_number>\n"
|
2012-03-21 09:34:00 +01:00
|
|
|
" Consistent mosaicing stdev threshold. The default is 10.0.\n\n"
|
2012-04-06 10:52:31 +02:00
|
|
|
" -mi, --motion-inpaint=(yes|no)\n"
|
2012-03-19 14:39:23 +01:00
|
|
|
" Do motion inpainting (requires GPU support). The default is no.\n"
|
2012-04-06 10:52:31 +02:00
|
|
|
" --mi-dist-thresh=<float_number>\n"
|
2012-03-21 09:34:00 +01:00
|
|
|
" Estimated flow distance threshold for motion inpainting. The default is 5.0.\n\n"
|
2012-04-06 10:52:31 +02:00
|
|
|
" -ci, --color-inpaint=(no|average|ns|telea)\n"
|
2012-03-19 14:39:23 +01:00
|
|
|
" Do color inpainting. The defailt is no.\n"
|
2012-04-06 10:52:31 +02:00
|
|
|
" --ci-radius=<float_number>\n"
|
2012-04-02 11:25:27 +02:00
|
|
|
" Set color inpainting radius (for ns and telea options only).\n"
|
|
|
|
" The default is 2.0\n\n"
|
2012-04-06 10:52:31 +02:00
|
|
|
" -ws, --wobble-suppress=(yes|no)\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"
|
2012-04-11 16:02:10 +02:00
|
|
|
" --ws-subset=(<int_number>|auto)\n"
|
|
|
|
" Number of random samples per one motion hypothesis. The default is auto.\n"
|
2012-04-10 12:33:19 +02:00
|
|
|
" --ws-outlier-ratio=<float_number>\n"
|
|
|
|
" Motion estimation outlier ratio hypothesis. The default is 0.5.\n"
|
2012-04-11 16:02:10 +02:00
|
|
|
" --ws-min-inlier-ratio=<float_number>\n"
|
|
|
|
" Minimum inlier ratio to decide if estimated motion is OK. The default is 0.1.\n"
|
2012-04-11 12:17:35 +02:00
|
|
|
" --ws-nkps=<int_number>\n"
|
|
|
|
" Number of keypoints to find in each frame. The default is 1000.\n"
|
|
|
|
" --ws-extra-kps=<int_number>\n"
|
|
|
|
" Extra keypoint grid size for motion estimation. The default is 0.\n\n"
|
2012-04-06 10:52:31 +02:00
|
|
|
" -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"
|
2012-03-22 08:52:17 +01:00
|
|
|
" -o, --output=(no|<file_path>)\n"
|
2012-03-19 14:39:23 +01:00
|
|
|
" Set output file path explicitely. The default is stabilized.avi.\n"
|
2012-04-06 10:52:31 +02:00
|
|
|
" --fps=(<float_number>|auto)\n"
|
|
|
|
" Set output video FPS explicitely. By default the source FPS is used (auto).\n"
|
2012-03-20 09:12:58 +01:00
|
|
|
" -q, --quiet\n"
|
2012-03-21 09:34:00 +01:00
|
|
|
" Don't show output video frames.\n\n"
|
2012-03-19 14:39:23 +01:00
|
|
|
" -h, --help\n"
|
2012-04-06 10:52:31 +02:00
|
|
|
" Print help.\n\n"
|
|
|
|
"Note: some argument configurations lead to two passes, some to single pass.\n\n";
|
2012-03-19 14:39:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, const char **argv)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
const char *keys =
|
|
|
|
"{ 1 | | | | }"
|
2012-04-02 11:25:27 +02:00
|
|
|
"{ m | model | affine| }"
|
2012-04-11 16:02:10 +02:00
|
|
|
"{ | subset | auto | }"
|
2012-04-02 11:25:27 +02:00
|
|
|
"{ | outlier-ratio | 0.5 | }"
|
2012-04-11 16:02:10 +02:00
|
|
|
"{ | min-inlier-ratio | 0.1 | }"
|
2012-04-11 12:17:35 +02:00
|
|
|
"{ | nkps | 1000 | }"
|
|
|
|
"{ | extra-kps | 0 | }"
|
2012-04-06 10:52:31 +02:00
|
|
|
"{ sm | save-motions | no | }"
|
|
|
|
"{ lm | load-motions | no | }"
|
2012-04-02 11:25:27 +02:00
|
|
|
"{ r | radius | 15 | }"
|
|
|
|
"{ | stdev | auto | }"
|
2012-04-16 08:41:06 +02:00
|
|
|
"{ lp | lp-stab | no | }"
|
|
|
|
"{ | lp-trim-ratio | auto | }"
|
|
|
|
"{ | lp-w1 | 1 | }"
|
|
|
|
"{ | lp-w2 | 10 | }"
|
|
|
|
"{ | lp-w3 | 100 | }"
|
|
|
|
"{ | lp-w4 | 100 | }"
|
2012-04-02 11:25:27 +02:00
|
|
|
"{ | deblur | no | }"
|
|
|
|
"{ | deblur-sens | 0.1 | }"
|
2012-04-06 10:52:31 +02:00
|
|
|
"{ et | est-trim | yes | }"
|
2012-04-02 14:26:05 +02:00
|
|
|
"{ t | trim-ratio | 0.1 | }"
|
2012-04-06 10:52:31 +02:00
|
|
|
"{ ic | incl-constr | no | }"
|
|
|
|
"{ bm | border-mode | replicate | }"
|
2012-04-02 11:25:27 +02:00
|
|
|
"{ | mosaic | no | }"
|
2012-04-06 10:52:31 +02:00
|
|
|
"{ ms | mosaic-stdev | 10.0 | }"
|
|
|
|
"{ mi | motion-inpaint | no | }"
|
|
|
|
"{ | mi-dist-thresh | 5.0 | }"
|
|
|
|
"{ ci | color-inpaint | no | }"
|
|
|
|
"{ | ci-radius | 2 | }"
|
|
|
|
"{ ws | wobble-suppress | no | }"
|
|
|
|
"{ | ws-period | 30 | }"
|
|
|
|
"{ | ws-model | homography | }"
|
2012-04-11 16:02:10 +02:00
|
|
|
"{ | ws-subset | auto | }"
|
2012-04-10 12:33:19 +02:00
|
|
|
"{ | ws-outlier-ratio | 0.5 | }"
|
2012-04-11 16:02:10 +02:00
|
|
|
"{ | ws-min-inlier-ratio | 0.1 | }"
|
2012-04-11 12:17:35 +02:00
|
|
|
"{ | ws-nkps | 1000 | }"
|
|
|
|
"{ | ws-extra-kps | 0 | }"
|
2012-04-06 10:52:31 +02:00
|
|
|
"{ sm2 | save-motions2 | no | }"
|
|
|
|
"{ lm2 | load-motions2 | no | }"
|
2012-03-19 14:39:23 +01:00
|
|
|
"{ o | output | stabilized.avi | }"
|
2012-04-02 11:25:27 +02:00
|
|
|
"{ | fps | auto | }"
|
2012-03-20 09:12:58 +01:00
|
|
|
"{ q | quiet | false | }"
|
2012-03-19 14:39:23 +01:00
|
|
|
"{ h | help | false | }";
|
|
|
|
CommandLineParser cmd(argc, argv, keys);
|
|
|
|
|
|
|
|
// parse command arguments
|
|
|
|
|
2012-04-02 11:25:27 +02:00
|
|
|
if (argb("help"))
|
2012-03-19 14:39:23 +01:00
|
|
|
{
|
|
|
|
printHelp();
|
|
|
|
return 0;
|
2012-04-16 08:41:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
string inputPath = arg("1");
|
|
|
|
if (inputPath.empty()) throw runtime_error("specify video file path");
|
|
|
|
|
|
|
|
VideoFileSource *source = new VideoFileSource(inputPath);
|
|
|
|
cout << "frame count (rough): " << source->count() << endl;
|
|
|
|
if (arg("fps") == "auto") outputFps = source->fps(); else outputFps = argd("fps");
|
2012-03-22 08:52:17 +01:00
|
|
|
|
|
|
|
StabilizerBase *stabilizer;
|
|
|
|
|
2012-04-05 15:23:42 +02:00
|
|
|
bool isTwoPass =
|
2012-04-16 08:41:06 +02:00
|
|
|
arg("est-trim") == "yes" || arg("wobble-suppress") == "yes" || arg("lp-stab") == "yes";
|
2012-04-05 15:23:42 +02:00
|
|
|
|
2012-03-22 08:52:17 +01:00
|
|
|
if (isTwoPass)
|
|
|
|
{
|
|
|
|
TwoPassStabilizer *twoPassStabilizer = new TwoPassStabilizer();
|
|
|
|
stabilizer = twoPassStabilizer;
|
2012-04-02 11:25:27 +02:00
|
|
|
twoPassStabilizer->setEstimateTrimRatio(arg("est-trim") == "yes");
|
2012-04-16 08:41:06 +02:00
|
|
|
if (arg("lp-stab") == "yes")
|
|
|
|
{
|
|
|
|
LpMotionStabilizer *stab = new LpMotionStabilizer();
|
|
|
|
stab->setFrameSize(Size(source->width(), source->height()));
|
|
|
|
stab->setTrimRatio(arg("lp-trim-ratio") == "auto" ? argf("trim-ratio") : argf("lp-trim-ratio"));
|
|
|
|
stab->setWeight1(argf("lp-w1"));
|
|
|
|
stab->setWeight2(argf("lp-w2"));
|
|
|
|
stab->setWeight3(argf("lp-w3"));
|
|
|
|
stab->setWeight4(argf("lp-w4"));
|
|
|
|
twoPassStabilizer->setMotionStabilizer(stab);
|
|
|
|
}
|
|
|
|
else if (arg("stdev") == "auto")
|
2012-04-02 11:25:27 +02:00
|
|
|
twoPassStabilizer->setMotionStabilizer(new GaussianMotionFilter(argi("radius")));
|
|
|
|
else
|
|
|
|
twoPassStabilizer->setMotionStabilizer(new GaussianMotionFilter(argi("radius"), argf("stdev")));
|
2012-04-05 15:23:42 +02:00
|
|
|
if (arg("wobble-suppress") == "yes")
|
|
|
|
{
|
2012-04-06 10:52:31 +02:00
|
|
|
MoreAccurateMotionWobbleSuppressor *ws = new MoreAccurateMotionWobbleSuppressor();
|
|
|
|
twoPassStabilizer->setWobbleSuppressor(ws);
|
|
|
|
ws->setPeriod(argi("ws-period"));
|
2012-04-10 12:33:19 +02:00
|
|
|
|
|
|
|
PyrLkRobustMotionEstimator *est = 0;
|
|
|
|
|
2012-04-06 10:52:31 +02:00
|
|
|
if (arg("ws-model") == "transl")
|
2012-04-16 08:41:06 +02:00
|
|
|
est = new PyrLkRobustMotionEstimator(MM_TRANSLATION);
|
2012-04-06 10:52:31 +02:00
|
|
|
else if (arg("ws-model") == "transl_and_scale")
|
2012-04-16 08:41:06 +02:00
|
|
|
est = new PyrLkRobustMotionEstimator(MM_TRANSLATION_AND_SCALE);
|
2012-04-06 10:52:31 +02:00
|
|
|
else if (arg("ws-model") == "linear_sim")
|
2012-04-16 08:41:06 +02:00
|
|
|
est = new PyrLkRobustMotionEstimator(MM_LINEAR_SIMILARITY);
|
2012-04-06 10:52:31 +02:00
|
|
|
else if (arg("ws-model") == "affine")
|
2012-04-16 08:41:06 +02:00
|
|
|
est = new PyrLkRobustMotionEstimator(MM_AFFINE);
|
2012-04-06 10:52:31 +02:00
|
|
|
else if (arg("ws-model") == "homography")
|
2012-04-16 08:41:06 +02:00
|
|
|
est = new PyrLkRobustMotionEstimator(MM_HOMOGRAPHY);
|
2012-04-06 10:52:31 +02:00
|
|
|
else
|
2012-04-10 12:33:19 +02:00
|
|
|
{
|
|
|
|
delete est;
|
2012-04-06 10:52:31 +02:00
|
|
|
throw runtime_error("unknown wobble suppression motion model: " + arg("ws-model"));
|
2012-04-10 12:33:19 +02:00
|
|
|
}
|
|
|
|
|
2012-04-11 12:17:35 +02:00
|
|
|
est->setDetector(new GoodFeaturesToTrackDetector(argi("ws-nkps")));
|
2012-04-10 12:33:19 +02:00
|
|
|
RansacParams ransac = est->ransacParams();
|
2012-04-11 16:02:10 +02:00
|
|
|
if (arg("ws-subset") != "auto")
|
|
|
|
ransac.size = argi("ws-subset");
|
2012-04-10 12:33:19 +02:00
|
|
|
ransac.eps = argf("ws-outlier-ratio");
|
|
|
|
est->setRansacParams(ransac);
|
|
|
|
est->setMinInlierRatio(argf("ws-min-inlier-ratio"));
|
2012-04-11 12:17:35 +02:00
|
|
|
est->setGridSize(Size(argi("ws-extra-kps"), argi("ws-extra-kps")));
|
|
|
|
ws->setMotionEstimator(est);
|
2012-04-10 12:33:19 +02:00
|
|
|
|
|
|
|
MotionModel model = est->motionModel();
|
2012-04-06 10:52:31 +02:00
|
|
|
if (arg("load-motions2") != "no")
|
2012-04-10 12:33:19 +02:00
|
|
|
{
|
2012-04-06 10:52:31 +02:00
|
|
|
ws->setMotionEstimator(new FromFileMotionReader(arg("load-motions2")));
|
2012-04-10 12:33:19 +02:00
|
|
|
ws->motionEstimator()->setMotionModel(model);
|
|
|
|
}
|
2012-04-06 10:52:31 +02:00
|
|
|
if (arg("save-motions2") != "no")
|
2012-04-05 15:23:42 +02:00
|
|
|
{
|
2012-04-10 12:33:19 +02:00
|
|
|
ws->setMotionEstimator(new ToFileMotionWriter(arg("save-motions2"), ws->motionEstimator()));
|
|
|
|
ws->motionEstimator()->setMotionModel(model);
|
2012-04-05 15:23:42 +02:00
|
|
|
}
|
|
|
|
}
|
2012-03-22 08:52:17 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-04-02 11:25:27 +02:00
|
|
|
OnePassStabilizer *onePassStabilizer = new OnePassStabilizer();
|
2012-03-22 08:52:17 +01:00
|
|
|
stabilizer = onePassStabilizer;
|
2012-04-02 11:25:27 +02:00
|
|
|
if (arg("stdev") == "auto")
|
|
|
|
onePassStabilizer->setMotionFilter(new GaussianMotionFilter(argi("radius")));
|
|
|
|
else
|
|
|
|
onePassStabilizer->setMotionFilter(new GaussianMotionFilter(argi("radius"), argf("stdev")));
|
2012-03-22 08:52:17 +01:00
|
|
|
}
|
2012-03-19 14:39:23 +01:00
|
|
|
|
2012-04-02 11:25:27 +02:00
|
|
|
stabilizer->setFrameSource(source);
|
2012-04-16 08:41:06 +02:00
|
|
|
stabilizedFrames = dynamic_cast<IFrameSource*>(stabilizer);
|
2012-04-02 11:25:27 +02:00
|
|
|
|
2012-04-06 12:51:46 +02:00
|
|
|
PyrLkRobustMotionEstimator *est = 0;
|
2012-04-10 12:33:19 +02:00
|
|
|
|
2012-04-06 12:51:46 +02:00
|
|
|
if (arg("model") == "transl")
|
2012-04-16 08:41:06 +02:00
|
|
|
est = new PyrLkRobustMotionEstimator(MM_TRANSLATION);
|
2012-04-06 12:51:46 +02:00
|
|
|
else if (arg("model") == "transl_and_scale")
|
2012-04-16 08:41:06 +02:00
|
|
|
est = new PyrLkRobustMotionEstimator(MM_TRANSLATION_AND_SCALE);
|
2012-04-06 12:51:46 +02:00
|
|
|
else if (arg("model") == "linear_sim")
|
2012-04-16 08:41:06 +02:00
|
|
|
est = new PyrLkRobustMotionEstimator(MM_LINEAR_SIMILARITY);
|
2012-04-06 12:51:46 +02:00
|
|
|
else if (arg("model") == "affine")
|
2012-04-16 08:41:06 +02:00
|
|
|
est = new PyrLkRobustMotionEstimator(MM_AFFINE);
|
2012-04-06 12:51:46 +02:00
|
|
|
else if (arg("model") == "homography")
|
2012-04-16 08:41:06 +02:00
|
|
|
est = new PyrLkRobustMotionEstimator(MM_HOMOGRAPHY);
|
2012-04-02 11:25:27 +02:00
|
|
|
else
|
2012-04-06 12:51:46 +02:00
|
|
|
{
|
|
|
|
delete est;
|
|
|
|
throw runtime_error("unknown motion model: " + arg("model"));
|
|
|
|
}
|
2012-04-10 12:33:19 +02:00
|
|
|
|
2012-04-11 12:17:35 +02:00
|
|
|
est->setDetector(new GoodFeaturesToTrackDetector(argi("nkps")));
|
2012-04-06 12:51:46 +02:00
|
|
|
RansacParams ransac = est->ransacParams();
|
2012-04-11 16:02:10 +02:00
|
|
|
if (arg("subset") != "auto")
|
|
|
|
ransac.size = argi("subset");
|
2012-04-06 12:51:46 +02:00
|
|
|
ransac.eps = argf("outlier-ratio");
|
|
|
|
est->setRansacParams(ransac);
|
|
|
|
est->setMinInlierRatio(argf("min-inlier-ratio"));
|
2012-04-11 12:17:35 +02:00
|
|
|
est->setGridSize(Size(argi("extra-kps"), argi("extra-kps")));
|
2012-04-06 12:51:46 +02:00
|
|
|
stabilizer->setMotionEstimator(est);
|
|
|
|
|
2012-04-10 12:33:19 +02:00
|
|
|
MotionModel model = stabilizer->motionEstimator()->motionModel();
|
2012-04-06 12:51:46 +02:00
|
|
|
if (arg("load-motions") != "no")
|
|
|
|
{
|
2012-04-06 10:52:31 +02:00
|
|
|
stabilizer->setMotionEstimator(new FromFileMotionReader(arg("load-motions")));
|
2012-04-06 12:51:46 +02:00
|
|
|
stabilizer->motionEstimator()->setMotionModel(model);
|
|
|
|
}
|
2012-04-02 11:25:27 +02:00
|
|
|
if (arg("save-motions") != "no")
|
2012-04-10 11:07:30 +02:00
|
|
|
{
|
2012-04-10 12:33:19 +02:00
|
|
|
stabilizer->setMotionEstimator(new ToFileMotionWriter(arg("save-motions"), stabilizer->motionEstimator()));
|
2012-04-10 11:07:30 +02:00
|
|
|
stabilizer->motionEstimator()->setMotionModel(model);
|
|
|
|
}
|
2012-03-19 14:39:23 +01:00
|
|
|
|
2012-04-02 11:25:27 +02:00
|
|
|
stabilizer->setRadius(argi("radius"));
|
|
|
|
if (arg("deblur") == "yes")
|
2012-03-19 14:39:23 +01:00
|
|
|
{
|
|
|
|
WeightingDeblurer *deblurer = new WeightingDeblurer();
|
2012-04-02 11:25:27 +02:00
|
|
|
deblurer->setRadius(argi("radius"));
|
|
|
|
deblurer->setSensitivity(argf("deblur-sens"));
|
2012-03-19 14:39:23 +01:00
|
|
|
stabilizer->setDeblurer(deblurer);
|
|
|
|
}
|
|
|
|
|
2012-04-02 11:25:27 +02:00
|
|
|
stabilizer->setTrimRatio(argf("trim-ratio"));
|
|
|
|
stabilizer->setCorrectionForInclusion(arg("incl-constr") == "yes");
|
2012-03-19 14:39:23 +01:00
|
|
|
|
2012-04-02 11:25:27 +02:00
|
|
|
if (arg("border-mode") == "reflect")
|
2012-03-20 13:24:51 +01:00
|
|
|
stabilizer->setBorderMode(BORDER_REFLECT);
|
2012-04-02 11:25:27 +02:00
|
|
|
else if (arg("border-mode") == "replicate")
|
2012-03-19 14:39:23 +01:00
|
|
|
stabilizer->setBorderMode(BORDER_REPLICATE);
|
2012-04-02 11:25:27 +02:00
|
|
|
else if (arg("border-mode") == "const")
|
2012-03-19 14:39:23 +01:00
|
|
|
stabilizer->setBorderMode(BORDER_CONSTANT);
|
2012-04-02 11:25:27 +02:00
|
|
|
else
|
|
|
|
throw runtime_error("unknown border extrapolation mode: "
|
|
|
|
+ cmd.get<string>("border-mode"));
|
2012-03-19 14:39:23 +01:00
|
|
|
|
|
|
|
InpaintingPipeline *inpainters = new InpaintingPipeline();
|
2012-04-02 11:25:27 +02:00
|
|
|
Ptr<InpainterBase> inpainters_(inpainters);
|
|
|
|
if (arg("mosaic") == "yes")
|
2012-03-19 14:39:23 +01:00
|
|
|
{
|
2012-04-02 11:25:27 +02:00
|
|
|
ConsistentMosaicInpainter *inp = new ConsistentMosaicInpainter();
|
|
|
|
inp->setStdevThresh(argf("mosaic-stdev"));
|
|
|
|
inpainters->pushBack(inp);
|
2012-03-19 14:39:23 +01:00
|
|
|
}
|
2012-04-02 11:25:27 +02:00
|
|
|
if (arg("motion-inpaint") == "yes")
|
2012-03-21 09:34:00 +01:00
|
|
|
{
|
2012-04-02 11:25:27 +02:00
|
|
|
MotionInpainter *inp = new MotionInpainter();
|
2012-04-06 10:52:31 +02:00
|
|
|
inp->setDistThreshold(argf("mi-dist-thresh"));
|
2012-04-02 11:25:27 +02:00
|
|
|
inpainters->pushBack(inp);
|
2012-03-21 09:34:00 +01:00
|
|
|
}
|
2012-04-02 11:25:27 +02:00
|
|
|
if (arg("color-inpaint") == "average")
|
|
|
|
inpainters->pushBack(new ColorAverageInpainter());
|
|
|
|
else if (arg("color-inpaint") == "ns")
|
2012-04-06 10:52:31 +02:00
|
|
|
inpainters->pushBack(new ColorInpainter(INPAINT_NS, argd("ci-radius")));
|
2012-04-02 11:25:27 +02:00
|
|
|
else if (arg("color-inpaint") == "telea")
|
2012-04-06 10:52:31 +02:00
|
|
|
inpainters->pushBack(new ColorInpainter(INPAINT_TELEA, argd("ci-radius")));
|
2012-04-02 11:25:27 +02:00
|
|
|
else if (arg("color-inpaint") != "no")
|
|
|
|
throw runtime_error("unknown color inpainting method: " + arg("color-inpaint"));
|
|
|
|
if (!inpainters->empty())
|
2012-03-20 13:24:51 +01:00
|
|
|
{
|
2012-04-02 11:25:27 +02:00
|
|
|
inpainters->setRadius(argi("radius"));
|
|
|
|
stabilizer->setInpainter(inpainters_);
|
2012-04-05 15:23:42 +02:00
|
|
|
}
|
2012-03-19 14:39:23 +01:00
|
|
|
|
2012-04-02 11:25:27 +02:00
|
|
|
if (arg("output") != "no")
|
|
|
|
outputPath = arg("output");
|
2012-03-19 14:39:23 +01:00
|
|
|
|
2012-04-02 11:25:27 +02:00
|
|
|
quietMode = argb("quite");
|
2012-03-22 08:52:17 +01:00
|
|
|
|
2012-03-19 14:39:23 +01:00
|
|
|
run();
|
|
|
|
}
|
|
|
|
catch (const exception &e)
|
|
|
|
{
|
2012-03-21 09:34:00 +01:00
|
|
|
cout << "error: " << e.what() << endl;
|
2012-03-22 08:52:17 +01:00
|
|
|
stabilizedFrames.release();
|
2012-03-19 14:39:23 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2012-03-22 08:52:17 +01:00
|
|
|
stabilizedFrames.release();
|
2012-03-19 14:39:23 +01:00
|
|
|
return 0;
|
|
|
|
}
|