videostab.cpp 23 KB
Newer Older
1 2
#include <string>
#include <iostream>
3
#include <fstream>
4 5
#include <sstream>
#include <stdexcept>
6
#include "opencv2/core.hpp"
7
#include <opencv2/core/utility.hpp>
8 9
#include "opencv2/video.hpp"
#include "opencv2/imgproc.hpp"
10
#include "opencv2/videoio.hpp"
11 12
#include "opencv2/highgui.hpp"
#include "opencv2/videostab.hpp"
13
#include "opencv2/opencv_modules.hpp"
14

15 16 17 18 19 20
#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)

21 22 23 24
using namespace std;
using namespace cv;
using namespace cv::videostab;

25
Ptr<IFrameSource> stabilizedFrames;
26
string saveMotionsPath;
27 28
double outputFps;
string outputPath;
29
bool quietMode;
30 31

void run();
32
void saveMotionsIfNecessary();
33
void printHelp();
34
MotionModel motionModel(const string &str);
35

36

37 38 39 40
void run()
{
    VideoWriter writer;
    Mat stabilizedFrame;
41
    int nframes = 0;
42

43
    // for each stabilized frame
44
    while (!(stabilizedFrame = stabilizedFrames->nextFrame()).empty())
45
    {
46
        nframes++;
47 48

        // init writer (once) and save stabilized frame
49 50 51
        if (!outputPath.empty())
        {
            if (!writer.isOpened())
52
                writer.open(outputPath, VideoWriter::fourcc('X','V','I','D'),
53
                            outputFps, stabilizedFrame.size());
54 55
            writer << stabilizedFrame;
        }
56 57

        // show stabilized frame
58 59 60 61
        if (!quietMode)
        {
            imshow("stabilizedFrame", stabilizedFrame);
            char key = static_cast<char>(waitKey(3));
62
            if (key == 27) { cout << endl; break; }
63
        }
64 65
    }

66
    cout << "processed frames: " << nframes << endl
67
         << "finished\n";
68 69 70 71 72 73 74 75
}


void printHelp()
{
    cout << "OpenCV video stabilizer.\n"
            "Usage: videostab <file_path> [arguments]\n\n"
            "Arguments:\n"
76
            "  -m, --model=(transl|transl_and_scale|rigid|similarity|affine|homography)\n"
77
            "      Set motion model. The default is affine.\n"
78 79
            "  -lp, --lin-prog-motion-est=(yes|no)\n"
            "      Turn on/off LP based motion estimation. The default is no.\n"
80 81
            "  --subset=(<int_number>|auto)\n"
            "      Number of random samples per one motion hypothesis. The default is auto.\n"
82 83
            "  --thresh=(<float_number>|auto)\n"
            "      Maximum error to classify match as inlier. The default is auto.\n"
84
            "  --outlier-ratio=<float_number>\n"
85
            "      Motion estimation outlier ratio hypothesis. The default is 0.5.\n"
86
            "  --min-inlier-ratio=<float_number>\n"
87 88 89
            "      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"
90 91
            "  --local-outlier-rejection=(yes|no)\n"
            "      Perform local outlier rejection. The default is no.\n\n"
92
            "  -sm, --save-motions=(<file_path>|no)\n"
93
            "      Save estimated motions into file. The default is no.\n"
94
            "  -lm, --load-motions=(<file_path>|no)\n"
95
            "      Load motions from file. The default is no.\n\n"
96
            "  -r, --radius=<int_number>\n"
97 98
            "      Set sliding window radius. The default is 15.\n"
            "  --stdev=(<float_number>|auto)\n"
99 100
            "      Set smoothing weights standard deviation. The default is auto\n"
            "      (i.e. sqrt(radius)).\n"
101
            "  -lps, --lin-prog-stab=(yes|no)\n"
102
            "      Turn on/off linear programming based stabilization method.\n"
103
            "  --lps-trim-ratio=(<float_number>|auto)\n"
104
            "      Trimming ratio used in linear programming based method.\n"
105
            "  --lps-w1=(<float_number>|1)\n"
106
            "      1st derivative weight. The default is 1.\n"
107
            "  --lps-w2=(<float_number>|10)\n"
108
            "      2nd derivative weight. The default is 10.\n"
109
            "  --lps-w3=(<float_number>|100)\n"
110
            "      3rd derivative weight. The default is 100.\n"
111
            "  --lps-w4=(<float_number>|100)\n"
112
            "      Non-translation motion components weight. The default is 100.\n\n"
113 114 115
            "  --deblur=(yes|no)\n"
            "      Do deblurring.\n"
            "  --deblur-sens=<float_number>\n"
116
            "      Set deblurring sensitivity (from 0 to +inf). The default is 0.1.\n\n"
117
            "  -t, --trim-ratio=<float_number>\n"
118
            "      Set trimming ratio (from 0 to 0.5). The default is 0.1.\n"
119 120 121
            "  -et, --est-trim=(yes|no)\n"
            "      Estimate trim ratio automatically. The default is yes.\n"
            "  -ic, --incl-constr=(yes|no)\n"
122
            "      Ensure the inclusion constraint is always satisfied. The default is no.\n\n"
123
            "  -bm, --border-mode=(replicate|reflect|const)\n"
124
            "      Set border extrapolation mode. The default is replicate.\n\n"
125 126 127
            "  --mosaic=(yes|no)\n"
            "      Do consistent mosaicing. The default is no.\n"
            "  --mosaic-stdev=<float_number>\n"
128
            "      Consistent mosaicing stdev threshold. The default is 10.0.\n\n"
129
            "  -mi, --motion-inpaint=(yes|no)\n"
130
            "      Do motion inpainting (requires CUDA support). The default is no.\n"
131
            "  --mi-dist-thresh=<float_number>\n"
132
            "      Estimated flow distance threshold for motion inpainting. The default is 5.0.\n\n"
133
            "  -ci, --color-inpaint=(no|average|ns|telea)\n"
134
            "      Do color inpainting. The defailt is no.\n"
135
            "  --ci-radius=<float_number>\n"
136 137
            "      Set color inpainting radius (for ns and telea options only).\n"
            "      The default is 2.0\n\n"
138 139
            "  -ws, --wobble-suppress=(yes|no)\n"
            "      Perform wobble suppression. The default is no.\n"
140 141
            "  --ws-lp=(yes|no)\n"
            "      Turn on/off LP based motion estimation. The default is no.\n"
142 143
            "  --ws-period=<int_number>\n"
            "      Set wobble suppression period. The default is 30.\n"
144
            "  --ws-model=(transl|transl_and_scale|rigid|similarity|affine|homography)\n"
145 146
            "      Set wobble suppression motion model (must have more DOF than motion \n"
            "      estimation model). The default is homography.\n"
147 148
            "  --ws-subset=(<int_number>|auto)\n"
            "      Number of random samples per one motion hypothesis. The default is auto.\n"
149 150
            "  --ws-thresh=(<float_number>|auto)\n"
            "      Maximum error to classify match as inlier. The default is auto.\n"
151 152
            "  --ws-outlier-ratio=<float_number>\n"
            "      Motion estimation outlier ratio hypothesis. The default is 0.5.\n"
153 154
            "  --ws-min-inlier-ratio=<float_number>\n"
            "      Minimum inlier ratio to decide if estimated motion is OK. The default is 0.1.\n"
155 156
            "  --ws-nkps=<int_number>\n"
            "      Number of keypoints to find in each frame. The default is 1000.\n"
157 158
            "  --ws-local-outlier-rejection=(yes|no)\n"
            "      Perform local outlier rejection. The default is no.\n\n"
159 160 161 162
            "  -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"
163
            "  -gpu=(yes|no)\n"
164
            "      Use CUDA optimization whenever possible. The default is no.\n\n"
165
            "  -o, --output=(no|<file_path>)\n"
166
            "      Set output file path explicitely. The default is stabilized.avi.\n"
167 168
            "  --fps=(<float_number>|auto)\n"
            "      Set output video FPS explicitely. By default the source FPS is used (auto).\n"
169
            "  -q, --quiet\n"
170
            "      Don't show output video frames.\n\n"
171
            "  -h, --help\n"
172 173
            "      Print help.\n\n"
            "Note: some argument configurations lead to two passes, some to single pass.\n\n";
174 175
}

176 177 178 179 180 181
// motion estimator builders are for concise creation of motion estimators

class IMotionEstimatorBuilder
{
public:
    virtual ~IMotionEstimatorBuilder() {}
182
    virtual Ptr<ImageMotionEstimatorBase> build() = 0;
183
protected:
184
    IMotionEstimatorBuilder(CommandLineParser &command) : cmd(command) {}
185 186 187 188
    CommandLineParser cmd;
};


189
class MotionEstimatorRansacL2Builder : public IMotionEstimatorBuilder
190 191
{
public:
192 193
    MotionEstimatorRansacL2Builder(CommandLineParser &command, bool use_gpu, const string &_prefix = "")
        : IMotionEstimatorBuilder(command), gpu(use_gpu), prefix(_prefix) {}
194

195
    virtual Ptr<ImageMotionEstimatorBase> build()
196
    {
197
        Ptr<MotionEstimatorRansacL2> est = makePtr<MotionEstimatorRansacL2>(motionModel(arg(prefix + "model")));
198 199 200 201 202

        RansacParams ransac = est->ransacParams();
        if (arg(prefix + "subset") != "auto")
            ransac.size = argi(prefix + "subset");
        if (arg(prefix + "thresh") != "auto")
203
            ransac.thresh = argf(prefix + "thresh");
204 205 206
        ransac.eps = argf(prefix + "outlier-ratio");
        est->setRansacParams(ransac);

207
        est->setMinInlierRatio(argf(prefix + "min-inlier-ratio"));
208

209
        Ptr<IOutlierRejector> outlierRejector = makePtr<NullOutlierRejector>();
210 211
        if (arg(prefix + "local-outlier-rejection") == "yes")
        {
212
            Ptr<TranslationBasedLocalOutlierRejector> tblor = makePtr<TranslationBasedLocalOutlierRejector>();
213
            RansacParams ransacParams = tblor->ransacParams();
214 215
            if (arg(prefix + "thresh") != "auto")
                ransacParams.thresh = argf(prefix + "thresh");
216 217
            tblor->setRansacParams(ransacParams);
            outlierRejector = tblor;
218 219
        }

220
#if defined(HAVE_OPENCV_CUDAIMGPROC) && defined(HAVE_OPENCV_CUDAOPTFLOW)
221
        if (gpu)
222
        {
223
            Ptr<KeypointBasedMotionEstimatorGpu> kbest = makePtr<KeypointBasedMotionEstimatorGpu>(est);
224 225
            kbest->setOutlierRejector(outlierRejector);
            return kbest;
226
        }
227
#endif
228

229
        Ptr<KeypointBasedMotionEstimator> kbest = makePtr<KeypointBasedMotionEstimator>(est);
230
        kbest->setDetector(GFTTDetector::create(argi(prefix + "nkps")));
231 232
        kbest->setOutlierRejector(outlierRejector);
        return kbest;
233 234
    }
private:
235
    bool gpu;
236 237 238 239
    string prefix;
};


240
class MotionEstimatorL1Builder : public IMotionEstimatorBuilder
241 242
{
public:
243 244
    MotionEstimatorL1Builder(CommandLineParser &command, bool use_gpu, const string &_prefix = "")
        : IMotionEstimatorBuilder(command), gpu(use_gpu), prefix(_prefix) {}
245

246
    virtual Ptr<ImageMotionEstimatorBase> build()
247
    {
248
        Ptr<MotionEstimatorL1> est = makePtr<MotionEstimatorL1>(motionModel(arg(prefix + "model")));
249

250
        Ptr<IOutlierRejector> outlierRejector = makePtr<NullOutlierRejector>();
251 252
        if (arg(prefix + "local-outlier-rejection") == "yes")
        {
253
            Ptr<TranslationBasedLocalOutlierRejector> tblor = makePtr<TranslationBasedLocalOutlierRejector>();
254
            RansacParams ransacParams = tblor->ransacParams();
255 256
            if (arg(prefix + "thresh") != "auto")
                ransacParams.thresh = argf(prefix + "thresh");
257 258
            tblor->setRansacParams(ransacParams);
            outlierRejector = tblor;
259 260
        }

261
#if defined(HAVE_OPENCV_CUDAIMGPROC) && defined(HAVE_OPENCV_CUDAOPTFLOW)
262 263
        if (gpu)
        {
264
            Ptr<KeypointBasedMotionEstimatorGpu> kbest = makePtr<KeypointBasedMotionEstimatorGpu>(est);
265 266 267 268 269
            kbest->setOutlierRejector(outlierRejector);
            return kbest;
        }
#endif

270
        Ptr<KeypointBasedMotionEstimator> kbest = makePtr<KeypointBasedMotionEstimator>(est);
271
        kbest->setDetector(GFTTDetector::create(argi(prefix + "nkps")));
272 273
        kbest->setOutlierRejector(outlierRejector);
        return kbest;
274 275
    }
private:
276
    bool gpu;
277 278 279
    string prefix;
};

280 281 282 283 284 285

int main(int argc, const char **argv)
{
    try
    {
        const char *keys =
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
                "{ @1                       |           | }"
                "{ m  model                 | affine    | }"
                "{ lp lin-prog-motion-est   | no        | }"
                "{  subset                  | auto      | }"
                "{  thresh                  | auto | }"
                "{  outlier-ratio           | 0.5 | }"
                "{  min-inlier-ratio        | 0.1 | }"
                "{  nkps                    | 1000 | }"
                "{  extra-kps               | 0 | }"
                "{  local-outlier-rejection | no | }"
                "{ sm  save-motions         | no | }"
                "{ lm  load-motions         | no | }"
                "{ r  radius                | 15 | }"
                "{  stdev                   | auto | }"
                "{ lps  lin-prog-stab       | no | }"
                "{  lps-trim-ratio          | auto | }"
                "{  lps-w1                  | 1 | }"
                "{  lps-w2                  | 10 | }"
                "{  lps-w3                  | 100 | }"
                "{  lps-w4                  | 100 | }"
                "{  deblur                  | no | }"
                "{  deblur-sens             | 0.1 | }"
                "{ et  est-trim             | yes | }"
                "{ t  trim-ratio            | 0.1 | }"
                "{ ic  incl-constr          | no | }"
                "{ bm  border-mode          | replicate | }"
                "{  mosaic                  | no | }"
                "{ 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 | }"
                "{  ws-subset               | auto | }"
                "{  ws-thresh               | auto | }"
                "{  ws-outlier-ratio        | 0.5 | }"
                "{  ws-min-inlier-ratio     | 0.1 | }"
                "{  ws-nkps                 | 1000 | }"
                "{  ws-extra-kps            | 0 | }"
                "{  ws-local-outlier-rejection | no | }"
                "{  ws-lp                   | no | }"
                "{ sm2 save-motions2        | no | }"
                "{ lm2 load-motions2        | no | }"
                "{ gpu                      | no | }"
                "{ o  output                | stabilized.avi | }"
                "{ fps                      | auto | }"
                "{ q quiet                  |  | }"
                "{ h help                   |  | }";
336 337 338 339
        CommandLineParser cmd(argc, argv, keys);

        // parse command arguments

340
        if (argb("help"))
341 342 343
        {
            printHelp();
            return 0;
344 345
        }

346 347 348 349
        if (arg("gpu") == "yes")
        {
            cout << "initializing GPU..."; cout.flush();
            Mat hostTmp = Mat::zeros(1, 1, CV_32F);
350
            cuda::GpuMat deviceTmp;
351 352 353 354
            deviceTmp.upload(hostTmp);
            cout << endl;
        }

355 356 357 358
        StabilizerBase *stabilizer = 0;

        // check if source video is specified

359
        string inputPath = arg(0);
360 361 362 363
        if (inputPath.empty())
            throw runtime_error("specify video file path");

        // get source video parameters
364

365
        Ptr<VideoFileSource> source = makePtr<VideoFileSource>(inputPath);
366
        cout << "frame count (rough): " << source->count() << endl;
367 368 369 370
        if (arg("fps") == "auto")
            outputFps = source->fps();
        else
            outputFps = argd("fps");
371

372
        // prepare motion estimation builders
373

374
        Ptr<IMotionEstimatorBuilder> motionEstBuilder;
375
        if (arg("lin-prog-motion-est") == "yes")
376
            motionEstBuilder.reset(new MotionEstimatorL1Builder(cmd, arg("gpu") == "yes"));
377
        else
378
            motionEstBuilder.reset(new MotionEstimatorRansacL2Builder(cmd, arg("gpu") == "yes"));
379 380

        Ptr<IMotionEstimatorBuilder> wsMotionEstBuilder;
381
        if (arg("ws-lp") == "yes")
382
            wsMotionEstBuilder.reset(new MotionEstimatorL1Builder(cmd, arg("gpu") == "yes", "ws-"));
383
        else
384
            wsMotionEstBuilder.reset(new MotionEstimatorRansacL2Builder(cmd, arg("gpu") == "yes", "ws-"));
385 386

        // determine whether we must use one pass or two pass stabilizer
387
        bool isTwoPass =
388
                arg("est-trim") == "yes" || arg("wobble-suppress") == "yes" || arg("lin-prog-stab") == "yes";
389

390 391
        if (isTwoPass)
        {
392 393
            // we must use two pass stabilizer

394 395
            TwoPassStabilizer *twoPassStabilizer = new TwoPassStabilizer();
            stabilizer = twoPassStabilizer;
396
            twoPassStabilizer->setEstimateTrimRatio(arg("est-trim") == "yes");
397 398 399

            // determine stabilization technique

400
            if (arg("lin-prog-stab") == "yes")
401
            {
402
                Ptr<LpMotionStabilizer> stab = makePtr<LpMotionStabilizer>();
403
                stab->setFrameSize(Size(source->width(), source->height()));
404 405 406 407 408
                stab->setTrimRatio(arg("lps-trim-ratio") == "auto" ? argf("trim-ratio") : argf("lps-trim-ratio"));
                stab->setWeight1(argf("lps-w1"));
                stab->setWeight2(argf("lps-w2"));
                stab->setWeight3(argf("lps-w3"));
                stab->setWeight4(argf("lps-w4"));
409 410 411
                twoPassStabilizer->setMotionStabilizer(stab);
            }
            else if (arg("stdev") == "auto")
412
                twoPassStabilizer->setMotionStabilizer(makePtr<GaussianMotionFilter>(argi("radius")));
413
            else
414
                twoPassStabilizer->setMotionStabilizer(makePtr<GaussianMotionFilter>(argi("radius"), argf("stdev")));
415

416
            // init wobble suppressor if necessary
417

418 419
            if (arg("wobble-suppress") == "yes")
            {
420
                Ptr<MoreAccurateMotionWobbleSuppressorBase> ws = makePtr<MoreAccurateMotionWobbleSuppressor>();
421
                if (arg("gpu") == "yes")
422
#ifdef HAVE_OPENCV_CUDAWARPING
423
                    ws = makePtr<MoreAccurateMotionWobbleSuppressorGpu>();
424
#else
425
                    throw runtime_error("OpenCV is built without CUDA support");
426
#endif
427

428
                ws->setMotionEstimator(wsMotionEstBuilder->build());
429
                ws->setPeriod(argi("ws-period"));
430
                twoPassStabilizer->setWobbleSuppressor(ws);
431

432
                MotionModel model = ws->motionEstimator()->motionModel();
433
                if (arg("load-motions2") != "no")
434
                {
435
                    ws->setMotionEstimator(makePtr<FromFileMotionReader>(arg("load-motions2")));
436 437
                    ws->motionEstimator()->setMotionModel(model);
                }
438
                if (arg("save-motions2") != "no")
439
                {
440
                    ws->setMotionEstimator(makePtr<ToFileMotionWriter>(arg("save-motions2"), ws->motionEstimator()));
441
                    ws->motionEstimator()->setMotionModel(model);
442
                }
443
            }
444 445 446
        }
        else
        {
447 448
            // we must use one pass stabilizer

449
            OnePassStabilizer *onePassStabilizer = new OnePassStabilizer();
450
            stabilizer = onePassStabilizer;
451
            if (arg("stdev") == "auto")
452
                onePassStabilizer->setMotionFilter(makePtr<GaussianMotionFilter>(argi("radius")));
453
            else
454
                onePassStabilizer->setMotionFilter(makePtr<GaussianMotionFilter>(argi("radius"), argf("stdev")));
455
        }
456

457
        stabilizer->setFrameSource(source);
458
        stabilizer->setMotionEstimator(motionEstBuilder->build());
459

460
        // cast stabilizer to simple frame source interface to read stabilized frames
461
        stabilizedFrames.reset(dynamic_cast<IFrameSource*>(stabilizer));
462

463
        MotionModel model = stabilizer->motionEstimator()->motionModel();
464 465
        if (arg("load-motions") != "no")
        {
466
            stabilizer->setMotionEstimator(makePtr<FromFileMotionReader>(arg("load-motions")));
467 468
            stabilizer->motionEstimator()->setMotionModel(model);
        }
469
        if (arg("save-motions") != "no")
470
        {
471
            stabilizer->setMotionEstimator(makePtr<ToFileMotionWriter>(arg("save-motions"), stabilizer->motionEstimator()));
472 473
            stabilizer->motionEstimator()->setMotionModel(model);
        }
474

475
        stabilizer->setRadius(argi("radius"));
476 477

        // init deblurer
478
        if (arg("deblur") == "yes")
479
        {
480
            Ptr<WeightingDeblurer> deblurer = makePtr<WeightingDeblurer>();
481 482
            deblurer->setRadius(argi("radius"));
            deblurer->setSensitivity(argf("deblur-sens"));
483 484 485
            stabilizer->setDeblurer(deblurer);
        }

486
        // set up trimming paramters
487 488
        stabilizer->setTrimRatio(argf("trim-ratio"));
        stabilizer->setCorrectionForInclusion(arg("incl-constr") == "yes");
489

490
        if (arg("border-mode") == "reflect")
491
            stabilizer->setBorderMode(BORDER_REFLECT);
492
        else if (arg("border-mode") == "replicate")
493
            stabilizer->setBorderMode(BORDER_REPLICATE);
494
        else if (arg("border-mode") == "const")
495
            stabilizer->setBorderMode(BORDER_CONSTANT);
496 497 498
        else
            throw runtime_error("unknown border extrapolation mode: "
                                 + cmd.get<string>("border-mode"));
499

500
        // init inpainter
501
        InpaintingPipeline *inpainters = new InpaintingPipeline();
502 503
        Ptr<InpainterBase> inpainters_(inpainters);
        if (arg("mosaic") == "yes")
504
        {
505
            Ptr<ConsistentMosaicInpainter> inp = makePtr<ConsistentMosaicInpainter>();
506 507
            inp->setStdevThresh(argf("mosaic-stdev"));
            inpainters->pushBack(inp);
508
        }
509
        if (arg("motion-inpaint") == "yes")
510
        {
511
            Ptr<MotionInpainter> inp = makePtr<MotionInpainter>();
512
            inp->setDistThreshold(argf("mi-dist-thresh"));
513
            inpainters->pushBack(inp);
514
        }
515
        if (arg("color-inpaint") == "average")
516
            inpainters->pushBack(makePtr<ColorAverageInpainter>());
517
        else if (arg("color-inpaint") == "ns")
518
            inpainters->pushBack(makePtr<ColorInpainter>(int(INPAINT_NS), argd("ci-radius")));
519
        else if (arg("color-inpaint") == "telea")
520
            inpainters->pushBack(makePtr<ColorInpainter>(int(INPAINT_TELEA), argd("ci-radius")));
521 522 523
        else if (arg("color-inpaint") != "no")
            throw runtime_error("unknown color inpainting method: " + arg("color-inpaint"));
        if (!inpainters->empty())
524
        {
525 526
            inpainters->setRadius(argi("radius"));
            stabilizer->setInpainter(inpainters_);
527
        }
528

529 530
        if (arg("output") != "no")
            outputPath = arg("output");
531

532
        quietMode = argb("quiet");
533

534 535 536 537
        run();
    }
    catch (const exception &e)
    {
538
        cout << "error: " << e.what() << endl;
539
        stabilizedFrames.release();
540 541
        return -1;
    }
542
    stabilizedFrames.release();
543 544
    return 0;
}
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562


MotionModel motionModel(const string &str)
{
    if (str == "transl")
        return MM_TRANSLATION;
    if (str == "transl_and_scale")
        return MM_TRANSLATION_AND_SCALE;
    if (str == "rigid")
        return MM_RIGID;
    if (str == "similarity")
        return MM_SIMILARITY;
    if (str == "affine")
        return MM_AFFINE;
    if (str == "homography")
        return MM_HOMOGRAPHY;
    throw runtime_error("unknown motion model: " + str);
}