2012-03-19 21:39:23 +08:00
|
|
|
#include <string>
|
|
|
|
#include <iostream>
|
2012-03-22 17:30:28 +08:00
|
|
|
#include <fstream>
|
2012-03-19 21:39:23 +08:00
|
|
|
#include <sstream>
|
|
|
|
#include <stdexcept>
|
2013-03-13 20:22:44 +08:00
|
|
|
#include "opencv2/core.hpp"
|
2013-03-15 00:10:54 +08:00
|
|
|
#include <opencv2/core/utility.hpp>
|
2013-03-13 20:22:44 +08:00
|
|
|
#include "opencv2/video.hpp"
|
|
|
|
#include "opencv2/imgproc.hpp"
|
2014-07-10 22:27:32 +08:00
|
|
|
#include "opencv2/videoio.hpp"
|
2013-03-13 20:22:44 +08:00
|
|
|
#include "opencv2/highgui.hpp"
|
|
|
|
#include "opencv2/videostab.hpp"
|
2012-04-19 01:00:07 +08:00
|
|
|
#include "opencv2/opencv_modules.hpp"
|
2012-03-19 21:39:23 +08:00
|
|
|
|
2012-04-02 17:25:27 +08: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 21:39:23 +08:00
|
|
|
using namespace std;
|
|
|
|
using namespace cv;
|
|
|
|
using namespace cv::videostab;
|
|
|
|
|
2012-03-22 15:52:17 +08:00
|
|
|
Ptr<IFrameSource> stabilizedFrames;
|
2012-03-22 17:30:28 +08:00
|
|
|
string saveMotionsPath;
|
2012-03-19 21:39:23 +08:00
|
|
|
double outputFps;
|
|
|
|
string outputPath;
|
2012-03-20 16:12:58 +08:00
|
|
|
bool quietMode;
|
2012-03-19 21:39:23 +08:00
|
|
|
|
|
|
|
void run();
|
2012-03-22 17:30:28 +08:00
|
|
|
void saveMotionsIfNecessary();
|
2012-03-19 21:39:23 +08:00
|
|
|
void printHelp();
|
2012-04-24 20:23:23 +08:00
|
|
|
MotionModel motionModel(const string &str);
|
2012-03-19 21:39:23 +08:00
|
|
|
|
2012-04-02 17:25:27 +08:00
|
|
|
|
2012-03-19 21:39:23 +08:00
|
|
|
void run()
|
|
|
|
{
|
|
|
|
VideoWriter writer;
|
|
|
|
Mat stabilizedFrame;
|
2012-04-05 17:15:13 +08:00
|
|
|
int nframes = 0;
|
2012-03-19 21:39:23 +08:00
|
|
|
|
2012-04-19 17:29:13 +08:00
|
|
|
// for each stabilized frame
|
2012-03-22 15:52:17 +08:00
|
|
|
while (!(stabilizedFrame = stabilizedFrames->nextFrame()).empty())
|
2012-03-19 21:39:23 +08:00
|
|
|
{
|
2012-04-05 17:15:13 +08:00
|
|
|
nframes++;
|
2012-04-19 17:29:13 +08:00
|
|
|
|
|
|
|
// init writer (once) and save stabilized frame
|
2012-03-19 21:39:23 +08:00
|
|
|
if (!outputPath.empty())
|
|
|
|
{
|
|
|
|
if (!writer.isOpened())
|
2013-04-08 02:45:38 +08:00
|
|
|
writer.open(outputPath, VideoWriter::fourcc('X','V','I','D'),
|
2012-04-19 17:29:13 +08:00
|
|
|
outputFps, stabilizedFrame.size());
|
2012-03-19 21:39:23 +08:00
|
|
|
writer << stabilizedFrame;
|
|
|
|
}
|
2012-04-19 17:29:13 +08:00
|
|
|
|
|
|
|
// show stabilized frame
|
2012-03-20 16:12:58 +08:00
|
|
|
if (!quietMode)
|
|
|
|
{
|
|
|
|
imshow("stabilizedFrame", stabilizedFrame);
|
|
|
|
char key = static_cast<char>(waitKey(3));
|
2012-04-19 17:29:13 +08:00
|
|
|
if (key == 27) { cout << endl; break; }
|
2012-03-20 16:12:58 +08:00
|
|
|
}
|
2012-03-19 21:39:23 +08:00
|
|
|
}
|
|
|
|
|
2012-04-19 17:29:13 +08:00
|
|
|
cout << "processed frames: " << nframes << endl
|
2012-04-05 17:15:13 +08:00
|
|
|
<< "finished\n";
|
2012-03-19 21:39:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void printHelp()
|
|
|
|
{
|
|
|
|
cout << "OpenCV video stabilizer.\n"
|
|
|
|
"Usage: videostab <file_path> [arguments]\n\n"
|
|
|
|
"Arguments:\n"
|
2012-04-24 20:23:23 +08:00
|
|
|
" -m, --model=(transl|transl_and_scale|rigid|similarity|affine|homography)\n"
|
2012-03-19 21:39:23 +08:00
|
|
|
" Set motion model. The default is affine.\n"
|
2012-04-25 21:21:38 +08:00
|
|
|
" -lp, --lin-prog-motion-est=(yes|no)\n"
|
|
|
|
" Turn on/off LP based motion estimation. The default is no.\n"
|
2012-04-11 22:02:10 +08:00
|
|
|
" --subset=(<int_number>|auto)\n"
|
|
|
|
" Number of random samples per one motion hypothesis. The default is auto.\n"
|
2012-04-17 17:12:14 +08:00
|
|
|
" --thresh=(<float_number>|auto)\n"
|
|
|
|
" Maximum error to classify match as inlier. The default is auto.\n"
|
2012-03-19 21:39:23 +08:00
|
|
|
" --outlier-ratio=<float_number>\n"
|
2012-04-11 18:17:35 +08:00
|
|
|
" Motion estimation outlier ratio hypothesis. The default is 0.5.\n"
|
2012-03-19 21:39:23 +08:00
|
|
|
" --min-inlier-ratio=<float_number>\n"
|
2012-04-11 18:17:35 +08: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"
|
2012-04-24 20:23:23 +08:00
|
|
|
" --local-outlier-rejection=(yes|no)\n"
|
|
|
|
" Perform local outlier rejection. The default is no.\n\n"
|
2012-04-06 16:52:31 +08:00
|
|
|
" -sm, --save-motions=(<file_path>|no)\n"
|
2012-04-02 17:25:27 +08:00
|
|
|
" Save estimated motions into file. The default is no.\n"
|
2012-04-06 16:52:31 +08:00
|
|
|
" -lm, --load-motions=(<file_path>|no)\n"
|
2012-04-02 17:25:27 +08:00
|
|
|
" Load motions from file. The default is no.\n\n"
|
2012-03-19 21:39:23 +08:00
|
|
|
" -r, --radius=<int_number>\n"
|
2012-04-02 17:25:27 +08:00
|
|
|
" Set sliding window radius. The default is 15.\n"
|
|
|
|
" --stdev=(<float_number>|auto)\n"
|
2012-04-06 16:52:31 +08:00
|
|
|
" Set smoothing weights standard deviation. The default is auto\n"
|
|
|
|
" (i.e. sqrt(radius)).\n"
|
2012-04-19 20:16:45 +08:00
|
|
|
" -lps, --lin-prog-stab=(yes|no)\n"
|
2012-04-16 14:41:06 +08:00
|
|
|
" Turn on/off linear programming based stabilization method.\n"
|
2012-04-23 14:42:55 +08:00
|
|
|
" --lps-trim-ratio=(<float_number>|auto)\n"
|
2012-04-16 14:41:06 +08:00
|
|
|
" Trimming ratio used in linear programming based method.\n"
|
2012-04-23 14:42:55 +08:00
|
|
|
" --lps-w1=(<float_number>|1)\n"
|
2012-04-16 14:41:06 +08:00
|
|
|
" 1st derivative weight. The default is 1.\n"
|
2012-04-23 14:42:55 +08:00
|
|
|
" --lps-w2=(<float_number>|10)\n"
|
2012-04-16 14:41:06 +08:00
|
|
|
" 2nd derivative weight. The default is 10.\n"
|
2012-04-23 14:42:55 +08:00
|
|
|
" --lps-w3=(<float_number>|100)\n"
|
2012-04-16 14:41:06 +08:00
|
|
|
" 3rd derivative weight. The default is 100.\n"
|
2012-04-23 14:42:55 +08:00
|
|
|
" --lps-w4=(<float_number>|100)\n"
|
2012-04-16 14:41:06 +08:00
|
|
|
" Non-translation motion components weight. The default is 100.\n\n"
|
2012-03-19 21:39:23 +08:00
|
|
|
" --deblur=(yes|no)\n"
|
|
|
|
" Do deblurring.\n"
|
|
|
|
" --deblur-sens=<float_number>\n"
|
2012-03-21 16:34:00 +08:00
|
|
|
" Set deblurring sensitivity (from 0 to +inf). The default is 0.1.\n\n"
|
2012-03-19 21:39:23 +08:00
|
|
|
" -t, --trim-ratio=<float_number>\n"
|
2012-04-02 20:26:05 +08:00
|
|
|
" Set trimming ratio (from 0 to 0.5). The default is 0.1.\n"
|
2012-04-06 16:52:31 +08: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 16:34:00 +08:00
|
|
|
" Ensure the inclusion constraint is always satisfied. The default is no.\n\n"
|
2012-04-06 16:52:31 +08:00
|
|
|
" -bm, --border-mode=(replicate|reflect|const)\n"
|
2012-03-21 16:34:00 +08:00
|
|
|
" Set border extrapolation mode. The default is replicate.\n\n"
|
2012-03-19 21:39:23 +08:00
|
|
|
" --mosaic=(yes|no)\n"
|
|
|
|
" Do consistent mosaicing. The default is no.\n"
|
|
|
|
" --mosaic-stdev=<float_number>\n"
|
2012-03-21 16:34:00 +08:00
|
|
|
" Consistent mosaicing stdev threshold. The default is 10.0.\n\n"
|
2012-04-06 16:52:31 +08:00
|
|
|
" -mi, --motion-inpaint=(yes|no)\n"
|
2013-07-24 17:55:18 +08:00
|
|
|
" Do motion inpainting (requires CUDA support). The default is no.\n"
|
2012-04-06 16:52:31 +08:00
|
|
|
" --mi-dist-thresh=<float_number>\n"
|
2012-03-21 16:34:00 +08:00
|
|
|
" Estimated flow distance threshold for motion inpainting. The default is 5.0.\n\n"
|
2012-04-06 16:52:31 +08:00
|
|
|
" -ci, --color-inpaint=(no|average|ns|telea)\n"
|
2012-03-19 21:39:23 +08:00
|
|
|
" Do color inpainting. The defailt is no.\n"
|
2012-04-06 16:52:31 +08:00
|
|
|
" --ci-radius=<float_number>\n"
|
2012-04-02 17:25:27 +08:00
|
|
|
" Set color inpainting radius (for ns and telea options only).\n"
|
|
|
|
" The default is 2.0\n\n"
|
2012-04-06 16:52:31 +08:00
|
|
|
" -ws, --wobble-suppress=(yes|no)\n"
|
|
|
|
" Perform wobble suppression. The default is no.\n"
|
2012-04-25 21:21:38 +08:00
|
|
|
" --ws-lp=(yes|no)\n"
|
|
|
|
" Turn on/off LP based motion estimation. The default is no.\n"
|
2012-04-06 16:52:31 +08:00
|
|
|
" --ws-period=<int_number>\n"
|
|
|
|
" Set wobble suppression period. The default is 30.\n"
|
2012-04-24 20:23:23 +08:00
|
|
|
" --ws-model=(transl|transl_and_scale|rigid|similarity|affine|homography)\n"
|
2012-04-06 16:52:31 +08:00
|
|
|
" Set wobble suppression motion model (must have more DOF than motion \n"
|
|
|
|
" estimation model). The default is homography.\n"
|
2012-04-11 22:02:10 +08:00
|
|
|
" --ws-subset=(<int_number>|auto)\n"
|
|
|
|
" Number of random samples per one motion hypothesis. The default is auto.\n"
|
2012-04-17 17:12:14 +08:00
|
|
|
" --ws-thresh=(<float_number>|auto)\n"
|
|
|
|
" Maximum error to classify match as inlier. The default is auto.\n"
|
2012-04-10 18:33:19 +08:00
|
|
|
" --ws-outlier-ratio=<float_number>\n"
|
|
|
|
" Motion estimation outlier ratio hypothesis. The default is 0.5.\n"
|
2012-04-11 22:02:10 +08: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 18:17:35 +08:00
|
|
|
" --ws-nkps=<int_number>\n"
|
|
|
|
" Number of keypoints to find in each frame. The default is 1000.\n"
|
2012-04-24 20:23:23 +08:00
|
|
|
" --ws-local-outlier-rejection=(yes|no)\n"
|
|
|
|
" Perform local outlier rejection. The default is no.\n\n"
|
2012-04-06 16:52:31 +08: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-04-19 18:27:17 +08:00
|
|
|
" -gpu=(yes|no)\n"
|
2013-07-24 17:55:18 +08:00
|
|
|
" Use CUDA optimization whenever possible. The default is no.\n\n"
|
2012-03-22 15:52:17 +08:00
|
|
|
" -o, --output=(no|<file_path>)\n"
|
2012-03-19 21:39:23 +08:00
|
|
|
" Set output file path explicitely. The default is stabilized.avi.\n"
|
2012-04-06 16:52:31 +08:00
|
|
|
" --fps=(<float_number>|auto)\n"
|
|
|
|
" Set output video FPS explicitely. By default the source FPS is used (auto).\n"
|
2012-03-20 16:12:58 +08:00
|
|
|
" -q, --quiet\n"
|
2012-03-21 16:34:00 +08:00
|
|
|
" Don't show output video frames.\n\n"
|
2012-03-19 21:39:23 +08:00
|
|
|
" -h, --help\n"
|
2012-04-06 16:52:31 +08:00
|
|
|
" Print help.\n\n"
|
|
|
|
"Note: some argument configurations lead to two passes, some to single pass.\n\n";
|
2012-03-19 21:39:23 +08:00
|
|
|
}
|
|
|
|
|
2012-04-25 21:21:38 +08:00
|
|
|
// motion estimator builders are for concise creation of motion estimators
|
|
|
|
|
|
|
|
class IMotionEstimatorBuilder
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual ~IMotionEstimatorBuilder() {}
|
2012-04-26 17:01:15 +08:00
|
|
|
virtual Ptr<ImageMotionEstimatorBase> build() = 0;
|
2012-04-25 21:21:38 +08:00
|
|
|
protected:
|
2012-06-12 22:46:12 +08:00
|
|
|
IMotionEstimatorBuilder(CommandLineParser &command) : cmd(command) {}
|
2012-04-25 21:21:38 +08:00
|
|
|
CommandLineParser cmd;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-04-26 17:01:15 +08:00
|
|
|
class MotionEstimatorRansacL2Builder : public IMotionEstimatorBuilder
|
2012-04-25 21:21:38 +08:00
|
|
|
{
|
|
|
|
public:
|
2012-06-12 22:46:12 +08:00
|
|
|
MotionEstimatorRansacL2Builder(CommandLineParser &command, bool use_gpu, const string &_prefix = "")
|
|
|
|
: IMotionEstimatorBuilder(command), gpu(use_gpu), prefix(_prefix) {}
|
2012-04-25 21:21:38 +08:00
|
|
|
|
2012-04-26 17:01:15 +08:00
|
|
|
virtual Ptr<ImageMotionEstimatorBase> build()
|
2012-04-25 21:21:38 +08:00
|
|
|
{
|
2013-09-06 19:44:44 +08:00
|
|
|
Ptr<MotionEstimatorRansacL2> est = makePtr<MotionEstimatorRansacL2>(motionModel(arg(prefix + "model")));
|
2012-04-25 21:21:38 +08:00
|
|
|
|
|
|
|
RansacParams ransac = est->ransacParams();
|
|
|
|
if (arg(prefix + "subset") != "auto")
|
|
|
|
ransac.size = argi(prefix + "subset");
|
|
|
|
if (arg(prefix + "thresh") != "auto")
|
2012-06-12 22:46:12 +08:00
|
|
|
ransac.thresh = argf(prefix + "thresh");
|
2012-04-25 21:21:38 +08:00
|
|
|
ransac.eps = argf(prefix + "outlier-ratio");
|
|
|
|
est->setRansacParams(ransac);
|
|
|
|
|
2012-04-26 17:01:15 +08:00
|
|
|
est->setMinInlierRatio(argf(prefix + "min-inlier-ratio"));
|
2012-04-25 21:21:38 +08:00
|
|
|
|
2013-09-06 19:44:44 +08:00
|
|
|
Ptr<IOutlierRejector> outlierRejector = makePtr<NullOutlierRejector>();
|
2012-04-25 21:21:38 +08:00
|
|
|
if (arg(prefix + "local-outlier-rejection") == "yes")
|
|
|
|
{
|
2013-09-06 19:44:44 +08:00
|
|
|
Ptr<TranslationBasedLocalOutlierRejector> tblor = makePtr<TranslationBasedLocalOutlierRejector>();
|
2012-04-26 17:01:15 +08:00
|
|
|
RansacParams ransacParams = tblor->ransacParams();
|
2012-04-25 21:21:38 +08:00
|
|
|
if (arg(prefix + "thresh") != "auto")
|
|
|
|
ransacParams.thresh = argf(prefix + "thresh");
|
2012-04-26 17:01:15 +08:00
|
|
|
tblor->setRansacParams(ransacParams);
|
|
|
|
outlierRejector = tblor;
|
2012-04-25 21:21:38 +08:00
|
|
|
}
|
|
|
|
|
2013-07-24 15:41:44 +08:00
|
|
|
#if defined(HAVE_OPENCV_CUDAIMGPROC) && defined(HAVE_OPENCV_CUDA) && defined(HAVE_OPENCV_CUDAOPTFLOW)
|
2012-04-26 17:01:15 +08:00
|
|
|
if (gpu)
|
2012-04-25 21:21:38 +08:00
|
|
|
{
|
2013-09-06 19:44:44 +08:00
|
|
|
Ptr<KeypointBasedMotionEstimatorGpu> kbest = makePtr<KeypointBasedMotionEstimatorGpu>(est);
|
2012-04-26 17:01:15 +08:00
|
|
|
kbest->setOutlierRejector(outlierRejector);
|
|
|
|
return kbest;
|
2012-04-25 21:21:38 +08:00
|
|
|
}
|
2012-04-26 17:01:15 +08:00
|
|
|
#endif
|
2012-04-25 21:21:38 +08:00
|
|
|
|
2013-09-06 19:44:44 +08:00
|
|
|
Ptr<KeypointBasedMotionEstimator> kbest = makePtr<KeypointBasedMotionEstimator>(est);
|
|
|
|
kbest->setDetector(makePtr<GoodFeaturesToTrackDetector>(argi(prefix + "nkps")));
|
2012-04-26 17:01:15 +08:00
|
|
|
kbest->setOutlierRejector(outlierRejector);
|
|
|
|
return kbest;
|
2012-04-25 21:21:38 +08:00
|
|
|
}
|
|
|
|
private:
|
2012-04-26 17:01:15 +08:00
|
|
|
bool gpu;
|
2012-04-25 21:21:38 +08:00
|
|
|
string prefix;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-04-26 17:01:15 +08:00
|
|
|
class MotionEstimatorL1Builder : public IMotionEstimatorBuilder
|
2012-04-25 21:21:38 +08:00
|
|
|
{
|
|
|
|
public:
|
2012-06-12 22:46:12 +08:00
|
|
|
MotionEstimatorL1Builder(CommandLineParser &command, bool use_gpu, const string &_prefix = "")
|
|
|
|
: IMotionEstimatorBuilder(command), gpu(use_gpu), prefix(_prefix) {}
|
2012-04-25 21:21:38 +08:00
|
|
|
|
2012-04-26 17:01:15 +08:00
|
|
|
virtual Ptr<ImageMotionEstimatorBase> build()
|
2012-04-25 21:21:38 +08:00
|
|
|
{
|
2013-09-06 19:44:44 +08:00
|
|
|
Ptr<MotionEstimatorL1> est = makePtr<MotionEstimatorL1>(motionModel(arg(prefix + "model")));
|
2012-04-25 21:21:38 +08:00
|
|
|
|
2013-09-06 19:44:44 +08:00
|
|
|
Ptr<IOutlierRejector> outlierRejector = makePtr<NullOutlierRejector>();
|
2012-04-25 21:21:38 +08:00
|
|
|
if (arg(prefix + "local-outlier-rejection") == "yes")
|
|
|
|
{
|
2013-09-06 19:44:44 +08:00
|
|
|
Ptr<TranslationBasedLocalOutlierRejector> tblor = makePtr<TranslationBasedLocalOutlierRejector>();
|
2012-04-26 17:01:15 +08:00
|
|
|
RansacParams ransacParams = tblor->ransacParams();
|
2012-04-25 21:21:38 +08:00
|
|
|
if (arg(prefix + "thresh") != "auto")
|
|
|
|
ransacParams.thresh = argf(prefix + "thresh");
|
2012-04-26 17:01:15 +08:00
|
|
|
tblor->setRansacParams(ransacParams);
|
|
|
|
outlierRejector = tblor;
|
2012-04-25 21:21:38 +08:00
|
|
|
}
|
|
|
|
|
2013-07-24 15:41:44 +08:00
|
|
|
#if defined(HAVE_OPENCV_CUDAIMGPROC) && defined(HAVE_OPENCV_CUDA) && defined(HAVE_OPENCV_CUDAOPTFLOW)
|
2012-04-26 17:01:15 +08:00
|
|
|
if (gpu)
|
|
|
|
{
|
2013-09-06 19:44:44 +08:00
|
|
|
Ptr<KeypointBasedMotionEstimatorGpu> kbest = makePtr<KeypointBasedMotionEstimatorGpu>(est);
|
2012-04-26 17:01:15 +08:00
|
|
|
kbest->setOutlierRejector(outlierRejector);
|
|
|
|
return kbest;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-09-06 19:44:44 +08:00
|
|
|
Ptr<KeypointBasedMotionEstimator> kbest = makePtr<KeypointBasedMotionEstimator>(est);
|
|
|
|
kbest->setDetector(makePtr<GoodFeaturesToTrackDetector>(argi(prefix + "nkps")));
|
2012-04-26 17:01:15 +08:00
|
|
|
kbest->setOutlierRejector(outlierRejector);
|
|
|
|
return kbest;
|
2012-04-25 21:21:38 +08:00
|
|
|
}
|
|
|
|
private:
|
2012-04-26 17:01:15 +08:00
|
|
|
bool gpu;
|
2012-04-25 21:21:38 +08:00
|
|
|
string prefix;
|
|
|
|
};
|
|
|
|
|
2012-03-19 21:39:23 +08:00
|
|
|
|
|
|
|
int main(int argc, const char **argv)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
const char *keys =
|
2012-09-07 17:24:48 +08:00
|
|
|
"{ @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 | | }";
|
2012-03-19 21:39:23 +08:00
|
|
|
CommandLineParser cmd(argc, argv, keys);
|
|
|
|
|
|
|
|
// parse command arguments
|
|
|
|
|
2012-04-02 17:25:27 +08:00
|
|
|
if (argb("help"))
|
2012-03-19 21:39:23 +08:00
|
|
|
{
|
|
|
|
printHelp();
|
|
|
|
return 0;
|
2012-04-16 14:41:06 +08:00
|
|
|
}
|
|
|
|
|
2013-07-24 15:41:44 +08:00
|
|
|
#ifdef HAVE_OPENCV_CUDA
|
2012-04-18 21:23:41 +08:00
|
|
|
if (arg("gpu") == "yes")
|
|
|
|
{
|
|
|
|
cout << "initializing GPU..."; cout.flush();
|
|
|
|
Mat hostTmp = Mat::zeros(1, 1, CV_32F);
|
2013-08-28 19:45:13 +08:00
|
|
|
cuda::GpuMat deviceTmp;
|
2012-04-18 21:23:41 +08:00
|
|
|
deviceTmp.upload(hostTmp);
|
|
|
|
cout << endl;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-04-25 21:21:38 +08:00
|
|
|
StabilizerBase *stabilizer = 0;
|
|
|
|
|
|
|
|
// check if source video is specified
|
|
|
|
|
2013-01-31 16:08:43 +08:00
|
|
|
string inputPath = arg(0);
|
2012-04-25 21:21:38 +08:00
|
|
|
if (inputPath.empty())
|
|
|
|
throw runtime_error("specify video file path");
|
|
|
|
|
|
|
|
// get source video parameters
|
2012-04-16 14:41:06 +08:00
|
|
|
|
2013-09-06 19:44:44 +08:00
|
|
|
Ptr<VideoFileSource> source = makePtr<VideoFileSource>(inputPath);
|
2012-04-16 14:41:06 +08:00
|
|
|
cout << "frame count (rough): " << source->count() << endl;
|
2012-04-25 21:21:38 +08:00
|
|
|
if (arg("fps") == "auto")
|
|
|
|
outputFps = source->fps();
|
|
|
|
else
|
|
|
|
outputFps = argd("fps");
|
2012-03-22 15:52:17 +08:00
|
|
|
|
2012-04-25 21:21:38 +08:00
|
|
|
// prepare motion estimation builders
|
2012-03-22 15:52:17 +08:00
|
|
|
|
2012-04-25 21:21:38 +08:00
|
|
|
Ptr<IMotionEstimatorBuilder> motionEstBuilder;
|
2012-04-26 17:01:15 +08:00
|
|
|
if (arg("lin-prog-motion-est") == "yes")
|
2013-09-06 19:44:44 +08:00
|
|
|
motionEstBuilder.reset(new MotionEstimatorL1Builder(cmd, arg("gpu") == "yes"));
|
2012-04-25 21:21:38 +08:00
|
|
|
else
|
2013-09-06 19:44:44 +08:00
|
|
|
motionEstBuilder.reset(new MotionEstimatorRansacL2Builder(cmd, arg("gpu") == "yes"));
|
2012-04-25 21:21:38 +08:00
|
|
|
|
|
|
|
Ptr<IMotionEstimatorBuilder> wsMotionEstBuilder;
|
2012-04-26 17:01:15 +08:00
|
|
|
if (arg("ws-lp") == "yes")
|
2013-09-06 19:44:44 +08:00
|
|
|
wsMotionEstBuilder.reset(new MotionEstimatorL1Builder(cmd, arg("gpu") == "yes", "ws-"));
|
2012-04-25 21:21:38 +08:00
|
|
|
else
|
2013-09-06 19:44:44 +08:00
|
|
|
wsMotionEstBuilder.reset(new MotionEstimatorRansacL2Builder(cmd, arg("gpu") == "yes", "ws-"));
|
2012-04-25 21:21:38 +08:00
|
|
|
|
|
|
|
// determine whether we must use one pass or two pass stabilizer
|
2012-04-05 21:23:42 +08:00
|
|
|
bool isTwoPass =
|
2012-04-19 18:27:17 +08:00
|
|
|
arg("est-trim") == "yes" || arg("wobble-suppress") == "yes" || arg("lin-prog-stab") == "yes";
|
2012-04-05 21:23:42 +08:00
|
|
|
|
2012-03-22 15:52:17 +08:00
|
|
|
if (isTwoPass)
|
|
|
|
{
|
2012-04-25 21:21:38 +08:00
|
|
|
// we must use two pass stabilizer
|
|
|
|
|
2012-03-22 15:52:17 +08:00
|
|
|
TwoPassStabilizer *twoPassStabilizer = new TwoPassStabilizer();
|
|
|
|
stabilizer = twoPassStabilizer;
|
2012-04-02 17:25:27 +08:00
|
|
|
twoPassStabilizer->setEstimateTrimRatio(arg("est-trim") == "yes");
|
2012-04-25 21:21:38 +08:00
|
|
|
|
|
|
|
// determine stabilization technique
|
|
|
|
|
2012-04-19 18:27:17 +08:00
|
|
|
if (arg("lin-prog-stab") == "yes")
|
2012-04-16 14:41:06 +08:00
|
|
|
{
|
2013-09-06 19:44:44 +08:00
|
|
|
Ptr<LpMotionStabilizer> stab = makePtr<LpMotionStabilizer>();
|
2012-04-16 14:41:06 +08:00
|
|
|
stab->setFrameSize(Size(source->width(), source->height()));
|
2012-04-23 14:42:55 +08:00
|
|
|
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"));
|
2012-04-16 14:41:06 +08:00
|
|
|
twoPassStabilizer->setMotionStabilizer(stab);
|
|
|
|
}
|
|
|
|
else if (arg("stdev") == "auto")
|
2013-09-06 19:44:44 +08:00
|
|
|
twoPassStabilizer->setMotionStabilizer(makePtr<GaussianMotionFilter>(argi("radius")));
|
2012-04-02 17:25:27 +08:00
|
|
|
else
|
2013-09-06 19:44:44 +08:00
|
|
|
twoPassStabilizer->setMotionStabilizer(makePtr<GaussianMotionFilter>(argi("radius"), argf("stdev")));
|
2012-04-18 21:23:41 +08:00
|
|
|
|
2012-04-25 21:21:38 +08:00
|
|
|
// init wobble suppressor if necessary
|
2012-04-24 20:23:23 +08:00
|
|
|
|
2012-04-25 21:21:38 +08:00
|
|
|
if (arg("wobble-suppress") == "yes")
|
|
|
|
{
|
2013-09-06 19:44:44 +08:00
|
|
|
Ptr<MoreAccurateMotionWobbleSuppressorBase> ws = makePtr<MoreAccurateMotionWobbleSuppressor>();
|
2012-04-25 21:21:38 +08:00
|
|
|
if (arg("gpu") == "yes")
|
2013-07-24 15:41:44 +08:00
|
|
|
#ifdef HAVE_OPENCV_CUDA
|
2013-09-06 19:44:44 +08:00
|
|
|
ws = makePtr<MoreAccurateMotionWobbleSuppressorGpu>();
|
2012-04-18 21:23:41 +08:00
|
|
|
#else
|
2013-07-24 17:55:18 +08:00
|
|
|
throw runtime_error("OpenCV is built without CUDA support");
|
2012-04-18 21:23:41 +08:00
|
|
|
#endif
|
2012-04-23 18:37:07 +08:00
|
|
|
|
2012-04-25 21:21:38 +08:00
|
|
|
ws->setMotionEstimator(wsMotionEstBuilder->build());
|
2012-04-23 18:37:07 +08:00
|
|
|
ws->setPeriod(argi("ws-period"));
|
2012-04-25 21:21:38 +08:00
|
|
|
twoPassStabilizer->setWobbleSuppressor(ws);
|
2012-04-10 18:33:19 +08:00
|
|
|
|
2012-04-18 21:23:41 +08:00
|
|
|
MotionModel model = ws->motionEstimator()->motionModel();
|
2012-04-06 16:52:31 +08:00
|
|
|
if (arg("load-motions2") != "no")
|
2012-04-10 18:33:19 +08:00
|
|
|
{
|
2013-09-06 19:44:44 +08:00
|
|
|
ws->setMotionEstimator(makePtr<FromFileMotionReader>(arg("load-motions2")));
|
2012-04-10 18:33:19 +08:00
|
|
|
ws->motionEstimator()->setMotionModel(model);
|
|
|
|
}
|
2012-04-06 16:52:31 +08:00
|
|
|
if (arg("save-motions2") != "no")
|
2012-04-05 21:23:42 +08:00
|
|
|
{
|
2013-09-06 19:44:44 +08:00
|
|
|
ws->setMotionEstimator(makePtr<ToFileMotionWriter>(arg("save-motions2"), ws->motionEstimator()));
|
2012-04-10 18:33:19 +08:00
|
|
|
ws->motionEstimator()->setMotionModel(model);
|
2012-04-05 21:23:42 +08:00
|
|
|
}
|
2012-04-18 21:23:41 +08:00
|
|
|
}
|
2012-03-22 15:52:17 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-04-25 21:21:38 +08:00
|
|
|
// we must use one pass stabilizer
|
|
|
|
|
2012-04-02 17:25:27 +08:00
|
|
|
OnePassStabilizer *onePassStabilizer = new OnePassStabilizer();
|
2012-03-22 15:52:17 +08:00
|
|
|
stabilizer = onePassStabilizer;
|
2012-04-02 17:25:27 +08:00
|
|
|
if (arg("stdev") == "auto")
|
2013-09-06 19:44:44 +08:00
|
|
|
onePassStabilizer->setMotionFilter(makePtr<GaussianMotionFilter>(argi("radius")));
|
2012-04-02 17:25:27 +08:00
|
|
|
else
|
2013-09-06 19:44:44 +08:00
|
|
|
onePassStabilizer->setMotionFilter(makePtr<GaussianMotionFilter>(argi("radius"), argf("stdev")));
|
2012-03-22 15:52:17 +08:00
|
|
|
}
|
2012-03-19 21:39:23 +08:00
|
|
|
|
2012-04-02 17:25:27 +08:00
|
|
|
stabilizer->setFrameSource(source);
|
2012-04-25 21:21:38 +08:00
|
|
|
stabilizer->setMotionEstimator(motionEstBuilder->build());
|
2012-04-24 20:23:23 +08:00
|
|
|
|
2012-04-25 21:21:38 +08:00
|
|
|
// cast stabilizer to simple frame source interface to read stabilized frames
|
2013-09-06 19:44:44 +08:00
|
|
|
stabilizedFrames.reset(dynamic_cast<IFrameSource*>(stabilizer));
|
2012-04-06 18:51:46 +08:00
|
|
|
|
2012-04-10 18:33:19 +08:00
|
|
|
MotionModel model = stabilizer->motionEstimator()->motionModel();
|
2012-04-06 18:51:46 +08:00
|
|
|
if (arg("load-motions") != "no")
|
|
|
|
{
|
2013-09-06 19:44:44 +08:00
|
|
|
stabilizer->setMotionEstimator(makePtr<FromFileMotionReader>(arg("load-motions")));
|
2012-04-06 18:51:46 +08:00
|
|
|
stabilizer->motionEstimator()->setMotionModel(model);
|
|
|
|
}
|
2012-04-02 17:25:27 +08:00
|
|
|
if (arg("save-motions") != "no")
|
2012-04-10 17:07:30 +08:00
|
|
|
{
|
2013-09-06 19:44:44 +08:00
|
|
|
stabilizer->setMotionEstimator(makePtr<ToFileMotionWriter>(arg("save-motions"), stabilizer->motionEstimator()));
|
2012-04-10 17:07:30 +08:00
|
|
|
stabilizer->motionEstimator()->setMotionModel(model);
|
|
|
|
}
|
2012-03-19 21:39:23 +08:00
|
|
|
|
2012-04-02 17:25:27 +08:00
|
|
|
stabilizer->setRadius(argi("radius"));
|
2012-04-25 21:21:38 +08:00
|
|
|
|
|
|
|
// init deblurer
|
2012-04-02 17:25:27 +08:00
|
|
|
if (arg("deblur") == "yes")
|
2012-03-19 21:39:23 +08:00
|
|
|
{
|
2013-09-06 19:44:44 +08:00
|
|
|
Ptr<WeightingDeblurer> deblurer = makePtr<WeightingDeblurer>();
|
2012-04-02 17:25:27 +08:00
|
|
|
deblurer->setRadius(argi("radius"));
|
|
|
|
deblurer->setSensitivity(argf("deblur-sens"));
|
2012-03-19 21:39:23 +08:00
|
|
|
stabilizer->setDeblurer(deblurer);
|
|
|
|
}
|
|
|
|
|
2012-04-25 21:21:38 +08:00
|
|
|
// set up trimming paramters
|
2012-04-02 17:25:27 +08:00
|
|
|
stabilizer->setTrimRatio(argf("trim-ratio"));
|
|
|
|
stabilizer->setCorrectionForInclusion(arg("incl-constr") == "yes");
|
2012-03-19 21:39:23 +08:00
|
|
|
|
2012-04-02 17:25:27 +08:00
|
|
|
if (arg("border-mode") == "reflect")
|
2012-03-20 20:24:51 +08:00
|
|
|
stabilizer->setBorderMode(BORDER_REFLECT);
|
2012-04-02 17:25:27 +08:00
|
|
|
else if (arg("border-mode") == "replicate")
|
2012-03-19 21:39:23 +08:00
|
|
|
stabilizer->setBorderMode(BORDER_REPLICATE);
|
2012-04-02 17:25:27 +08:00
|
|
|
else if (arg("border-mode") == "const")
|
2012-03-19 21:39:23 +08:00
|
|
|
stabilizer->setBorderMode(BORDER_CONSTANT);
|
2012-04-02 17:25:27 +08:00
|
|
|
else
|
|
|
|
throw runtime_error("unknown border extrapolation mode: "
|
|
|
|
+ cmd.get<string>("border-mode"));
|
2012-03-19 21:39:23 +08:00
|
|
|
|
2012-04-25 21:21:38 +08:00
|
|
|
// init inpainter
|
2012-03-19 21:39:23 +08:00
|
|
|
InpaintingPipeline *inpainters = new InpaintingPipeline();
|
2012-04-02 17:25:27 +08:00
|
|
|
Ptr<InpainterBase> inpainters_(inpainters);
|
|
|
|
if (arg("mosaic") == "yes")
|
2012-03-19 21:39:23 +08:00
|
|
|
{
|
2013-09-06 19:44:44 +08:00
|
|
|
Ptr<ConsistentMosaicInpainter> inp = makePtr<ConsistentMosaicInpainter>();
|
2012-04-02 17:25:27 +08:00
|
|
|
inp->setStdevThresh(argf("mosaic-stdev"));
|
|
|
|
inpainters->pushBack(inp);
|
2012-03-19 21:39:23 +08:00
|
|
|
}
|
2012-04-02 17:25:27 +08:00
|
|
|
if (arg("motion-inpaint") == "yes")
|
2012-03-21 16:34:00 +08:00
|
|
|
{
|
2013-09-06 19:44:44 +08:00
|
|
|
Ptr<MotionInpainter> inp = makePtr<MotionInpainter>();
|
2012-04-06 16:52:31 +08:00
|
|
|
inp->setDistThreshold(argf("mi-dist-thresh"));
|
2012-04-02 17:25:27 +08:00
|
|
|
inpainters->pushBack(inp);
|
2012-03-21 16:34:00 +08:00
|
|
|
}
|
2012-04-02 17:25:27 +08:00
|
|
|
if (arg("color-inpaint") == "average")
|
2013-09-06 19:44:44 +08:00
|
|
|
inpainters->pushBack(makePtr<ColorAverageInpainter>());
|
2012-04-02 17:25:27 +08:00
|
|
|
else if (arg("color-inpaint") == "ns")
|
2013-09-06 19:44:44 +08:00
|
|
|
inpainters->pushBack(makePtr<ColorInpainter>(int(INPAINT_NS), argd("ci-radius")));
|
2012-04-02 17:25:27 +08:00
|
|
|
else if (arg("color-inpaint") == "telea")
|
2013-09-06 19:44:44 +08:00
|
|
|
inpainters->pushBack(makePtr<ColorInpainter>(int(INPAINT_TELEA), argd("ci-radius")));
|
2012-04-02 17:25:27 +08:00
|
|
|
else if (arg("color-inpaint") != "no")
|
|
|
|
throw runtime_error("unknown color inpainting method: " + arg("color-inpaint"));
|
|
|
|
if (!inpainters->empty())
|
2012-03-20 20:24:51 +08:00
|
|
|
{
|
2012-04-02 17:25:27 +08:00
|
|
|
inpainters->setRadius(argi("radius"));
|
|
|
|
stabilizer->setInpainter(inpainters_);
|
2012-10-17 15:12:04 +08:00
|
|
|
}
|
2012-03-19 21:39:23 +08:00
|
|
|
|
2012-04-02 17:25:27 +08:00
|
|
|
if (arg("output") != "no")
|
|
|
|
outputPath = arg("output");
|
2012-03-19 21:39:23 +08:00
|
|
|
|
2012-04-16 19:14:30 +08:00
|
|
|
quietMode = argb("quiet");
|
2012-03-22 15:52:17 +08:00
|
|
|
|
2012-03-19 21:39:23 +08:00
|
|
|
run();
|
|
|
|
}
|
|
|
|
catch (const exception &e)
|
|
|
|
{
|
2012-03-21 16:34:00 +08:00
|
|
|
cout << "error: " << e.what() << endl;
|
2012-03-22 15:52:17 +08:00
|
|
|
stabilizedFrames.release();
|
2012-03-19 21:39:23 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2012-03-22 15:52:17 +08:00
|
|
|
stabilizedFrames.release();
|
2012-03-19 21:39:23 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2012-04-24 20:23:23 +08:00
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|